StoreProduct.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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\admin\store;
  12. use app\common\repositories\store\product\SpuRepository;
  13. use app\common\repositories\system\merchant\MerchantRepository;
  14. use app\common\repositories\system\operate\OperateLogRepository;
  15. use think\App;
  16. use crmeb\basic\BaseController;
  17. use app\validate\merchant\StoreProductAdminValidate as validate;
  18. use app\common\repositories\store\product\ProductRepository as repository;
  19. use think\facade\Queue;
  20. /**
  21. * 主商品
  22. */
  23. class StoreProduct extends BaseController
  24. {
  25. /**
  26. * @var repository
  27. */
  28. protected $repository;
  29. /**
  30. * StoreProduct constructor.
  31. * @param App $app
  32. * @param repository $repository
  33. */
  34. public function __construct(App $app, repository $repository)
  35. {
  36. parent::__construct($app);
  37. $this->repository = $repository;
  38. }
  39. /**
  40. * 列表
  41. * @Author:Qinii
  42. * @Date: 2020/5/18
  43. * @return mixed
  44. */
  45. public function lst()
  46. {
  47. [$page, $limit] = $this->getPage();
  48. $type = $this->request->param('type', 1);
  49. $where = $this->request->params(['cate_id', 'keyword', 'mer_cate_id', 'pid', 'store_name', 'is_trader', 'us_status', 'product_id', 'star', 'sys_labels', 'hot_type', 'svip_price_type', 'is_ficti', 'product_ids', 'form_id','cate_hot']);
  50. $mer_id = $this->request->param('mer_id', '');
  51. $merId = $mer_id ?: null;
  52. $where['is_gift_bag'] = 0;
  53. $_where = $this->repository->switchType($type, null, 0);
  54. unset($_where['star']);
  55. $where = array_merge($where, $_where);
  56. return app('json')->success($this->repository->getAdminList($merId, $where, $page, $limit));
  57. }
  58. /**
  59. * 礼包列表
  60. * @Author:Qinii
  61. * @Date: 2020/5/18
  62. * @return mixed
  63. */
  64. public function bagList()
  65. {
  66. [$page, $limit] = $this->getPage();
  67. $where = $this->request->params(['cate_id', 'keyword', 'mer_cate_id', 'is_trader', 'us_status']);
  68. $merId = $this->request->param('mer_id') ? $this->request->param('mer_id') : null;
  69. $type = $this->request->param('type', 1);
  70. $_where = $this->repository->switchType($type, null, 10);
  71. $where = array_merge($where, $_where);
  72. $where['order'] = 'rank';
  73. unset($where['star']);
  74. return app('json')->success($this->repository->getAdminList($merId, $where, $page, $limit));
  75. }
  76. /**
  77. * 顶部统计 和 状态区分
  78. * @Author:Qinii
  79. * @Date: 2020/5/18
  80. * @return mixed
  81. */
  82. public function getStatusFilter()
  83. {
  84. return app('json')->success($this->repository->getFilter(null, '商品', 0));
  85. }
  86. /**
  87. * 礼包表头
  88. * @Author:Qinii
  89. * @Date: 2020/5/18
  90. * @return mixed
  91. */
  92. public function getBagStatusFilter()
  93. {
  94. return app('json')->success($this->repository->getFilter(null, '礼包', 10));
  95. }
  96. /**
  97. * 详情
  98. * @Author:Qinii
  99. * @Date: 2020/5/18
  100. * @param $id
  101. * @return mixed
  102. */
  103. public function detail($id)
  104. {
  105. if (!$this->repository->merExists(null, $id))
  106. return app('json')->fail('数据不存在');
  107. return app('json')->success($this->repository->getAdminOneProduct($id, 0));
  108. }
  109. /**
  110. * 编辑
  111. * @Author:Qinii
  112. * @Date: 2020/5/11
  113. * @param $id
  114. * @param validate $validate
  115. * @return mixed
  116. */
  117. public function update($id, validate $validate)
  118. {
  119. $merId = $this->request->param('mer_id') ? $this->request->param('mer_id') : null;
  120. var_dump($merId);die();
  121. $data = $this->checkParams($validate);
  122. $this->repository->adminUpdate($id, $data);
  123. return app('json')->success('编辑成功');
  124. }
  125. /**
  126. * 审核 / 下架
  127. * @Author:Qinii
  128. * @Date: 2020/5/18
  129. * @param int $id
  130. * @return mixed
  131. */
  132. public function switchStatus()
  133. {
  134. //0:审核中,1:审核通过 -1: 未通过 -2: 下架
  135. $id = $this->request->param('id');
  136. $data = $this->request->params(['status', 'refusal']);
  137. if (in_array($data['status'], [1, 0, -2, -1]))
  138. if ($data['status'] == -1 && empty($data['refusal']))
  139. return app('json')->fail('请填写拒绝理由');
  140. if (is_array($id)) {
  141. $this->repository->batchSwitchStatus($id, $data, $this->request->adminInfo());
  142. } else {
  143. $this->repository->switchStatus($id, $data, $this->request->adminInfo());
  144. }
  145. return app('json')->success('操作成功');
  146. }
  147. /**
  148. * 字段验证
  149. * @Author:Qinii
  150. * @Date: 2020/5/11
  151. * @param validate $validate
  152. * @return array
  153. */
  154. public function checkParams(validate $validate)
  155. {
  156. $data = $this->request->params(['cate_hot','is_hot', 'is_best', 'is_benefit', 'is_new', 'store_name', 'content',
  157. 'rank', ['star',0],'cate_hot']);
  158. $validate->check($data);
  159. return $data;
  160. }
  161. /**
  162. * 检测商品佣金队列 - 弃用
  163. * @author Qinii
  164. * @day 2020-06-24
  165. */
  166. public function checkProduct()
  167. {
  168. Queue::push(CheckProductExtensionJob::class, []);
  169. return app('json')->success('后台已开始检测');
  170. }
  171. /**
  172. * 商户下啦筛选 - 弃用
  173. * @return \think\response\Json
  174. * @author Qinii
  175. */
  176. public function lists()
  177. {
  178. $make = app()->make(MerchantRepository::class);
  179. $data = $make->selectWhere(['is_del' => 0], 'mer_id,mer_name');
  180. return app('json')->success($data);
  181. }
  182. /**
  183. * 增加虚拟销量表单
  184. * @Author:Qinii
  185. * @Date: 2020/10/9
  186. * @param $id
  187. * @return mixed
  188. */
  189. public function addFictiForm($id)
  190. {
  191. if (!$this->repository->merExists(null, $id))
  192. return app('json')->fail('数据不存在');
  193. return app('json')->success(formToData($this->repository->fictiForm($id)));
  194. }
  195. /**
  196. * 修改虚拟销量
  197. * @Author:Qinii
  198. * @Date: 2020/10/9
  199. * @param $id
  200. * @return mixed
  201. *
  202. */
  203. public function addFicti($id)
  204. {
  205. $data = $this->request->params(['type', 'ficti']);
  206. if (!in_array($data['type'], [1, 2])) return app('json')->fail('类型错误');
  207. if (!$data['ficti'] || $data['ficti'] < 0) return app('json')->fail('已售数量必须大于0');
  208. $res = $this->repository->getWhere(['product_id' => $id], 'ficti,sales');
  209. if (!$res) return app('json')->fail('数据不存在');
  210. if ($data['type'] == 2 && $res['ficti'] < $data['ficti']) return app('json')->fail('已售数量不足');
  211. $ficti = ($data['type'] == 1) ? $data['ficti'] : '-' . $data['ficti'];
  212. $data = [
  213. 'ficti' => $res['ficti'] + $ficti,
  214. 'sales' => $res['sales'] + $ficti
  215. ];
  216. $this->repository->update($id, $data);
  217. return app('json')->success('修改成功');
  218. }
  219. /**
  220. * 修改排序
  221. * @param $id
  222. * @return \think\response\Json
  223. * @author Qinii
  224. * @day 3/17/21
  225. */
  226. public function updateSort($id)
  227. {
  228. $sort = $this->request->param('sort');
  229. $this->repository->updateSort($id, null, ['rank' => $sort]);
  230. return app('json')->success('修改成功');
  231. }
  232. /**
  233. * 设置标签
  234. * @param $id
  235. * @return \think\response\Json
  236. * @author Qinii
  237. */
  238. public function setLabels($id)
  239. {
  240. $data = $this->request->params(['sys_labels']);
  241. app()->make(SpuRepository::class)->setLabels($id, 0, $data, 0);
  242. return app('json')->success('修改成功');
  243. }
  244. /**
  245. * 是否隐藏
  246. * @param $id
  247. * @return mixed
  248. * @author Qinii
  249. * @day 2020-07-17
  250. */
  251. public function changeUsed($id)
  252. {
  253. if (!$this->repository->merExists(null, $id))
  254. return app('json')->fail('数据不存在');
  255. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  256. $this->repository->switchShow($id, $status, 'is_used', 0, $this->request->adminInfo());
  257. return app('json')->success('修改成功');
  258. }
  259. /**
  260. * 批量显示隐藏
  261. * @return \think\response\Json
  262. * @author Qinii
  263. * @day 2022/11/14
  264. */
  265. public function batchShow()
  266. {
  267. $ids = $this->request->param('ids');
  268. $status = $this->request->param('status') == 1 ? 1 : 0;
  269. $this->repository->batchSwitchShow($ids, $status, 'is_used', 0, $this->request->adminInfo());
  270. return app('json')->success('修改成功');
  271. }
  272. /**
  273. * 批量标签
  274. * @return \think\response\Json
  275. * @author Qinii
  276. * @day 2022/9/6
  277. */
  278. public function batchLabels()
  279. {
  280. $ids = $this->request->param('ids');
  281. $data = $this->request->params(['sys_labels']);
  282. if (empty($ids)) return app('json')->fail('请选择商品');
  283. app()->make(SpuRepository::class)->batchLabels($ids, $data, 0);
  284. return app('json')->success('修改成功');
  285. }
  286. /**
  287. * 批量设置推荐类型
  288. * @return \think\response\Json
  289. * @author Qinii
  290. * @day 2022/9/6
  291. */
  292. //public function batchHot()
  293. //{
  294. // $ids = $this->request->param('ids');
  295. // $data = $this->request->params([['is_hot', 0], ['is_benefit', 0], ['is_best', 0], ['is_new', 0]]);
  296. // if (empty($ids)) return app('json')->fail('请选择商品');
  297. // $this->repository->updates($ids, $data);
  298. // return app('json')->success('修改成功');
  299. //}
  300. /**
  301. * 获取商品操作记录
  302. * @param $product_id
  303. *
  304. * @date 2023/10/13
  305. * @author yyw
  306. */
  307. public function getOperateList($product_id)
  308. {
  309. $where = $this->request->params([
  310. ['type', ''],
  311. ['date', '']
  312. ]);
  313. $where['relevance_id'] = $product_id;
  314. $where['relevance_type'] = OperateLogRepository::RELEVANCE_PRODUCT;
  315. [$page, $limit] = $this->getPage();
  316. return app('json')->success(app()->make(OperateLogRepository::class)->lst($where, $page, $limit));
  317. }
  318. /**
  319. * 获取平台自营产品
  320. * @return void
  321. * FerryZhao 2024/4/12
  322. */
  323. public function get_self_product_list()
  324. {
  325. $productWhere = $this->request->params(['keyword', 'sys_labels', 'us_status', 'cate_id', 'active_id']);
  326. $productWhere['cate_pid'] = $this->request->param('level_one_cate_ids');
  327. $productWhere['product_type'] = 0;
  328. $productWhere['is_ficti'] = '0,1';
  329. $merWhere = $this->request->params(['type_id', 'category_id', 'mer_id']);
  330. $merWhere['status'] = 1;
  331. $merWhere['mer_state'] = 1;
  332. $merWhere['is_del'] = 0;
  333. $merWhere['is_trader'] = 1;//自营
  334. [$page, $limit] = $this->getPage();
  335. $result = $this->repository->getProductList($merWhere, $productWhere, $page, $limit,true);
  336. return app('json')->success('获取成功', $result);
  337. }
  338. /**
  339. * 批量设置 分类推荐
  340. * @return \think\response\Json
  341. * @author Qinii
  342. * @day 2024/6/5
  343. */
  344. public function batchHot()
  345. {
  346. $ids = $this->request->param('ids',[]);
  347. if (empty($ids)) return app('json')->fail('请选择商品');
  348. $ids = !is_array($ids) ? [$ids] : $ids;
  349. $data = $this->request->params([['cate_hot',''],['is_hot',''], ['is_best',''], ['is_benefit',''], ['is_new','']]);
  350. $res = [];
  351. foreach ($data as $k => $v) {
  352. if ($v !== '') {
  353. $res[$k] = $v;
  354. }
  355. }
  356. if (!$res) return app('json')->fail('无信息提交');
  357. $this->repository->updates($ids, $res);
  358. return app('json')->success('修改成功');
  359. }
  360. }