Product.php 14 KB

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