LuckLottery.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\controller\admin\v1\marketing\lottery;
  13. use app\controller\admin\AuthController;
  14. use app\services\activity\lottery\LuckLotteryServices;
  15. use think\facade\App;
  16. /**
  17. * 抽奖活动
  18. * Class LuckLottery
  19. * @package app\controller\admin\v1\marketing\lottery
  20. */
  21. class LuckLottery extends AuthController
  22. {
  23. /**
  24. * LuckLottery constructor.
  25. * @param App $app
  26. * @param LuckLotteryServices $services
  27. */
  28. public function __construct(App $app, LuckLotteryServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. }
  33. /**
  34. * 列表
  35. * @return mixed
  36. */
  37. public function index()
  38. {
  39. $where = $this->request->postMore([
  40. ['start_status', '', '', 'start'],
  41. ['status', ''],
  42. ['factor', ''],
  43. ['store_name', '', '', 'keyword'],
  44. ]);
  45. return $this->success($this->services->getList($where));
  46. }
  47. /**
  48. * id查询详情
  49. * @param $id
  50. * @return mixed
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function detail($id)
  56. {
  57. if (!$id) {
  58. return $this->fail('缺少参数id');
  59. }
  60. return $this->success($this->services->getlotteryInfo((int)$id));
  61. }
  62. /**
  63. * 活动类型查询详情
  64. * @param $factor
  65. * @return mixed
  66. */
  67. public function factorInfo($factor)
  68. {
  69. if (!$factor) {
  70. return app('json')->fail('缺少参数');
  71. }
  72. return app('json')->success($this->services->getlotteryFactorInfo((int)$factor));
  73. }
  74. /**
  75. * 添加
  76. * @return mixed
  77. */
  78. public function add()
  79. {
  80. $data = $this->request->postMore([
  81. ['name', ''],
  82. ['desc', ''],
  83. ['image', ''],
  84. ['factor', 1],
  85. ['factor_num', 1],
  86. ['attends_user', 1],
  87. ['user_level', []],
  88. ['user_label', []],
  89. ['is_svip', 0],
  90. ['period', [0, 0]],
  91. ['lottery_num_term', 1],
  92. ['lottery_num', 1],
  93. ['total_lottery_num', 1],
  94. ['spread_num', 1],
  95. ['is_all_record', 1],
  96. ['is_personal_record', 1],
  97. ['is_content', 1],
  98. ['content', ''],
  99. ['status', 1],
  100. ['prize', []]
  101. ]);
  102. if (!$data['name']) {
  103. return $this->fail('请添加抽奖活动名称');
  104. }
  105. if ($data['is_content'] && !$data['content']) {
  106. return $this->fail('请添加抽奖描述等文案');
  107. }
  108. [$start, $end] = $data['period'];
  109. unset($data['period']);
  110. $data['start_time'] = $start ? strtotime($start) : 0;
  111. $data['end_time'] = $end ? strtotime($end) : 0;
  112. if ($data['start_time'] && $data['end_time'] && $data['end_time'] <= $data['start_time']) {
  113. return $this->fail('活动结束时间必须大于开始时间');
  114. }
  115. if (!$data['prize']) {
  116. return $this->fail('请添加奖品');
  117. }
  118. if (in_array($data['factor'], [1, 2]) && !$data['factor_num']) {
  119. return $this->fail('请填写消耗' . ($data['factor'] == '1' ? '积分' : '余额') . '数量');
  120. }
  121. if ($data['factor'] == 1 && $data['lottery_num'] > $data['total_lottery_num']) {
  122. return $this->fail('积分抽奖时每天抽奖次数不能大于总次数');
  123. }
  124. $this->services->add($data);
  125. return $this->success( '保存成功');
  126. }
  127. /**
  128. * 修改
  129. * @param $id
  130. * @return mixed
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. public function edit($id)
  136. {
  137. $data = $this->request->postMore([
  138. ['name', ''],
  139. ['desc', ''],
  140. ['image', ''],
  141. ['factor', 1],
  142. ['factor_num', 1],
  143. ['attends_user', 1],
  144. ['user_level', []],
  145. ['user_label', []],
  146. ['is_svip', 0],
  147. ['period', [0, 0]],
  148. ['lottery_num_term', 1],
  149. ['lottery_num', 1],
  150. ['total_lottery_num', 1],
  151. ['spread_num', 1],
  152. ['is_all_record', 1],
  153. ['is_personal_record', 1],
  154. ['is_content', 1],
  155. ['content', ''],
  156. ['status', 1],
  157. ['prize', []]
  158. ]);
  159. if (!$id) {
  160. return $this->fail('缺少参数id');
  161. }
  162. if (!$data['name']) {
  163. return $this->fail('请添加抽奖活动名称');
  164. }
  165. [$start, $end] = $data['period'];
  166. unset($data['period']);
  167. $data['start_time'] = $start ? strtotime($start) : 0;
  168. $data['end_time'] = $end ? strtotime($end) : 0;
  169. if ($data['start_time'] && $data['end_time'] && $data['end_time'] <= $data['start_time']) {
  170. return $this->fail('活动结束时间必须大于开始时间');
  171. }
  172. if ($data['is_content'] && !$data['content']) {
  173. return $this->fail('请添加抽奖描述等文案');
  174. }
  175. if (!$data['prize']) {
  176. return $this->fail('请添加奖品');
  177. }
  178. if (in_array($data['factor'], [1, 2]) && !$data['factor_num']) {
  179. return $this->fail('请填写消耗' . ($data['factor'] == '1' ? '积分' : '余额') . '数量');
  180. }
  181. if ($data['factor'] == 1 && $data['lottery_num'] > $data['total_lottery_num']) {
  182. return $this->fail('积分抽奖时每天抽奖次数不能大于总次数');
  183. }
  184. $this->services->edit((int)$id, $data);
  185. return $this->success('保存成功');
  186. }
  187. /**
  188. * 删除
  189. * @param $id
  190. * @throws \Exception
  191. */
  192. public function delete()
  193. {
  194. [$id] = $this->request->getMore([
  195. ['id', 0],
  196. ], true);
  197. if (!$id) return $this->fail('数据不存在');
  198. $this->services->delLottery((int)$id);
  199. return $this->success('刪除成功!');
  200. }
  201. /**
  202. * 设置活动状态
  203. * @return json
  204. */
  205. public function setStatus($id = '', $status = '')
  206. {
  207. if ($status == '' || $id == '') return $this->fail('缺少参数');
  208. return $this->success($this->services->setStatus((int)$id, (int)$status) ? '上架成功' : '下架成功');
  209. }
  210. }