123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace app\admin\controller;
- /**
- * 发卡
- */
- class Card extends Admin{
- protected $Card = null;
- protected function _initialize(){
- parent::_initialize();
- $this->Card = model('Card');
- }
- public function index(){
- $this->assign('meta_title','卡片发行');
- return $this->fetch();
- }
- public function load(){
- $page = input('get.page');
- $limit = input('get.limit');
- $where = [];
- $list = $this->Card->where($where)->order('id desc')->paginate($limit,false,['page'=>$page]);
- $data = [];
- foreach ($list as $key => $value) {
- $data[$key]['id'] = $value['id'];
- $data[$key]['year'] = $value['year'];
- $data[$key]['start'] = $value['start'];
- $data[$key]['end'] = $value['end'];
- $data[$key]['num'] = $value['num'];
- $data[$key]['create_time'] = $value['create_time'];
- }
- $this->output(0,'获取成功',$data,$list->total());
- }
- public function add(){
- if ($this->request->isPost()) {
- $num = input('post.num');
- $num = intval($num);
- if ($num <= 0) {
- $this->output(1,'数量必须大于0');
- }
- $start = 1;
- $last = $this->Card->order('id desc')->find();
- if ($last) {
- $CardNumber = model('card.Number');
- $start += $CardNumber->max('id');
- }
- $this->Card->startTrans();
- $this->Card->year = date('Y');
- $this->Card->num = $num;
- $this->Card->start = $start;
- $this->Card->end = $start + $num - 1;
- $result = $this->Card->save();
- if (!$result) {
- $this->Card->rollback();
- $this->output(1,'保存失败');
- }
- $prefix = substr($this->Card->year ,-2,2);
- $data= [];
- for ($i=0; $i < $num; $i++) {
- $guid = md5(guid());
- $no = preg_replace( '/[^0-9]/i', '', $guid);
- $data[$i]['no'] = $prefix.substr($no,0,10);
- $data[$i]['secret'] = substr($guid,8,8);
- $data[$i]['state'] = 1;
- }
- $result = $this->Card->numbers()->saveAll($data);
- if (!$result) {
- $this->Card->rollback();
- $this->output(1,'保存失败');
- }
- $this->Card->commit();
- $this->output(0,'保存成功');
- }
- }
- public function delete(){
- if ($this->request->isPost()) {
- $id = input('post.id');
- $card = $this->Card->where(['id'=>$id])->find();
- if (!$card) {
- $this->output(1,'参数错误');
- }
- $this->Card->startTrans();
- $numbers = $card['numbers'];
- if (count($numbers) >= 0) {
- foreach ($numbers as $key => $value) {
- if ($value['sale_time'] > 0 || $value['bind_time'] > 0) {
- $this->Card->rollback();
- $this->output(1,'存在已销售或已绑定卡号');
- }
- $result = $value->delete();
- if (!$result) {
- $this->Card->rollback();
- $this->output(1,'删除卡号失败');
- }
- }
- }
-
- $result = $card->delete();
- if (!$result) {
- $this->Card->rollback();
- $this->output(1,'删除卡片失败');
- }
- $this->Card->commit();
- $this->output(0,'删除成功');
- }
- }
- }
|