Company.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. $is_deduplication = request('is_deduplication', 0);
  114. $all_data['logo_url'] = $logo_url ? $logo_url : '';
  115. $all_data['snapshot_status'] = $snapshot_status;
  116. $all_data['is_deduplication'] = $is_deduplication;
  117. //查询是否存在
  118. $map = ['social_credit_code' => $all_data['social_credit_code']];
  119. $data = $CompanyModel->where($map)->first();
  120. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  121. // 写入数据表
  122. $result = $CompanyModel->addCompany($all_data);
  123. // 如果操作失败
  124. if (!$result) return json_send(['code' => 'error', 'msg' => '新增失败']);
  125. // 记录行为
  126. $admin_id = request('access_token.uid', 0); //用户ID
  127. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  128. $table_name = $CompanyModel->getTable();
  129. $notes_type = 1; //操作类型,1添加,2修改,3=删除
  130. $this->addAdminHistory('外部-公司管理', 0, $admin_id, $is_admin, $table_name, $notes_type, [], $all_data, '新增了公司' . $all_data['company_name'] . '信息');
  131. // 告知结果
  132. return json_send(['code' => 'success', 'msg' => '新增成功']);
  133. }
  134. /**
  135. * 修改
  136. * @author 唐远望
  137. * @version 1.0
  138. * @date 2026-02-03
  139. *
  140. */
  141. public function edit(Request $request, CompanyModel $CompanyModel)
  142. {
  143. $request->scene('edit')->validate();
  144. // 接收参数
  145. $id = request('id', 0);
  146. // 接收数据
  147. $all_data = request()->all();
  148. $logo_url = request('logo_url', '');
  149. $snapshot_status = request('snapshot_status', 0);
  150. $is_deduplication = request('is_deduplication', 0);
  151. $all_data['logo_url'] = $logo_url ? $logo_url : '';
  152. $all_data['snapshot_status'] = $snapshot_status;
  153. $all_data['is_deduplication'] = $is_deduplication;
  154. //查询是否存在
  155. $map = ['social_credit_code' => $all_data['social_credit_code']];
  156. $data = $CompanyModel->where($map)->where('id', '!=', $id)->first();
  157. if ($data) return json_send(['code' => 'error', 'msg' => '记录已存在']);
  158. // 更新数据表
  159. $where = ['id' => $id];
  160. $Company = $CompanyModel->where($where)->first();
  161. if (!$Company) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  162. $oldData = $Company->toarray();
  163. $result = $CompanyModel->editCompany_content($Company, $all_data);
  164. // 如果操作失败
  165. if (!$result) return json_send(['code' => 'error', 'msg' => '修改失败']);
  166. // 记录行为
  167. $admin_id = request('access_token.uid', 0); //用户ID
  168. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  169. $table_name = $CompanyModel->getTable();
  170. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  171. $this->addAdminHistory('外部-公司管理', 0, $admin_id, $is_admin, $table_name, $notes_type, $oldData, $all_data, '修改了公司' . $oldData['company_name'] . '信息');
  172. // 告知结果
  173. return json_send(['code' => 'success', 'msg' => '修改成功']);
  174. }
  175. /**
  176. * 修改状态
  177. * @author 唐远望
  178. * @version 1.0
  179. * @date 2026-02-03
  180. *
  181. */
  182. public function set_status(Request $request, CompanyModel $CompanyModel)
  183. {
  184. // 验证参数
  185. $request->scene('set_status')->validate();
  186. $is_admin = request('access_token.is_admin', '0');
  187. // 接收数据
  188. $id = request('id', 0);
  189. $status = request('status', 0);
  190. $where = ['id' => $id];
  191. $Company = $CompanyModel->where($where)->first();
  192. if (!$Company) return json_send(['code' => 'error', 'msg' => '记录不存在']);
  193. // 执行修改
  194. $result = $CompanyModel->changeStatus($Company, $status);
  195. // 提示新增失败
  196. if (!$result) return json_send(['code' => 'error', 'msg' => '设置失败']);
  197. // 记录行为
  198. $admin_id = request('access_token.uid', 0); //用户ID
  199. $table_name = $CompanyModel->getTable();
  200. $notes_type = 2; //操作类型,1添加,2修改,3=删除
  201. $this->addAdminHistory('外部-公司管理', 0, $admin_id, $is_admin, $table_name, $notes_type, [], ['status' => $status], '修改了公司' . $Company->company_name . '状态');
  202. // 告知结果
  203. return json_send(['code' => 'success', 'msg' => '设置成功']);
  204. }
  205. /**
  206. * 删除
  207. * @author 唐远望
  208. * @version 1.0
  209. * @date 2026-02-03
  210. *
  211. */
  212. public function delete(Request $request, CompanyModel $CompanyModel)
  213. {
  214. // 验证参数
  215. $request->scene('delete')->validate();
  216. // 接收数据
  217. $id = request('id', 0);
  218. // 查询用户
  219. $where = ['id' => $id];
  220. // 执行删除
  221. $Company = $CompanyModel->where($where)->first();
  222. if (!$Company) {
  223. return json_send(['code' => 'error', 'msg' => '删除失败,记录不存在']);
  224. }
  225. DB::beginTransaction();
  226. try {
  227. $Company->delete();
  228. // 记录行为
  229. $admin_id = request('access_token.uid', 0); //用户ID
  230. $is_admin = request('access_token.is_admin'); //是否管理员操作 0=是1=否
  231. $table_name = $CompanyModel->getTable();
  232. $notes_type = 3; //操作类型,1添加,2修改,3=删除
  233. $this->addAdminHistory('外部-公司管理', 0, $admin_id, $is_admin, $table_name, $notes_type, $Company->toarray(), [], '删除了公司' . $Company->company_name . '信息');
  234. // 告知结果
  235. DB::commit();
  236. return json_send(['code' => 'success', 'msg' => '删除成功']);
  237. // 成功处理...
  238. } catch (\Exception $e) {
  239. DB::rollBack();
  240. // 错误处理...
  241. return json_send(['code' => 'error', 'msg' => '删除失败']);
  242. }
  243. }
  244. }