Product.php 15 KB

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