ProductGroup.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\controller\merchant\store\product;
  3. use app\common\repositories\store\product\ProductRepository;
  4. use ln\jobs\ChangeSpuStatusJob;
  5. use think\App;
  6. use ln\basic\BaseController;
  7. use app\common\repositories\store\product\ProductGroupRepository;
  8. use app\validate\merchant\StoreProductGroupValidate;
  9. use think\exception\ValidateException;
  10. class ProductGroup extends BaseController
  11. {
  12. protected $repository ;
  13. /**
  14. * ProductGroup constructor.
  15. * @param App $app
  16. * @param ProductGroupRepository $repository
  17. */
  18. public function __construct(App $app ,ProductGroupRepository $repository)
  19. {
  20. parent::__construct($app);
  21. $this->repository = $repository;
  22. }
  23. public function lst()
  24. {
  25. [$page, $limit] = $this->getPage();
  26. $where = $this->request->params(['product_status','keyword','active_type','status','us_status','product_group_id']);
  27. $where['is_show'] = $where['status'];
  28. unset($where['status']);
  29. $where['mer_id'] = $this->request->merId();
  30. return app('json')->success($this->repository->getMerchantList($where,$page,$limit));
  31. }
  32. public function create(StoreProductGroupValidate $validate)
  33. {
  34. $data = $this->checkParams($validate);
  35. $this->repository->create($this->request->merId(),$data);
  36. return app('json')->success('添加成功');
  37. }
  38. public function detail($id)
  39. {
  40. $where = [
  41. $this->repository->getPk() => $id,
  42. 'mer_id' => $this->request->merId()
  43. ];
  44. if(!$this->repository->getWhere($where))
  45. return app('json')->fail('数据不存在');
  46. $data = $this->repository->detail($this->request->merId(),$id);
  47. return app('json')->success($data);
  48. }
  49. public function delete($id)
  50. {
  51. $where = [
  52. $this->repository->getPk() => $id,
  53. 'mer_id' => $this->request->merId()
  54. ];
  55. if(!$this->repository->getWhere($where))
  56. return app('json')->fail('数据不存在');
  57. $this->repository->update($id,['is_del' => 1]);
  58. Queue(ChangeSpuStatusJob::class,['product_type' => 4 ,'id' => $id]);
  59. return app('json')->success('删除成功');
  60. }
  61. public function update($id,StoreProductGroupValidate $validate)
  62. {
  63. $data = $this->checkParams($validate);
  64. $where = [
  65. $this->repository->getPk() => $id,
  66. 'mer_id' => $this->request->merId()
  67. ];
  68. if(!$this->repository->getWhere($where))
  69. return app('json')->fail('数据不存在');
  70. $this->repository->edit($id,$data);
  71. return app('json')->success('编辑成功');
  72. }
  73. public function switchStatus($id)
  74. {
  75. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  76. if(!$this->repository->detail($this->request->merId(),$id))
  77. return app('json')->fail('数据不存在');
  78. $this->repository->update($id, ['is_show' => $status]);
  79. Queue(ChangeSpuStatusJob::class,['product_type' => 4 ,'id' => $id]);
  80. return app('json')->success('修改成功');
  81. }
  82. public function checkParams(StoreProductGroupValidate $validate)
  83. {
  84. $params = [
  85. "image", "slider_image", "store_name", "store_info", "product_id","is_show","temp_id","once_pay_count","guarantee_template_id",
  86. "start_time", "end_time", "buying_num", "buying_count_num", "status","pay_count","time","ficti_status","ficti_num","attrValue",'unit_name','content','sort'
  87. ];
  88. $data = $this->request->params($params);
  89. if($data['buying_count_num'] < 2) throw new ValidateException('开团人数不得少于2人');
  90. if($data['end_time'] < $data['start_time']) throw new ValidateException('活动开始时间必须大于结束时间');
  91. $validate->check($data);
  92. return $data;
  93. }
  94. public function updateSort($id)
  95. {
  96. $sort = $this->request->param('sort');
  97. $this->repository->updateSort($id,$this->request->merId(),['sort' => $sort]);
  98. return app('json')->success('修改成功');
  99. }
  100. public function preview(ProductRepository $repository)
  101. {
  102. $data = $this->request->param();
  103. $data['merchant'] = [
  104. 'mer_name' => $this->request->merchant()->mer_name,
  105. 'is_trader' => $this->request->merchant()->is_trader,
  106. 'mer_avatar' => $this->request->merchant()->mer_avatar,
  107. 'product_score' => $this->request->merchant()->product_score,
  108. 'service_score' => $this->request->merchant()->service_score,
  109. 'postage_score' => $this->request->merchant()->postage_score,
  110. 'service_phone' => $this->request->merchant()->service_phone,
  111. 'care_count' => $this->request->merchant()->care_count,
  112. 'type_name' => $this->request->merchant()->type_name->type_name ?? '',
  113. 'care' => true,
  114. 'recommend' => $this->request->merchant()->recommend,
  115. ];
  116. $data['mer_id'] = $this->request->merId();
  117. $data['status'] = 1;
  118. $data['mer_status'] = 1;
  119. $data['rate'] = 3;
  120. return app('json')->success($repository->preview($data));
  121. }
  122. }