ProductPresell.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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\ProductPresellRepository 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\StoreProductPresellValidate;
  18. use think\exception\ValidateException;
  19. class ProductPresell 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', 'type', 'presell_type', 'is_show', 'us_status','product_presell_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 StoreProductPresellValidate $validate
  48. * @return mixed
  49. * @author Qinii
  50. * @day 2020-10-12
  51. */
  52. public function create(StoreProductPresellValidate $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. *
  72. * @param $id
  73. * @param StoreProductPresellValidate $validate
  74. * @return mixed
  75. * @author Qinii
  76. * @day 2020-10-13
  77. */
  78. public function update($id, StoreProductPresellValidate $validate)
  79. {
  80. $data = $this->checkParams($validate);
  81. $where = [
  82. $this->repository->getPk() => $id,
  83. 'mer_id' => $this->request->merId()
  84. ];
  85. if (!$this->repository->getWhere($where))
  86. return app('json')->fail('数据不存在');
  87. $data['mer_id'] = $this->request->merId();
  88. $this->repository->edit($id, $data);
  89. return app('json')->success('编辑成功');
  90. }
  91. /**
  92. * 根据ID删除数据
  93. *
  94. * @param int $id 要删除的数据ID
  95. * @return \think\response\Json 返回JSON格式的删除结果
  96. */
  97. public function delete($id)
  98. {
  99. // 构造查询条件
  100. $where = [
  101. $this->repository->getPk() => $id, // 主键ID
  102. 'mer_id' => $this->request->merId() // 商家ID
  103. ];
  104. // 调用Repository的delete方法删除数据
  105. $this->repository->delete($where);
  106. // 返回JSON格式的删除结果
  107. return app('json')->success('删除成功');
  108. }
  109. /**
  110. * 切换商品状态
  111. *
  112. * @param int $id 商品ID
  113. * @return \think\response\Json
  114. */
  115. public function switchStatus($id)
  116. {
  117. // 获取请求参数中的状态值,默认为0
  118. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  119. // 判断商品是否存在
  120. if (!$this->repository->detail($this->request->merId(), $id))
  121. return app('json')->fail('数据不存在');
  122. // 更新商品状态
  123. $this->repository->update($id, ['is_show' => $status]);
  124. // 调用SpuRepository类的changeStatus方法,将商品状态改为2
  125. app()->make(SpuRepository::class)->changeStatus($id, 2);
  126. // 返回操作结果
  127. return app('json')->success('修改成功');
  128. }
  129. /**
  130. * 获取数字统计信息
  131. *
  132. * @return \Illuminate\Http\JsonResponse
  133. */
  134. public function number()
  135. {
  136. // merId 方法用于获取当前请求的商户 ID
  137. return app('json')->success($this->repository->stat($this->request->merId()));
  138. }
  139. /**
  140. * 检查参数是否合法
  141. *
  142. * @param StoreProductPresellValidate $validate 商品预售验证器
  143. * @return array 返回合法的参数数组
  144. * @throws ValidateException 如果限量大于库存则抛出异常
  145. */
  146. public function checkParams(StoreProductPresellValidate $validate)
  147. {
  148. // 定义需要验证的参数列表
  149. $params = [
  150. "image", "slider_image", "store_name", "store_info", "product_id", "is_show", "temp_id", "attrValue", "sort", "guarantee_template_id",
  151. "start_time", "end_time", "final_start_time", "final_end_time", "status", "presell_type", 'pay_count', "delivery_type", "delivery_day",
  152. "product_status", 'mer_labels', 'delivery_way', 'delivery_free'
  153. ];
  154. // 获取请求参数
  155. $data = $this->request->params($params);
  156. // 遍历属性值数组,判断限量是否大于库存
  157. foreach ($data['attrValue'] as $datum) {
  158. if ($datum['stock'] > $datum['old_stock']) throw new ValidateException('限量不能大于库存');
  159. }
  160. // 使用验证器验证参数是否合法
  161. $validate->check($data);
  162. // 返回合法的参数数组
  163. return $data;
  164. }
  165. /**
  166. * 更新指定ID的记录的排序值
  167. *
  168. * @param int $id 记录ID
  169. * @return \think\response\Json 返回JSON格式的操作结果
  170. */
  171. public function updateSort($id)
  172. {
  173. // 从请求参数中获取排序值
  174. $sort = $this->request->param('sort');
  175. // 调用仓库类的更新排序方法,传入记录ID、商家ID和排序值
  176. $this->repository->updateSort($id, $this->request->merId(), ['sort' => $sort]);
  177. // 返回JSON格式的操作结果,表示修改成功
  178. return app('json')->success('修改成功');
  179. }
  180. /**
  181. * 预览商品
  182. *
  183. * @param ProductRepository $repository 商品仓库
  184. * @return \think\response\Json
  185. */
  186. public function preview(ProductRepository $repository)
  187. {
  188. // 获取请求参数
  189. $data = $this->request->param();
  190. // 添加商家信息到请求参数中
  191. $data['merchant'] = [
  192. 'mer_name' => $this->request->merchant()->mer_name,
  193. 'is_trader' => $this->request->merchant()->is_trader,
  194. 'mer_avatar' => $this->request->merchant()->mer_avatar,
  195. 'product_score' => $this->request->merchant()->product_score,
  196. 'service_score' => $this->request->merchant()->service_score,
  197. 'postage_score' => $this->request->merchant()->postage_score,
  198. 'service_phone' => $this->request->merchant()->service_phone,
  199. 'care_count' => $this->request->merchant()->care_count,
  200. 'type_name' => $this->request->merchant()->type_name->type_name ?? '',
  201. 'care' => true,
  202. 'recommend' => $this->request->merchant()->recommend,
  203. ];
  204. // 添加商家ID和状态到请求参数中
  205. $data['mer_id'] = $this->request->merId();
  206. $data['status'] = 1;
  207. $data['mer_status'] = 1;
  208. $data['rate'] = 3;
  209. // 调用商品仓库的预览方法并返回结果
  210. return app('json')->success($repository->preview($data));
  211. }
  212. /**
  213. * 设置商品标签
  214. *
  215. * @param int $id 商品ID
  216. * @return \think\response\Json
  217. */
  218. public function setLabels($id)
  219. {
  220. // 从请求参数中获取标签数据
  221. $data = $this->request->params(['mer_labels']);
  222. // if (empty($data['mer_labels'])) return app('json')->fail('标签为空');
  223. // 调用 SpuRepository 类的 setLabels 方法,设置商品标签
  224. app()->make(SpuRepository::class)->setLabels($id, 2, $data, $this->request->merId());
  225. // 返回成功信息
  226. return app('json')->success('修改成功');
  227. }
  228. }