Product.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. $request->scene('product_name_specs_list')->validate();
  77. $admin_company_id = request('admin_company_id', '0');
  78. $company_id = request('access_token.company_id', '0');
  79. $is_admin = request('access_token.is_admin', '0');
  80. // 查询条件
  81. $map = [];
  82. $start_time = request('start_time', '');
  83. $end_time = request('end_time', '');
  84. $product_name = request('product_name', '');
  85. $platform = request('platform', '');
  86. // 时间条件
  87. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  88. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  89. // 其他条件
  90. $map[] = ['status', '=', 0];
  91. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  92. if (is_numeric($platform) || $platform) $map[] = ['platform', 'like', "%$platform%"];
  93. // 权限判断
  94. if ($is_admin != 1 && $company_id != 0){
  95. $map[] = ['company_id', '=', $company_id];
  96. }else{
  97. $map[] = ['company_id', '=', $admin_company_id];
  98. }
  99. // 查询数据
  100. $result = $ProductModel->query()
  101. ->where($map)
  102. ->select(['id','product_name'])
  103. ->distinct('product_name')
  104. ->orderByDesc('id')->toarray();
  105. // 分配数据
  106. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  107. if (isset($result['data']) && count($result['data']) > 0) {
  108. foreach ($result['data'] as $key => $value) {
  109. $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : '';
  110. $product_specs = isset($value['product_specs']) ? explode(',', $value['product_specs']) : '';
  111. //移除空数组
  112. $result['data'][$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. //采集信息配置
  170. $enable_full_quantity = request('enable_full_quantity', 1);//全量,0启用,1禁用
  171. $all_data['enable_full_quantity'] = $enable_full_quantity;
  172. if($enable_full_quantity == 1 && !isset($all_data['product_specs'])){
  173. return json_send(['code' => 'error', 'msg' => '非全量采集时,商品规格不能为空']);
  174. }
  175. $minimum_order_quantity = request('minimum_order_quantity', 1);
  176. $sampling_cycle = request('sampling_cycle', '0');
  177. $sampling_start_time = request('sampling_start_time', '');
  178. $sampling_end_time = request('sampling_end_time', '');
  179. $all_data['sampling_cycle'] = $sampling_cycle;
  180. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  181. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  182. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  183. if ($all_data['sampling_start_time'] && $all_data['sampling_start_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集开始时间必须大于当前时间']);
  184. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  185. //查询是否存在
  186. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  187. if ($is_admin != 1 && $company_id != 0) {
  188. $map['company_id'] = $company_id;
  189. $all_data['company_id'] = $company_id;
  190. } else {
  191. $map['company_id'] = $admin_company_id;
  192. $all_data['company_id'] = $admin_company_id;
  193. }
  194. $data = $ProductModel->where($map)->first();
  195. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  196. // 写入数据表
  197. $result = $ProductModel->addProduct($all_data);
  198. // 如果操作失败
  199. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  200. // 记录行为
  201. $admin_id = request('access_token.uid', 0); //用户ID
  202. $table_name = $ProductModel->getTable();
  203. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  204. $this->addAdminHistory('采集配置-商品管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  205. // 告知结果
  206. return json_send(['code' => 'success', 'msg' => '新增成功']);
  207. }
  208. /**
  209. * 修改
  210. * @author 唐远望
  211. * @version 1.0
  212. * @date 2025-12-30
  213. *
  214. */
  215. public function edit(Request $request, ProductModel $ProductModel)
  216. {
  217. $request->scene('edit')->validate();
  218. $admin_company_id = request('admin_company_id', '0');
  219. $company_id = request('access_token.company_id', '0');
  220. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  221. // 接收参数
  222. $id = request('id', 0);
  223. // 接收数据
  224. $all_data = request()->all();
  225. //采集信息配置
  226. $enable_full_quantity = request('enable_full_quantity', 1);//全量,0启用,1禁用
  227. $all_data['enable_full_quantity'] = $enable_full_quantity;
  228. if($enable_full_quantity == 1 && !isset($all_data['product_specs'])){
  229. return json_send(['code' => 'error', 'msg' => '非全量采集时,商品规格不能为空']);
  230. }
  231. $minimum_order_quantity = request('minimum_order_quantity', 1);
  232. $sampling_cycle = request('sampling_cycle', '0');
  233. $sampling_start_time = request('sampling_start_time', '');
  234. $sampling_end_time = request('sampling_end_time', '');
  235. $all_data['sampling_cycle'] = $sampling_cycle;
  236. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  237. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  238. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  239. if ($all_data['sampling_start_time'] && $all_data['sampling_start_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集开始时间必须大于当前时间']);
  240. if ($all_data['sampling_end_time'] && $all_data['sampling_end_time'] < time()) return json_send(['code' => 'error', 'msg' => '采集结束时间必须大于当前时间']);
  241. //查询是否存在
  242. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  243. if ($is_admin != 1 && $company_id != 0) {
  244. $map['company_id'] = $company_id;
  245. } else {
  246. $map['company_id'] = $admin_company_id;
  247. }
  248. $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
  249. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  250. // 更新数据表
  251. $where = ['id' => $id];
  252. // 权限判断
  253. if ($is_admin != 1 && $company_id != 0){
  254. $where['company_id'] = $company_id;
  255. }else{
  256. $where['company_id'] = $admin_company_id;
  257. }
  258. $Product = $ProductModel->where($where)->first();
  259. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  260. $oldData = $Product->toarray();
  261. $result = $ProductModel->editProduct_content($Product, $all_data);
  262. // 如果操作失败
  263. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  264. // 记录行为
  265. $admin_id = request('access_token.uid', 0); //用户ID
  266. $table_name = $ProductModel->getTable();
  267. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  268. $this->addAdminHistory('采集配置-商品管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  269. // 告知结果
  270. return json_send(['code' => 'success', 'msg' => '修改成功']);
  271. }
  272. /**
  273. * 修改状态
  274. * @author 唐远望
  275. * @version 1.0
  276. * @date 2025-12-30
  277. *
  278. */
  279. public function set_status(Request $request, ProductModel $ProductModel)
  280. {
  281. // 验证参数
  282. $request->scene('set_status')->validate();
  283. $admin_company_id = request('admin_company_id', '0');
  284. $company_id = request('access_token.company_id', '0');
  285. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  286. // 接收数据
  287. $id = request('id', 0);
  288. $status = request('status', 0);
  289. if ($status == 0) {
  290. //获取商品启用数量
  291. // $product_count = $ProductModel->where('status', 0)->count();
  292. //判断是否超过限制
  293. // if ($product_count >= 50) {
  294. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  295. // }
  296. }
  297. // 查询用户
  298. $where = ['id' => $id];
  299. if ($is_admin != 1 && $company_id != 0){
  300. $where['company_id'] = $company_id;
  301. }else{
  302. $where['company_id'] = $admin_company_id;
  303. }
  304. $Product = $ProductModel->where($where)->first();
  305. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  306. // 执行修改
  307. $result = $ProductModel->changeStatus($Product, $status);
  308. // 提示新增失败
  309. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  310. // 记录行为
  311. $admin_id = request('access_token.uid', 0); //用户ID
  312. $table_name = $ProductModel->getTable();
  313. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  314. $this->addAdminHistory('采集配置-商品管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
  315. // 告知结果
  316. return json_send(['code' => 'success', 'msg' => '设置成功']);
  317. }
  318. /**
  319. * 删除
  320. * @author 唐远望
  321. * @version 1.0
  322. * @date 2025-12-30
  323. *
  324. */
  325. public function delete(Request $request, ProductModel $ProductModel)
  326. {
  327. // 验证参数
  328. $request->scene('delete')->validate();
  329. $admin_company_id = request('admin_company_id', '0');
  330. $company_id = request('access_token.company_id', '0');
  331. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  332. // 接收数据
  333. $id = request('id', 0);
  334. // 查询用户
  335. $where = ['id' => $id];
  336. if ($is_admin != 1 && $company_id != 0){
  337. $where['company_id'] = $company_id;
  338. }else{
  339. $where['company_id'] = $admin_company_id;
  340. }
  341. // 执行删除
  342. $Product = $ProductModel->where($where)->first();
  343. if (!$Product) {
  344. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  345. }
  346. DB::beginTransaction();
  347. try {
  348. $Product->delete();
  349. // 记录行为
  350. $admin_id = request('access_token.uid', 0); //用户ID
  351. $table_name = $ProductModel->getTable();
  352. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  353. $this->addAdminHistory('采集配置-商品管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  354. // 告知结果
  355. DB::commit();
  356. return json_send(['code' => 'success', 'msg' => '删除成功']);
  357. // 成功处理...
  358. } catch (\Exception $e) {
  359. DB::rollBack();
  360. // 错误处理...
  361. return json_send(['code' => 'error', 'msg' => '删除失败']);
  362. }
  363. }
  364. }