CompanyCategory.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace App\Http\Controllers\manager\washConfig;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\WashConfig\CompanyCategory as Request;
  5. use App\Models\Manager\WashConfig\CompanyCategory as CompanyCategoryModel;
  6. use App\Models\Manager\WashConfig\ViolationStore as ViolationStoreModel;
  7. /**
  8. * 清洗配置-公司分类
  9. * @author 唐远望
  10. * @version 1.0
  11. * @date 2025-12-19
  12. */
  13. class CompanyCategory extends Controller
  14. {
  15. /**
  16. * 列表
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2025-12-19
  20. *
  21. */
  22. public function list(Request $request, CompanyCategoryModel $CompanyCategoryModel)
  23. {
  24. $request->scene('list')->validate();
  25. $company_id = request('access_token.company_id', '0');
  26. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  27. // 查询条件
  28. $map = [];
  29. $limit = request('limit', config('page_num', 10));
  30. $status = request('status', '');
  31. $start_time = request('start_time', '');
  32. $end_time = request('end_time', '');
  33. $name = request('name', '');
  34. // 时间条件
  35. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  36. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  37. // 其他条件
  38. if (is_numeric($status)) $map[] = ['status', '=', $status];
  39. if ($name) $map[] = ['name', 'like', "%$name%"];
  40. // 查询数据
  41. if ($is_admin != 1 && $company_id != 0) $map[] = ['company_id', '=', $company_id];
  42. $result = $CompanyCategoryModel->query()
  43. ->where($map)
  44. ->orderByDesc('id')
  45. ->paginate($limit);
  46. // 分配数据
  47. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  48. // 加载模板
  49. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  50. }
  51. /**
  52. * 所有分类
  53. * @author 唐远望
  54. * @version 1.0
  55. * @date 2025-12-08
  56. *
  57. */
  58. public function all(CompanyCategoryModel $CompanyCategoryModel)
  59. {
  60. $map = [];
  61. $company_id = request('access_token.company_id', '0');
  62. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  63. $status = request('status', '0');
  64. $start_time = request('start_time', '');
  65. $end_time = request('end_time', '');
  66. $name = request('name', '');
  67. // 时间条件
  68. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  69. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  70. // 其他条件
  71. if ($name) $map[] = ['name', 'like', "%$name%"];
  72. if (is_numeric($status)) $map[] = ['status', '=', $status];
  73. if ($is_admin != 1 && $company_id != 0) $map[] = ['company_id', '=', $company_id];
  74. $result = $CompanyCategoryModel->query()
  75. ->where($map)
  76. ->select(['id', 'name'])
  77. ->orderByDesc('id')
  78. ->get();
  79. // 分配数据
  80. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  81. // 加载模板
  82. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  83. }
  84. /**
  85. * 详情
  86. * @author 唐远望
  87. * @version 1.0
  88. * @date 2025-12-19
  89. */
  90. public function detail(Request $request, CompanyCategoryModel $CompanyCategoryModel)
  91. {
  92. $request->scene('detail')->validate();
  93. $company_id = request('access_token.company_id', '0');
  94. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  95. // 接收参数
  96. $id = request('id', 0);
  97. $map = ['id' => $id];
  98. if ($is_admin != 1 && $company_id != 0) $map['company_id'] = $company_id;
  99. $data = $CompanyCategoryModel->where($map)->first();
  100. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  101. // 加载模板
  102. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  103. }
  104. /**
  105. * 添加
  106. * @author 唐远望
  107. * @version 1.0
  108. * @date 2025-12-19
  109. *
  110. */
  111. public function add(Request $request, CompanyCategoryModel $CompanyCategoryModel)
  112. {
  113. $request->scene('add')->validate();
  114. $company_id = request('access_token.company_id', '0');
  115. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  116. // 接收数据
  117. $all_data = request()->all();
  118. $store_scope = request('store_scope', '');
  119. $all_data['store_scope'] = $store_scope;
  120. //查询是否存在
  121. $map = ['name' => $all_data['name']];
  122. if ($is_admin != 1 && $company_id != 0) $map['company_id'] = $company_id;
  123. $data = $CompanyCategoryModel->where($map)->first();
  124. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  125. // 写入数据表
  126. $all_data['company_id'] = $company_id;
  127. $result = $CompanyCategoryModel->addCompanyCategory($all_data);
  128. // 如果操作失败
  129. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  130. // 记录行为
  131. $admin_id = request('access_token.uid', 0); //用户ID
  132. $table_name = $CompanyCategoryModel->getTable();
  133. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  134. $this->addAdminHistory('清洗配置-公司分类管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type,[],$all_data, '新增了分类' . $all_data['name'] . '信息');
  135. // 告知结果
  136. return json_send(['code' => 'success', 'msg' => '新增成功']);
  137. }
  138. /**
  139. * 修改
  140. * @author 唐远望
  141. * @version 1.0
  142. * @date 2025-12-19
  143. *
  144. */
  145. public function edit(Request $request, CompanyCategoryModel $CompanyCategoryModel)
  146. {
  147. $request->scene('edit')->validate();
  148. $company_id = request('access_token.company_id', '0');
  149. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  150. // 接收参数
  151. $id = request('id', 0);
  152. // 接收数据
  153. $all_data = request()->all();
  154. $store_scope = request('store_scope', '');
  155. $all_data['store_scope'] = $store_scope;
  156. //查询是否存在
  157. $map = ['name' => $all_data['name']];
  158. if ($is_admin != 1 && $company_id != 0) $map['company_id'] = $company_id;
  159. $data = $CompanyCategoryModel->where($map)->where('id', '!=', $id)->first();
  160. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  161. // 更新数据表
  162. $where = ['id' => $id];
  163. if ($is_admin != 1 && $company_id != 0) $where['company_id'] = $company_id;
  164. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  165. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  166. $oldData = $CompanyCategory->toArray();
  167. $result = $CompanyCategoryModel->editCompanyCategory_content($CompanyCategory, $all_data);
  168. // 如果操作失败
  169. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  170. // 记录行为
  171. $admin_id = request('access_token.uid', 0); //用户ID
  172. $table_name = $CompanyCategoryModel->getTable();
  173. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  174. $this->addAdminHistory('清洗配置-公司分类管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type,$oldData,$all_data, '修改了分类' .$oldData['name'] . '信息');
  175. // 告知结果
  176. return json_send(['code' => 'success', 'msg' => '修改成功']);
  177. }
  178. /**
  179. * 修改状态
  180. * @author 唐远望
  181. * @version 1.0
  182. * @date 2025-12-19
  183. *
  184. */
  185. public function set_status(Request $request, CompanyCategoryModel $CompanyCategoryModel,ViolationStoreModel $ViolationStoreModel)
  186. {
  187. // 验证参数
  188. $request->scene('set_status')->validate();
  189. $company_id = request('access_token.company_id', '0');
  190. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  191. // 接收数据
  192. $id = request('id', 0);
  193. $status = request('status', 0);
  194. //如果是禁用,校验是否被使用
  195. if ($status == 1) {
  196. $violation_store_where = ['category_id' => $id];
  197. if ($is_admin != 1 && $company_id != 0) $violation_store_where['company_id'] = $company_id;
  198. $violation_store_count = $ViolationStoreModel->where($violation_store_where)->count();
  199. if ($violation_store_count > 0) {
  200. return json_send(['code' => 'error', 'msg' => '该分类下存在公司配置,禁用失败']);
  201. }
  202. }
  203. // 查询用户
  204. $where = ['id' => $id];
  205. if ($is_admin != 1 && $company_id != 0) $where['company_id'] = $company_id;
  206. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  207. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  208. // 执行修改
  209. $result = $CompanyCategoryModel->changeStatus($CompanyCategory, $status);
  210. // 提示新增失败
  211. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  212. // 记录行为
  213. $admin_id = request('access_token.uid', 0); //用户ID
  214. $table_name = $CompanyCategoryModel->getTable();
  215. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  216. $this->addAdminHistory('清洗配置-公司分类管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type,[], ['status' => $status], '修改了分类' .$CompanyCategory->name . '状态');
  217. // 告知结果
  218. return json_send(['code' => 'success', 'msg' => '设置成功']);
  219. }
  220. /**
  221. * 删除
  222. * @author 唐远望
  223. * @version 1.0
  224. * @date 2025-12-19
  225. *
  226. */
  227. public function delete(Request $request, CompanyCategoryModel $CompanyCategoryModel,ViolationStoreModel $ViolationStoreModel)
  228. {
  229. // 验证参数
  230. $request->scene('delete')->validate();
  231. $company_id = request('access_token.company_id', '0');
  232. $is_admin = request('access_token.is_admin', '0');//是否管理员操作 0=是1=否
  233. // 接收数据
  234. $id = request('id', 0);
  235. $violation_store_where = ['category_id' => $id];
  236. if ($is_admin != 1 && $company_id != 0) $violation_store_where['company_id'] = $company_id;
  237. $violation_store_count = $ViolationStoreModel->where($violation_store_where)->count();
  238. if ($violation_store_count > 0) {
  239. return json_send(['code' => 'error', 'msg' => '该分类下存在公司配置,删除失败']);
  240. }
  241. // 查询用户
  242. $where = ['id' => $id];
  243. // 执行删除
  244. if ($is_admin != 1 && $company_id != 0) $where['company_id'] = $company_id;
  245. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  246. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  247. $result = $CompanyCategory->delete();
  248. // 提示删除失败
  249. if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']);
  250. // 记录行为
  251. $admin_id = request('access_token.uid', 0); //用户ID
  252. $table_name = $CompanyCategoryModel->getTable();
  253. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  254. $this->addAdminHistory('清洗配置-公司分类管理',$company_id, $admin_id, $is_admin, $table_name, $notes_type, $CompanyCategory->toArray(), [], '删除了分类' .$CompanyCategory->name . '信息');
  255. // 告知结果
  256. return json_send(['code' => 'success', 'msg' => '删除成功']);
  257. }
  258. }