CompanyCategory.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. $table_name = $CompanyCategoryModel->getTable();
  120. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  121. $this->addAdminHistory('清洗配置-公司分类管理', $admin_id, $table_name, $notes_type,[],$all_data, '新增了分类' . $all_data['name'] . '信息');
  122. // 告知结果
  123. return json_send(['code' => 'success', 'msg' => '新增成功']);
  124. }
  125. /**
  126. * 修改
  127. * @author 唐远望
  128. * @version 1.0
  129. * @date 2025-12-19
  130. *
  131. */
  132. public function edit(Request $request, CompanyCategoryModel $CompanyCategoryModel)
  133. {
  134. $request->scene('edit')->validate();
  135. // 接收参数
  136. $id = request('id', 0);
  137. // 接收数据
  138. $all_data = request()->all();
  139. $store_scope = request('store_scope', '');
  140. $all_data['store_scope'] = $store_scope;
  141. //查询是否存在
  142. $map = ['name' => $all_data['name']];
  143. $data = $CompanyCategoryModel->where($map)->where('id', '!=', $id)->first();
  144. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  145. // 更新数据表
  146. $where = ['id' => $id];
  147. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  148. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  149. $result = $CompanyCategoryModel->editCompanyCategory_content($CompanyCategory, $all_data);
  150. // 如果操作失败
  151. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  152. // 记录行为
  153. $admin_id = request('access_token.uid', 0); //用户ID
  154. $table_name = $CompanyCategoryModel->getTable();
  155. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  156. $this->addAdminHistory('清洗配置-公司分类管理', $admin_id, $table_name, $notes_type,$CompanyCategory->toarray(),$all_data, '修改了分类' .$CompanyCategory->name . '信息');
  157. // 告知结果
  158. return json_send(['code' => 'success', 'msg' => '修改成功']);
  159. }
  160. /**
  161. * 修改状态
  162. * @author 唐远望
  163. * @version 1.0
  164. * @date 2025-12-19
  165. *
  166. */
  167. public function set_status(Request $request, CompanyCategoryModel $CompanyCategoryModel,ViolationStoreModel $ViolationStoreModel)
  168. {
  169. // 验证参数
  170. $request->scene('set_status')->validate();
  171. // 接收数据
  172. $id = request('id', 0);
  173. $status = request('status', 0);
  174. //如果是禁用,校验是否被使用
  175. if ($status == 1) {
  176. $violation_store_count = $ViolationStoreModel->where(['category_id' => $id])->count();
  177. if ($violation_store_count > 0) {
  178. return json_send(['code' => 'error', 'msg' => '该分类下存在公司配置,禁用失败']);
  179. }
  180. }
  181. // 查询用户
  182. $where = ['id' => $id];
  183. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  184. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  185. // 执行修改
  186. $result = $CompanyCategoryModel->changeStatus($CompanyCategory, $status);
  187. // 提示新增失败
  188. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  189. // 记录行为
  190. $admin_id = request('access_token.uid', 0); //用户ID
  191. $table_name = $CompanyCategoryModel->getTable();
  192. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  193. $this->addAdminHistory('清洗配置-公司分类管理', $admin_id, $table_name, $notes_type,[], ['status' => $status], '修改了分类' .$CompanyCategory->name . '状态');
  194. // 告知结果
  195. return json_send(['code' => 'success', 'msg' => '设置成功']);
  196. }
  197. /**
  198. * 删除
  199. * @author 唐远望
  200. * @version 1.0
  201. * @date 2025-12-19
  202. *
  203. */
  204. public function delete(Request $request, CompanyCategoryModel $CompanyCategoryModel,ViolationStoreModel $ViolationStoreModel)
  205. {
  206. // 验证参数
  207. $request->scene('delete')->validate();
  208. // 接收数据
  209. $id = request('id', 0);
  210. $violation_store_count = $ViolationStoreModel->where(['category_id' => $id])->count();
  211. if ($violation_store_count > 0) {
  212. return json_send(['code' => 'error', 'msg' => '该分类下存在公司配置,删除失败']);
  213. }
  214. // 查询用户
  215. $where = ['id' => $id];
  216. // 执行删除
  217. $CompanyCategory = $CompanyCategoryModel->where($where)->first();
  218. if (!$CompanyCategory) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  219. $result = $CompanyCategory->delete();
  220. // 提示删除失败
  221. if (!$result) return json_send(['code' => 'error', 'msg' => '删除失败']);
  222. // 记录行为
  223. $admin_id = request('access_token.uid', 0); //用户ID
  224. $table_name = $CompanyCategoryModel->getTable();
  225. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  226. $this->addAdminHistory('清洗配置-公司分类管理', $admin_id, $table_name, $notes_type, $CompanyCategory->toArray(), [], '删除了分类' .$CompanyCategory->name . '信息');
  227. // 告知结果
  228. return json_send(['code' => 'success', 'msg' => '删除成功']);
  229. }
  230. }