Product.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // 查询条件
  27. $map = [];
  28. $limit = request('limit', config('page_num', 10));
  29. $status = request('status', '');
  30. $start_time = request('start_time', '');
  31. $end_time = request('end_time', '');
  32. $product_name = request('product_name', '');
  33. $platform = request('platform', '');
  34. // 时间条件
  35. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  36. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  37. // 其他条件
  38. if (is_numeric($status)) $map[] = ['status', '=', $status];
  39. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  40. if (is_numeric($platform) || $platform) $map[] = ['platform', 'like', "%$platform%"];
  41. // 查询数据
  42. $result = $ProductModel->query()
  43. ->where($map)
  44. ->orderByDesc('id')
  45. ->paginate($limit)->toarray();
  46. // 分配数据
  47. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  48. if (isset($result['data']) && count($result['data']) > 0) {
  49. foreach ($result['data'] as $key => $value) {
  50. $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : '';
  51. }
  52. }
  53. // 加载模板
  54. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  55. }
  56. /**
  57. * 详情
  58. * @author 唐远望
  59. * @version 1.0
  60. * @date 2025-12-30
  61. */
  62. public function detail(Request $request, ProductModel $ProductModel)
  63. {
  64. $request->scene('detail')->validate();
  65. // 接收参数
  66. $id = request('id', 0);
  67. $map = ['id' => $id];
  68. $data = $ProductModel->where($map)->first();
  69. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  70. $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
  71. // 加载模板
  72. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  73. }
  74. /**
  75. * 添加
  76. * @author 唐远望
  77. * @version 1.0
  78. * @date 2025-12-30
  79. *
  80. */
  81. public function add(Request $request, ProductModel $ProductModel)
  82. {
  83. $request->scene('add')->validate();
  84. //商品启用数量
  85. // $product_count = $ProductModel->where('status', 0)->count();
  86. //判断是否超过限制
  87. // if ($product_count >= 50) {
  88. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  89. // }
  90. // 接收数据
  91. $all_data = request()->all();
  92. //采集信息配置
  93. $minimum_order_quantity = request('minimum_order_quantity', 1);
  94. $sampling_cycle = request('sampling_cycle', '0');
  95. $sampling_start_time = request('sampling_start_time', '');
  96. $sampling_end_time = request('sampling_end_time', '');
  97. $all_data['sampling_cycle'] = $sampling_cycle;
  98. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  99. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  100. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  101. $allow_sampling_time = Carbon::tomorrow()->startOfDay()->getTimestamp(); // 明天的开始时间(允许开始采集时间校验)
  102. if ($all_data['sampling_start_time'] && $all_data['sampling_start_time'] < $allow_sampling_time) return json_send(['code' => 'error', 'msg' => '采集最早开始时间为明天']);
  103. //查询是否存在
  104. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  105. $data = $ProductModel->where($map)->first();
  106. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  107. // 写入数据表
  108. $result = $ProductModel->addProduct($all_data);
  109. // 如果操作失败
  110. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  111. // 记录行为
  112. $admin_id = request('access_token.uid', 0); //用户ID
  113. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  114. $table_name = $ProductModel->getTable();
  115. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  116. $this->addAdminHistory('采集配置-商品管理', $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  117. // 告知结果
  118. return json_send(['code' => 'success', 'msg' => '新增成功']);
  119. }
  120. /**
  121. * 修改
  122. * @author 唐远望
  123. * @version 1.0
  124. * @date 2025-12-30
  125. *
  126. */
  127. public function edit(Request $request, ProductModel $ProductModel)
  128. {
  129. $request->scene('edit')->validate();
  130. // 接收参数
  131. $id = request('id', 0);
  132. // 接收数据
  133. $all_data = request()->all();
  134. //采集信息配置
  135. $minimum_order_quantity = request('minimum_order_quantity', 0);
  136. $sampling_cycle = request('sampling_cycle', '0');
  137. $sampling_start_time = request('sampling_start_time', '');
  138. $sampling_end_time = request('sampling_end_time', '');
  139. $all_data['sampling_cycle'] = $sampling_cycle;
  140. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  141. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  142. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  143. //查询是否存在
  144. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  145. $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
  146. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  147. // 更新数据表
  148. $where = ['id' => $id];
  149. $Product = $ProductModel->where($where)->first();
  150. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  151. $oldData = $Product->toarray();
  152. //如果修改采集周期,则校验采集时间是否在明天以后
  153. if ($sampling_cycle != $Product->sampling_cycle) {
  154. $allow_sampling_time = Carbon::tomorrow()->startOfDay()->getTimestamp(); // 明天的开始时间(允许开始采集时间校验)
  155. if ($all_data['sampling_start_time'] && $all_data['sampling_start_time'] < $allow_sampling_time) return json_send(['code' => 'error', 'msg' => '采集最早开始时间为明天']);
  156. }
  157. $result = $ProductModel->editProduct_content($Product, $all_data);
  158. // 如果操作失败
  159. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  160. // 记录行为
  161. $admin_id = request('access_token.uid', 0); //用户ID
  162. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  163. $table_name = $ProductModel->getTable();
  164. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  165. $this->addAdminHistory('采集配置-商品管理', $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  166. // 告知结果
  167. return json_send(['code' => 'success', 'msg' => '修改成功']);
  168. }
  169. /**
  170. * 修改状态
  171. * @author 唐远望
  172. * @version 1.0
  173. * @date 2025-12-30
  174. *
  175. */
  176. public function set_status(Request $request, ProductModel $ProductModel)
  177. {
  178. // 验证参数
  179. $request->scene('set_status')->validate();
  180. // 接收数据
  181. $id = request('id', 0);
  182. $status = request('status', 0);
  183. if ($status == 0) {
  184. //获取商品启用数量
  185. // $product_count = $ProductModel->where('status', 0)->count();
  186. //判断是否超过限制
  187. if ($product_count >= 50) {
  188. return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  189. }
  190. }
  191. // 查询用户
  192. $where = ['id' => $id];
  193. $Product = $ProductModel->where($where)->first();
  194. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  195. // 执行修改
  196. $result = $ProductModel->changeStatus($Product, $status);
  197. // 提示新增失败
  198. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  199. // 记录行为
  200. $admin_id = request('access_token.uid', 0); //用户ID
  201. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  202. $table_name = $ProductModel->getTable();
  203. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  204. $this->addAdminHistory('采集配置-商品管理', $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->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 delete(Request $request, ProductModel $ProductModel)
  216. {
  217. // 验证参数
  218. $request->scene('delete')->validate();
  219. // 接收数据
  220. $id = request('id', 0);
  221. // 查询用户
  222. $where = ['id' => $id];
  223. // 执行删除
  224. $Product = $ProductModel->where($where)->first();
  225. if (!$Product) {
  226. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  227. }
  228. DB::beginTransaction();
  229. try {
  230. $Product->delete();
  231. // 记录行为
  232. $admin_id = request('access_token.uid', 0); //用户ID
  233. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  234. $table_name = $ProductModel->getTable();
  235. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  236. $this->addAdminHistory('采集配置-商品管理', $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  237. // 告知结果
  238. DB::commit();
  239. return json_send(['code' => 'success', 'msg' => '删除成功']);
  240. // 成功处理...
  241. } catch (\Exception $e) {
  242. DB::rollBack();
  243. // 错误处理...
  244. return json_send(['code' => 'error', 'msg' => '删除失败']);
  245. }
  246. }
  247. }