CompanyCategory.php 10 KB

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