CompanyCategory.php 11 KB

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