ProductGroup.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant\store\product;
  12. use app\common\repositories\store\product\ProductRepository;
  13. use app\common\repositories\store\product\SpuRepository;
  14. use think\App;
  15. use crmeb\basic\BaseController;
  16. use app\common\repositories\store\product\ProductGroupRepository;
  17. use app\validate\merchant\StoreProductGroupValidate;
  18. use think\exception\ValidateException;
  19. class ProductGroup extends BaseController
  20. {
  21. protected $repository ;
  22. /**
  23. * ProductGroup constructor.
  24. * @param App $app
  25. * @param ProductGroupRepository $repository
  26. */
  27. public function __construct(App $app ,ProductGroupRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. public function lst()
  33. {
  34. [$page, $limit] = $this->getPage();
  35. $where = $this->request->params(['product_status','keyword','active_type','status','us_status','product_group_id','mer_labels']);
  36. $where['is_show'] = $where['status'];
  37. unset($where['status']);
  38. $where['mer_id'] = $this->request->merId();
  39. return app('json')->success($this->repository->getMerchantList($where,$page,$limit));
  40. }
  41. public function create(StoreProductGroupValidate $validate)
  42. {
  43. $data = $this->checkParams($validate);
  44. $this->repository->create($this->request->merId(),$data);
  45. return app('json')->success('添加成功');
  46. }
  47. public function detail($id)
  48. {
  49. $where = [
  50. $this->repository->getPk() => $id,
  51. 'mer_id' => $this->request->merId()
  52. ];
  53. if(!$this->repository->getWhere($where))
  54. return app('json')->fail('数据不存在');
  55. $data = $this->repository->detail($this->request->merId(),$id);
  56. return app('json')->success($data);
  57. }
  58. public function delete($id)
  59. {
  60. $where = [
  61. $this->repository->getPk() => $id,
  62. 'mer_id' => $this->request->merId()
  63. ];
  64. if(!$this->repository->getWhere($where))
  65. return app('json')->fail('数据不存在');
  66. $this->repository->update($id,['is_del' => 1]);
  67. event('product.groupDelete',compact('id'));
  68. app()->make(SpuRepository::class)->changeStatus($id,4);
  69. return app('json')->success('删除成功');
  70. }
  71. public function update($id,StoreProductGroupValidate $validate)
  72. {
  73. $data = $this->checkParams($validate);
  74. $where = [
  75. $this->repository->getPk() => $id,
  76. 'mer_id' => $this->request->merId()
  77. ];
  78. if(!$this->repository->getWhere($where))
  79. return app('json')->fail('数据不存在');
  80. $this->repository->edit($id,$data);
  81. return app('json')->success('编辑成功');
  82. }
  83. public function switchStatus($id)
  84. {
  85. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  86. if(!$this->repository->detail($this->request->merId(),$id))
  87. return app('json')->fail('数据不存在');
  88. $this->repository->update($id, ['is_show' => $status]);
  89. app()->make(SpuRepository::class)->changeStatus($id,4);
  90. return app('json')->success('修改成功');
  91. }
  92. public function checkParams(StoreProductGroupValidate $validate)
  93. {
  94. $params = [
  95. "image", "slider_image", "store_name", "store_info", "product_id","is_show","temp_id","once_pay_count","guarantee_template_id",
  96. "start_time", "end_time", "buying_num", "buying_count_num", "status","pay_count","time","ficti_status","ficti_num","attrValue",
  97. 'unit_name','content','sort','mer_labels','delivery_way','delivery_free'
  98. ];
  99. $data = $this->request->params($params);
  100. if($data['buying_count_num'] < 2) throw new ValidateException('开团人数不得少于2人');
  101. if($data['end_time'] < $data['start_time']) throw new ValidateException('活动开始时间必须大于结束时间');
  102. $validate->check($data);
  103. return $data;
  104. }
  105. public function updateSort($id)
  106. {
  107. $sort = $this->request->param('sort');
  108. $this->repository->updateSort($id,$this->request->merId(),['sort' => $sort]);
  109. return app('json')->success('修改成功');
  110. }
  111. public function preview(ProductRepository $repository)
  112. {
  113. $data = $this->request->param();
  114. $data['merchant'] = [
  115. 'mer_name' => $this->request->merchant()->mer_name,
  116. 'is_trader' => $this->request->merchant()->is_trader,
  117. 'mer_avatar' => $this->request->merchant()->mer_avatar,
  118. 'product_score' => $this->request->merchant()->product_score,
  119. 'service_score' => $this->request->merchant()->service_score,
  120. 'postage_score' => $this->request->merchant()->postage_score,
  121. 'service_phone' => $this->request->merchant()->service_phone,
  122. 'care_count' => $this->request->merchant()->care_count,
  123. 'type_name' => $this->request->merchant()->type_name->type_name ?? '',
  124. 'care' => true,
  125. 'recommend' => $this->request->merchant()->recommend,
  126. ];
  127. $data['mer_id'] = $this->request->merId();
  128. $data['status'] = 1;
  129. $data['mer_status'] = 1;
  130. $data['rate'] = 3;
  131. return app('json')->success($repository->preview($data));
  132. }
  133. public function setLabels($id)
  134. {
  135. $data = $this->request->params(['mer_labels']);
  136. // if (empty($data['mer_labels'])) return app('json')->fail('标签为空');
  137. app()->make(SpuRepository::class)->setLabels($id,4,$data,$this->request->merId());
  138. return app('json')->success('修改成功');
  139. }
  140. }