CompanyCategory.php 13 KB

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