ProductAssist.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\ProductAssistRepository as repository;
  13. use app\common\repositories\store\product\ProductRepository;
  14. use app\common\repositories\store\product\SpuRepository;
  15. use crmeb\basic\BaseController;
  16. use think\App;
  17. use app\validate\merchant\StoreProductAssistValidate;
  18. use think\exception\ValidateException;
  19. class ProductAssist extends BaseController
  20. {
  21. protected $repository ;
  22. /**
  23. * Product constructor.
  24. * @param App $app
  25. * @param repository $repository
  26. */
  27. public function __construct(App $app ,repository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * 列表
  34. * @return mixed
  35. * @author Qinii
  36. * @day 2020-10-12
  37. */
  38. public function lst()
  39. {
  40. [$page, $limit] = $this->getPage();
  41. $where = $this->request->params(['product_status','keyword','is_show','type','presell_type','us_status','product_assist_id','mer_labels']);
  42. $where['mer_id'] = $this->request->merId();
  43. return app('json')->success($this->repository->getMerchantList($where,$page,$limit));
  44. }
  45. /**
  46. * 添加
  47. * @param StoreProductAssistValidate $validate
  48. * @return mixed
  49. * @author Qinii
  50. * @day 2020-10-12
  51. */
  52. public function create(StoreProductAssistValidate $validate)
  53. {
  54. $data = $this->checkParams($validate);
  55. $this->repository->create($this->request->merId(),$data);
  56. return app('json')->success('添加成功');
  57. }
  58. /**
  59. * 详情
  60. * @param $id
  61. * @return mixed
  62. * @author Qinii
  63. * @day 2020-10-12
  64. */
  65. public function detail($id)
  66. {
  67. $data = $this->repository->detail($this->request->merId(),$id);
  68. return app('json')->success($data);
  69. }
  70. /**
  71. * 更新指定ID的商品辅助信息
  72. *
  73. * @param int $id 商品辅助信息ID
  74. * @param StoreProductAssistValidate $validate 商品辅助信息验证器
  75. * @return \Psr\Http\Message\ResponseInterface JSON格式的编辑成功结果
  76. */
  77. public function update($id, StoreProductAssistValidate $validate)
  78. {
  79. // 获取参数并验证
  80. // 获取参数并验证
  81. $data = $this->checkParams($validate->isUpdate());
  82. // 调用仓库的编辑方法
  83. $this->repository->edit($id, $data);
  84. // 返回JSON格式的编辑成功结果
  85. // 返回JSON格式的编辑成功结果
  86. return app('json')->success('编辑成功');
  87. }
  88. /**
  89. * 删除商品助力活动
  90. * @param int $id 商品助力活动ID
  91. * @return \think\response\Json 返回JSON格式的删除结果
  92. */
  93. public function delete($id)
  94. {
  95. // 构造查询条件
  96. $where = [
  97. $this->repository->getPk() => $id,
  98. 'mer_id' => $this->request->merId()
  99. ];
  100. // 调用仓库的删除方法
  101. $this->repository->delete($where);
  102. // 返回JSON格式的删除成功结果
  103. return app('json')->success('删除成功');
  104. }
  105. /**
  106. * 切换商品助力活动状态
  107. * @param int $id 商品助力活动ID
  108. * @return \think\response\Json 返回JSON格式的修改结果
  109. */
  110. public function switchStatus($id)
  111. {
  112. // 获取状态值
  113. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  114. if (!$this->repository->detail($this->request->merId(), $id))
  115. return app('json')->fail('数据不存在');
  116. // 调用仓库的更新方法
  117. $this->repository->update($id, ['is_show' => $status]);
  118. app()->make(SpuRepository::class)->changeStatus($id, 3);
  119. // 返回JSON格式的修改成功结果
  120. return app('json')->success('修改成功');
  121. }
  122. /**
  123. * 检查参数是否合法
  124. *
  125. * @param StoreProductAssistValidate $validate 商品助力验证器
  126. * @return array 返回参数数组
  127. * @throws ValidateException 如果限量大于库存则抛出异常
  128. */
  129. public function checkParams(StoreProductAssistValidate $validate)
  130. {
  131. // 定义参数数组
  132. // 定义参数数组
  133. $params = [
  134. "image", "slider_image", "store_name", "store_info", "product_id", "is_show", "temp_id", "attrValue", "guarantee_template_id",
  135. "start_time", "end_time", "assist_user_count", "assist_count", "status", "pay_count", "product_status", "sort", 'mer_labels', 'delivery_way', 'delivery_free',
  136. ];
  137. // 获取请求参数
  138. $data = $this->request->params($params);
  139. // 检查属性值库存是否合法
  140. foreach ($data['attrValue'] as $datum) {
  141. if ($datum['stock'] > $datum['old_stock']) throw new ValidateException('限量不能大于库存');
  142. }
  143. // 验证参数是否合法
  144. $validate->check($data);
  145. // 返回参数数组
  146. return $data;
  147. }
  148. /**
  149. * 更新商品排序
  150. *
  151. * @param int $id 商品ID
  152. * @return \think\response\Json 返回JSON格式的修改结果
  153. */
  154. public function updateSort($id)
  155. {
  156. // 获取请求参数中的排序值
  157. $sort = $this->request->param('sort');
  158. $this->repository->updateSort($id, $this->request->merId(), ['sort' => $sort]);
  159. // 返回JSON格式的修改成功结果
  160. return app('json')->success('修改成功');
  161. }
  162. /**
  163. * 预览商品
  164. *
  165. * @param ProductRepository $repository 商品仓库实例
  166. * @return \think\response\Json 返回JSON格式的预览结果
  167. */
  168. public function preview(ProductRepository $repository)
  169. {
  170. $data = $this->request->param();
  171. // 设置商家信息
  172. $data['merchant'] = [
  173. 'mer_name' => $this->request->merchant()->mer_name,
  174. 'is_trader' => $this->request->merchant()->is_trader,
  175. 'mer_avatar' => $this->request->merchant()->mer_avatar,
  176. 'product_score' => $this->request->merchant()->product_score,
  177. 'service_score' => $this->request->merchant()->service_score,
  178. 'postage_score' => $this->request->merchant()->postage_score,
  179. 'service_phone' => $this->request->merchant()->service_phone,
  180. 'care_count' => $this->request->merchant()->care_count,
  181. 'type_name' => $this->request->merchant()->type_name->type_name ?? '',
  182. 'care' => true,
  183. 'recommend' => $this->request->merchant()->recommend,
  184. ];
  185. // 设置商家ID和状态
  186. $data['mer_id'] = $this->request->merId();
  187. $data['status'] = 1;
  188. $data['mer_status'] = 1;
  189. $data['rate'] = 3;
  190. // 调用商品仓库的预览方法并返回JSON格式的预览结果
  191. return app('json')->success($repository->preview($data));
  192. }
  193. /**
  194. * 设置商品标签
  195. *
  196. * @param int $id 商品ID
  197. * @return \think\response\Json
  198. */
  199. public function setLabels($id)
  200. {
  201. // 从请求参数中获取标签数据
  202. $data = $this->request->params(['mer_labels']);
  203. // if (empty($data['mer_labels'])) return app('json')->fail('标签为空');
  204. // 调用 SpuRepository 类的 setLabels 方法,设置商品标签
  205. app()->make(SpuRepository::class)->setLabels($id, 3, $data, $this->request->merId());
  206. // 返回成功信息
  207. return app('json')->success('修改成功');
  208. }
  209. }