SeckillProduct.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace app\controller\merchant\store\seckill;
  3. use app\common\repositories\store\product\SpuRepository;
  4. use app\common\repositories\store\StoreSeckillActiveRepository;
  5. use app\common\repositories\store\StoreSeckillProductRepository;
  6. use app\validate\merchant\StoreSeckillProductValidate as validate;
  7. use crmeb\basic\BaseController;
  8. use app\common\repositories\store\product\ProductRepository;
  9. use think\App;
  10. use think\exception\ValidateException;
  11. class SeckillProduct extends BaseController
  12. {
  13. protected $repository ;
  14. /**
  15. * SeckillProduct constructor.
  16. * @param App $app
  17. * @param ProductRepository $repository
  18. */
  19. public function __construct(App $app ,ProductRepository $repository)
  20. {
  21. parent::__construct($app);
  22. $this->repository = $repository;
  23. }
  24. /**
  25. * 获取添加秒杀商品时的商品列表
  26. * @return void
  27. * FerryZhao 2024/4/16
  28. */
  29. public function get_product_list()
  30. {
  31. $productWhere = $this->request->params(['keyword','sys_labels','us_status','cate_ids',['in_type','0,1'],
  32. ['status',1]]);
  33. $merWhere = $this->request->params(['type_id','category_id']);
  34. $merWhere['status'] = 1;
  35. $merWhere['mer_state'] = 1;
  36. $merWhere['is_del'] = 0;
  37. if($this->request->merId()){
  38. $merWhere['mer_id'] = $this->request->merId();
  39. $productWhere['active_id'] = $this->request->param('active_id');
  40. $productWhere['product_type'] = 0;
  41. }
  42. [$page, $limit] = $this->getPage();
  43. return app('json')->success($this->repository->getProductList($merWhere,$productWhere,$page,$limit));
  44. }
  45. /**
  46. * 商户秒杀商品列表
  47. * FerryZhao 2024/4/17
  48. */
  49. public function get_page_list()
  50. {
  51. [$page, $limit] = $this->getPage();
  52. $where = $this->request->params(['keyword','status','seckill_active_id','us_status','active_status','active_name','sys_labels','mer_cate_id','keyword','is_trader','sort','mer_labels']);
  53. if($where['is_trader'] == ''){
  54. unset($where['is_trader']);
  55. }
  56. $merId = $this->request->merId();
  57. $type = $this->request->param('type');
  58. if($type){
  59. $statusWhere = $this->repository->switchType($type, $merId, 1);
  60. unset($statusWhere['star']);
  61. $where = array_merge($where, $statusWhere);
  62. }
  63. $where['product_type'] = 1;
  64. return app('json')->success($this->repository->getAdminSeckillPageList($merId, $where, $page, $limit));
  65. }
  66. /**
  67. * 商户秒杀商品列表
  68. * FerryZhao 2024/4/17
  69. */
  70. public function get_list()
  71. {
  72. $where = $this->request->params(['keyword','status','seckill_active_id','us_status','active_status','active_name','sys_labels','mer_cate_id','keyword','is_trader','sort','mer_labels']);
  73. if($where['is_trader'] == ''){
  74. unset($where['is_trader']);
  75. }
  76. $merId = $this->request->merId();
  77. $type = $this->request->param('type');
  78. if($type){
  79. $statusWhere = $this->repository->switchType($type, $merId, 1);
  80. unset($statusWhere['star']);
  81. $where = array_merge($where, $statusWhere);
  82. }
  83. $where['product_type'] = 1;
  84. return app('json')->success($this->repository->getAdminSeckillList($merId, $where));
  85. }
  86. /**
  87. * 商家参与活动添加商品
  88. * FerryZhao 2024/4/17
  89. */
  90. public function create()
  91. {
  92. $param = $this->request->param();
  93. if(!isset($param['active_id']) || !$param['active_id'])
  94. return app('json')->fail('参数错误');
  95. if(!isset($param['product_list']))
  96. return app('json')->fail('参数错误');
  97. app()->make(StoreSeckillProductRepository::class)->createSeckillProduct($param['active_id'],$param['product_list'],$this->request->merchant()->is_audit ? 0 : 1);
  98. app()->make(StoreSeckillActiveRepository::class)->updateActiveChart($param['active_id']);//回填商家数和商品数
  99. return app('json')->success('活动参加成功');
  100. }
  101. /**
  102. * 秒杀商品详情
  103. * @param $id
  104. * FerryZhao 2024/4/17
  105. */
  106. public function detail($id)
  107. {
  108. if(!$this->repository->merExists($this->request->merId(),$id))
  109. return app('json')->fail('数据不存在');
  110. return app('json')->success($this->repository->getAdminOneProduct($id,null));
  111. }
  112. /**
  113. * 改变商品状态
  114. * @param $id
  115. * FerryZhao 2024/4/19
  116. */
  117. public function switchStatus($id)
  118. {
  119. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  120. if(!$this->repository->merExists($this->request->merId(),$id))
  121. return app('json')->fail('数据不存在');
  122. $this->repository->switchShow($id, $status,'is_show',$this->request->merId());
  123. return app('json')->success('修改成功');
  124. }
  125. /**
  126. * 编辑秒杀商品
  127. * @param $id
  128. * @param validate $validate
  129. * FerryZhao 2024/4/19
  130. */
  131. public function update($id, validate $validate)
  132. {
  133. if(!$id){
  134. return app('json')->fail('参数错误');
  135. }
  136. if(!$this->repository->merExists($this->request->merId(),$id)){
  137. return app('json')->fail('数据不存在');
  138. }
  139. $attrValue = $this->request->param('attr_value');
  140. if(!is_array($attrValue) || empty($attrValue)){
  141. return app('json')->fail('sku参数错误');
  142. }
  143. $field = ['value_id','price','stock'];
  144. try {
  145. $minPrice = $this->repository->saveAllSku($id,$attrValue,$field);
  146. }catch (\Exception $e){
  147. return app('json')->fail($e->getMessage());
  148. }
  149. // $updateProductStatusResult = app(ProductRepository::class)->getSearch([])->where(['product_id'=>$id])->update(['status'=>0]);
  150. $sort = $this->request->param('sort');
  151. $this->repository->updateSort($id,$this->request->merId(),['sort' => $sort ?? 0, 'price' => $minPrice]);
  152. return app('json')->success('操作成功');
  153. }
  154. /**
  155. * 秒杀状态统计
  156. * FerryZhao 2024/4/19
  157. */
  158. public function getStatusFilter()
  159. {
  160. return app('json')->success($this->repository->getFilter($this->request->merId(),'秒杀商品',1));
  161. }
  162. /**
  163. * 排序
  164. * @param $id
  165. * FerryZhao 2024/4/22
  166. */
  167. public function updateSort($id)
  168. {
  169. $sort = $this->request->param('sort');
  170. $this->repository->updateSort($id,$this->request->merId(),['sort' => $sort]);
  171. return app('json')->success('修改成功');
  172. }
  173. /**
  174. * 预览
  175. * FerryZhao 2024/4/22
  176. */
  177. public function preview()
  178. {
  179. $data = $this->request->param();
  180. $data['merchant'] = [
  181. 'mer_name' => $this->request->merchant()->mer_name,
  182. 'is_trader' => $this->request->merchant()->is_trader,
  183. 'mer_avatar' => $this->request->merchant()->mer_avatar,
  184. 'product_score' => $this->request->merchant()->product_score,
  185. 'service_score' => $this->request->merchant()->service_score,
  186. 'postage_score' => $this->request->merchant()->postage_score,
  187. 'service_phone' => $this->request->merchant()->service_phone,
  188. 'care_count' => $this->request->merchant()->care_count,
  189. 'type_name' => $this->request->merchant()->type_name->type_name ?? '',
  190. 'care' => true,
  191. 'recommend' => $this->request->merchant()->recommend,
  192. ];
  193. $data['mer_id'] = $this->request->merId();
  194. $data['status'] = 1;
  195. $data['mer_status'] = 1;
  196. $data['rate'] = 3;
  197. return app('json')->success($this->repository->preview($data));
  198. }
  199. /**
  200. * 设置商品标签
  201. * @param $id
  202. * FerryZhao 2024/4/19
  203. */
  204. public function setLabels($id)
  205. {
  206. if(!$id)
  207. return app('json')->fail('参数错误');
  208. $data = $this->request->params(['mer_labels']);
  209. app()->make(SpuRepository::class)->setLabels($id,1,$data,$this->request->merId());
  210. return app('json')->success('修改成功');
  211. }
  212. /**
  213. * 加入回收站
  214. * @param $id
  215. * FerryZhao 2024/4/22
  216. */
  217. public function delete($id)
  218. {
  219. if(!$id)
  220. return app('json')->fail('参数错误');
  221. if(!$this->repository->merExists($this->request->merId(),$id))
  222. return app('json')->fail('数据不存在');
  223. if($this->repository->getWhereCount(['product_id' => $id,'is_show' => 1,'status' => 1]))
  224. return app('json')->fail('商品上架中');
  225. $activeId = $this->repository->getSearch([])->where(['product_id'=>$id])->value('active_id');
  226. $this->repository->delete($id);
  227. $storeSeckillActiveRepository = app()->make(StoreSeckillActiveRepository::class);
  228. $storeSeckillActiveRepository->updateActiveChart($activeId);
  229. // $this->repository->destory($id);
  230. return app('json')->success('已成功加入回收站');
  231. }
  232. /**
  233. * 删除秒杀商品
  234. * @param $id
  235. */
  236. public function destory($id,StoreSeckillActiveRepository $storeSeckillActiveRepository)
  237. {
  238. if(!$id)
  239. return app('json')->fail('参数错误');
  240. if(!$this->repository->merDeleteExists($this->request->merId(),$id))
  241. return app('json')->fail('只能删除回收站的商品');
  242. $activeId = $this->repository->getOnlyTranshed(['product_id' => $id])->value('active_id');
  243. $this->repository->destory($id);
  244. $storeSeckillActiveRepository->updateActiveChart($activeId);
  245. return app('json')->success('删除成功');
  246. }
  247. /**
  248. * 恢复秒杀商品
  249. * @param $id
  250. * FerryZhao 2024/4/22
  251. */
  252. public function restore($id)
  253. {
  254. if(!$this->repository->merDeleteExists($this->request->merId(),$id))
  255. return app('json')->fail('只能恢复回收站的商品');
  256. $this->repository->restore($id);
  257. $activeId = $this->repository->getSearch([])->where(['product_id'=>$id])->value('active_id');
  258. $storeSeckillActiveRepository = app()->make(StoreSeckillActiveRepository::class);
  259. $storeSeckillActiveRepository->updateActiveChart($activeId);
  260. return app('json')->success('商品已恢复');
  261. }
  262. }