ApiService.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qiuzijian
  5. * Date: 6/22/24
  6. * Time: 12:34 PM
  7. */
  8. namespace Modules\Admin\Services;
  9. use App\Enum\ApiEnum;
  10. use Illuminate\Support\Facades\Cache;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use Modules\Admin\Entities\Menu;
  14. class ApiService{
  15. public function __construct()
  16. {
  17. }
  18. //网格化整改
  19. public static function editConition($params)
  20. {
  21. $result['status'] = true;
  22. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  23. $result['data'] = [];
  24. $update = [
  25. 'zgzp' => $params['zgpz'],
  26. 'jcwt' => '无'
  27. ];
  28. DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where('id',$params['id'])->update($update);
  29. DB::connection('mysql_fwe10')->table('uf_zhxx_qlctzgg')->where('gglx_obj',$params['id'])->update(['IS_DELETE'=>1]);
  30. return $result;
  31. }
  32. //网格化录入
  33. public static function addConition($params)
  34. {
  35. $result['status'] = true;
  36. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  37. $result['data'] = [];
  38. //区域字典录入
  39. $area = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->where('szxmc',$params['xjdw'])->where('IS_DELETE',0)->orderBy('areaid','desc')->get();
  40. $is_insert = true;
  41. if(count($area) > 0){
  42. for($i=0;$i<count($area);$i++){
  43. if($params['jcqy'] == $area[$i]->qymc){
  44. $is_insert = false;
  45. }
  46. }
  47. }
  48. if($is_insert){
  49. $data['ID'] = mt_rand(pow(10, 18), pow(10, 18) + 999999999);
  50. $data['FORM_DATA_ID'] = $data['ID'];
  51. $data['DATA_INDEX'] = 0.0;
  52. $data['CREATE_TIME'] = date('Y-m-d H:i:s');
  53. $data['TENANT_KEY'] = 't1zz9w8165';
  54. $data['IS_DELETE'] = 0;
  55. $data['DELETE_TYPE'] = 0;
  56. $data['FT_STATUS'] = 0;
  57. if(count($area) > 0){
  58. preg_match_all('/[0-9]+/', $area[0]->areaid, $matches);
  59. if(isset($matches[0][0])){
  60. $data['areaid'] = $params['xjdw'].($matches[0][0] + 1);
  61. }
  62. }else{
  63. $data['areaid'] = $params['xjdw'].'1';
  64. }
  65. $data['qymc'] = $params['jcqy'];
  66. $data['szxmc'] = $params['xjdw'];
  67. DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->insert($data);
  68. }
  69. //巡检记录录入
  70. $id = mt_rand(pow(10, 18), pow(10, 18) + 999999999);
  71. $insert = [
  72. 'ID' => $id,
  73. 'FORM_DATA_ID' => $id,
  74. 'DATA_INDEX' => 0.0,
  75. 'CREATE_TIME' => date('Y-m-d H:i:s'),
  76. 'TENANT_KEY' => 't1zz9w8165',
  77. 'IS_DELETE' => 0,
  78. 'DELETE_TYPE' => 0,
  79. 'FT_STATUS' => 0,
  80. 'xjqy' => $params['jcqy'],
  81. 'dkpz' => $params['dkpz'],
  82. 'lrr' => $params['lrmc'].'|'.$params['lrr'],
  83. 'jclx' => $params['jclx'],
  84. 'lrsj' => $params['lrsj'],
  85. 'dwmc' => $params['dwmc'],
  86. 'jcwt' => $params['jcwt'],
  87. 'xjdw' => $params['xjdw'],
  88. 'wtms' => $params['wtms'],
  89. 'sjr' => $params['sjr'],
  90. 'lat' => $params['lat'],
  91. 'lng' => $params['lng'],
  92. 'dkwz' => $params['dkwz']
  93. ];
  94. if($params['jcwt'] == '有'){
  95. $insert['zgr'] = $params['zgr'];
  96. }
  97. DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->insert($insert);
  98. //发通知
  99. if($params['jcwt'] == '有'){
  100. $tzgg_first = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->orderBy('CREATE_TIME','desc')->limit(1)->get();
  101. $id = mt_rand(pow(10, 18), pow(10, 18) + 999999999);
  102. $zgr_name = explode('|',$params['zgr'])[0];
  103. $tzgg = [
  104. 'ID' => $id,
  105. 'FORM_DATA_ID' => $id,
  106. 'DATA_INDEX' => 0.0,
  107. 'CREATE_TIME' => date('Y-m-d H:i:s'),
  108. 'TENANT_KEY' => 't1zz9w8165',
  109. 'IS_DELETE' => 0,
  110. 'DELETE_TYPE' => 0,
  111. 'FT_STATUS' => 0,
  112. 'gglx' => 3,
  113. 'gglx_obj' => $tzgg_first[0]->ID,
  114. 'ggrq' => date('Y-m-d H:i:s'),
  115. 'ggnr' => $zgr_name.',你有一条巡检记录需要整改,请及时'
  116. ];
  117. DB::connection('mysql_fwe10')->table('uf_zhxx_qlctzgg')->insert($tzgg);
  118. }
  119. return $result;
  120. }
  121. //网格化人员轨迹查询条件
  122. public static function getQueryConitions($date = '')
  123. {
  124. $result['status'] = true;
  125. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  126. $result['data'] = [];
  127. if (!$date) {
  128. $date = date('Y-m-d', time());
  129. }
  130. // $depart = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->pluck('szxmc')->toArray();
  131. // $depart = array_unique($depart);
  132. // $depart = array_values($depart);
  133. // $data = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where(DB::raw('QUARTER(lrsj)'), DB::raw('QUARTER(CURDATE())'))->pluck('xjdw')->toArray();
  134. $data = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where('lrsj', '>=', DB::raw("DATE_SUB(NOW(), INTERVAL 6 MONTH)"))->pluck('xjdw')->toArray();
  135. $counts = array_count_values($data);// 1. 统计每个值的出现次数
  136. arsort($counts);// 2. 按出现次数降序排序
  137. $depart = array_keys($counts);// 3. 只保留键(去重),并按频率排序
  138. $depart_arr = [];
  139. foreach ($depart as $key => $val) {
  140. $depart_arr[$key] = [
  141. 'name' => $val
  142. ];
  143. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  144. ->where('xjdw', $val)
  145. ->where('lrsj', 'like', '%' . $date . '%')
  146. ->groupBy('lrr')->select(
  147. [
  148. 'lrr'
  149. ]
  150. )->get();
  151. if(count($query) > 0){
  152. for($i=0;$i<count($query);$i++){
  153. $lrr = explode('|',$query[$i]->lrr);
  154. $query[$i]->person_name = $lrr[0];
  155. $query[$i]->person_id = $lrr[1];
  156. }
  157. }
  158. $query = json_decode($query);
  159. $depart_arr[$key]['person_list'] = $query;
  160. }
  161. $result['data'] = $depart_arr;
  162. return $result;
  163. }
  164. //网格化人员轨迹
  165. public static function getPersonTravel($person_id, $depart, $date = '')
  166. {
  167. $result['status'] = true;
  168. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  169. $result['data'] = [];
  170. if (!$date) {
  171. $date = date('Y-m-d', time());
  172. }
  173. $res = [];
  174. $area_arr = [];
  175. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  176. ->where('lrr', $person_id)
  177. ->where('lrsj', 'like', '%' . $date . '%')
  178. ->where('xjdw', $depart)
  179. ->orderBy('create_time', 'asc')->get();
  180. $query = json_decode($query);
  181. $result['list2'] = [];
  182. if ($query) {
  183. foreach ($query as $key => $val) {
  184. // dd($val);
  185. if (!in_array($val->xjqy, $area_arr)) {
  186. $res[] = [
  187. 'area' => $val->xjqy,
  188. 'date' => $val->lrsj,
  189. 'data_id' => $val->ID,
  190. 'url' => 'http://anstatic.nxmy.com:8011/assets/html/xixuan/pages/detail.html?id='.$val->ID,
  191. ];
  192. $area_arr[] = $val->xjqy;
  193. }
  194. $result['list2'][$key]['area'] = $val->xjqy;
  195. $result['list2'][$key]['time'] = $val->CREATE_TIME;
  196. $result['list2'][$key]['data_id'] = $val->ID;
  197. }
  198. $area_list = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')
  199. ->whereNotIn('qymc', $area_arr)
  200. ->where('szxmc', $depart)->where('IS_DELETE',0)->orderBy('areaid', 'asc')->get();
  201. foreach ($area_list as $area) {
  202. $res[] = [
  203. 'area' => $area->qymc,
  204. 'date' => '',
  205. 'data_id' => '',
  206. 'url' => '',
  207. ];
  208. }
  209. $result['list1'] = $res;
  210. }
  211. return $result;
  212. }
  213. //网格化巡检记录查询条件
  214. public static function getResultConition()
  215. {
  216. $result['status'] = true;
  217. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  218. $result['data'] = [];
  219. // $area_list = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->pluck('szxmc')->toArray();
  220. // $area_list = array_unique($area_list);
  221. // $area_list = array_values($area_list);
  222. $area_list = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->pluck('szxmc')->toArray();
  223. $area_list = array_unique($area_list);
  224. $area_list = array_values($area_list);
  225. // $data = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where(DB::raw('QUARTER(lrsj)'), DB::raw('QUARTER(CURDATE())'))->pluck('xjdw')->toArray();
  226. $data = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where('lrsj', '>=', DB::raw("DATE_SUB(NOW(), INTERVAL 6 MONTH)"))->pluck('xjdw')->toArray();
  227. $counts = array_count_values($data);// 1. 统计每个值的出现次数
  228. arsort($counts);// 2. 按出现次数降序排序
  229. $area_list = array_keys($counts);// 3. 只保留键(去重),并按频率排序
  230. $result['data'] = $area_list;
  231. return $result;
  232. }
  233. //网格化巡检记录查询
  234. public static function getResultRecord($depart = 'all', $date_type = 'week')
  235. {
  236. $result['status'] = true;
  237. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  238. $result['data'] = [];
  239. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl');
  240. if ($depart != 'all') {
  241. $query = $query->where('xjdw', $depart);
  242. }
  243. if ($date_type == 'month') {
  244. $query = $query->where(DB::raw('MONTH(lrsj)'), DB::raw('MONTH(CURDATE())'));
  245. } else if ($date_type == 'quarter') {
  246. $query = $query->where(DB::raw('QUARTER(lrsj)'), DB::raw('QUARTER(CURDATE())'));
  247. } else {
  248. $query = $query->where(DB::raw('WEEK(lrsj)'), DB::raw('WEEK(CURDATE())'));
  249. }
  250. $query = $query->get();
  251. $people_list = [];
  252. if(count($query) > 0){
  253. for($i=0;$i<count($query);$i++){
  254. if(!in_array($query[$i]->lrr,$people_list)){
  255. $people_list[] = $query[$i]->lrr;
  256. }
  257. }
  258. }
  259. $list = [];
  260. $pie = [];
  261. if(count($people_list) > 0){
  262. for($i=0;$i<count($people_list);$i++){
  263. $area_list = [];
  264. for($j=0;$j<count($query);$j++){
  265. if($people_list[$i] == $query[$j]->lrr){
  266. $list[$i]['department'] = str_replace("洗选中心", "", $query[$j]->xjdw);
  267. $area_list[] = $query[$j]->xjqy;
  268. }
  269. }
  270. $unit_zd = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->where('szxmc',$list[$i]['department'])->where('IS_DELETE',0)->pluck('qymc');
  271. $minCount = PHP_INT_MAX;
  272. foreach ($unit_zd as $itemB) {
  273. // 如果数组A中不包含数组B中的某个项,则直接退出
  274. if (!in_array($itemB, $area_list)) {
  275. $minCount = 0;
  276. }
  277. // 计算该项在数组A中出现的次数
  278. $count = 0;
  279. foreach ($area_list as $itemA) {
  280. if ($itemA == $itemB) {
  281. $count++;
  282. }
  283. }
  284. // 更新最少的数量
  285. $minCount = min($minCount, $count);
  286. }
  287. $lrr = explode('|',$people_list[$i]);
  288. $list[$i]['lastname'] = $lrr[0];
  289. $list[$i]['person_id'] = $lrr[1];
  290. $list[$i]['count'] = $minCount;
  291. $pie[$i]['name'] = $lrr[0];
  292. $pie[$i]['value'] = $minCount;
  293. }
  294. }
  295. usort($list, function($a, $b) {
  296. return $b['count'] <=> $a['count']; // 降序
  297. });
  298. $result['data']['list'] = $list;
  299. $result['data']['pie'] = $pie;
  300. return $result;
  301. }
  302. //全流程巡检记录排名
  303. public static function getRecordSort($depart,$start_date,$end_date)
  304. {
  305. $result['status'] = true;
  306. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  307. $result['data'] = [];
  308. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl');
  309. if ($depart != 'all') {
  310. $query = $query->where('xjdw', $depart);
  311. }
  312. $query = $query->whereBetween('lrsj',[$start_date,$end_date]);
  313. $query = $query->get();
  314. $people_list = [];
  315. if(count($query) > 0){
  316. for($i=0;$i<count($query);$i++){
  317. if(!in_array($query[$i]->lrr,$people_list)){
  318. $people_list[] = $query[$i]->lrr;
  319. }
  320. }
  321. }
  322. $list = [];
  323. $pie = [];
  324. if(count($people_list) > 0){
  325. for($i=0;$i<count($people_list);$i++){
  326. $area_list = [];
  327. for($j=0;$j<count($query);$j++){
  328. if($people_list[$i] == $query[$j]->lrr){
  329. $list[$i]['department'] = str_replace("洗选中心", "", $query[$j]->xjdw);
  330. $area_list[] = $query[$j]->xjqy;
  331. }
  332. }
  333. $unit_zd = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->where('szxmc',$list[$i]['department'])->where('IS_DELETE',0)->pluck('qymc');
  334. $minCount = PHP_INT_MAX;
  335. foreach ($unit_zd as $itemB) {
  336. // 如果数组A中不包含数组B中的某个项,则直接退出
  337. if (!in_array($itemB, $area_list)) {
  338. $minCount = 0;
  339. }
  340. // 计算该项在数组A中出现的次数
  341. $count = 0;
  342. foreach ($area_list as $itemA) {
  343. if ($itemA == $itemB) {
  344. $count++;
  345. }
  346. }
  347. // 更新最少的数量
  348. $minCount = min($minCount, $count);
  349. }
  350. $lrr = explode('|',$people_list[$i]);
  351. $list[$i]['lastname'] = $lrr[0];
  352. $list[$i]['person_id'] = $lrr[1];
  353. $list[$i]['count'] = $minCount;
  354. $pie[$i]['name'] = $lrr[0];
  355. $pie[$i]['value'] = $minCount;
  356. }
  357. }
  358. usort($list, function($a, $b) {
  359. return $b['count'] <=> $a['count']; // 降序
  360. });
  361. $result['data']['list'] = $list;
  362. $result['data']['pie'] = $pie;
  363. return $result;
  364. }
  365. //网格化巡检记录列表
  366. public static function getResultList($person_id, $depart = 'all', $date_type = 'week')
  367. {
  368. $result['status'] = true;
  369. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  370. $result['data'] = [];
  371. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where('lrr', 'like' ,'%'.$person_id.'%');
  372. if ($depart != 'all') {
  373. $query = $query->where('xjdw', $depart);
  374. }
  375. if ($date_type == 'month') {
  376. $query = $query->where(DB::raw('MONTH(lrsj)'), DB::raw('MONTH(CURDATE())'));
  377. } else if ($date_type == 'quarter') {
  378. $query = $query->where(DB::raw('QUARTER(lrsj)'), DB::raw('QUARTER(CURDATE())'));
  379. } else {
  380. $query = $query->where(DB::raw('WEEK(lrsj)'), DB::raw('WEEK(CURDATE())'));
  381. }
  382. $query = $query->select(
  383. [
  384. 'xjqy',
  385. DB::raw('DATE(lrsj) as lrdate'),
  386. 'lrr',
  387. 'xjdw',
  388. 'ID'
  389. ]
  390. )->get();
  391. $data = [];
  392. foreach ($query as $key => $val) {
  393. $lrr = explode('|',$val->lrr);
  394. $data[] = [
  395. 'lastname' => $lrr[0],
  396. 'person_id' => $val->lrr,
  397. 'depart' => $val->xjdw,
  398. 'date' => $val->lrdate,
  399. 'xjqy' => $val->xjqy,
  400. 'data_id' => $val->ID
  401. ];
  402. }
  403. $data_arr = array_column($data, 'date');
  404. array_multisort($data_arr, SORT_DESC, $data);
  405. $result['data']['list'] = $data;
  406. return $result;
  407. }
  408. //网格化巡检记录列表明细
  409. public static function getResultDlist($person_id, $depart, $date)
  410. {
  411. $result['status'] = true;
  412. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  413. $result['data'] = [];
  414. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  415. ->where('lrr', $person_id)
  416. ->where('xjdw', $depart)
  417. ->where('lrsj', 'like', $date . '%')
  418. ->orderBy('lrsj', 'desc')
  419. ->select([
  420. 'ID',
  421. 'lrr',
  422. 'xjqy',
  423. 'lrsj',
  424. ])->get();
  425. if(count($query) > 0){
  426. for($i=0;$i<count($query);$i++){
  427. $lrr = explode('|',$query[$i]->lrr);
  428. $query[$i]->lrr_name = $lrr[0];
  429. }
  430. }
  431. $query = json_decode($query);
  432. $result['data'] = $query;
  433. return $result;
  434. }
  435. //网格化巡检记录明细
  436. public static function getResultDetails($id)
  437. {
  438. $result['status'] = true;
  439. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  440. $result['data'] = [];
  441. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  442. ->where('id', $id)
  443. ->select(
  444. [
  445. 'lrr',
  446. 'dwmc',
  447. 'xjdw',
  448. 'xjqy',
  449. 'jclx',
  450. 'lrsj',
  451. 'jcwt',
  452. 'wtms',
  453. 'dkpz',
  454. 'zgzp',
  455. 'zgr',
  456. 'dkwz',
  457. 'CREATE_TIME'
  458. ]
  459. )->get();
  460. if(count($query) > 0){
  461. for($i=0;$i<count($query);$i++){
  462. $lrr = explode('|',$query[$i]->lrr);
  463. $query[$i]->lrr_name = $lrr[0];
  464. }
  465. }
  466. $query = json_decode($query);
  467. $result['data'] = $query;
  468. return $result;
  469. }
  470. //网格化巡检记录
  471. public static function getFinalList($start_date, $end_date, $depart = '', $person = '')
  472. {
  473. $result['status'] = true;
  474. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  475. $result['data'] = [];
  476. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl');
  477. if ($start_date == $end_date) {
  478. $query = $query->where('lrsj', 'like', $start_date . '%');
  479. } else {
  480. $query = $query->where('lrsj', '>=', $start_date . ' 00:00:00')->where('lrsj', '<=', $end_date . ' 23:59:59');
  481. }
  482. if ($depart) {
  483. $query = $query->where('xjdw', $depart);
  484. }
  485. if ($person) {
  486. // $person_id = DB::connection('mysql_fwview')->table('hrmresource')
  487. // ->where('lastname', 'like', '%' . $person . '%')->pluck('id')->all();1
  488. $query = $query->where('lrr', 'like','%'.$person.'%');
  489. }
  490. $query = $query->orderBy('lrdate', 'desc')->groupBy('lrsj', 'xjdw', 'lrr','jcwt','id','zgr')
  491. ->select(
  492. [
  493. DB::raw('DATE(lrsj) as lrdate'),
  494. 'xjdw',
  495. 'lrr',
  496. 'jcwt',
  497. 'id',
  498. 'zgr'
  499. ]
  500. )
  501. ->get();
  502. $data = [];
  503. foreach ($query as $key => $val) {
  504. $lastname = explode('|',$val->lrr)[0];
  505. if($val->jcwt == '有'){
  506. $is_complete = 0;
  507. }else{
  508. $is_complete = 1;
  509. }
  510. $zgr = explode('|',$val->zgr);
  511. $zgr_id = '';
  512. if(count($zgr) > 1){
  513. $zgr_id = $zgr[1];
  514. }
  515. $data[] = [
  516. 'depart' => $val->xjdw,
  517. 'lastname' => $lastname,
  518. 'person_id' => $val->lrr,
  519. 'date' => $val->lrdate,
  520. 'is_complete' => $is_complete,
  521. 'id' => $val->id,
  522. 'zgr_id' => $zgr_id
  523. ];
  524. }
  525. $result['data'] = $data;
  526. return $result;
  527. }
  528. }