ViolationProduct.php 18 KB

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