Product.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. }
  61. }
  62. // 加载模板
  63. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  64. }
  65. /**
  66. * 详情
  67. * @author 唐远望
  68. * @version 1.0
  69. * @date 2025-12-30
  70. */
  71. public function detail(Request $request, ProductModel $ProductModel)
  72. {
  73. $request->scene('detail')->validate();
  74. $admin_company_id = request('admin_company_id', '0');
  75. $company_id = request('access_token.company_id', '0');
  76. $is_admin = request('access_token.is_admin', '0');
  77. // 接收参数
  78. $id = request('id', 0);
  79. $map = ['id' => $id];
  80. // 权限判断
  81. if ($is_admin != 1 && $company_id != 0) {
  82. $map['company_id'] = $company_id;
  83. } else {
  84. $map['company_id'] = $admin_company_id;
  85. }
  86. $data = $ProductModel->where($map)->first();
  87. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  88. $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
  89. // 加载模板
  90. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  91. }
  92. /**
  93. * 添加
  94. * @author 唐远望
  95. * @version 1.0
  96. * @date 2025-12-30
  97. *
  98. */
  99. public function add(Request $request, ProductModel $ProductModel)
  100. {
  101. $request->scene('add')->validate();
  102. $admin_company_id = request('admin_company_id', '0');
  103. $company_id = request('access_token.company_id', '0');
  104. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  105. //商品启用数量
  106. // $product_count = $ProductModel->where('status', 0)->count();
  107. //判断是否超过限制
  108. // if ($product_count >= 50) {
  109. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  110. // }
  111. // 接收数据
  112. $all_data = request()->all();
  113. //采集信息配置
  114. $minimum_order_quantity = request('minimum_order_quantity', 1);
  115. $sampling_cycle = request('sampling_cycle', '0');
  116. $sampling_start_time = request('sampling_start_time', '');
  117. $sampling_end_time = request('sampling_end_time', '');
  118. $all_data['sampling_cycle'] = $sampling_cycle;
  119. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  120. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  121. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  122. $allow_sampling_time = Carbon::tomorrow()->startOfDay()->getTimestamp(); // 明天的开始时间(允许开始采集时间校验)
  123. if ($all_data['sampling_start_time'] && $all_data['sampling_start_time'] < $allow_sampling_time) return json_send(['code' => 'error', 'msg' => '采集最早开始时间为明天']);
  124. //查询是否存在
  125. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  126. if ($is_admin != 1 && $company_id != 0) {
  127. $map['company_id'] = $company_id;
  128. $all_data['company_id'] = $company_id;
  129. } else {
  130. $map['company_id'] = $admin_company_id;
  131. $all_data['company_id'] = $admin_company_id;
  132. }
  133. $data = $ProductModel->where($map)->first();
  134. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  135. // 写入数据表
  136. $result = $ProductModel->addProduct($all_data);
  137. // 如果操作失败
  138. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  139. // 记录行为
  140. $admin_id = request('access_token.uid', 0); //用户ID
  141. $table_name = $ProductModel->getTable();
  142. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  143. $this->addAdminHistory('采集配置-商品管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了商品' . $all_data['product_name'] . '信息');
  144. // 告知结果
  145. return json_send(['code' => 'success', 'msg' => '新增成功']);
  146. }
  147. /**
  148. * 修改
  149. * @author 唐远望
  150. * @version 1.0
  151. * @date 2025-12-30
  152. *
  153. */
  154. public function edit(Request $request, ProductModel $ProductModel)
  155. {
  156. $request->scene('edit')->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. $id = request('id', 0);
  162. // 接收数据
  163. $all_data = request()->all();
  164. //采集信息配置
  165. $minimum_order_quantity = request('minimum_order_quantity', 1);
  166. $sampling_cycle = request('sampling_cycle', '0');
  167. $sampling_start_time = request('sampling_start_time', '');
  168. $sampling_end_time = request('sampling_end_time', '');
  169. $all_data['sampling_cycle'] = $sampling_cycle;
  170. $all_data['sampling_start_time'] = $sampling_start_time ? strtotime($sampling_start_time . '00:00:00') : '0';
  171. $all_data['sampling_end_time'] = $sampling_end_time ? strtotime($sampling_end_time . '23:59:59') : '0';
  172. $all_data['minimum_order_quantity'] = $minimum_order_quantity;
  173. //查询是否存在
  174. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  175. if ($is_admin != 1 && $company_id != 0) {
  176. $map['company_id'] = $company_id;
  177. } else {
  178. $map['company_id'] = $admin_company_id;
  179. }
  180. $data = $ProductModel->where($map)->where('id', '!=', $id)->first();
  181. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  182. // 更新数据表
  183. $where = ['id' => $id];
  184. // 权限判断
  185. if ($is_admin != 1 && $company_id != 0){
  186. $where['company_id'] = $company_id;
  187. }else{
  188. $where['company_id'] = $admin_company_id;
  189. }
  190. $Product = $ProductModel->where($where)->first();
  191. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  192. $oldData = $Product->toarray();
  193. $allow_sampling_time = Carbon::tomorrow()->startOfDay()->getTimestamp(); // 明天的开始时间(允许开始采集时间校验)
  194. if ($all_data['sampling_start_time'] && $all_data['sampling_start_time'] < $allow_sampling_time) return json_send(['code' => 'error', 'msg' => '采集最早开始时间为明天']);
  195. $result = $ProductModel->editProduct_content($Product, $all_data);
  196. // 如果操作失败
  197. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  198. // 记录行为
  199. $admin_id = request('access_token.uid', 0); //用户ID
  200. $table_name = $ProductModel->getTable();
  201. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  202. $this->addAdminHistory('采集配置-商品管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了商品' . $oldData['product_name'] . '信息');
  203. // 告知结果
  204. return json_send(['code' => 'success', 'msg' => '修改成功']);
  205. }
  206. /**
  207. * 修改状态
  208. * @author 唐远望
  209. * @version 1.0
  210. * @date 2025-12-30
  211. *
  212. */
  213. public function set_status(Request $request, ProductModel $ProductModel)
  214. {
  215. // 验证参数
  216. $request->scene('set_status')->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. $status = request('status', 0);
  223. if ($status == 0) {
  224. //获取商品启用数量
  225. // $product_count = $ProductModel->where('status', 0)->count();
  226. //判断是否超过限制
  227. // if ($product_count >= 50) {
  228. // return json_send(['code' => 'error', 'msg' => '启用数量超过限制,不能超过50条']);
  229. // }
  230. }
  231. // 查询用户
  232. $where = ['id' => $id];
  233. if ($is_admin != 1 && $company_id != 0){
  234. $where['company_id'] = $company_id;
  235. }else{
  236. $where['company_id'] = $admin_company_id;
  237. }
  238. $Product = $ProductModel->where($where)->first();
  239. if (!$Product) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  240. // 执行修改
  241. $result = $ProductModel->changeStatus($Product, $status);
  242. // 提示新增失败
  243. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  244. // 记录行为
  245. $admin_id = request('access_token.uid', 0); //用户ID
  246. $table_name = $ProductModel->getTable();
  247. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  248. $this->addAdminHistory('采集配置-商品管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了商品' . $Product->product_name . '状态');
  249. // 告知结果
  250. return json_send(['code' => 'success', 'msg' => '设置成功']);
  251. }
  252. /**
  253. * 删除
  254. * @author 唐远望
  255. * @version 1.0
  256. * @date 2025-12-30
  257. *
  258. */
  259. public function delete(Request $request, ProductModel $ProductModel)
  260. {
  261. // 验证参数
  262. $request->scene('delete')->validate();
  263. $admin_company_id = request('admin_company_id', '0');
  264. $company_id = request('access_token.company_id', '0');
  265. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  266. // 接收数据
  267. $id = request('id', 0);
  268. // 查询用户
  269. $where = ['id' => $id];
  270. if ($is_admin != 1 && $company_id != 0){
  271. $where['company_id'] = $company_id;
  272. }else{
  273. $where['company_id'] = $admin_company_id;
  274. }
  275. // 执行删除
  276. $Product = $ProductModel->where($where)->first();
  277. if (!$Product) {
  278. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  279. }
  280. DB::beginTransaction();
  281. try {
  282. $Product->delete();
  283. // 记录行为
  284. $admin_id = request('access_token.uid', 0); //用户ID
  285. $table_name = $ProductModel->getTable();
  286. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  287. $this->addAdminHistory('采集配置-商品管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, $Product->toarray(), [], '删除了商品' . $Product->product_name . '信息');
  288. // 告知结果
  289. DB::commit();
  290. return json_send(['code' => 'success', 'msg' => '删除成功']);
  291. // 成功处理...
  292. } catch (\Exception $e) {
  293. DB::rollBack();
  294. // 错误处理...
  295. return json_send(['code' => 'error', 'msg' => '删除失败']);
  296. }
  297. }
  298. }