ViolationProduct.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. namespace App\Http\Controllers\Manager\WashConfig;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\WashConfig\ViolationProduct as Request;
  5. use App\Models\Manager\WashConfig\ViolationProduct as ViolationProductModel;
  6. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  7. use App\Models\Manager\WashConfig\ViolationProductCompany as ViolationProductCompanyModel;
  8. use App\Models\Manager\WashConfig\ProductCategory as ProductCategoryModel;
  9. use Illuminate\Support\Facades\DB;
  10. use App\Models\Manager\WashConfig\LowPriceGoods as LowPriceGoodsModel;
  11. use Illuminate\Support\Carbon;
  12. /**
  13. * 数据清洗-禁止产品配置
  14. * @author 唐远望
  15. * @version 1.0
  16. * @date 2025-12-03
  17. */
  18. class ViolationProduct extends Controller
  19. {
  20. /**
  21. * 列表
  22. * @author 唐远望
  23. * @version 1.0
  24. * @date 2025-12-03
  25. *
  26. */
  27. public function list(Request $request, ViolationProductModel $ViolationProductModel, ViolationStoreModel $ViolationStoreModel, ViolationProductCompanyModel $ViolationProductCompanyModel, ProductCategoryModel $ProductCategoryModel)
  28. {
  29. $request->scene('list')->validate();
  30. // 查询条件
  31. $map = [];
  32. $limit = request('limit', config('page_num', 10));
  33. $status = request('status', '');
  34. $start_time = request('start_time', '');
  35. $end_time = request('end_time', '');
  36. $product_name = request('product_name', '');
  37. $platform = request('platform', '');
  38. $store_scope = request('store_scope', '');
  39. $company_scope = request('company_scope', '');
  40. $category_id = request('category_id', '');
  41. // 时间条件
  42. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  43. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  44. // 其他条件
  45. if (is_numeric($status)) $map[] = ['status', '=', $status];
  46. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  47. if ($platform) $map[] = ['platform', 'like', "%$platform%"];
  48. if ($store_scope) $map[] = ['store_scope', '=', $store_scope];
  49. if ($company_scope) $map[] = ['company_scope', '=', $company_scope];
  50. if ($category_id) $map[] = ['category_id', '=', $category_id];
  51. // 查询数据
  52. $result = $ViolationProductModel->query()
  53. ->where($map)
  54. ->orderByDesc('id')
  55. ->paginate($limit)->toarray();
  56. // 分配数据
  57. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  58. if (isset($result['data']) && count($result['data']) > 0) {
  59. foreach ($result['data'] as $key => $value) {
  60. $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',', $value['platform']) : '';
  61. $category_name = $value['category_id'] > 0 ? $ProductCategoryModel->where('id', $value['category_id'])->value('name') : '';
  62. $result['data'][$key]['category_name'] = $category_name;
  63. //查询店铺名称
  64. if (trim($value['store_scope']) == '') {
  65. $result['data'][$key]['store_name'] = ['全部店铺'];
  66. } else {
  67. $result['data'][$key]['store_name'] = '';
  68. }
  69. //查询公司名称
  70. if ($value['company_scope'] == '1') {
  71. $result['data'][$key]['company_name'] = ['全部公司'];
  72. } else {
  73. $company_data = $ViolationProductCompanyModel->where('violation_product_logid', $value['id'])
  74. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_violation_product_company.company_id')
  75. ->select(['washconfig_violation_store.id', 'washconfig_violation_store.company_name', 'washconfig_violation_product_company.company_id'])->get()->toArray();
  76. $result['data'][$key]['company_name'] = !empty($company_data) ? array_column($company_data, 'company_name') : '';
  77. }
  78. }
  79. }
  80. // 加载模板
  81. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  82. }
  83. /**
  84. * 详情
  85. * @author 唐远望
  86. * @version 1.0
  87. * @date 2025-12-03
  88. */
  89. public function detail(Request $request, ViolationProductModel $ViolationProductModel, ViolationProductCompanyModel $ViolationProductCompanyModel, ProductCategoryModel $ProductCategoryModel)
  90. {
  91. $request->scene('detail')->validate();
  92. // 接收参数
  93. $id = request('id', 0);
  94. $map = ['id' => $id];
  95. $data = $ViolationProductModel->where($map)->first();
  96. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  97. //查询店铺名称
  98. if (trim($data['store_scope']) == '') {
  99. $data['store_name'] = ['全部店铺'];
  100. } else {
  101. $data['store_name'] = '';
  102. }
  103. //查询公司名称
  104. if ($data->company_scope == '1') {
  105. $data->company_name = ['全部公司'];
  106. $data->company_ids = '';
  107. } else {
  108. $company_data = $ViolationProductCompanyModel->where('violation_product_logid', $data->id)
  109. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_violation_product_company.company_id')
  110. ->select(['washconfig_violation_store.id', 'washconfig_violation_store.company_name', 'washconfig_violation_product_company.company_id'])->get()->toArray();
  111. $data->company_name = !empty($company_data) ? array_column($company_data, 'company_name') : '';
  112. $data->company_ids = !empty($company_data) ? array_column($company_data, 'company_id') : '';
  113. }
  114. $data->platform = isset($data->platform) ? explode(',', $data->platform) : '';
  115. $data->category_name = $data->category_id > 0 ? $ProductCategoryModel->where('id', $data->category_id)->value('name') : '';
  116. // 加载模板
  117. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  118. }
  119. /**
  120. * 添加
  121. * @author 唐远望
  122. * @version 1.0
  123. * @date 2025-12-03
  124. *
  125. */
  126. public function add(Request $request, ViolationProductModel $ViolationProductModel,LowPriceGoodsModel $LowPriceGoodsModel)
  127. {
  128. $request->scene('add')->validate();
  129. //获取禁止商品启动数量
  130. $violation_product_count = $ViolationProductModel->where('status', 0)->count();
  131. //获取低价挂网商品启用数量
  132. $lowprice_product_count = $LowPriceGoodsModel->where('status', 0)->count();
  133. //计算总数量
  134. $product_totle = $violation_product_count + $lowprice_product_count;
  135. //判断是否超过限制
  136. if ($product_totle >= 50) {
  137. return json_send(['code' => 'error', 'msg' => '启用数量超过限制,低价挂网和禁止商品不能超过50条']);
  138. }
  139. // 接收数据
  140. $all_data = request()->all();
  141. $store_scope = request('store_scope', '');
  142. $all_data['store_scope'] = $store_scope;
  143. $company_scope = request('company_scope', '');
  144. $all_data['company_scope'] = $company_scope;
  145. $category_id = request('category_id', '');
  146. $all_data['category_id'] = $category_id;
  147. $specify_responsible_person = request('specify_responsible_person', '0');
  148. $all_data['specify_responsible_person'] = $specify_responsible_person;
  149. //查询是否存在
  150. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  151. $data = $ViolationProductModel->where($map)->first();
  152. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  153. // 写入数据表
  154. $result = $ViolationProductModel->addViolationProduct($all_data);
  155. // 如果操作失败
  156. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  157. // 记录行为
  158. $admin_id = request('access_token.uid', 0); //用户ID
  159. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  160. $table_name = $ViolationProductModel->getTable();
  161. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  162. $this->addAdminHistory('清洗配置-禁止商品管理', $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了禁止商品' . $all_data['product_name'] . '信息');
  163. // 告知结果
  164. return json_send(['code' => 'success', 'msg' => '新增成功']);
  165. }
  166. /**
  167. * 修改
  168. * @author 唐远望
  169. * @version 1.0
  170. * @date 2025-12-03
  171. *
  172. */
  173. public function edit(Request $request, ViolationProductModel $ViolationProductModel)
  174. {
  175. $request->scene('edit')->validate();
  176. // 接收参数
  177. $id = request('id', 0);
  178. // 接收数据
  179. $all_data = request()->all();
  180. $store_scope = request('store_scope', '');
  181. $all_data['store_scope'] = $store_scope;
  182. $company_scope = request('company_scope', '');
  183. $all_data['company_scope'] = $company_scope;
  184. $category_id = request('category_id', '');
  185. $all_data['category_id'] = $category_id;
  186. $specify_responsible_person = request('specify_responsible_person', '0');
  187. $all_data['specify_responsible_person'] = $specify_responsible_person;
  188. //查询是否存在
  189. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'], 'platform' => $all_data['platform']];
  190. $data = $ViolationProductModel->where($map)->where('id', '!=', $id)->first();
  191. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  192. // 更新数据表
  193. $where = ['id' => $id];
  194. $ViolationProduct = $ViolationProductModel->where($where)->first();
  195. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  196. $oldData = $ViolationProduct->toarray();
  197. $result = $ViolationProductModel->editViolationProduct_content($ViolationProduct, $all_data);
  198. // 如果操作失败
  199. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  200. // 记录行为
  201. $admin_id = request('access_token.uid', 0); //用户ID
  202. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  203. $table_name = $ViolationProductModel->getTable();
  204. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  205. $this->addAdminHistory('清洗配置-禁止商品管理', $admin_id, $is_admin, $table_name, $notes_type,$oldData, $all_data, '修改了禁止商品' . $oldData['product_name'] . '信息');
  206. // 告知结果
  207. return json_send(['code' => 'success', 'msg' => '修改成功']);
  208. }
  209. /**
  210. * 修改状态
  211. * @author 唐远望
  212. * @version 1.0
  213. * @date 2025-12-03
  214. *
  215. */
  216. public function set_status(Request $request, ViolationProductModel $ViolationProductModel,LowPriceGoodsModel $LowPriceGoodsModel)
  217. {
  218. // 验证参数
  219. $request->scene('set_status')->validate();
  220. // 接收数据
  221. $id = request('id', 0);
  222. $status = request('status', 0);
  223. if ($status == 0) {
  224. //获取禁止商品启动数量
  225. $violation_product_count = $ViolationProductModel->where('status', 0)->count();
  226. //获取低价挂网商品启用数量
  227. $lowprice_product_count = $LowPriceGoodsModel->where('status', 0)->count();
  228. //计算总数量
  229. $product_totle = $violation_product_count + $lowprice_product_count;
  230. //判断是否超过限制
  231. if ($product_totle >= 50) {
  232. return json_send(['code' => 'error', 'msg' => '启用数量超过限制,低价挂网和禁止商品不能超过50条']);
  233. }
  234. }
  235. // 查询用户
  236. $where = ['id' => $id];
  237. $ViolationProduct = $ViolationProductModel->where($where)->first();
  238. if (!$ViolationProduct) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  239. // 执行修改
  240. $result = $ViolationProductModel->changeStatus($ViolationProduct, $status);
  241. // 提示新增失败
  242. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  243. // 记录行为
  244. $admin_id = request('access_token.uid', 0); //用户ID
  245. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  246. $table_name = $ViolationProductModel->getTable();
  247. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  248. $this->addAdminHistory('清洗配置-禁止商品管理', $admin_id, $is_admin, $table_name, $notes_type,[],['status'=> $status], '修改了禁止商品' . $ViolationProduct->product_name . '状态');
  249. // 告知结果
  250. return json_send(['code' => 'success', 'msg' => '设置成功']);
  251. }
  252. /**
  253. * 删除
  254. * @author 唐远望
  255. * @version 1.0
  256. * @date 2025-12-03
  257. *
  258. */
  259. public function delete(Request $request, ViolationProductModel $ViolationProductModel)
  260. {
  261. // 验证参数
  262. $request->scene('delete')->validate();
  263. // 接收数据
  264. $id = request('id', 0);
  265. // 查询用户
  266. $where = ['id' => $id];
  267. // 执行删除
  268. $ViolationProduct = $ViolationProductModel->where($where)->first();
  269. if (!$ViolationProduct) {
  270. return false;
  271. }
  272. $ViolationProduct_log = $ViolationProduct->toArray();
  273. DB::beginTransaction();
  274. try {
  275. $ViolationProductCompanyModel = new ViolationProductCompanyModel();
  276. $company_id_log = $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->get();
  277. if (!empty($company_id_log)) {
  278. $ViolationProductCompanyModel->where('violation_product_logid', $ViolationProduct->id)->delete();
  279. }
  280. $ViolationProductModel->where($where)->delete();
  281. // 记录行为
  282. $admin_id = request('access_token.uid', 0); //用户ID
  283. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  284. $table_name = $ViolationProductModel->getTable();
  285. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  286. $this->addAdminHistory('清洗配置-禁止商品管理', $admin_id, $is_admin, $table_name, $notes_type,$ViolationProduct_log, [], '删除了禁止商品' . $ViolationProduct_log['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' => '删除失败','data' => $e->getMessage(),'k'=>$ViolationProduct_log]);
  295. }
  296. }
  297. }