StoreSeckillActive.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\controller\admin\store;
  3. use app\common\repositories\store\StoreSeckillProductRepository;
  4. use think\App;
  5. use crmeb\basic\BaseController;
  6. use app\common\repositories\store\StoreSeckillActiveRepository as repository;
  7. use app\validate\admin\StoreSeckillActiveValidate as validate;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. /**
  12. * 秒杀活动
  13. */
  14. class StoreSeckillActive extends BaseController
  15. {
  16. protected $repository;
  17. //创建活动参数
  18. const DATA_PARAMS = [
  19. 'name',
  20. 'seckill_time_ids',
  21. 'start_day',
  22. 'end_day',
  23. 'all_pay_count',
  24. 'once_pay_count',
  25. 'product_category_ids',
  26. 'status',
  27. 'atmosphere_pic',
  28. 'border_pic',
  29. 'product_list',
  30. 'active_status',
  31. 'sign',
  32. ['start_time',0],
  33. ['end_time',0]
  34. ];
  35. /**
  36. * StoreSeckillActive constructor.
  37. * @param App $app
  38. * @param repository $repository
  39. */
  40. public function __construct(App $app, repository $repository)
  41. {
  42. parent::__construct($app);
  43. $this->repository = $repository;
  44. }
  45. /**
  46. * 获取秒杀活动列表
  47. * @return void
  48. * FerryZhao 2024/4/11
  49. */
  50. public function list()
  51. {
  52. $where = $this->request->params([
  53. 'name',
  54. 'date',
  55. 'active_status',
  56. 'seckill_active_status'
  57. ]);
  58. [$page, $limit] = $this->getPage();
  59. $append = ['status_text', 'seckill_time_text_arr', 'atmosphere_pic', 'border_pic'];
  60. $list = $this->repository->getList($where, $page, $limit, $append);
  61. return app('json')->success($list);
  62. }
  63. /**
  64. * 返回所有列表数据-下拉
  65. * FerryZhao 2024/4/12
  66. */
  67. public function select()
  68. {
  69. return app('json')->success('获取成功', $this->repository->getActiveAll());
  70. }
  71. /**
  72. * 获取秒杀活动详情
  73. * @return void
  74. * FerryZhao 2024/4/12
  75. */
  76. public function detail($id)
  77. {
  78. if (!$id) {
  79. return app('json')->fail('参数异常');
  80. }
  81. $exists = $this->repository->exists($id);
  82. if (!$exists)
  83. return app('json')->fail('数据不存在');
  84. $info = $this->repository->get($id)->append(['status_text', 'seckill_time_text_arr', 'atmosphere_pic', 'border_pic']) ?? [];
  85. return app('json')->success('获取成功', $info);
  86. }
  87. /**
  88. * 创建秒杀活动(平台添加秒杀商品)
  89. * @param validate $validate
  90. * @return void
  91. * FerryZhao 2024/4/11
  92. */
  93. public function create(validate $validate)
  94. {
  95. $param = $this->request->params(self::DATA_PARAMS);
  96. $validate->check($param);
  97. if($param['once_pay_count'] > $param['all_pay_count']){
  98. return app('json')->fail('单次限购不能大于活动限购');
  99. }
  100. $activeInfo = $this->repository->create($param);
  101. //需要添加商品
  102. if (isset($param['product_list']) && !empty($param['product_list'])) {
  103. app()->make(StoreSeckillProductRepository::class)->createSeckillProduct($activeInfo->seckill_active_id, $param['product_list'],1);
  104. $this->repository->updateActiveChart($activeInfo->seckill_active_id);
  105. }
  106. return app('json')->success('添加成功', $activeInfo);
  107. }
  108. /**
  109. * 编辑秒杀活动(平台编辑秒杀商品)
  110. * @param int $id 活动ID
  111. * @param validate $validate
  112. * @return void
  113. * FerryZhao 2024/4/12
  114. */
  115. public function update($id, validate $validate)
  116. {
  117. if (!$id) {
  118. return app('json')->fail('参数异常');
  119. }
  120. $param = $this->request->params(self::DATA_PARAMS);
  121. $validate->check($param);
  122. if($param['once_pay_count'] > $param['all_pay_count']){
  123. return app('json')->fail('单次限购不能大于活动限购');
  124. }
  125. $this->repository->updateActive($id, $param);
  126. //需要更新商品
  127. if (isset($param['product_list'])) {
  128. // app()->make(StoreSeckillProductRepository::class)->updateSeckillProduct($id, $param['product_list']);
  129. app()->make(StoreSeckillProductRepository::class)->createSeckillProduct($id, $param['product_list'],1);
  130. $this->repository->updateActiveChart($id);
  131. }
  132. return app('json')->success('编辑成功');
  133. }
  134. /**
  135. * 修改秒杀活动状态
  136. * @param $id
  137. * @return void
  138. * FerryZhao 2024/4/12
  139. */
  140. public function update_status($id)
  141. {
  142. if (!$id) {
  143. return app('json')->fail('参数异常');
  144. }
  145. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  146. $exists = $this->repository->exists($id);
  147. if (!$exists)
  148. return app('json')->fail('数据不存在');
  149. $result = $this->repository->updateStatus($id, $status);
  150. if ($result) {
  151. return app('json')->success('操作成功');
  152. } else {
  153. return app('json')->fail('操作失败');
  154. }
  155. }
  156. /**
  157. * 删除秒杀活动
  158. * @param $id
  159. * @return void
  160. * FerryZhao 2024/4/12
  161. * @throws DataNotFoundException
  162. * @throws DbException
  163. * @throws ModelNotFoundException
  164. */
  165. public function delete($id)
  166. {
  167. if (!$id) {
  168. return app('json')->fail('参数异常');
  169. }
  170. $result = $this->repository->deleteActive($id);
  171. if ($result) {
  172. return app('json')->success('删除成功');
  173. } else {
  174. return app('json')->fail('上岗失败');
  175. }
  176. }
  177. /**
  178. * 活动统计-面板
  179. * @return array[]|void
  180. * FerryZhao 2024/4/19
  181. */
  182. public function chart_panel($id)
  183. {
  184. if (!$id) {
  185. return app('json')->fail('参数错误');
  186. }
  187. $exists = $this->repository->exists($id);
  188. if (!$exists)
  189. return app('json')->fail('活动不存在');
  190. return app('json')->success($this->repository->chartPanel($id));
  191. }
  192. /**
  193. * 活动参与人统计列表
  194. * @param $id
  195. * FerryZhao 2024/4/22
  196. */
  197. public function chart_people($id)
  198. {
  199. if(!$id){
  200. return app('json')->fail('参数异常');
  201. }
  202. $exists = $this->repository->exists($id);
  203. if (!$exists)
  204. return app('json')->fail('活动不存在');
  205. [$page, $limit] = $this->getPage();
  206. $where = $this->request->params(['keyword','date']);
  207. $merId = $this->request->param('mer_id') ?: null;
  208. return app('json')->success($this->repository->chartPeople($id,$merId,$where,$page,$limit));
  209. }
  210. /**
  211. * 活动订单统计列表
  212. * @param $id
  213. * FerryZhao 2024/4/28
  214. */
  215. public function chart_order($id)
  216. {
  217. if(!$id){
  218. return app('json')->fail('参数异常');
  219. }
  220. $exists = $this->repository->exists($id);
  221. if (!$exists)
  222. return app('json')->fail('活动不存在');
  223. [$page, $limit] = $this->getPage();
  224. $where = $this->request->params(['keyword','status','date']);
  225. $merId = $this->request->param('mer_id') ?: null;
  226. return app('json')->success($this->repository->chartOrder($id,$merId,$where,$page,$limit));
  227. }
  228. /**
  229. * 活动商品统计列表
  230. * @param $id
  231. * FerryZhao 2024/4/28
  232. */
  233. public function chart_product($id)
  234. {
  235. if (!$id) {
  236. return app('json')->fail('参数异常');
  237. }
  238. $exists = $this->repository->exists($id);
  239. if (!$exists)
  240. return app('json')->fail('活动不存在');
  241. [$page, $limit] = $this->getPage();
  242. $where = $this->request->params(['keyword']);
  243. $merId = $this->request->param('mer_id') ?: null;
  244. return app('json')->success($this->repository->chartProduct($id,$merId,$where,$page,$limit));
  245. }
  246. }