ApiService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. $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)->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 getResultList($person_id, $depart = 'all', $date_type = 'week')
  304. {
  305. $result['status'] = true;
  306. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  307. $result['data'] = [];
  308. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->where('lrr', 'like' ,'%'.$person_id.'%');
  309. if ($depart != 'all') {
  310. $query = $query->where('xjdw', $depart);
  311. }
  312. if ($date_type == 'month') {
  313. $query = $query->where(DB::raw('MONTH(lrsj)'), DB::raw('MONTH(CURDATE())'));
  314. } else if ($date_type == 'quarter') {
  315. $query = $query->where(DB::raw('QUARTER(lrsj)'), DB::raw('QUARTER(CURDATE())'));
  316. } else {
  317. $query = $query->where(DB::raw('WEEK(lrsj)'), DB::raw('WEEK(CURDATE())'));
  318. }
  319. $query = $query->select(
  320. [
  321. 'xjqy',
  322. DB::raw('DATE(lrsj) as lrdate'),
  323. 'lrr',
  324. 'xjdw',
  325. 'ID'
  326. ]
  327. )->get();
  328. $data = [];
  329. foreach ($query as $key => $val) {
  330. $lrr = explode('|',$val->lrr);
  331. $data[] = [
  332. 'lastname' => $lrr[0],
  333. 'person_id' => $val->lrr,
  334. 'depart' => $val->xjdw,
  335. 'date' => $val->lrdate,
  336. 'xjqy' => $val->xjqy,
  337. 'data_id' => $val->ID
  338. ];
  339. }
  340. $data_arr = array_column($data, 'date');
  341. array_multisort($data_arr, SORT_DESC, $data);
  342. $result['data']['list'] = $data;
  343. return $result;
  344. }
  345. //网格化巡检记录列表明细
  346. public static function getResultDlist($person_id, $depart, $date)
  347. {
  348. $result['status'] = true;
  349. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  350. $result['data'] = [];
  351. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  352. ->where('lrr', $person_id)
  353. ->where('xjdw', $depart)
  354. ->where('lrsj', 'like', $date . '%')
  355. ->orderBy('lrsj', 'desc')
  356. ->select([
  357. 'ID',
  358. 'lrr',
  359. 'xjqy',
  360. 'lrsj',
  361. ])->get();
  362. if(count($query) > 0){
  363. for($i=0;$i<count($query);$i++){
  364. $lrr = explode('|',$query[$i]->lrr);
  365. $query[$i]->lrr_name = $lrr[0];
  366. }
  367. }
  368. $query = json_decode($query);
  369. $result['data'] = $query;
  370. return $result;
  371. }
  372. //网格化巡检记录明细
  373. public static function getResultDetails($id)
  374. {
  375. $result['status'] = true;
  376. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  377. $result['data'] = [];
  378. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  379. ->where('id', $id)
  380. ->select(
  381. [
  382. 'lrr',
  383. 'dwmc',
  384. 'xjdw',
  385. 'xjqy',
  386. 'jclx',
  387. 'lrsj',
  388. 'jcwt',
  389. 'wtms',
  390. 'dkpz',
  391. 'zgzp',
  392. 'zgr',
  393. 'dkwz',
  394. 'CREATE_TIME'
  395. ]
  396. )->get();
  397. if(count($query) > 0){
  398. for($i=0;$i<count($query);$i++){
  399. $lrr = explode('|',$query[$i]->lrr);
  400. $query[$i]->lrr_name = $lrr[0];
  401. }
  402. }
  403. $query = json_decode($query);
  404. $result['data'] = $query;
  405. return $result;
  406. }
  407. //网格化巡检记录
  408. public static function getFinalList($start_date, $end_date, $depart = '', $person = '')
  409. {
  410. $result['status'] = true;
  411. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  412. $result['data'] = [];
  413. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl');
  414. if ($start_date == $end_date) {
  415. $query = $query->where('lrsj', 'like', $start_date . '%');
  416. } else {
  417. $query = $query->where('lrsj', '>=', $start_date . ' 00:00:00')->where('lrsj', '<=', $end_date . ' 23:59:59');
  418. }
  419. if ($depart) {
  420. $query = $query->where('xjdw', $depart);
  421. }
  422. if ($person) {
  423. // $person_id = DB::connection('mysql_fwview')->table('hrmresource')
  424. // ->where('lastname', 'like', '%' . $person . '%')->pluck('id')->all();1
  425. $query = $query->where('lrr', 'like','%'.$person.'%');
  426. }
  427. $query = $query->orderBy('lrdate', 'desc')->groupBy('lrsj', 'xjdw', 'lrr','jcwt','id','zgr')
  428. ->select(
  429. [
  430. DB::raw('DATE(lrsj) as lrdate'),
  431. 'xjdw',
  432. 'lrr',
  433. 'jcwt',
  434. 'id',
  435. 'zgr'
  436. ]
  437. )
  438. ->get();
  439. $data = [];
  440. foreach ($query as $key => $val) {
  441. $lastname = explode('|',$val->lrr)[0];
  442. if($val->jcwt == '有'){
  443. $is_complete = 0;
  444. }else{
  445. $is_complete = 1;
  446. }
  447. $zgr = explode('|',$val->zgr);
  448. $zgr_id = '';
  449. if(count($zgr) > 1){
  450. $zgr_id = $zgr[1];
  451. }
  452. $data[] = [
  453. 'depart' => $val->xjdw,
  454. 'lastname' => $lastname,
  455. 'person_id' => $val->lrr,
  456. 'date' => $val->lrdate,
  457. 'is_complete' => $is_complete,
  458. 'id' => $val->id,
  459. 'zgr_id' => $zgr_id
  460. ];
  461. }
  462. $result['data'] = $data;
  463. return $result;
  464. }
  465. }