Product.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace App\Http\Controllers\Manager\Collect;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\Collect\Product as Request;
  5. use App\Models\Manager\Collect\Product as ProductModel;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Carbon;
  8. /**
  9. * 采集配置-商品管理
  10. * @author 唐远望
  11. * @version 1.0
  12. * @date 2025-12-30
  13. */
  14. class Product extends Controller
  15. {
  16. /**
  17. * 列表
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-12-30
  21. *
  22. */
  23. public function list(Request $request, ProductModel $ProductModel)
  24. {
  25. $request->scene('list')->validate();
  26. $admin_company_id = request('admin_company_id', '0');
  27. $company_id = request('access_token.company_id', '0');
  28. $is_admin = request('access_token.is_admin', '0');
  29. // 查询条件
  30. $map = [];
  31. $limit = request('limit', config('page_num', 10));
  32. $status = request('status', '');
  33. $start_time = request('start_time', '');
  34. $end_time = request('end_time', '');
  35. $product_name = request('product_name', '');
  36. $platform = request('platform', '');
  37. // 时间条件
  38. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  39. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  40. // 其他条件
  41. if (is_numeric($status)) $map[] = ['status', '=', $status];
  42. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  43. if (is_numeric($platform) || $platform) $map[] = ['platform', 'like', "%$platform%"];
  44. // 权限判断
  45. if ($is_admin != 1 && $company_id != 0) {
  46. $map[] = ['company_id', '=', $company_id];
  47. } else {
  48. $map[] = ['company_id', '=', $admin_company_id];
  49. }
  50. // 查询数据
  51. $result = $ProductModel->query()
  52. ->where($map)
  53. ->orderByDesc('id')
  54. ->paginate($limit)->toarray();
  55. // 分配数据
  56. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  57. if (isset($result['data']) && count($result['data']) > 0) {
  58. foreach ($result['data'] as $key => $value) {
  59. $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : '';
  60. $product_specs = isset($value['product_specs']) ? explode(',', $value['product_specs']) : '';
  61. //移除空数组
  62. $result['data'][$key]['product_specs'] = $product_specs ? array_filter($product_specs) : '';
  63. }
  64. }
  65. // 加载模板
  66. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  67. }
  68. /**
  69. * 商品名称规格列表
  70. * @author 唐远望
  71. * @version 1.0
  72. * @date 2026-03-25
  73. *
  74. */
  75. public function product_name_specs_list(Request $request, ProductModel $ProductModel)
  76. {
  77. $request->scene('product_name_specs_list')->validate();
  78. $admin_company_id = request('admin_company_id', '0');
  79. $company_id = request('access_token.company_id', '0');
  80. $is_admin = request('access_token.is_admin', '0');
  81. // 查询条件
  82. $map = [];
  83. $start_time = request('start_time', '');
  84. $end_time = request('end_time', '');
  85. $product_name = request('product_name', '');
  86. $platform = request('platform', '');
  87. // 时间条件
  88. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  89. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  90. // 其他条件
  91. $map[] = ['status', '=', 0];
  92. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  93. if (is_numeric($platform) || $platform) $map[] = ['platform', 'like', "%$platform%"];
  94. // 权限判断
  95. if ($is_admin != 1 && $company_id != 0) {
  96. $map[] = ['company_id', '=', $company_id];
  97. } else {
  98. $map[] = ['company_id', '=', $admin_company_id];
  99. }
  100. // 查询数据
  101. $result = $ProductModel->query()
  102. ->where($map)
  103. ->select(['id', 'product_name', 'product_specs'])
  104. ->distinct('product_name')
  105. ->orderByDesc('id')->get()->toarray();
  106. // 分配数据
  107. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  108. if (count($result) > 0) {
  109. foreach ($result as $key => $value) {
  110. $product_specs = isset($value['product_specs']) ? explode(',', $value['product_specs']) : '';
  111. //移除空数组
  112. $result[$key]['product_specs'] = $product_specs ? array_filter($product_specs) : '';
  113. }
  114. }
  115. // 加载模板
  116. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  117. }
  118. /**
  119. * 详情
  120. * @author 唐远望
  121. * @version 1.0
  122. * @date 2025-12-30
  123. */
  124. public function detail(Request $request, ProductModel $ProductModel)
  125. {
  126. $request->scene('detail')->validate();
  127. $admin_company_id = request('admin_company_id', '0');
  128. $company_id = request('access_token.company_id', '0');
  129. $is_admin = request('access_token.is_admin', '0');
  130. // 接收参数
  131. $id = request('id', 0);
  132. $map = ['id' => $id];
  133. // 权限判断
  134. if ($is_admin != 1 && $company_id != 0) {
  135. $map['company_id'] = $company_id;
  136. } else {
  137. $map['company_id'] = $admin_company_id;
  138. }
  139. $data = $ProductModel->where($map)->first();
  140. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  141. $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
  142. $product_specs = isset($data->product_specs) ? explode(',', $data->product_specs) : '';
  143. //移除空数组
  144. $data->product_specs = $product_specs ? array_filter($product_specs) : '';
  145. // 加载模板
  146. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  147. }
  148. /**
  149. * 添加
  150. * @author 唐远望
  151. * @version 1.0
  152. * @date 2025-12-30
  153. *
  154. */
  155. public function add(Request $request, ProductModel $ProductModel)
  156. {
  157. $request->scene('add')->validate();
  158. $admin_company_id = request('admin_company_id', '0');
  159. $company_id = request('access_token.company_id', '0');
  160. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  161. //商品启用数量
  162. // $product_count = $ProductModel->where('status', 0)->count();
  163. //判断是否超过限制
  164. // if ($product_count >= 50) {
  165. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  166. // }
  167. // 接收数据
  168. $all_data = request()->all();
  169. $product_brand = request('product_brand', '');
  170. $product_keyword = request('product_keyword', '');
  171. $all_data['product_brand'] = $product_brand;
  172. $all_data['product_keyword'] = $product_keyword;
  173. //采集信息配置
  174. $enable_full_quantity = request('enable_full_quantity', 1); //全量,0启用,1禁用
  175. $all_data['enable_full_quantity'] = $enable_full_quantity;
  176. if ($enable_full_quantity == 1 && !isset($all_data['product_specs'])) {
  177. return json_send(['code' => 'error', 'msg' => '非全量采集时,商品规格不能为空']);
  178. }
  179. $minimum_order_quantity = request('minimum_order_quantity', 1);
  180. $sampling_cycle = request('sampling_cycle', '0');
  181. $sampling_start_time = request('sampling_start_time', '');
  182. $sampling_end_time = request('sampling_end_time', '');
  183. $all_data['sampling_cycle'] = $sampling_cycle;
  184. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '23:59:59') : '0';
  185. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  186. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  187. if ($all_data['sampling_start_time'] && $all_data['sampling_start_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集开始时间必须大于当前时间']);
  188. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  189. //查询是否存在
  190. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  191. if ($is_admin != 1 && $company_id != 0) {
  192. $map['company_id'] = $company_id;
  193. $all_data['company_id'] = $company_id;
  194. } else {
  195. $map['company_id'] = $admin_company_id;
  196. $all_data['company_id'] = $admin_company_id;
  197. }
  198. $data = $ProductModel->where($map)->first();
  199. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  200. // 写入数据表
  201. $result = $ProductModel->addProduct($all_data);
  202. // 如果操作失败
  203. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  204. // 记录行为
  205. $admin_id = request('access_token.uid', 0); //用户ID
  206. $table_name = $ProductModel->getTable();
  207. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  208. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  209. // 告知结果
  210. return json_send(['code' => 'success', 'msg' => '新增成功']);
  211. }
  212. /**
  213. * 修改
  214. * @author 唐远望
  215. * @version 1.0
  216. * @date 2025-12-30
  217. *
  218. */
  219. public function edit(Request $request, ProductModel $ProductModel)
  220. {
  221. $request->scene('edit')->validate();
  222. $admin_company_id = request('admin_company_id', '0');
  223. $company_id = request('access_token.company_id', '0');
  224. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  225. // 接收参数
  226. $id = request('id', 0);
  227. // 接收数据
  228. $all_data = request()->all();
  229. $product_brand = request('product_brand', '');
  230. $product_keyword = request('product_keyword', '');
  231. $all_data['product_brand'] = $product_brand;
  232. $all_data['product_keyword'] = $product_keyword;
  233. //采集信息配置
  234. $enable_full_quantity = request('enable_full_quantity', 1); //全量,0启用,1禁用
  235. $all_data['enable_full_quantity'] = $enable_full_quantity;
  236. if ($enable_full_quantity == 1 && !isset($all_data['product_specs'])) {
  237. return json_send(['code' => 'error', 'msg' => '非全量采集时,商品规格不能为空']);
  238. }
  239. $minimum_order_quantity = request('minimum_order_quantity', 1);
  240. $sampling_cycle = request('sampling_cycle', '0');
  241. $sampling_start_time = request('sampling_start_time', '');
  242. $sampling_end_time = request('sampling_end_time', '');
  243. $all_data['sampling_cycle'] = $sampling_cycle;
  244. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '23:59:59') : '0';
  245. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  246. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  247. if ($all_data['sampling_start_time'] && $all_data['sampling_start_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集开始时间必须大于当前时间']);
  248. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  249. //查询是否存在
  250. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  251. if ($is_admin != 1 && $company_id != 0) {
  252. $map['company_id'] = $company_id;
  253. } else {
  254. $map['company_id'] = $admin_company_id;
  255. }
  256. $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
  257. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  258. // 更新数据表
  259. $where = ['id' => $id];
  260. // 权限判断
  261. if ($is_admin != 1 && $company_id != 0) {
  262. $where['company_id'] = $company_id;
  263. } else {
  264. $where['company_id'] = $admin_company_id;
  265. }
  266. $Product = $ProductModel->where($where)->first();
  267. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  268. $oldData = $Product->toarray();
  269. $result = $ProductModel->editProduct_content($Product, $all_data);
  270. // 如果操作失败
  271. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  272. // 记录行为
  273. $admin_id = request('access_token.uid', 0); //用户ID
  274. $table_name = $ProductModel->getTable();
  275. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  276. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  277. // 告知结果
  278. return json_send(['code' => 'success', 'msg' => '修改成功']);
  279. }
  280. /**
  281. * 修改状态
  282. * @author 唐远望
  283. * @version 1.0
  284. * @date 2025-12-30
  285. *
  286. */
  287. public function set_status(Request $request, ProductModel $ProductModel)
  288. {
  289. // 验证参数
  290. $request->scene('set_status')->validate();
  291. $admin_company_id = request('admin_company_id', '0');
  292. $company_id = request('access_token.company_id', '0');
  293. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  294. // 接收数据
  295. $id = request('id', 0);
  296. $status = request('status', 0);
  297. if ($status == 0) {
  298. //获取商品启用数量
  299. // $product_count = $ProductModel->where('status', 0)->count();
  300. //判断是否超过限制
  301. // if ($product_count >= 50) {
  302. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  303. // }
  304. }
  305. // 查询用户
  306. $where = ['id' => $id];
  307. if ($is_admin != 1 && $company_id != 0) {
  308. $where['company_id'] = $company_id;
  309. } else {
  310. $where['company_id'] = $admin_company_id;
  311. }
  312. $Product = $ProductModel->where($where)->first();
  313. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  314. // 执行修改
  315. $result = $ProductModel->changeStatus($Product, $status);
  316. // 提示新增失败
  317. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  318. // 记录行为
  319. $admin_id = request('access_token.uid', 0); //用户ID
  320. $table_name = $ProductModel->getTable();
  321. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  322. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
  323. // 告知结果
  324. return json_send(['code' => 'success', 'msg' => '设置成功']);
  325. }
  326. /**
  327. * 删除
  328. * @author 唐远望
  329. * @version 1.0
  330. * @date 2025-12-30
  331. *
  332. */
  333. public function delete(Request $request, ProductModel $ProductModel)
  334. {
  335. // 验证参数
  336. $request->scene('delete')->validate();
  337. $admin_company_id = request('admin_company_id', '0');
  338. $company_id = request('access_token.company_id', '0');
  339. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  340. // 接收数据
  341. $id = request('id', 0);
  342. // 查询用户
  343. $where = ['id' => $id];
  344. if ($is_admin != 1 && $company_id != 0) {
  345. $where['company_id'] = $company_id;
  346. } else {
  347. $where['company_id'] = $admin_company_id;
  348. }
  349. // 执行删除
  350. $Product = $ProductModel->where($where)->first();
  351. if (!$Product) {
  352. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  353. }
  354. DB::beginTransaction();
  355. try {
  356. $Product->delete();
  357. // 记录行为
  358. $admin_id = request('access_token.uid', 0); //用户ID
  359. $table_name = $ProductModel->getTable();
  360. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  361. $this->addAdminHistory('采集配置-商品管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  362. // 告知结果
  363. DB::commit();
  364. return json_send(['code' => 'success', 'msg' => '删除成功']);
  365. // 成功处理...
  366. } catch (\Exception $e) {
  367. DB::rollBack();
  368. // 错误处理...
  369. return json_send(['code' => 'error', 'msg' => '删除失败']);
  370. }
  371. }
  372. }