ProductGroup.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. /**
  33. * 获取商家列表
  34. *
  35. * @return \think\response\Json
  36. */
  37. public function lst()
  38. {
  39. // 获取分页参数
  40. [$page, $limit] = $this->getPage();
  41. $where = $this->request->params(['product_status', 'keyword', 'active_type', 'status', 'us_status', 'product_group_id', 'mer_labels']);
  42. // 设置查询条件
  43. $where['is_show'] = $where['status'];
  44. unset($where['status']);
  45. $where['mer_id'] = $this->request->merId();
  46. return app('json')->success($this->repository->getMerchantList($where, $page, $limit));
  47. }
  48. /**
  49. * 创建商品组
  50. *
  51. * @param StoreProductGroupValidate $validate 商品组验证器
  52. * @return \think\response\Json
  53. */
  54. public function create(StoreProductGroupValidate $validate)
  55. {
  56. // 校验参数
  57. $data = $this->checkParams($validate);
  58. $this->repository->create($this->request->merId(), $data);
  59. // 返回操作成功结果
  60. return app('json')->success('添加成功');
  61. }
  62. /**
  63. * 获取指定ID的商品详情
  64. *
  65. * @param int $id 商品ID
  66. * @return \think\response\Json
  67. */
  68. public function detail($id)
  69. {
  70. // 构建查询条件
  71. $where = [
  72. $this->repository->getPk() => $id,
  73. 'mer_id' => $this->request->merId()
  74. ];
  75. if (!$this->repository->getWhere($where))
  76. return app('json')->fail('数据不存在');
  77. $data = $this->repository->detail($this->request->merId(), $id);
  78. // 返回成功响应
  79. return app('json')->success($data);
  80. }
  81. /**
  82. * 根据ID删除商品组
  83. *
  84. * @param int $id 商品组ID
  85. * @return \think\response\Json
  86. */
  87. public function delete($id)
  88. {
  89. // 构建查询条件
  90. $where = [
  91. $this->repository->getPk() => $id, // 主键ID
  92. 'mer_id' => $this->request->merId() // 商家ID
  93. ];
  94. // 判断数据是否存在
  95. if (!$this->repository->getWhere($where))
  96. return app('json')->fail('数据不存在');
  97. // 更新商品组状态为已删除
  98. $this->repository->update($id, ['is_del' => 1]);
  99. // 触发商品组删除事件
  100. event('product.groupDelete', compact('id'));
  101. // 修改SPU状态为已删除
  102. app()->make(SpuRepository::class)->changeStatus($id, 4);
  103. // 返回删除成功信息
  104. return app('json')->success('删除成功');
  105. }
  106. /**
  107. * 更新商品分组
  108. *
  109. * @param int $id 商品分组ID
  110. * @param StoreProductGroupValidate $validate 验证器实例
  111. * @return \think\response\Json
  112. */
  113. public function update($id, StoreProductGroupValidate $validate)
  114. {
  115. // 获取验证通过的参数
  116. $data = $this->checkParams($validate);
  117. // 构建查询条件
  118. $where = [
  119. $this->repository->getPk() => $id,
  120. 'mer_id' => $this->request->merId()
  121. ];
  122. // 判断数据是否存在
  123. if (!$this->repository->getWhere($where))
  124. return app('json')->fail('数据不存在');
  125. // 更新数据
  126. $this->repository->edit($id, $data);
  127. // 返回操作结果
  128. return app('json')->success('编辑成功');
  129. }
  130. /**
  131. * 切换商品组状态
  132. *
  133. * @param int $id 商品组ID
  134. * @return \think\response\Json
  135. */
  136. public function switchStatus($id)
  137. {
  138. // 获取状态值
  139. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  140. if (!$this->repository->detail($this->request->merId(), $id))
  141. return app('json')->fail('数据不存在');
  142. // 更新商品组状态
  143. $this->repository->update($id, ['is_show' => $status]);
  144. app()->make(SpuRepository::class)->changeStatus($id, 4);
  145. return app('json')->success('修改成功');
  146. }
  147. /**
  148. * 检查参数并返回数据
  149. * @param StoreProductGroupValidate $validate 商品组验证器
  150. * @return array 返回验证后的数据
  151. * @throws ValidateException 如果开团人数小于2或活动开始时间大于结束时间或团长分佣比例不在0-100之间则抛出异常
  152. */
  153. public function checkParams(StoreProductGroupValidate $validate)
  154. {
  155. // 定义需要验证的参数
  156. $params = [
  157. "image", "slider_image", "store_name", "store_info", "product_id", "is_show", "temp_id", "once_pay_count", "guarantee_template_id",
  158. "start_time", "end_time", "buying_num", "buying_count_num", "status", "pay_count", "time", "ficti_status", "ficti_num", "attrValue",
  159. 'unit_name', 'content', 'sort', 'mer_labels', 'delivery_way', 'delivery_free', ['leader_extension', 0], ['leader_rate', 0]
  160. ];
  161. // 获取请求参数
  162. $data = $this->request->params($params);
  163. // 如果开团人数小于2则抛出异常
  164. if ($data['buying_count_num'] < 2) throw new ValidateException('开团人数不得少于2人');
  165. // 如果活动开始时间大于结束时间则抛出异常
  166. if ($data['end_time'] < $data['start_time']) throw new ValidateException('活动开始时间必须大于结束时间');
  167. // 如果团长分佣比例不在0-100之间则抛出异常
  168. if ($data['leader_extension'] && ($data['leader_rate'] < 0 || $data['leader_rate'] > 100)) {
  169. throw new ValidateException('团长分佣比例需在0-100之间');
  170. }
  171. // 验证数据
  172. $validate->check($data);
  173. // 返回验证后的数据
  174. return $data;
  175. }
  176. /**
  177. * 更新商品组排序
  178. * @param int $id 商品组ID
  179. * @return \think\response\Json 返回操作结果
  180. */
  181. public function updateSort($id)
  182. {
  183. $sort = $this->request->param('sort');
  184. // 调用商品组仓库的更新排序方法
  185. $this->repository->updateSort($id, $this->request->merId(), ['sort' => $sort]);
  186. // 返回操作成功提示
  187. return app('json')->success('修改成功');
  188. }
  189. /**
  190. * 预览商品信息
  191. *
  192. * @param ProductRepository $repository 商品仓库对象
  193. * @return \think\response\Json 返回JSON格式的预览结果
  194. */
  195. public function preview(ProductRepository $repository)
  196. {
  197. // 获取请求参数
  198. $data = $this->request->param();
  199. // 添加商家信息到请求参数中
  200. $data['merchant'] = [
  201. 'mer_name' => $this->request->merchant()->mer_name,
  202. 'is_trader' => $this->request->merchant()->is_trader,
  203. 'mer_avatar' => $this->request->merchant()->mer_avatar,
  204. 'product_score' => $this->request->merchant()->product_score,
  205. 'service_score' => $this->request->merchant()->service_score,
  206. 'postage_score' => $this->request->merchant()->postage_score,
  207. 'service_phone' => $this->request->merchant()->service_phone,
  208. 'care_count' => $this->request->merchant()->care_count,
  209. 'type_name' => $this->request->merchant()->type_name->type_name ?? '',
  210. 'care' => true,
  211. 'recommend' => $this->request->merchant()->recommend,
  212. ];
  213. // 添加商家ID和状态信息到请求参数中
  214. $data['mer_id'] = $this->request->merId();
  215. $data['status'] = 1;
  216. $data['mer_status'] = 1;
  217. $data['rate'] = 3;
  218. // 调用商品仓库的预览方法并返回JSON格式的预览结果
  219. return app('json')->success($repository->preview($data));
  220. }
  221. /**
  222. * 设置商品标签
  223. *
  224. * @param int $id 商品ID
  225. * @return \think\response\Json 返回JSON格式的操作结果
  226. */
  227. public function setLabels($id)
  228. {
  229. // 获取请求参数中的标签信息
  230. $data = $this->request->params(['mer_labels']);
  231. // if (empty($data['mer_labels'])) return app('json')->fail('标签为空');
  232. // 调用SPU仓库的设置标签方法并返回JSON格式的操作结果
  233. app()->make(SpuRepository::class)->setLabels($id, 4, $data, $this->request->merId());
  234. return app('json')->success('修改成功');
  235. }
  236. }