Device.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppAuth;
  4. use think\Db;
  5. class Device extends Base {
  6. public function updates(){
  7. $data = request()->post();
  8. $data['org_id'] = cur_org_id();
  9. $validate = new \app\common\validate\Device();
  10. $result = $validate->check($data,[]);
  11. if(true !== $result){
  12. $this->error = $validate->getError();
  13. return false;
  14. }
  15. if(!$data['device_user']){
  16. $this->error = '维修人员不能为空';
  17. return false;
  18. }
  19. $data['imgs']=empty($data['imgs'])?"":implode(',',$data['imgs']);
  20. //修改操作
  21. if($data['id'] > 0){
  22. Db::startTrans();
  23. try {
  24. $data['update_time'] = getTime();
  25. $addDevice=$this::update($data,['id'=>$data['id']]);
  26. if(!$addDevice){
  27. exception('编辑失败');
  28. }
  29. $newDeviceUser=explode(',',$data['device_user']);
  30. $deviceUser=$this->findUser($data['id']);
  31. $oldDeviceUser = [];
  32. foreach ($deviceUser as $k=>$v){
  33. $oldDeviceUser[]=$v['user_id'];
  34. }
  35. $usernew = array_diff($newDeviceUser,$oldDeviceUser);
  36. $userdef = array_diff($oldDeviceUser,$newDeviceUser);
  37. if($userdef){
  38. $delDeviceUser=Db::name('device_user')->where('device_id',$data['id'])->whereIn('user_id',$userdef)->delete();
  39. if(!$delDeviceUser){
  40. exception('编辑维修人员失败');
  41. }
  42. }
  43. if($usernew){
  44. $buser = array();
  45. foreach ($usernew as $k=>$v){
  46. $buser[] = array(
  47. 'device_id' => $data['id'],
  48. 'user_id' => $v
  49. );
  50. }
  51. $saveDeviceUser=Db::name('device_user')->insertAll($buser);
  52. if(!$saveDeviceUser){
  53. exception('编辑维修人员失败');
  54. }
  55. }
  56. Db::commit();
  57. return true;
  58. } catch (\Exception $e) {
  59. // 回滚事务
  60. $this->error = $e->getMessage();
  61. Db::rollback();
  62. return false;
  63. }
  64. }else{
  65. //添加操作
  66. Db::startTrans();
  67. try {
  68. unset($data['id']);
  69. $data['create_time'] = getTime();
  70. $addres=$this->save($data);
  71. if(!$addres){
  72. exception('操作失败');
  73. }
  74. $addDeviceID = $this->id;
  75. $deviceUser=explode(',',$data['device_user']);
  76. foreach ($deviceUser as $k=>$v){
  77. $data=[
  78. 'user_id'=>$v,
  79. 'device_id'=>$addDeviceID,
  80. ];
  81. $addDeviceUser=Db::name('device_user')->insert($data);
  82. if(!$addDeviceUser){
  83. exception('操作失败');
  84. }
  85. }
  86. Db::commit();
  87. return true;
  88. } catch (\Exception $e) {
  89. // 回滚事务
  90. $this->error = $e->getMessage();
  91. Db::rollback();
  92. return false;
  93. }
  94. }
  95. }
  96. //获取维护用户
  97. public function findUser($deviceId){
  98. $ret=Db::name('device_user')->where('device_id',$deviceId)->select();
  99. return $ret;
  100. }
  101. public function sweepCode($code,$userId,$orgId,$taskId=0,$deviceId=0) {
  102. $codeArr = get_qrcode_arr($code);
  103. if(!$codeArr){
  104. $this->error = '设备不存在';
  105. return false;
  106. }
  107. if($codeArr['type'] != 'device'){
  108. $this->error = '设备不存在';
  109. return false;
  110. }
  111. if($codeArr['ucode'] != config('app.ucode')){
  112. $this->error = '设备不存在';
  113. return false;
  114. }
  115. if($taskId > 0 ){
  116. if($codeArr['id'] != $deviceId){
  117. $this->error = '设备不存在';
  118. return false;
  119. }
  120. $task = Db::name('device_task')
  121. ->where('id',$taskId)
  122. ->where('del',0)
  123. ->where('org_id',$orgId)
  124. ->find();
  125. if(!$task){
  126. $this->error = '任务不存在';
  127. return false;
  128. }
  129. if($task['end_time'] < date('Y-m-d H:i:s') || $task['start_time'] > date('Y-m-d H:i:s')){
  130. $this->error = '不在时间范围内';
  131. return false;
  132. }
  133. // $taskAddr = Db::name('device_task_addr')
  134. // ->where('task_id',$task['id'])
  135. // ->column('device_id');
  136. // $taskAddr = $taskAddr?$taskAddr:[];
  137. // if(!in_array($codeArr['id'],$taskAddr)){
  138. // $this->error = '地点不正确';
  139. // return false;
  140. // }
  141. $workerAuth =1;
  142. }else{
  143. $device =Db::name('device_user')
  144. ->where('device_id',$codeArr['id'])
  145. ->where('user_id',$userId)
  146. ->find();
  147. $workerAuth = $device?1:0;
  148. }
  149. $map[] = ['org_id','=',$orgId];
  150. $map[] = ['id','=',$codeArr['id']];
  151. $map[] = ['enable','=',1];
  152. $map[] = ['del','=',0];
  153. $device = Db::name('device')
  154. ->field('del,enable',true)
  155. ->where($map)
  156. ->find();
  157. if(!$device){
  158. $this->error = '设备不存在';
  159. return false;
  160. }
  161. $lookAllAuth = (new Roles())->getAppAuth($userId,AppAuth::DEVICE_LOOK_ALL_AUTH);
  162. $ret['device_id'] = $codeArr['id'];
  163. $ret['worker_auth'] = $workerAuth;
  164. $ret['look_all_auth'] = $lookAllAuth?1:0;
  165. return $ret;
  166. }
  167. public function deviceInfo($id,$orgId){
  168. $map[] = ['org_id','=',$orgId];
  169. $map[] = ['id','=',$id];
  170. $map[] = ['enable','=',1];
  171. $map[] = ['del','=',0];
  172. $device = Db::name('device')
  173. ->field('del,enable',true)
  174. ->where($map)
  175. ->find();
  176. if(!$device){
  177. $this->error = '设备不存在';
  178. return false;
  179. }
  180. $formId=empty($device['device_form']) ? [] : explode(',',$device['device_form']);
  181. $deviceForm = Db::name('device_form')
  182. ->field('del,enable',true)
  183. ->where('enable',1)
  184. ->where('del',0)
  185. ->whereIn('id',$formId)
  186. ->select();
  187. $device['device_form'] = $deviceForm;
  188. return $device;
  189. }
  190. /**
  191. * 设备台账工作量统计
  192. *
  193. * @author wst
  194. * @date 2021/4/13 8:38
  195. */
  196. public function DeviceWork($orgId){
  197. $list = $this
  198. ->field('id,title')
  199. ->where('enable',1)
  200. ->where('del',0)
  201. ->where('org_id',$orgId)
  202. ->select();
  203. $list = $list?$list:array();
  204. $count = $count1 = $count2 =$count3 = 0;
  205. foreach ($list as $k=>$v){
  206. $query = Db::name('device_record')
  207. ->where('org_id',$orgId)
  208. ->where('device_id',$v['id']);
  209. $list[$k]['count'] = $query
  210. ->count();
  211. $count += $list[$k]['count'];
  212. //本月
  213. $list[$k]['count1'] = $query
  214. ->where('create_yyyymm',date('Ym'))
  215. ->count();
  216. $count1 += $list[$k]['count1'];
  217. //本周
  218. $list[$k]['count2'] = $query
  219. ->whereIn('create_yyyymmdd',get_week_date())
  220. ->count();
  221. $count2 += $list[$k]['count2'];
  222. //今天
  223. $list[$k]['count3'] = $query
  224. ->where('create_yyyymmdd',date('Ymd'))
  225. ->count();
  226. $count3 += $list[$k]['count3'];
  227. }
  228. return array('list'=>$list,'count'=>$count,'count1'=>$count1,'count2'=>$count2,'count3'=>$count3);
  229. }
  230. /**
  231. * 根据设备id获取维修人员列表
  232. *
  233. * @author wst
  234. * @date 2021/9/6 11:27
  235. * @return array
  236. */
  237. public function getDeviceUserList($id){
  238. $list = Db::name('device_user')
  239. ->where('device_id',$id)
  240. ->select();
  241. $user = [];
  242. foreach ($list as $k=>$v){
  243. $user[]=(string)$v['user_id'];
  244. }
  245. return $user;
  246. }
  247. /**
  248. * 根据设备id获取维修人员姓名
  249. *
  250. * @author wst
  251. * @date 2021/9/6 11:27
  252. * @return array
  253. */
  254. public function getByIdUserName($id){
  255. $user_name=Db::name('device_user')
  256. ->alias('du')
  257. ->join('user u','u.id=du.user_id')
  258. ->field('u.real_name')
  259. ->where('du.device_id',$id)
  260. ->select();
  261. $name=[];
  262. foreach ($user_name as $kk=>$vv){
  263. $name[$kk]=$vv['real_name'];
  264. }
  265. return $name;
  266. }
  267. public function taskAddrAll($taskId){
  268. $info = Db::name('device_task')
  269. ->field('id,start_time,end_time,title,status,create_time')
  270. ->where('id',$taskId)
  271. ->where('del',0)
  272. ->find();
  273. $info['users'] = Db::name('user')
  274. ->alias('u')
  275. ->field('u.real_name')
  276. ->join('device_task_user dtu','dtu.user_id=u.id')
  277. ->where('dtu.task_id',$info['id'])
  278. ->where('u.del',0)
  279. ->where('u.enable',1)
  280. ->select();
  281. $info['device'] = Db::name('device')
  282. ->alias('d')
  283. ->field('d.id,d.title')
  284. ->join('device_task_addr dta','dta.device_id=d.id')
  285. ->where('dta.task_id',$info['id'])
  286. ->where('d.del',0)
  287. ->where('d.enable',1)
  288. ->select();
  289. foreach ($info['device'] as $k=>$v){
  290. $record = Db::name('device_record')
  291. ->where('task_id',$taskId)
  292. ->where('device_id',$v['id'])
  293. ->find();
  294. $info['device'][$k]['status'] = $record ? 1 : 0;
  295. $info['device'][$k]['device_record_id'] = $record ? $record['id'] : 0;
  296. }
  297. return $info;
  298. }
  299. public function myTaskRecord($type,$page,$size,$userId,$orgId,$status){
  300. // $etime = date('Y-m-d H:i:s',time() + 12*60*60);
  301. $record = [];
  302. if($type == 1){
  303. $record = Db::name('device_task_user')
  304. ->alias('a')
  305. ->join('device_task b','a.task_id = b.id')
  306. ->join('task t','t.bus_id = b.id')
  307. ->where('b.del',0)
  308. ->where('t.type',4)
  309. // ->where('b.start_time','<',$etime)
  310. ->where('a.user_id',$userId)
  311. ->where('b.org_id',$orgId)
  312. ->where('b.status','in',[0,1])
  313. ->field('b.id,b.title,b.start_time,b.end_time,b.status')
  314. ->order('b.start_time desc,b.id desc')
  315. ->distinct(true)
  316. ->page($page,$size)
  317. ->select();
  318. }else if($type == 2){
  319. if($status != ''){
  320. $map[] = ['b.status','=',$status];
  321. }else{
  322. $map[] = ['b.status','in',[2,3]];
  323. }
  324. $record = Db::name('device_task_user')
  325. ->alias('a')
  326. ->join('device_task b','a.task_id = b.id')
  327. ->where('b.del',0)
  328. // ->where('b.start_time','<',$etime)
  329. ->where('a.user_id',$userId)
  330. ->where('b.org_id',$orgId)
  331. ->where($map)
  332. ->field('b.id,b.title,b.start_time,b.end_time,b.status')
  333. ->order('b.status desc,b.start_time desc,b.id desc')
  334. ->page($page,$size)
  335. ->distinct(true)
  336. ->select();
  337. }
  338. foreach ($record as $k=>$v){
  339. $info = Db::name('device_task_delay')
  340. ->where('task_id',$v['id'])
  341. ->where('status',1)
  342. ->field('end_time')
  343. ->order('id desc')
  344. ->find();
  345. if($info){
  346. $record[$k]['end_time'] = $info['end_time'];
  347. }
  348. }
  349. return $record?$record:[];
  350. }
  351. public function taskCount($userId,$orgId){
  352. $record = Db::name('device_task_user')
  353. ->alias('a')
  354. ->join('device_task b','a.task_id = b.id')
  355. ->join('task t','t.bus_id = b.id')
  356. ->where('b.del',0)
  357. ->where('t.type',4)
  358. // ->where('b.start_time','<',$etime)
  359. ->where('a.user_id',$userId)
  360. ->where('b.org_id',$orgId)
  361. ->where('b.status','in',[0,1])
  362. ->field('b.id,b.title,b.start_time,b.end_time,b.status')
  363. ->order('b.start_time desc,b.id desc')
  364. ->distinct(true)
  365. ->count();
  366. return $record?$record:0;
  367. }
  368. }