ApiService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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'])->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. $counts = array_count_values($data);// 1. 统计每个值的出现次数
  135. arsort($counts);// 2. 按出现次数降序排序
  136. $depart = array_keys($counts);// 3. 只保留键(去重),并按频率排序
  137. $depart_arr = [];
  138. foreach ($depart as $key => $val) {
  139. $depart_arr[$key] = [
  140. 'name' => $val
  141. ];
  142. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  143. ->where('xjdw', $val)
  144. ->where('lrsj', 'like', '%' . $date . '%')
  145. ->groupBy('lrr')->select(
  146. [
  147. 'lrr'
  148. ]
  149. )->get();
  150. if(count($query) > 0){
  151. for($i=0;$i<count($query);$i++){
  152. $lrr = explode('|',$query[$i]->lrr);
  153. $query[$i]->person_name = $lrr[0];
  154. $query[$i]->person_id = $lrr[1];
  155. }
  156. }
  157. $query = json_decode($query);
  158. $depart_arr[$key]['person_list'] = $query;
  159. }
  160. $result['data'] = $depart_arr;
  161. return $result;
  162. }
  163. //网格化人员轨迹
  164. public static function getPersonTravel($person_id, $depart, $date = '')
  165. {
  166. $result['status'] = true;
  167. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  168. $result['data'] = [];
  169. if (!$date) {
  170. $date = date('Y-m-d', time());
  171. }
  172. $res = [];
  173. $area_arr = [];
  174. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  175. ->where('lrr', $person_id)
  176. ->where('lrsj', 'like', '%' . $date . '%')
  177. ->where('xjdw', $depart)
  178. ->orderBy('create_time', 'asc')->get();
  179. $query = json_decode($query);
  180. $result['list2'] = [];
  181. if ($query) {
  182. foreach ($query as $key => $val) {
  183. // dd($val);
  184. if (!in_array($val->xjqy, $area_arr)) {
  185. $res[] = [
  186. 'area' => $val->xjqy,
  187. 'date' => $val->lrsj,
  188. 'data_id' => $val->ID,
  189. 'url' => 'http://anstatic.nxmy.com:8011/assets/html/xixuan/pages/detail.html?id='.$val->ID,
  190. ];
  191. $area_arr[] = $val->xjqy;
  192. }
  193. $result['list2'][$key]['area'] = $val->xjqy;
  194. $result['list2'][$key]['time'] = $val->CREATE_TIME;
  195. $result['list2'][$key]['data_id'] = $val->ID;
  196. }
  197. $area_list = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')
  198. ->whereNotIn('qymc', $area_arr)
  199. ->where('szxmc', $depart)->orderBy('areaid', 'asc')->get();
  200. foreach ($area_list as $area) {
  201. $res[] = [
  202. 'area' => $area->qymc,
  203. 'date' => '',
  204. 'data_id' => '',
  205. 'url' => '',
  206. ];
  207. }
  208. $result['list1'] = $res;
  209. }
  210. return $result;
  211. }
  212. //网格化巡检记录查询条件
  213. public static function getResultConition()
  214. {
  215. $result['status'] = true;
  216. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  217. $result['data'] = [];
  218. // $area_list = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->pluck('szxmc')->toArray();
  219. // $area_list = array_unique($area_list);
  220. // $area_list = array_values($area_list);
  221. $area_list = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->pluck('szxmc')->toArray();
  222. $area_list = array_unique($area_list);
  223. $area_list = array_values($area_list);
  224. $data = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where(DB::raw('QUARTER(lrsj)'), DB::raw('QUARTER(CURDATE())'))->pluck('xjdw')->toArray();
  225. $counts = array_count_values($data);// 1. 统计每个值的出现次数
  226. arsort($counts);// 2. 按出现次数降序排序
  227. $area_list = array_keys($counts);// 3. 只保留键(去重),并按频率排序
  228. $result['data'] = $area_list;
  229. return $result;
  230. }
  231. //网格化巡检记录查询
  232. public static function getResultRecord($depart = 'all', $date_type = 'week')
  233. {
  234. $result['status'] = true;
  235. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  236. $result['data'] = [];
  237. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl');
  238. if ($depart != 'all') {
  239. $query = $query->where('xjdw', $depart);
  240. }
  241. if ($date_type == 'month') {
  242. $query = $query->where(DB::raw('MONTH(lrsj)'), DB::raw('MONTH(CURDATE())'));
  243. } else if ($date_type == 'quarter') {
  244. $query = $query->where(DB::raw('QUARTER(lrsj)'), DB::raw('QUARTER(CURDATE())'));
  245. } else {
  246. $query = $query->where(DB::raw('WEEK(lrsj)'), DB::raw('WEEK(CURDATE())'));
  247. }
  248. $query = $query->get();
  249. $people_list = [];
  250. if(count($query) > 0){
  251. for($i=0;$i<count($query);$i++){
  252. if(!in_array($query[$i]->lrr,$people_list)){
  253. $people_list[] = $query[$i]->lrr;
  254. }
  255. }
  256. }
  257. $list = [];
  258. $pie = [];
  259. if(count($people_list) > 0){
  260. for($i=0;$i<count($people_list);$i++){
  261. $area_list = [];
  262. for($j=0;$j<count($query);$j++){
  263. if($people_list[$i] == $query[$j]->lrr){
  264. $list[$i]['department'] = str_replace("洗选中心", "", $query[$j]->xjdw);
  265. $area_list[] = $query[$j]->xjqy;
  266. }
  267. }
  268. $unit_zd = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->where('szxmc',$list[$i]['department'])->where('IS_DELETE',0)->pluck('qymc');
  269. $minCount = PHP_INT_MAX;
  270. foreach ($unit_zd as $itemB) {
  271. // 如果数组A中不包含数组B中的某个项,则直接退出
  272. if (!in_array($itemB, $area_list)) {
  273. $minCount = 0;
  274. }
  275. // 计算该项在数组A中出现的次数
  276. $count = 0;
  277. foreach ($area_list as $itemA) {
  278. if ($itemA == $itemB) {
  279. $count++;
  280. }
  281. }
  282. // 更新最少的数量
  283. $minCount = min($minCount, $count);
  284. }
  285. $lrr = explode('|',$people_list[$i]);
  286. $list[$i]['lastname'] = $lrr[0];
  287. $list[$i]['person_id'] = $lrr[1];
  288. $list[$i]['count'] = $minCount;
  289. $pie[$i]['name'] = $lrr[0];
  290. $pie[$i]['value'] = $minCount;
  291. }
  292. }
  293. usort($list, function($a, $b) {
  294. return $b['count'] <=> $a['count']; // 降序
  295. });
  296. $result['data']['list'] = $list;
  297. $result['data']['pie'] = $pie;
  298. return $result;
  299. }
  300. //网格化巡检记录列表
  301. public static function getResultList($person_id, $depart = 'all', $date_type = 'week')
  302. {
  303. $result['status'] = true;
  304. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  305. $result['data'] = [];
  306. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where('lrr', 'like' ,'%'.$person_id.'%');
  307. if ($depart != 'all') {
  308. $query = $query->where('xjdw', $depart);
  309. }
  310. if ($date_type == 'month') {
  311. $query = $query->where(DB::raw('MONTH(lrsj)'), DB::raw('MONTH(CURDATE())'));
  312. } else if ($date_type == 'quarter') {
  313. $query = $query->where(DB::raw('QUARTER(lrsj)'), DB::raw('QUARTER(CURDATE())'));
  314. } else {
  315. $query = $query->where(DB::raw('WEEK(lrsj)'), DB::raw('WEEK(CURDATE())'));
  316. }
  317. $query = $query->select(
  318. [
  319. 'xjqy',
  320. DB::raw('DATE(lrsj) as lrdate'),
  321. 'lrr',
  322. 'xjdw',
  323. 'ID'
  324. ]
  325. )->get();
  326. $data = [];
  327. foreach ($query as $key => $val) {
  328. $lrr = explode('|',$val->lrr);
  329. $data[] = [
  330. 'lastname' => $lrr[0],
  331. 'person_id' => $val->lrr,
  332. 'depart' => $val->xjdw,
  333. 'date' => $val->lrdate,
  334. 'xjqy' => $val->xjqy,
  335. 'data_id' => $val->ID
  336. ];
  337. }
  338. $data_arr = array_column($data, 'date');
  339. array_multisort($data_arr, SORT_DESC, $data);
  340. $result['data']['list'] = $data;
  341. return $result;
  342. }
  343. //网格化巡检记录列表明细
  344. public static function getResultDlist($person_id, $depart, $date)
  345. {
  346. $result['status'] = true;
  347. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  348. $result['data'] = [];
  349. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  350. ->where('lrr', $person_id)
  351. ->where('xjdw', $depart)
  352. ->where('lrsj', 'like', $date . '%')
  353. ->orderBy('lrsj', 'desc')
  354. ->select([
  355. 'ID',
  356. 'lrr',
  357. 'xjqy',
  358. 'lrsj',
  359. ])->get();
  360. if(count($query) > 0){
  361. for($i=0;$i<count($query);$i++){
  362. $lrr = explode('|',$query[$i]->lrr);
  363. $query[$i]->lrr_name = $lrr[0];
  364. }
  365. }
  366. $query = json_decode($query);
  367. $result['data'] = $query;
  368. return $result;
  369. }
  370. //网格化巡检记录明细
  371. public static function getResultDetails($id)
  372. {
  373. $result['status'] = true;
  374. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  375. $result['data'] = [];
  376. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  377. ->where('id', $id)
  378. ->select(
  379. [
  380. 'lrr',
  381. 'dwmc',
  382. 'xjdw',
  383. 'xjqy',
  384. 'jclx',
  385. 'lrsj',
  386. 'jcwt',
  387. 'wtms',
  388. 'dkpz',
  389. 'zgzp',
  390. 'zgr',
  391. 'dkwz',
  392. 'CREATE_TIME'
  393. ]
  394. )->get();
  395. if(count($query) > 0){
  396. for($i=0;$i<count($query);$i++){
  397. $lrr = explode('|',$query[$i]->lrr);
  398. $query[$i]->lrr_name = $lrr[0];
  399. }
  400. }
  401. $query = json_decode($query);
  402. $result['data'] = $query;
  403. return $result;
  404. }
  405. //网格化巡检记录
  406. public static function getFinalList($start_date, $end_date, $depart = '', $person = '')
  407. {
  408. $result['status'] = true;
  409. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  410. $result['data'] = [];
  411. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl');
  412. if ($start_date == $end_date) {
  413. $query = $query->where('lrsj', 'like', $start_date . '%');
  414. } else {
  415. $query = $query->where('lrsj', '>=', $start_date . ' 00:00:00')->where('lrsj', '<=', $end_date . ' 23:59:59');
  416. }
  417. if ($depart) {
  418. $query = $query->where('xjdw', $depart);
  419. }
  420. if ($person) {
  421. // $person_id = DB::connection('mysql_fwview')->table('hrmresource')
  422. // ->where('lastname', 'like', '%' . $person . '%')->pluck('id')->all();1
  423. $query = $query->where('lrr', 'like','%'.$person.'%');
  424. }
  425. $query = $query->orderBy('lrdate', 'desc')->groupBy('lrsj', 'xjdw', 'lrr','jcwt','id','zgr')
  426. ->select(
  427. [
  428. DB::raw('DATE(lrsj) as lrdate'),
  429. 'xjdw',
  430. 'lrr',
  431. 'jcwt',
  432. 'id',
  433. 'zgr'
  434. ]
  435. )
  436. ->get();
  437. $data = [];
  438. foreach ($query as $key => $val) {
  439. $lastname = explode('|',$val->lrr)[0];
  440. if($val->jcwt == '有'){
  441. $is_complete = 0;
  442. }else{
  443. $is_complete = 1;
  444. }
  445. $zgr = explode('|',$val->zgr);
  446. $zgr_id = '';
  447. if(count($zgr) > 1){
  448. $zgr_id = $zgr[1];
  449. }
  450. $data[] = [
  451. 'depart' => $val->xjdw,
  452. 'lastname' => $lastname,
  453. 'person_id' => $val->lrr,
  454. 'date' => $val->lrdate,
  455. 'is_complete' => $is_complete,
  456. 'id' => $val->id,
  457. 'zgr_id' => $zgr_id
  458. ];
  459. }
  460. $result['data'] = $data;
  461. return $result;
  462. }
  463. }