CompanyCategory.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. $all_data['company_id'] = $company_id;
  141. } else {
  142. $map['company_id'] = $admin_company_id;
  143. $all_data['company_id'] = $admin_company_id;
  144. }
  145. $data = $CompanyCategoryModel->where($map)->first();
  146. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  147. // 写入数据表
  148. $result = $CompanyCategoryModel->addCompanyCategory($all_data);
  149. // 如果操作失败
  150. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  151. // 记录行为
  152. $admin_id = request('access_token.uid', 0); //用户ID
  153. $table_name = $CompanyCategoryModel->getTable();
  154. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  155. $this->addAdminHistory('清洗配置-公司分类管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了分类' . $all_data['name'] . '信息');
  156. // 告知结果
  157. return json_send(['code' => 'success', 'msg' => '新增成功']);
  158. }
  159. /**
  160. * 修改
  161. * @author 唐远望
  162. * @version 1.0
  163. * @date 2025-12-19
  164. *
  165. */
  166. public function edit(Request $request, CompanyCategoryModel $CompanyCategoryModel)
  167. {
  168. $request->scene('edit')->validate();
  169. $admin_company_id = request('admin_company_id', '0');
  170. $company_id = request('access_token.company_id', '0');
  171. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  172. // 接收参数
  173. $id = request('id', 0);
  174. // 接收数据
  175. $all_data = request()->all();
  176. $store_scope = request('store_scope', '');
  177. $all_data['store_scope'] = $store_scope;
  178. //查询是否存在
  179. $map = ['name' => $all_data['name']];
  180. if ($is_admin != 1 && $company_id != 0) {
  181. $map['company_id'] = $company_id;
  182. } else {
  183. $map['company_id'] = $admin_company_id;
  184. }
  185. $data = $CompanyCategoryModel->where($map)->where('id', '!=', $id)->first();
  186. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  187. // 更新数据表
  188. $where = ['id' => $id];
  189. if ($is_admin != 1 && $company_id != 0) {
  190. $where['company_id'] = $company_id;
  191. $all_data['company_id'] = $company_id;
  192. } else {
  193. $where['company_id'] = $admin_company_id;
  194. $all_data['company_id'] = $admin_company_id;
  195. }
  196. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  197. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  198. $oldData = $CompanyCategory->toArray();
  199. $result = $CompanyCategoryModel->editCompanyCategory_content($CompanyCategory, $all_data);
  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, $oldData, $all_data, '修改了分类' . $oldData['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 set_status(Request $request, CompanyCategoryModel $CompanyCategoryModel, ViolationStoreModel $ViolationStoreModel)
  218. {
  219. // 验证参数
  220. $request->scene('set_status')->validate();
  221. $admin_company_id = request('admin_company_id', '0');
  222. $company_id = request('access_token.company_id', '0');
  223. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  224. // 接收数据
  225. $id = request('id', 0);
  226. $status = request('status', 0);
  227. //如果是禁用,校验是否被使用
  228. if ($status == 1) {
  229. $violation_store_where = ['category_id' => $id];
  230. if ($is_admin != 1 && $company_id != 0) {
  231. $violation_store_where['company_id'] = $company_id;
  232. } else {
  233. $violation_store_where['company_id'] = $admin_company_id;
  234. }
  235. $violation_store_count = $ViolationStoreModel->where($violation_store_where)->count();
  236. if ($violation_store_count > 0) {
  237. return json_send(['code' => 'error', 'msg' => '该分类下存在公司配置,禁用失败']);
  238. }
  239. }
  240. // 查询用户
  241. $where = ['id' => $id];
  242. if ($is_admin != 1 && $company_id != 0) {
  243. $where['company_id'] = $company_id;
  244. } else {
  245. $where['company_id'] = $admin_company_id;
  246. }
  247. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  248. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  249. // 执行修改
  250. $result = $CompanyCategoryModel->changeStatus($CompanyCategory, $status);
  251. // 提示新增失败
  252. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  253. // 记录行为
  254. $admin_id = request('access_token.uid', 0); //用户ID
  255. $table_name = $CompanyCategoryModel->getTable();
  256. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  257. $this->addAdminHistory('清洗配置-公司分类管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了分类' . $CompanyCategory->name . '状态');
  258. // 告知结果
  259. return json_send(['code' => 'success', 'msg' => '设置成功']);
  260. }
  261. /**
  262. * 删除
  263. * @author 唐远望
  264. * @version 1.0
  265. * @date 2025-12-19
  266. *
  267. */
  268. public function delete(Request $request, CompanyCategoryModel $CompanyCategoryModel, ViolationStoreModel $ViolationStoreModel)
  269. {
  270. // 验证参数
  271. $request->scene('delete')->validate();
  272. $admin_company_id = request('admin_company_id', '0');
  273. $company_id = request('access_token.company_id', '0');
  274. $is_admin = request('access_token.is_admin', '0'); //是否管理员操作 0=是1=否
  275. // 接收数据
  276. $id = request('id', 0);
  277. $violation_store_where = ['category_id' => $id];
  278. if ($is_admin != 1 && $company_id != 0) {
  279. $violation_store_where['company_id'] = $company_id;
  280. } else {
  281. $violation_store_where['company_id'] = $admin_company_id;
  282. }
  283. $violation_store_count = $ViolationStoreModel->where($violation_store_where)->count();
  284. if ($violation_store_count > 0) {
  285. return json_send(['code' => 'error', 'msg' => '该分类下存在公司配置,删除失败']);
  286. }
  287. // 查询用户
  288. $where = ['id' => $id];
  289. // 执行删除
  290. if ($is_admin != 1 && $company_id != 0) {
  291. $where['company_id'] = $company_id;
  292. } else {
  293. $where['company_id'] = $admin_company_id;
  294. }
  295. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  296. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  297. $result = $CompanyCategory->delete();
  298. // 提示删除失败
  299. if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']);
  300. // 记录行为
  301. $admin_id = request('access_token.uid', 0); //用户ID
  302. $table_name = $CompanyCategoryModel->getTable();
  303. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  304. $this->addAdminHistory('清洗配置-公司分类管理', $company_id, $admin_id, $is_admin, $table_name, $notes_type, $CompanyCategory->toArray(), [], '删除了分类' . $CompanyCategory->name . '信息');
  305. // 告知结果
  306. return json_send(['code' => 'success', 'msg' => '删除成功']);
  307. }
  308. }