ViolationProduct.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /**
  9. * 数据清洗-违规产品配置
  10. * @author 唐远望
  11. * @version 1.0
  12. * @date 2025-12-03
  13. */
  14. class ViolationProduct extends Controller
  15. {
  16. /**
  17. * 列表
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2025-12-03
  21. *
  22. */
  23. public function list(Request $request, ViolationProductModel $ViolationProductModel,ViolationStoreModel $ViolationStoreModel,ViolationProductCompanyModel $ViolationProductCompanyModel)
  24. {
  25. $request->scene('list')->validate();
  26. // 查询条件
  27. $map = [];
  28. $limit = request('limit', config('page_num', 10));
  29. $status = request('status', '');
  30. $start_time = request('start_time', '');
  31. $end_time = request('end_time', '');
  32. $product_name = request('product_name', '');
  33. $platform = request('platform', '');
  34. $store_scope = request('store_scope', '');
  35. $company_scope = request('company_scope', '');
  36. // 时间条件
  37. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  38. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  39. // 其他条件
  40. if (is_numeric($status)) $map[] = ['status', '=', $status];
  41. if ($product_name) $map[] = ['product_name', 'like', "%$product_name%"];
  42. if ($platform) $map[] = ['platform', 'like', "%$platform%"];
  43. if ($store_scope) $map[] = ['store_scope', '=',$store_scope];
  44. if ($company_scope) $map[] = ['company_scope', '=',$company_scope];
  45. // 查询数据
  46. $result = $ViolationProductModel->query()
  47. ->where($map)
  48. ->orderByDesc('id')
  49. ->paginate($limit)->toarray();
  50. // 分配数据
  51. if (!$result) return json_send(['code' => 'error', 'msg' => '暂无数据']);
  52. if(isset($result['data']) && count($result['data']) > 0){
  53. foreach ($result['data'] as $key => $value) {
  54. $result['data'][$key]['platform'] = isset($value['platform']) ? explode(',',$value['platform']) : '';
  55. //查询店铺名称
  56. if(trim($value['store_scope']) == ''){
  57. $result['data'][$key]['store_name'] =['全部店铺'];
  58. }else{
  59. $result['data'][$key]['store_name'] = '';
  60. }
  61. //查询公司名称
  62. if($value['company_scope'] == '1'){
  63. $result['data'][$key]['company_name'] =['全部公司'];
  64. }else{
  65. $company_data = $ViolationProductCompanyModel->where('violation_product_logid',$value['id'])
  66. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_violation_product_company.company_id')
  67. ->select(['washconfig_violation_store.id','washconfig_violation_store.company_name','washconfig_violation_product_company.company_id'])->get()->toArray();
  68. $result['data'][$key]['company_name'] = !empty($company_data) ? array_column($company_data,'company_name') : '';
  69. }
  70. }
  71. }
  72. // 加载模板
  73. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  74. }
  75. /**
  76. * 详情
  77. * @author 唐远望
  78. * @version 1.0
  79. * @date 2025-12-03
  80. */
  81. public function detail(Request $request, ViolationProductModel $ViolationProductModel,ViolationStoreModel $ViolationStoreModel,ViolationProductCompanyModel $ViolationProductCompanyModel)
  82. {
  83. $request->scene('detail')->validate();
  84. // 接收参数
  85. $id = request('id', 0);
  86. $map = ['id' => $id];
  87. $data = $ViolationProductModel->where($map)->first();
  88. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  89. //查询店铺名称
  90. if(trim($data['store_scope']) == ''){
  91. $data['store_name'] = ['全部店铺'];
  92. }else{
  93. $data['store_name'] = '';
  94. }
  95. //查询公司名称
  96. if ($data->company_scope == '1') {
  97. $data->company_name = ['全部公司'];
  98. } else {
  99. $company_data = $ViolationProductCompanyModel->where('violation_product_logid', $data->id)
  100. ->join('washconfig_violation_store', 'washconfig_violation_store.id', '=', 'washconfig_violation_product_company.company_id')
  101. ->select(['washconfig_violation_store.id', 'washconfig_violation_store.company_name', 'washconfig_violation_product_company.company_id'])->get()->toArray();
  102. $data->company_name = !empty($company_data) ? array_column($company_data, 'company_name') : '';
  103. }
  104. $data->platform = isset($data->platform) ? explode(',',$data->platform) : '';
  105. // 加载模板
  106. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  107. }
  108. /**
  109. * 添加
  110. * @author 唐远望
  111. * @version 1.0
  112. * @date 2025-12-03
  113. *
  114. */
  115. public function add(Request $request, ViolationProductModel $ViolationProductModel)
  116. {
  117. $request->scene('add')->validate();
  118. // 接收数据
  119. $all_data = request()->all();
  120. $store_scope = request('store_scope', '');
  121. $all_data['store_scope'] = $store_scope;
  122. //查询是否存在
  123. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'],'platform'=> $all_data['platform']];
  124. $data = $ViolationProductModel->where($map)->first();
  125. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  126. // 写入数据表
  127. $result = $ViolationProductModel->addViolationProduct($all_data);
  128. // 如果操作失败
  129. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  130. // 告知结果
  131. return json_send(['code' => 'success', 'msg' => '新增成功']);
  132. }
  133. /**
  134. * 修改
  135. * @author 唐远望
  136. * @version 1.0
  137. * @date 2025-12-03
  138. *
  139. */
  140. public function edit(Request $request, ViolationProductModel $ViolationProductModel)
  141. {
  142. $request->scene('edit')->validate();
  143. // 接收参数
  144. $id = request('id', 0);
  145. // 接收数据
  146. $all_data = request()->all();
  147. $store_scope = request('store_scope','');
  148. $all_data['store_scope'] = $store_scope;
  149. //查询是否存在
  150. $map = ['product_name' => $all_data['product_name'], 'product_specs' => $all_data['product_specs'],'platform'=> $all_data['platform']];
  151. $data = $ViolationProductModel->where($map)->where('id', '!=', $id)->first();
  152. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  153. // 更新数据表
  154. $where = ['id' => $id];
  155. $result = $ViolationProductModel->updateViolationProduct($where, $all_data);
  156. // 如果操作失败
  157. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  158. // 告知结果
  159. return json_send(['code' => 'success', 'msg' => '修改成功']);
  160. }
  161. /**
  162. * 修改状态
  163. * @author 唐远望
  164. * @version 1.0
  165. * @date 2025-12-03
  166. *
  167. */
  168. public function set_status(Request $request, ViolationProductModel $ViolationProductModel)
  169. {
  170. // 验证参数
  171. $request->scene('set_status')->validate();
  172. // 接收数据
  173. $id = request('id', 0);
  174. $status = request('status', 0);
  175. // 查询用户
  176. $where = ['id' => $id];
  177. // 执行修改
  178. $result = $ViolationProductModel->changeStatus($where, $status);
  179. // 提示新增失败
  180. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  181. // 告知结果
  182. return json_send(['code' => 'success', 'msg' => '设置成功']);
  183. }
  184. /**
  185. * 删除
  186. * @author 唐远望
  187. * @version 1.0
  188. * @date 2025-12-03
  189. *
  190. */
  191. public function delete(Request $request, ViolationProductModel $ViolationProductModel)
  192. {
  193. // 验证参数
  194. $request->scene('delete')->validate();
  195. // 接收数据
  196. $id = request('id', 0);
  197. // 查询用户
  198. $where = ['id' => $id];
  199. // 执行删除
  200. $result = $ViolationProductModel->deleteViolationProduct($where);
  201. // 提示删除失败
  202. if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']);
  203. // 告知结果
  204. return json_send(['code' => 'success', 'msg' => '删除成功']);
  205. }
  206. }