Company.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Http\Controllers\Manager\External;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Manager\External\Company as Request;
  5. use App\Models\Manager\External\Company as CompanyModel;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Carbon;
  8. /**
  9. * 外部-公司管理
  10. * @author 唐远望
  11. * @version 1.0
  12. * @date 2026-02-03
  13. */
  14. class Company extends Controller
  15. {
  16. /**
  17. * 列表
  18. * @author 唐远望
  19. * @version 1.0
  20. * @date 2026-02-03
  21. *
  22. */
  23. public function list(Request $request, CompanyModel $CompanyModel)
  24. {
  25. $request->scene('list')->validate();
  26. // 查询条件
  27. $map = [];
  28. $limit = request('limit', config('page_num', 10));
  29. $status = request('status', '');
  30. $start_time = request('start_time', '');
  31. $end_time = request('end_time', '');
  32. $company_name = request('company_name', '');
  33. $snapshot_status = request('snapshot_status', '');
  34. // 时间条件
  35. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  36. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  37. if (is_numeric($snapshot_status)) $map[] = ['snapshot_status', '=', $snapshot_status];
  38. // 其他条件
  39. if (is_numeric($status)) $map[] = ['status', '=', $status];
  40. if ($company_name) $map[] = ['company_name', 'like', "%$company_name%"];
  41. // 查询数据
  42. $result = $CompanyModel->query()
  43. ->where($map)
  44. ->orderByDesc('id')
  45. ->paginate($limit)->toarray();
  46. // 分配数据
  47. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  48. // 加载模板
  49. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  50. }
  51. /**
  52. * 全部
  53. * @author 唐远望
  54. * @version 1.0
  55. * @date 2026-02-03
  56. *
  57. */
  58. public function all(Request $request, CompanyModel $CompanyModel)
  59. {
  60. $request->scene('all')->validate();
  61. // 查询条件
  62. $map = [];
  63. $start_time = request('start_time', '');
  64. $end_time = request('end_time', '');
  65. $company_name = request('company_name', '');
  66. // 时间条件
  67. if ($start_time) $map[] = ['insert_time', '>=', strtotime($start_time)];
  68. if ($end_time) $map[] = ['insert_time', '<=', strtotime($end_time)];
  69. // 其他条件
  70. $map[] = ['status', '=', 0];
  71. if ($company_name) $map[] = ['company_name', 'like', "%$company_name%"];
  72. // 查询数据
  73. $result = $CompanyModel->query()
  74. ->where($map)
  75. ->orderByDesc('id')
  76. ->get();
  77. // 分配数据
  78. if (!$result) return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => []]);
  79. // 加载模板
  80. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $result]);
  81. }
  82. /**
  83. * 详情
  84. * @author 唐远望
  85. * @version 1.0
  86. * @date 2026-02-03
  87. */
  88. public function detail(Request $request, CompanyModel $CompanyModel)
  89. {
  90. $request->scene('detail')->validate();
  91. // 接收参数
  92. $id = request('id', 0);
  93. $map = ['id' => $id];
  94. // 权限判断
  95. $data = $CompanyModel->where($map)->first();
  96. if (!$data) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  97. // 加载模板
  98. return json_send(['code' => 'success', 'msg' => '获取成功', 'data' => $data]);
  99. }
  100. /**
  101. * 添加
  102. * @author 唐远望
  103. * @version 1.0
  104. * @date 2026-02-03
  105. *
  106. */
  107. public function add(Request $request, CompanyModel $CompanyModel)
  108. {
  109. $request->scene('add')->validate();
  110. $all_data = request()->all();
  111. $logo_url = request('logo_url', '');
  112. $snapshot_status = request('snapshot_status', 0);
  113. $all_data['logo_url'] = $logo_url ? $logo_url : '';
  114. $all_data['snapshot_status'] = $snapshot_status;
  115. //查询是否存在
  116. $map = ['social_credit_code' => $all_data['social_credit_code']];
  117. $data = $CompanyModel->where($map)->first();
  118. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  119. // 写入数据表
  120. $result = $CompanyModel->addCompany($all_data);
  121. // 如果操作失败
  122. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  123. // 记录行为
  124. $admin_id = request('access_token.uid', 0); //用户ID
  125. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  126. $table_name = $CompanyModel->getTable();
  127. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  128. $this->addAdminHistory('外部-公司管理', 0, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了公司' . $all_data['company_name'] . '信息');
  129. // 告知结果
  130. return json_send(['code' => 'success', 'msg' => '新增成功']);
  131. }
  132. /**
  133. * 修改
  134. * @author 唐远望
  135. * @version 1.0
  136. * @date 2026-02-03
  137. *
  138. */
  139. public function edit(Request $request, CompanyModel $CompanyModel)
  140. {
  141. $request->scene('edit')->validate();
  142. // 接收参数
  143. $id = request('id', 0);
  144. // 接收数据
  145. $all_data = request()->all();
  146. $logo_url = request('logo_url', '');
  147. $snapshot_status = request('snapshot_status', 0);
  148. $all_data['logo_url'] = $logo_url ? $logo_url : '';
  149. $all_data['snapshot_status'] = $snapshot_status;
  150. //查询是否存在
  151. $map = ['social_credit_code' => $all_data['social_credit_code']];
  152. $data = $CompanyModel->where($map)->where('id', '!=', $id)->first();
  153. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  154. // 更新数据表
  155. $where = ['id' => $id];
  156. $Company = $CompanyModel->where($where)->first();
  157. if (!$Company) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  158. $oldData = $Company->toarray();
  159. $result = $CompanyModel->editCompany_content($Company, $all_data);
  160. // 如果操作失败
  161. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  162. // 记录行为
  163. $admin_id = request('access_token.uid', 0); //用户ID
  164. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  165. $table_name = $CompanyModel->getTable();
  166. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  167. $this->addAdminHistory('外部-公司管理', 0, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了公司' . $oldData['company_name'] . '信息');
  168. // 告知结果
  169. return json_send(['code' => 'success', 'msg' => '修改成功']);
  170. }
  171. /**
  172. * 修改状态
  173. * @author 唐远望
  174. * @version 1.0
  175. * @date 2026-02-03
  176. *
  177. */
  178. public function set_status(Request $request, CompanyModel $CompanyModel)
  179. {
  180. // 验证参数
  181. $request->scene('set_status')->validate();
  182. $is_admin = request('access_token.is_admin', '0');
  183. // 接收数据
  184. $id = request('id', 0);
  185. $status = request('status', 0);
  186. $where = ['id' => $id];
  187. $Company = $CompanyModel->where($where)->first();
  188. if (!$Company) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  189. // 执行修改
  190. $result = $CompanyModel->changeStatus($Company, $status);
  191. // 提示新增失败
  192. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  193. // 记录行为
  194. $admin_id = request('access_token.uid', 0); //用户ID
  195. $table_name = $CompanyModel->getTable();
  196. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  197. $this->addAdminHistory('外部-公司管理', 0, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了公司' . $Company->company_name . '状态');
  198. // 告知结果
  199. return json_send(['code' => 'success', 'msg' => '设置成功']);
  200. }
  201. /**
  202. * 删除
  203. * @author 唐远望
  204. * @version 1.0
  205. * @date 2026-02-03
  206. *
  207. */
  208. public function delete(Request $request, CompanyModel $CompanyModel)
  209. {
  210. // 验证参数
  211. $request->scene('delete')->validate();
  212. // 接收数据
  213. $id = request('id', 0);
  214. // 查询用户
  215. $where = ['id' => $id];
  216. // 执行删除
  217. $Company = $CompanyModel->where($where)->first();
  218. if (!$Company) {
  219. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  220. }
  221. DB::beginTransaction();
  222. try {
  223. $Company->delete();
  224. // 记录行为
  225. $admin_id = request('access_token.uid', 0); //用户ID
  226. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  227. $table_name = $CompanyModel->getTable();
  228. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  229. $this->addAdminHistory('外部-公司管理', 0, $admin_id, $is_admin, $table_name, $notes_type, $Company->toarray(), [], '删除了公司' . $Company->company_name . '信息');
  230. // 告知结果
  231. DB::commit();
  232. return json_send(['code' => 'success', 'msg' => '删除成功']);
  233. // 成功处理...
  234. } catch (\Exception $e) {
  235. DB::rollBack();
  236. // 错误处理...
  237. return json_send(['code' => 'error', 'msg' => '删除失败']);
  238. }
  239. }
  240. }