ApiService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. ];
  91. if($params['jcwt'] == '有'){
  92. $insert['zgr'] = $params['zgr'];
  93. }
  94. DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->insert($insert);
  95. //发通知
  96. if($params['jcwt'] == '有'){
  97. $tzgg_first = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')->orderBy('CREATE_TIME','desc')->limit(1)->get();
  98. $id = mt_rand(pow(10, 18), pow(10, 18) + 999999999);
  99. $zgr_name = explode('|',$params['zgr'])[0];
  100. $tzgg = [
  101. 'ID' => $id,
  102. 'FORM_DATA_ID' => $id,
  103. 'DATA_INDEX' => 0.0,
  104. 'CREATE_TIME' => date('Y-m-d H:i:s'),
  105. 'TENANT_KEY' => 't1zz9w8165',
  106. 'IS_DELETE' => 0,
  107. 'DELETE_TYPE' => 0,
  108. 'FT_STATUS' => 0,
  109. 'gglx' => 3,
  110. 'gglx_obj' => $tzgg_first[0]->ID,
  111. 'ggrq' => date('Y-m-d H:i:s'),
  112. 'ggnr' => $zgr_name.',你有一条巡检记录需要整改,请及时'
  113. ];
  114. DB::connection('mysql_fwe10')->table('uf_zhxx_qlctzgg')->insert($tzgg);
  115. }
  116. return $result;
  117. }
  118. //网格化人员轨迹查询条件
  119. public static function getQueryConitions($date = '')
  120. {
  121. $result['status'] = true;
  122. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  123. $result['data'] = [];
  124. if (!$date) {
  125. $date = date('Y-m-d', time());
  126. }
  127. $depart = DB::connection('mysql_fwe10_eteams')->table('department')
  128. ->where('parent', '100001980000000049')
  129. ->where('name', 'like', '%' . '洗煤厂')
  130. ->where('id','!=','100001590000001602')
  131. ->get();
  132. $depart_arr = [];
  133. foreach ($depart as $key => $val) {
  134. $depart_name = str_replace('洗选中心', '', $val->fullname);
  135. $depart_arr[$key] = [
  136. 'name' => $depart_name,
  137. ];
  138. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  139. ->where('xjdw', $depart_name)
  140. ->where('lrsj', 'like', '%' . $date . '%')
  141. ->groupBy('lrr')->select(
  142. [
  143. 'lrr'
  144. ]
  145. )->get();
  146. if(count($query) > 0){
  147. for($i=0;$i<count($query);$i++){
  148. $lrr = explode('|',$query[$i]->lrr);
  149. $query[$i]->person_name = $lrr[0];
  150. $query[$i]->person_id = $lrr[1];
  151. }
  152. }
  153. $query = json_decode($query);
  154. $depart_arr[$key]['person_list'] = $query;
  155. }
  156. $result['data'] = $depart_arr;
  157. return $result;
  158. }
  159. //网格化人员轨迹
  160. public static function getPersonTravel($person_id, $depart, $date = '')
  161. {
  162. $result['status'] = true;
  163. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  164. $result['data'] = [];
  165. if (!$date) {
  166. $date = date('Y-m-d', time());
  167. }
  168. $res = [];
  169. $area_arr = [];
  170. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl')
  171. ->where('lrr', $person_id)
  172. ->where('lrsj', 'like', '%' . $date . '%')
  173. ->where('xjdw', $depart)
  174. ->orderBy('lrsj', 'asc')->get();
  175. $query = json_decode($query);
  176. if ($query) {
  177. foreach ($query as $key => $val) {
  178. // dd($val);
  179. $area_arr[] = $val->xjqy;
  180. $res[] = [
  181. 'area' => $val->xjqy,
  182. 'date' => $val->lrsj,
  183. 'data_id' => $val->ID,
  184. 'url' => 'http://anstatic.nxmy.com:8011/assets/html/xixuan/pages/detail.html?id='.$val->ID,
  185. ];
  186. }
  187. $area_list = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')
  188. ->whereNotIn('qymc', $area_arr)
  189. ->where('szxmc', $depart)->orderBy('areaid', 'asc')->get();
  190. foreach ($area_list as $area) {
  191. $res[] = [
  192. 'area' => $area->qymc,
  193. 'date' => '',
  194. 'data_id' => '',
  195. 'url' => '',
  196. ];
  197. }
  198. $result['data'] = $res;
  199. }
  200. return $result;
  201. }
  202. //网格化巡检记录查询条件
  203. public static function getResultConition()
  204. {
  205. // $result['status'] = true;
  206. // $result['msg'] = ApiEnum::RETURN_SUCCESS;
  207. // $result['data'] = [];
  208. //
  209. // $depart = DB::connection('mysql_fwe10_eteams')->table('department')
  210. // ->where('parent', '100001980000000049')
  211. // ->where('name', 'like', '%' . '洗煤厂')
  212. // ->where('id','!=','100001590000001602')
  213. // ->get();
  214. //
  215. // $depart_arr = [];
  216. // foreach ($depart as $key => $val) {
  217. // $depart_name = str_replace('洗选中心', '', $val->fullname);
  218. // $depart_arr[$key] = $depart_name;
  219. // }
  220. //
  221. // $result['data'] = $depart_arr;
  222. //
  223. // return $result;
  224. $result['status'] = true;
  225. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  226. $result['data'] = [];
  227. $area_list = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcqyzd')->pluck('szxmc')->toArray();
  228. $area_list = array_unique($area_list);
  229. $area_list = array_values($area_list);
  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. ]
  326. )->get();
  327. $data = [];
  328. foreach ($query as $key => $val) {
  329. $lrr = explode('|',$val->lrr);
  330. $data[] = [
  331. 'lastname' => $lrr[0],
  332. 'person_id' => $val->lrr,
  333. 'depart' => $val->xjdw,
  334. 'date' => $val->lrdate,
  335. 'xjqy' => $val->xjqy
  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. ]
  392. )->get();
  393. if(count($query) > 0){
  394. for($i=0;$i<count($query);$i++){
  395. $lrr = explode('|',$query[$i]->lrr);
  396. $query[$i]->lrr_name = $lrr[0];
  397. }
  398. }
  399. $query = json_decode($query);
  400. $result['data'] = $query;
  401. return $result;
  402. }
  403. //网格化巡检记录
  404. public static function getFinalList($start_date, $end_date, $depart = '', $person = '')
  405. {
  406. $result['status'] = true;
  407. $result['msg'] = ApiEnum::RETURN_SUCCESS;
  408. $result['data'] = [];
  409. $query = DB::connection('mysql_fwe10')->table('uf_zhxx_qlcxjjl');
  410. if ($start_date == $end_date) {
  411. $query = $query->where('lrsj', 'like', $start_date . '%');
  412. } else {
  413. $query = $query->where('lrsj', '>=', $start_date . ' 00:00:00')->where('lrsj', '<=', $end_date . ' 23:59:59');
  414. }
  415. if ($depart) {
  416. $query = $query->where('xjdw', $depart);
  417. }
  418. if ($person) {
  419. // $person_id = DB::connection('mysql_fwview')->table('hrmresource')
  420. // ->where('lastname', 'like', '%' . $person . '%')->pluck('id')->all();1
  421. $query = $query->where('lrr', 'like','%'.$person.'%');
  422. }
  423. $query = $query->orderBy('lrdate', 'desc')->groupBy('lrsj', 'xjdw', 'lrr','jcwt','id','zgr')
  424. ->select(
  425. [
  426. DB::raw('DATE(lrsj) as lrdate'),
  427. 'xjdw',
  428. 'lrr',
  429. 'jcwt',
  430. 'id',
  431. 'zgr'
  432. ]
  433. )
  434. ->get();
  435. $data = [];
  436. foreach ($query as $key => $val) {
  437. $lastname = explode('|',$val->lrr)[0];
  438. if($val->jcwt == '有'){
  439. $is_complete = 0;
  440. }else{
  441. $is_complete = 1;
  442. }
  443. $zgr = explode('|',$val->zgr);
  444. $zgr_id = '';
  445. if(count($zgr) > 1){
  446. $zgr_id = $zgr[1];
  447. }
  448. $data[] = [
  449. 'depart' => $val->xjdw,
  450. 'lastname' => $lastname,
  451. 'person_id' => $val->lrr,
  452. 'date' => $val->lrdate,
  453. 'is_complete' => $is_complete,
  454. 'id' => $val->id,
  455. 'zgr_id' => $zgr_id
  456. ];
  457. }
  458. $result['data'] = $data;
  459. return $result;
  460. }
  461. }