SpecialBatch.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\ump;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\ump\SpecialExchange;
  14. use app\admin\model\ump\SpecialBatch as SpecialBatchModel;
  15. use service\JsonService as Json;
  16. use app\admin\model\special\Special;
  17. /**
  18. * 专题兑换码管理控制器
  19. * Class SpecialBatch
  20. * @package app\admin\controller\ump
  21. */
  22. class SpecialBatch extends AuthController
  23. {
  24. public function index()
  25. {
  26. $list = Special::PreWhere()->field('id,title')->select();
  27. $this->assign(['activity_type' => 1, 'special' => $list]);
  28. return $this->fetch('batch_index');
  29. }
  30. public function specialList()
  31. {
  32. $list = Special::PreWhere()->field('id,title')->select();
  33. return Json::successful($list);
  34. }
  35. public function batch_list()
  36. {
  37. $where = parent::getMore([
  38. ['start_time', ''],
  39. ['end_time', ''],
  40. ['title', ''],
  41. ['special_id', 0],
  42. ['page', 1],
  43. ['limit', 20]
  44. ]);
  45. $batch_list = SpecialBatchModel::getBatchList($where);
  46. return Json::successlayui($batch_list);
  47. }
  48. public function add_batch()
  49. {
  50. return $this->fetch();
  51. }
  52. public function save_batch()
  53. {
  54. $data = parent::postMore([
  55. ['title', ''],
  56. ['special_id', 0],
  57. ['total_num', 1],
  58. ['status', 0],
  59. ['remark', '']
  60. ]);
  61. if (!isset($data['special_id']) || $data['special_id'] <= 0 || !is_numeric($data['special_id'])) return Json::fail('请选择专题');
  62. if (!isset($data['total_num']) || $data['total_num'] <= 0 || !is_numeric($data['total_num'])) return Json::fail('制卡未填写或不合法');
  63. if ($data['total_num'] > 6000) return Json::fail('单次制卡数量最高不得超过6000张');
  64. try {
  65. SpecialBatchModel::beginTrans();
  66. $special_id = $data['special_id'];
  67. $data['add_time'] = time();
  68. $batch_id = SpecialBatchModel::addBatch($data);
  69. $batch_card = SpecialExchange::addCard($batch_id, $data['total_num'], $special_id);
  70. if ($batch_id && $batch_card) {
  71. $qrcodeUrl = SpecialExchange::qrcodes_url($special_id, 5);
  72. SpecialBatchModel::where('id', $batch_id)->update(['qrcode' => $qrcodeUrl]);
  73. }
  74. SpecialBatchModel::commitTrans();
  75. return Json::successful('添加成功');
  76. } catch (\Exception $e) {
  77. SpecialBatchModel::rollbackTrans();
  78. return Json::fail('添加失败');
  79. }
  80. }
  81. /**
  82. * 快速编辑
  83. * @param string $field 字段名
  84. * @param int $id 修改的主键
  85. * @param string value 修改后的值
  86. * @return json
  87. */
  88. public function set_value($field, $id, $value, $model_type)
  89. {
  90. if ($model_type == 'special_batch' && $field != 'remark') {
  91. $use = SpecialExchange::where('card_batch_id', $id)->where('use_uid', '>', 0)->count();
  92. if ($use) return Json::fail('此批次卡片已经在使用当中,无法进行此非法操作');
  93. }
  94. if ($model_type == 'special_exchange' && $id) {
  95. $card = SpecialExchange::where(['id' => $id, 'use_uid' => ['>', 0]])->find();
  96. if ($card) return Json::fail('此卡片已经在使用当中,无法进行此非法操作');
  97. }
  98. $res1 = true;
  99. if ($model_type == 'special_batch') {
  100. $res = SpecialBatchModel::saveFieldByWhere(['id' => $id], [$field => $value]);
  101. if ($res && $field == 'status') {
  102. $res1 = SpecialExchange::saveFieldByWhere(['card_batch_id' => $id], [$field => $value]);
  103. }
  104. } else {
  105. $res = SpecialExchange::saveFieldByWhere(['id' => $id], [$field => $value]);
  106. }
  107. if ($res && $res1)
  108. return Json::successful('保存成功');
  109. else
  110. return Json::fail('保存失败');
  111. }
  112. /**兑换码列表
  113. * @return mixed
  114. */
  115. public function card_index()
  116. {
  117. $data = parent::getMore([
  118. ['activity_type', 2],
  119. ['card_batch_id', 0],
  120. ]);
  121. $batch_list = SpecialBatchModel::getBatchAll([]);
  122. $this->assign([
  123. 'activity_type' => $data['activity_type'],
  124. 'card_batch_id' => $data['card_batch_id'],
  125. 'batch_list' => $batch_list ? $batch_list->toArray() : []
  126. ]);
  127. return $this->fetch();
  128. }
  129. /**
  130. * 获取兑换码
  131. */
  132. public function card_list()
  133. {
  134. $card_batch_id = $this->request->param('card_batch_id', 0);
  135. $excel = $this->request->param('excel', 0);
  136. $where = parent::getMore([
  137. ['start_time', ''],
  138. ['end_time', ''],
  139. ['exchange_code', ''],
  140. ['phone', ''],
  141. ['card_batch_id', 0],
  142. ['is_use', ''],
  143. ['is_status', ''],
  144. ['page', 1],
  145. ['limit', 20],
  146. ['excel', $excel],
  147. ]);
  148. $where['card_batch_id'] = $where['card_batch_id'] > 0 ? $where['card_batch_id'] : $card_batch_id;
  149. $card_list = SpecialExchange::getCardList($where);
  150. return Json::successlayui($card_list);
  151. }
  152. /**删除
  153. * @param int $id
  154. */
  155. public function delete($id = 0)
  156. {
  157. $res = SpecialBatchModel::delSpecialBatch($id);
  158. if (!$res)
  159. return Json::fail(SpecialBatchModel::getErrorInfo('删除失败,请稍候再试!'));
  160. else
  161. return Json::successful('删除成功!');
  162. }
  163. }