PatrolRecord.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\common\model\Task;
  5. use app\hander\HelpHander;
  6. use think\Db;
  7. use think\Exception;
  8. class PatrolRecord extends Base
  9. {
  10. public function updateBySn()
  11. {
  12. $deviceSn = input('deviceSn');
  13. $power = input('power');
  14. $ret = Db::name('address')
  15. ->where('sn',$deviceSn)->update(['power'=>$power]);
  16. if($ret){
  17. HelpHander::success([],'修改成功');
  18. }else {
  19. HelpHander::error('修改失败');
  20. }
  21. }
  22. public function add(){
  23. // $jsonArray = [
  24. // 'userId'=>$this->userId,
  25. // 'orgId'=>$this->orgId,
  26. // 'patrolTaskId'=>input('patrolTaskId'),
  27. // 'patrolAddrFormId'=>input('patrolAddrFormId'),
  28. // 'patrolAddrId'=>input('patrolAddrId'),
  29. // 'images'=>input('images'),
  30. // 'content'=>input('content'),
  31. // 'isNormal'=>input('isNormal'),
  32. // 'patrolMode'=>input('patrolMode'),
  33. // 'checkJson'=>input('checkJson'),
  34. // ];
  35. // if(!$jsonArray['checkJson']){
  36. // HelpHander::error('检查结果不能为空');
  37. // }
  38. // checkJson新格式:[{"id":1,"title":"表单名称","forms":[{"remark":"","result":"1","title":"测试","type":"0"}]}]
  39. $json = input('json');
  40. if (!$json) {
  41. HelpHander::error('参数不正确');
  42. }
  43. $jsonArray=json_decode($json,true);
  44. if(!$jsonArray['checkJson']){
  45. HelpHander::error('检查结果不能为空');
  46. }
  47. $patrolTask = Db::name('patrol_task')
  48. ->where('id',$jsonArray['patrolTaskId'])
  49. ->where('del',0)
  50. ->find();
  51. $task = $patrolTask;
  52. if(!$patrolTask){
  53. HelpHander::error('任务不存在');
  54. }
  55. if(!in_array($patrolTask['status'],[0,1])){
  56. HelpHander::error('任务已处理');
  57. }
  58. $curTime = time();
  59. $startTime = strtotime($patrolTask['start_time']);
  60. $endTime = strtotime($patrolTask['end_time']);
  61. if($curTime < $startTime || $curTime > $endTime){
  62. HelpHander::error('不在任务时间范围内,无法提交');
  63. }
  64. // 检查该任务是已保存
  65. $res = Db::name('patrol_record')
  66. ->where('patrol_task_id',$jsonArray['patrolTaskId'])
  67. ->where('patrol_addr_form_id',$jsonArray['patrolAddrFormId'])
  68. ->find();
  69. if($res){
  70. HelpHander::error('该地点已上传记录');
  71. }
  72. $countrecord = Db::name('patrol_record')
  73. ->where('patrol_task_id',$jsonArray['patrolTaskId'])
  74. ->count();
  75. $time = 0; // 0=正常 1=异常
  76. $rmax = 0;
  77. $rmin = 0;
  78. $wctime = 0;
  79. if($task['in_order'] == 1){ // 按顺序巡更,检查地点循序
  80. $fcount = Db::name('patrol_task_addr')
  81. ->where('id','<',$jsonArray['patrolAddrFormId'])
  82. ->where('patrol_task_id',$jsonArray['patrolTaskId'])
  83. ->count();
  84. if($fcount > 0 && ($countrecord == 0 || ($countrecord > 0 && $countrecord != $fcount))){
  85. HelpHander::error('请按正确顺序执行计划');
  86. }
  87. if($task['lines']){ // 路线时间设置
  88. $lines = json_decode($task['lines'],true);
  89. if($countrecord > 0 && $fcount > 0){ // 说明不是第一个点,获取路线开始节点
  90. $saddrid = Db::name('patrol_task_addr')
  91. ->where('id','<',$jsonArray['patrolAddrFormId'])
  92. ->where('patrol_task_id',$jsonArray['patrolTaskId'])
  93. ->value('address_id');
  94. $eaddrid = $jsonArray['patrolAddrId'];
  95. $lastTime = Db::name('patrol_record')
  96. ->where('patrol_task_id',$jsonArray['patrolTaskId'])
  97. ->order('id desc')
  98. ->value('create_time');
  99. $ctime = round((time() - strtotime($lastTime))/60);
  100. foreach ($lines as $k=>$v){
  101. if($v['s'] = $saddrid && $v['e'] == $eaddrid){ // 是同一路线,计算时长
  102. $min = round($v['kd'] - $v['fd']);
  103. $max = round($v['kd'] + $v['fd']);
  104. $min = $min > 0 ?$min:0;
  105. $max = $max > 0 ?$max:0;
  106. if($ctime < $min || $max < $ctime){
  107. $time = 1;
  108. }
  109. $rmin = $min;
  110. $rmax = $max;
  111. $wctime = $ctime;
  112. break;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. Db::startTrans();
  119. try {
  120. $data=[
  121. 'org_id'=>$jsonArray['orgId'],
  122. 'user_id'=>$jsonArray['userId'],
  123. 'patrol_task_id'=>$jsonArray['patrolTaskId'],
  124. 'patrol_addr_id'=>$jsonArray['patrolAddrId'],
  125. 'images'=>$jsonArray['images'],
  126. 'content'=>isset($jsonArray['content'])?$jsonArray['content']:'',
  127. 'check_json'=>$jsonArray['checkJson'],
  128. 'is_normal'=>$jsonArray['isNormal'],
  129. 'create_time'=>date('Y-m-d H:i:s'),
  130. 'create_yyyy'=>date('Y'),
  131. 'create_yyyymm'=>date('Ym'),
  132. 'create_yyyymmdd'=>date('Ymd'),
  133. 'patrol_mode'=>$jsonArray['patrolMode'],
  134. 'patrol_addr_form_id'=>$jsonArray['patrolAddrFormId'],
  135. 'time' => $time,
  136. 'min' => $rmin,
  137. 'max' => $rmax,
  138. 'wc_time' => $wctime
  139. ];
  140. $addRecord=Db::name('patrol_record')->insert($data);
  141. if(!$addRecord){
  142. exception('保存失败');
  143. }
  144. $countaddr = Db::name('patrol_task_addr')
  145. ->where('patrol_task_id',$jsonArray['patrolTaskId'])->count();
  146. if($countaddr == $countrecord + 1){
  147. $status = 2;
  148. }else{
  149. $status = 1;
  150. }
  151. if($jsonArray['isNormal']==1 && $task['is_exception'] == 0){
  152. Db::name('patrol_task')->where('id',$jsonArray['patrolTaskId'])->setField('is_exception',1);
  153. }
  154. if($task['status'] != $status){
  155. $a = [
  156. 'status'=>$status,
  157. 'update_time'=>getTime(),
  158. ];
  159. $save = Db::name('patrol_task')
  160. ->where('id',$jsonArray['patrolTaskId'])
  161. ->update($a);
  162. if(!$save){
  163. exception('操作失败');
  164. }
  165. if(in_array($status,[2,3,5])){
  166. Db::name('task')
  167. ->where('bus_id', $jsonArray['patrolTaskId'])
  168. ->where('type', (new Task())::TASK_TYPE_PATROL)
  169. ->delete();
  170. }
  171. }
  172. Db::commit();
  173. } catch (\Exception $e) {
  174. // 回滚事务
  175. Db::rollback();
  176. HelpHander::error( $e->getMessage());
  177. }
  178. HelpHander::success([],'操作成功');
  179. }
  180. public function recordByAddr(){
  181. $id = input('id/d',0);
  182. $mode = input('mode/d',1);
  183. $page = input('page/d',1);
  184. $size = input('size/d',10);
  185. $lists = Db::name('patrol_record')
  186. ->alias('a')
  187. ->join('user b','a.user_id = b.id')
  188. ->join('patrol_task c','a.patrol_task_id = c.id')
  189. ->where('a.patrol_addr_id',$id)
  190. ->where('a.patrol_mode',$mode)
  191. ->field('a.id,a.patrol_task_id,a.order_id,a.patrol_mode,a.patrol_addr_form_id,a.is_normal,a.create_time,b.real_name,a.time,c.title')
  192. ->order('a.id desc')
  193. ->page($page,$size)
  194. ->select();
  195. $lists = $lists?$lists:[];
  196. foreach ($lists as $kk=>$val){
  197. $status = 0;
  198. if($val['order_id'] > 0 ){
  199. $order = Db::name('orders')->where('id',$val['order_id'])->whereIn('order_mode',[5,6])->where('del',0)->find();
  200. if($order){
  201. $status = 2;
  202. }else{
  203. $status = 1;
  204. }
  205. }
  206. $lists[$kk]['zg_type'] = $status;
  207. }
  208. HelpHander::success($lists?$lists:[],'成功');
  209. }
  210. }