Card.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 发卡
  5. */
  6. class Card extends Admin{
  7. protected $Card = null;
  8. protected function _initialize(){
  9. parent::_initialize();
  10. $this->Card = model('Card');
  11. }
  12. public function index(){
  13. $this->assign('meta_title','卡片发行');
  14. return $this->fetch();
  15. }
  16. public function load(){
  17. $page = input('get.page');
  18. $limit = input('get.limit');
  19. $where = [];
  20. $list = $this->Card->where($where)->order('id desc')->paginate($limit,false,['page'=>$page]);
  21. $data = [];
  22. foreach ($list as $key => $value) {
  23. $data[$key]['id'] = $value['id'];
  24. $data[$key]['year'] = $value['year'];
  25. $data[$key]['start'] = $value['start'];
  26. $data[$key]['end'] = $value['end'];
  27. $data[$key]['num'] = $value['num'];
  28. $data[$key]['create_time'] = $value['create_time'];
  29. }
  30. $this->output(0,'获取成功',$data,$list->total());
  31. }
  32. public function add(){
  33. if ($this->request->isPost()) {
  34. $num = input('post.num');
  35. $num = intval($num);
  36. if ($num <= 0) {
  37. $this->output(1,'数量必须大于0');
  38. }
  39. $start = 1;
  40. $last = $this->Card->order('id desc')->find();
  41. if ($last) {
  42. $CardNumber = model('card.Number');
  43. $start += $CardNumber->max('id');
  44. }
  45. $this->Card->startTrans();
  46. $this->Card->year = date('Y');
  47. $this->Card->num = $num;
  48. $this->Card->start = $start;
  49. $this->Card->end = $start + $num - 1;
  50. $result = $this->Card->save();
  51. if (!$result) {
  52. $this->Card->rollback();
  53. $this->output(1,'保存失败');
  54. }
  55. $prefix = substr($this->Card->year ,-2,2);
  56. $data= [];
  57. for ($i=0; $i < $num; $i++) {
  58. $guid = md5(guid());
  59. $no = preg_replace( '/[^0-9]/i', '', $guid);
  60. $data[$i]['no'] = $prefix.substr($no,0,10);
  61. $data[$i]['secret'] = substr($guid,8,8);
  62. $data[$i]['state'] = 1;
  63. }
  64. $result = $this->Card->numbers()->saveAll($data);
  65. if (!$result) {
  66. $this->Card->rollback();
  67. $this->output(1,'保存失败');
  68. }
  69. $this->Card->commit();
  70. $this->output(0,'保存成功');
  71. }
  72. }
  73. public function delete(){
  74. if ($this->request->isPost()) {
  75. $id = input('post.id');
  76. $card = $this->Card->where(['id'=>$id])->find();
  77. if (!$card) {
  78. $this->output(1,'参数错误');
  79. }
  80. $this->Card->startTrans();
  81. $numbers = $card['numbers'];
  82. if (count($numbers) >= 0) {
  83. foreach ($numbers as $key => $value) {
  84. if ($value['sale_time'] > 0 || $value['bind_time'] > 0) {
  85. $this->Card->rollback();
  86. $this->output(1,'存在已销售或已绑定卡号');
  87. }
  88. $result = $value->delete();
  89. if (!$result) {
  90. $this->Card->rollback();
  91. $this->output(1,'删除卡号失败');
  92. }
  93. }
  94. }
  95. $result = $card->delete();
  96. if (!$result) {
  97. $this->Card->rollback();
  98. $this->output(1,'删除卡片失败');
  99. }
  100. $this->Card->commit();
  101. $this->output(0,'删除成功');
  102. }
  103. }
  104. }