Product.php 17 KB

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