StoreProduct.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. $data = $this->checkParams($validate);
  120. $this->repository->adminUpdate($id, $data);
  121. return app('json')->success('编辑成功');
  122. }
  123. /**
  124. * 审核 / 下架
  125. * @Author:Qinii
  126. * @Date: 2020/5/18
  127. * @param int $id
  128. * @return mixed
  129. */
  130. public function switchStatus()
  131. {
  132. //0:审核中,1:审核通过 -1: 未通过 -2: 下架
  133. $id = $this->request->param('id');
  134. $data = $this->request->params(['status', 'refusal']);
  135. if (in_array($data['status'], [1, 0, -2, -1]))
  136. if ($data['status'] == -1 && empty($data['refusal']))
  137. return app('json')->fail('请填写拒绝理由');
  138. if (is_array($id)) {
  139. $this->repository->batchSwitchStatus($id, $data, $this->request->adminInfo());
  140. } else {
  141. $this->repository->switchStatus($id, $data, $this->request->adminInfo());
  142. }
  143. return app('json')->success('操作成功');
  144. }
  145. /**
  146. * 字段验证
  147. * @Author:Qinii
  148. * @Date: 2020/5/11
  149. * @param validate $validate
  150. * @return array
  151. */
  152. public function checkParams(validate $validate)
  153. {
  154. $data = $this->request->params(['cate_hot','is_hot', 'is_best', 'is_benefit', 'is_new', 'store_name', 'content',
  155. 'rank', ['star',0],'cate_hot']);
  156. $validate->check($data);
  157. return $data;
  158. }
  159. /**
  160. * 检测商品佣金队列 - 弃用
  161. * @author Qinii
  162. * @day 2020-06-24
  163. */
  164. public function checkProduct()
  165. {
  166. Queue::push(CheckProductExtensionJob::class, []);
  167. return app('json')->success('后台已开始检测');
  168. }
  169. /**
  170. * 商户下啦筛选 - 弃用
  171. * @return \think\response\Json
  172. * @author Qinii
  173. */
  174. public function lists()
  175. {
  176. $make = app()->make(MerchantRepository::class);
  177. $data = $make->selectWhere(['is_del' => 0], 'mer_id,mer_name');
  178. return app('json')->success($data);
  179. }
  180. /**
  181. * 增加虚拟销量表单
  182. * @Author:Qinii
  183. * @Date: 2020/10/9
  184. * @param $id
  185. * @return mixed
  186. */
  187. public function addFictiForm($id)
  188. {
  189. if (!$this->repository->merExists(null, $id))
  190. return app('json')->fail('数据不存在');
  191. return app('json')->success(formToData($this->repository->fictiForm($id)));
  192. }
  193. /**
  194. * 修改虚拟销量
  195. * @Author:Qinii
  196. * @Date: 2020/10/9
  197. * @param $id
  198. * @return mixed
  199. *
  200. */
  201. public function addFicti($id)
  202. {
  203. $data = $this->request->params(['type', 'ficti']);
  204. if (!in_array($data['type'], [1, 2])) return app('json')->fail('类型错误');
  205. if (!$data['ficti'] || $data['ficti'] < 0) return app('json')->fail('已售数量必须大于0');
  206. $res = $this->repository->getWhere(['product_id' => $id], 'ficti,sales');
  207. if (!$res) return app('json')->fail('数据不存在');
  208. if ($data['type'] == 2 && $res['ficti'] < $data['ficti']) return app('json')->fail('已售数量不足');
  209. $ficti = ($data['type'] == 1) ? $data['ficti'] : '-' . $data['ficti'];
  210. $data = [
  211. 'ficti' => $res['ficti'] + $ficti,
  212. 'sales' => $res['sales'] + $ficti
  213. ];
  214. $this->repository->update($id, $data);
  215. return app('json')->success('修改成功');
  216. }
  217. /**
  218. * 修改排序
  219. * @param $id
  220. * @return \think\response\Json
  221. * @author Qinii
  222. * @day 3/17/21
  223. */
  224. public function updateSort($id)
  225. {
  226. $sort = $this->request->param('sort');
  227. $this->repository->updateSort($id, null, ['rank' => $sort]);
  228. return app('json')->success('修改成功');
  229. }
  230. /**
  231. * 设置标签
  232. * @param $id
  233. * @return \think\response\Json
  234. * @author Qinii
  235. */
  236. public function setLabels($id)
  237. {
  238. $data = $this->request->params(['sys_labels']);
  239. app()->make(SpuRepository::class)->setLabels($id, 0, $data, 0);
  240. return app('json')->success('修改成功');
  241. }
  242. /**
  243. * 是否隐藏
  244. * @param $id
  245. * @return mixed
  246. * @author Qinii
  247. * @day 2020-07-17
  248. */
  249. public function changeUsed($id)
  250. {
  251. if (!$this->repository->merExists(null, $id))
  252. return app('json')->fail('数据不存在');
  253. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  254. $this->repository->switchShow($id, $status, 'is_used', 0, $this->request->adminInfo());
  255. return app('json')->success('修改成功');
  256. }
  257. /**
  258. * 批量显示隐藏
  259. * @return \think\response\Json
  260. * @author Qinii
  261. * @day 2022/11/14
  262. */
  263. public function batchShow()
  264. {
  265. $ids = $this->request->param('ids');
  266. $status = $this->request->param('status') == 1 ? 1 : 0;
  267. $this->repository->batchSwitchShow($ids, $status, 'is_used', 0, $this->request->adminInfo());
  268. return app('json')->success('修改成功');
  269. }
  270. /**
  271. * 批量标签
  272. * @return \think\response\Json
  273. * @author Qinii
  274. * @day 2022/9/6
  275. */
  276. public function batchLabels()
  277. {
  278. $ids = $this->request->param('ids');
  279. $data = $this->request->params(['sys_labels']);
  280. if (empty($ids)) return app('json')->fail('请选择商品');
  281. app()->make(SpuRepository::class)->batchLabels($ids, $data, 0);
  282. return app('json')->success('修改成功');
  283. }
  284. /**
  285. * 批量设置推荐类型
  286. * @return \think\response\Json
  287. * @author Qinii
  288. * @day 2022/9/6
  289. */
  290. //public function batchHot()
  291. //{
  292. // $ids = $this->request->param('ids');
  293. // $data = $this->request->params([['is_hot', 0], ['is_benefit', 0], ['is_best', 0], ['is_new', 0]]);
  294. // if (empty($ids)) return app('json')->fail('请选择商品');
  295. // $this->repository->updates($ids, $data);
  296. // return app('json')->success('修改成功');
  297. //}
  298. /**
  299. * 获取商品操作记录
  300. * @param $product_id
  301. *
  302. * @date 2023/10/13
  303. * @author yyw
  304. */
  305. public function getOperateList($product_id)
  306. {
  307. $where = $this->request->params([
  308. ['type', ''],
  309. ['date', '']
  310. ]);
  311. $where['relevance_id'] = $product_id;
  312. $where['relevance_type'] = OperateLogRepository::RELEVANCE_PRODUCT;
  313. [$page, $limit] = $this->getPage();
  314. return app('json')->success(app()->make(OperateLogRepository::class)->lst($where, $page, $limit));
  315. }
  316. /**
  317. * 获取平台自营产品
  318. * @return void
  319. * FerryZhao 2024/4/12
  320. */
  321. public function get_self_product_list()
  322. {
  323. $productWhere = $this->request->params(['keyword', 'sys_labels', 'us_status', 'cate_id', 'active_id']);
  324. $productWhere['cate_pid'] = $this->request->param('level_one_cate_ids');
  325. $productWhere['product_type'] = 0;
  326. $productWhere['is_ficti'] = '0,1';
  327. $merWhere = $this->request->params(['type_id', 'category_id', 'mer_id']);
  328. $merWhere['status'] = 1;
  329. $merWhere['mer_state'] = 1;
  330. $merWhere['is_del'] = 0;
  331. $merWhere['is_trader'] = 1;//自营
  332. [$page, $limit] = $this->getPage();
  333. $result = $this->repository->getProductList($merWhere, $productWhere, $page, $limit,true);
  334. return app('json')->success('获取成功', $result);
  335. }
  336. /**
  337. * 批量设置 分类推荐
  338. * @return \think\response\Json
  339. * @author Qinii
  340. * @day 2024/6/5
  341. */
  342. public function batchHot()
  343. {
  344. $ids = $this->request->param('ids',[]);
  345. if (empty($ids)) return app('json')->fail('请选择商品');
  346. $ids = !is_array($ids) ? [$ids] : $ids;
  347. $data = $this->request->params([['cate_hot',''],['is_hot',''], ['is_best',''], ['is_benefit',''], ['is_new','']]);
  348. $res = [];
  349. foreach ($data as $k => $v) {
  350. if ($v !== '') {
  351. $res[$k] = $v;
  352. }
  353. }
  354. if (!$res) return app('json')->fail('无信息提交');
  355. $this->repository->updates($ids, $res);
  356. return app('json')->success('修改成功');
  357. }
  358. }