ProductGroup.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 crmeb\jobs\ChangeSpuStatusJob;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. use app\common\repositories\store\product\ProductGroupRepository;
  16. use app\validate\merchant\StoreProductGroupValidate;
  17. use think\exception\ValidateException;
  18. class ProductGroup extends BaseController
  19. {
  20. protected $repository ;
  21. /**
  22. * ProductGroup constructor.
  23. * @param App $app
  24. * @param ProductGroupRepository $repository
  25. */
  26. public function __construct(App $app ,ProductGroupRepository $repository)
  27. {
  28. parent::__construct($app);
  29. $this->repository = $repository;
  30. }
  31. public function lst()
  32. {
  33. [$page, $limit] = $this->getPage();
  34. $where = $this->request->params(['product_status','keyword','active_type','status','us_status','product_group_id']);
  35. $where['is_show'] = $where['status'];
  36. unset($where['status']);
  37. $where['mer_id'] = $this->request->merId();
  38. return app('json')->success($this->repository->getMerchantList($where,$page,$limit));
  39. }
  40. public function create(StoreProductGroupValidate $validate)
  41. {
  42. $data = $this->checkParams($validate);
  43. $this->repository->create($this->request->merId(),$data);
  44. return app('json')->success('添加成功');
  45. }
  46. public function detail($id)
  47. {
  48. $where = [
  49. $this->repository->getPk() => $id,
  50. 'mer_id' => $this->request->merId()
  51. ];
  52. if(!$this->repository->getWhere($where))
  53. return app('json')->fail('数据不存在');
  54. $data = $this->repository->detail($this->request->merId(),$id);
  55. return app('json')->success($data);
  56. }
  57. public function delete($id)
  58. {
  59. $where = [
  60. $this->repository->getPk() => $id,
  61. 'mer_id' => $this->request->merId()
  62. ];
  63. if(!$this->repository->getWhere($where))
  64. return app('json')->fail('数据不存在');
  65. $this->repository->update($id,['is_del' => 1]);
  66. Queue(ChangeSpuStatusJob::class,['product_type' => 4 ,'id' => $id]);
  67. return app('json')->success('删除成功');
  68. }
  69. public function update($id,StoreProductGroupValidate $validate)
  70. {
  71. $data = $this->checkParams($validate);
  72. $where = [
  73. $this->repository->getPk() => $id,
  74. 'mer_id' => $this->request->merId()
  75. ];
  76. if(!$this->repository->getWhere($where))
  77. return app('json')->fail('数据不存在');
  78. $this->repository->edit($id,$data);
  79. return app('json')->success('编辑成功');
  80. }
  81. public function switchStatus($id)
  82. {
  83. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  84. if(!$this->repository->detail($this->request->merId(),$id))
  85. return app('json')->fail('数据不存在');
  86. $this->repository->update($id, ['is_show' => $status]);
  87. Queue(ChangeSpuStatusJob::class,['product_type' => 4 ,'id' => $id]);
  88. return app('json')->success('修改成功');
  89. }
  90. public function checkParams(StoreProductGroupValidate $validate)
  91. {
  92. $params = [
  93. "image", "slider_image", "store_name", "store_info", "product_id","is_show","temp_id","once_pay_count",
  94. "start_time", "end_time", "buying_num", "buying_count_num", "status","pay_count","time","ficti_status","ficti_num","attrValue",'unit_name','content','sort'
  95. ];
  96. $data = $this->request->params($params);
  97. if($data['buying_count_num'] < 2) throw new ValidateException('开团人数不得少于2人');
  98. if($data['end_time'] < $data['start_time']) throw new ValidateException('活动开始时间必须大于结束时间');
  99. $validate->check($data);
  100. return $data;
  101. }
  102. public function updateSort($id)
  103. {
  104. $sort = $this->request->param('sort');
  105. $this->repository->updateSort($id,$this->request->merId(),['sort' => $sort]);
  106. return app('json')->success('修改成功');
  107. }
  108. }