StoreProduct.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\controller\admin\store;
  3. use app\common\repositories\store\product\ProductAttrValueRepository;
  4. use app\common\repositories\system\merchant\MerchantRepository;
  5. use app\validate\merchant\StoreProductValidate;
  6. use ln\jobs\ChangeSpuStatusJob;
  7. use ln\jobs\CheckProductExtensionJob;
  8. use think\App;
  9. use ln\basic\BaseController;
  10. use app\validate\merchant\StoreProductAdminValidate as validate;
  11. use app\common\repositories\store\product\ProductRepository as repository;
  12. use think\facade\Queue;
  13. class StoreProduct extends BaseController
  14. {
  15. /**
  16. * @var repository
  17. */
  18. protected $repository;
  19. /**
  20. * StoreProduct constructor.
  21. * @param App $app
  22. * @param repository $repository
  23. */
  24. public function __construct(App $app, repository $repository)
  25. {
  26. parent::__construct($app);
  27. $this->repository = $repository;
  28. }
  29. /**
  30. * @Author:Qinii
  31. * @Date: 2020/5/18
  32. * @return mixed
  33. */
  34. public function lst()
  35. {
  36. [$page, $limit] = $this->getPage();
  37. $where = $this->request->params(['cate_id', 'keyword', ['type', 1], 'mer_cate_id', 'pid','store_name','is_trader','us_status','product_id','star']);
  38. $mer_id = $this->request->param('mer_id','');
  39. $merId = $mer_id ? $mer_id : null;
  40. $where['is_gift_bag'] = 0;
  41. $_where = $this->repository->switchType($where['type'], null,0);
  42. unset($_where['star']);
  43. $where = array_merge($where, $_where);
  44. return app('json')->success($this->repository->getAdminList($merId, $where, $page, $limit));
  45. }
  46. /**
  47. * @Author:Qinii
  48. * @Date: 2020/5/18
  49. * @return mixed
  50. */
  51. public function bagList()
  52. {
  53. [$page, $limit] = $this->getPage();
  54. $where = $this->request->params(['cate_id','keyword',['type',1],'mer_cate_id' ,'is_trader','us_status','star']);
  55. $merId = $this->request->param('mer_id') ? $this->request->param('mer_id') : null;
  56. $res = $this->repository->switchType($where['type'],$merId,10);
  57. $where = array_merge($where,$res);
  58. return app('json')->success($this->repository->getAdminList($merId,$where, $page, $limit));
  59. }
  60. /**
  61. * @Author:Qinii
  62. * @Date: 2020/5/18
  63. * @return mixed
  64. */
  65. public function getStatusFilter()
  66. {
  67. return app('json')->success($this->repository->getFilter(null,'商品',0));
  68. }
  69. /**
  70. * TODO 礼包表头
  71. * @Author:Qinii
  72. * @Date: 2020/5/18
  73. * @return mixed
  74. */
  75. public function getBagStatusFilter()
  76. {
  77. return app('json')->success($this->repository->getFilter(null,'礼包',10));
  78. }
  79. /**
  80. * @Author:Qinii
  81. * @Date: 2020/5/18
  82. * @param $id
  83. * @return mixed
  84. */
  85. public function detail($id)
  86. {
  87. if(!$this->repository->merExists(null,$id))
  88. return app('json')->fail('数据不存在');
  89. return app('json')->success($this->repository->getAdminOneProduct($id,0));
  90. }
  91. /**
  92. * @Author:Qinii
  93. * @Date: 2020/5/11
  94. * @param $id
  95. * @param validate $validate
  96. * @return mixed
  97. */
  98. public function update($id,validate $validate)
  99. {
  100. $data = $this->checkParams($validate);
  101. $this->repository->adminUpdate($id,$data);
  102. return app('json')->success('编辑成功');
  103. }
  104. /**
  105. * @Author:Qinii
  106. * @Date: 2020/5/18
  107. * @param int $id
  108. * @return mixed
  109. */
  110. public function switchStatus()
  111. {
  112. $id = $this->request->param('id');
  113. $data = $this->request->params(['status','refusal']);
  114. if($data['status'] == -1 && empty($data['refusal']))
  115. return app('json')->fail('请填写拒绝理由');
  116. if(!is_array($id)) $id = [$id];
  117. $this->repository->switchStatus($id,$data);
  118. return app('json')->success('操作成功');
  119. }
  120. /**
  121. * TODO 是否隐藏
  122. * @param $id
  123. * @return mixed
  124. * @author Qinii
  125. * @day 2020-07-17
  126. */
  127. public function changeUsed($id)
  128. {
  129. if(!$this->repository->merExists(null,$id))
  130. return app('json')->fail('数据不存在');
  131. $status = $this->request->param('status',0) == 1 ? 1 : 0;
  132. $status = ['is_used' => $status];
  133. $this->repository->update($id,$status);
  134. queue(ChangeSpuStatusJob::class,['id' => $id,'product_type' => 0]);
  135. return app('json')->success('修改成功');
  136. }
  137. /**
  138. * @Author:Qinii
  139. * @Date: 2020/5/11
  140. * @param validate $validate
  141. * @return array
  142. */
  143. public function checkParams(validate $validate)
  144. {
  145. $data = $this->request->params(['is_hot','is_best','is_benefit','is_new','store_name','content','rank','star']);
  146. $validate->check($data);
  147. return $data;
  148. }
  149. /**
  150. * TODO
  151. * @author Qinii
  152. * @day 2020-06-24
  153. */
  154. public function checkProduct()
  155. {
  156. Queue::push(CheckProductExtensionJob::class,[]);
  157. return app('json')->success('后台已开始检测');
  158. }
  159. public function lists()
  160. {
  161. $make = app()->make(MerchantRepository::class);
  162. $data = $make->selectWhere(['is_del' => 0],'mer_id,mer_name');
  163. return app('json')->success($data);
  164. }
  165. /**
  166. * 增加虚拟销量表单
  167. * @Author:Qinii
  168. * @Date: 2020/10/9
  169. * @param $id
  170. * @return mixed
  171. */
  172. public function addFictiForm($id)
  173. {
  174. if(!$this->repository->merExists(null,$id))
  175. return app('json')->fail('数据不存在');
  176. return app('json')->success(formToData($this->repository->fictiForm($id)));
  177. }
  178. /**
  179. * 修改虚拟销量
  180. * @Author:Qinii
  181. * @Date: 2020/10/9
  182. * @param $id
  183. * @return mixed
  184. *
  185. */
  186. public function addFicti($id)
  187. {
  188. $data = $this->request->params(['type','ficti']);
  189. if(!in_array($data['type'],[1,2])) return app('json')->fail('类型错误');
  190. if(!$data['ficti'] || $data['ficti'] < 0) return app('json')->fail('虚拟销量必须大于0');
  191. $res = $this->repository->getWhere(['product_id' => $id],'ficti,sales');
  192. if(!$res) return app('json')->fail('数据不存在');
  193. if($data['type'] == 2 && $res['ficti'] < $data['ficti']) return app('json')->fail('虚拟销量不足');
  194. $ficti = ($data['type'] == 1) ? $data['ficti'] : '-' . $data['ficti'];
  195. $data = [
  196. 'ficti' => $res['ficti'] + $ficti,
  197. 'sales' => $res['sales'] + $ficti
  198. ];
  199. $this->repository->update($id,$data);
  200. return app('json')->success('修改成功');
  201. }
  202. /**
  203. * TODO
  204. * @param $id
  205. * @return \think\response\Json
  206. * @author Qinii
  207. * @day 3/17/21
  208. */
  209. public function updateSort($id)
  210. {
  211. $sort = $this->request->param('sort');
  212. $this->repository->updateSort($id,null,['rank' => $sort]);
  213. return app('json')->success('修改成功');
  214. }
  215. }