CompanyCategory.php 9.7 KB

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