LotteryPrize.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace app\admin\controller\lottery;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\ValidateException;
  6. use think\exception\PDOException;
  7. use Exception;
  8. /**
  9. * 抽奖奖品管理
  10. *
  11. * @icon fa fa-gift
  12. */
  13. class LotteryPrize extends Backend
  14. {
  15. /**
  16. * LotteryPrize模型对象
  17. * @var \app\admin\model\lottery\LotteryPrize
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\lottery\LotteryPrize;
  24. // 获取枚举值列表
  25. $this->view->assign("typeList", $this->model->getTypeList());
  26. $this->view->assign("deliverTypeList", $this->model->getDeliverTypeList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //当前是否为关联查询
  35. $this->relationSearch = true;
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $list = $this->model
  45. ->with(['activity'])
  46. ->where($where)
  47. ->order($sort, $order)
  48. ->paginate($limit);
  49. foreach ($list as $row) {
  50. $row->visible(['id','activity_id','name','type','image','probability','total_stock','remain_stock','win_count','sort_order','deliver_type','status']);
  51. $row->visible(['activity']);
  52. $row->getRelation('activity')->visible(['name']);
  53. }
  54. $result = array("total" => $list->total(), "rows" => $list->items());
  55. return json($result);
  56. }
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 添加
  61. */
  62. public function add()
  63. {
  64. if ($this->request->isPost()) {
  65. $params = $this->request->post("row/a");
  66. if ($params) {
  67. $params = $this->preExcludeFields($params);
  68. // 处理兑换码
  69. if (isset($params['exchange_codes']) && is_array($params['exchange_codes'])) {
  70. $params['exchange_codes'] = json_encode($params['exchange_codes']);
  71. }
  72. // 初始化剩余库存
  73. if (isset($params['total_stock'])) {
  74. $params['remain_stock'] = $params['total_stock'];
  75. }
  76. $result = false;
  77. Db::startTrans();
  78. try {
  79. //是否采用模型验证
  80. if ($this->modelValidate) {
  81. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  82. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  83. $this->model->validateFailException(true)->validate($validate);
  84. }
  85. $result = $this->model->allowField(true)->save($params);
  86. Db::commit();
  87. } catch (ValidateException $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. } catch (PDOException $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. } catch (Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if ($result !== false) {
  98. $this->success();
  99. } else {
  100. $this->error(__('No rows were inserted'));
  101. }
  102. }
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. return $this->view->fetch();
  106. }
  107. /**
  108. * 编辑
  109. */
  110. public function edit($ids = null)
  111. {
  112. $row = $this->model->get($ids);
  113. if (!$row) {
  114. $this->error(__('No Results were found'));
  115. }
  116. $adminIds = $this->getDataLimitAdminIds();
  117. if (is_array($adminIds)) {
  118. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  119. $this->error(__('You have no permission'));
  120. }
  121. }
  122. if ($this->request->isPost()) {
  123. $params = $this->request->post("row/a");
  124. if ($params) {
  125. $params = $this->preExcludeFields($params);
  126. // 处理兑换码
  127. if (isset($params['exchange_codes']) && is_array($params['exchange_codes'])) {
  128. $params['exchange_codes'] = json_encode($params['exchange_codes']);
  129. }
  130. // 如果修改了总库存,同步修改剩余库存
  131. if (isset($params['total_stock']) && $params['total_stock'] != $row->total_stock) {
  132. $diff = $params['total_stock'] - $row->total_stock;
  133. $params['remain_stock'] = $row->remain_stock + $diff;
  134. }
  135. $result = false;
  136. Db::startTrans();
  137. try {
  138. //是否采用模型验证
  139. if ($this->modelValidate) {
  140. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  141. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  142. $row->validateFailException(true)->validate($validate);
  143. }
  144. $result = $row->allowField(true)->save($params);
  145. Db::commit();
  146. } catch (ValidateException $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. } catch (PDOException $e) {
  150. Db::rollback();
  151. $this->error($e->getMessage());
  152. } catch (Exception $e) {
  153. Db::rollback();
  154. $this->error($e->getMessage());
  155. }
  156. if ($result !== false) {
  157. $this->success();
  158. } else {
  159. $this->error(__('No rows were updated'));
  160. }
  161. }
  162. $this->error(__('Parameter %s can not be empty', ''));
  163. }
  164. // 处理兑换码显示
  165. if ($row->exchange_codes) {
  166. $row->exchange_codes = json_decode($row->exchange_codes, true);
  167. }
  168. $this->view->assign("row", $row);
  169. return $this->view->fetch();
  170. }
  171. /**
  172. * 批量导入兑换码
  173. */
  174. public function importCodes($ids = null)
  175. {
  176. $row = $this->model->get($ids);
  177. if (!$row) {
  178. $this->error(__('No Results were found'));
  179. }
  180. if ($this->request->isPost()) {
  181. $codes = $this->request->post('codes');
  182. if (empty($codes)) {
  183. $this->error('兑换码不能为空');
  184. }
  185. // 处理兑换码列表
  186. $codeArray = array_filter(array_map('trim', explode("\n", $codes)));
  187. $existingCodes = $row->exchange_codes ? json_decode($row->exchange_codes, true) : [];
  188. $newCodes = array_merge($existingCodes, $codeArray);
  189. // 更新奖品
  190. $row->exchange_codes = json_encode($newCodes);
  191. $row->total_stock = count($newCodes);
  192. $row->remain_stock = count($newCodes) - $row->win_count;
  193. if ($row->save()) {
  194. $this->success('导入成功,共导入' . count($codeArray) . '个兑换码');
  195. } else {
  196. $this->error('导入失败');
  197. }
  198. }
  199. $this->view->assign("row", $row);
  200. return $this->view->fetch();
  201. }
  202. /**
  203. * 补充库存
  204. */
  205. public function addStock($ids = null)
  206. {
  207. $row = $this->model->get($ids);
  208. if (!$row) {
  209. $this->error(__('No Results were found'));
  210. }
  211. if ($this->request->isPost()) {
  212. $addCount = $this->request->post('add_count', 0);
  213. if ($addCount <= 0) {
  214. $this->error('补充数量必须大于0');
  215. }
  216. $row->total_stock += $addCount;
  217. $row->remain_stock += $addCount;
  218. if ($row->save()) {
  219. $this->success('库存补充成功');
  220. } else {
  221. $this->error('库存补充失败');
  222. }
  223. }
  224. $this->view->assign("row", $row);
  225. return $this->view->fetch();
  226. }
  227. }