Product.php 17 KB

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