123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\Base;
- use app\common\model\Task;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- class PatrolRecord extends Base
- {
- public function updateBySn()
- {
- $deviceSn = input('deviceSn');
- $power = input('power');
- $ret = Db::name('address')
- ->where('sn',$deviceSn)->update(['power'=>$power]);
- if($ret){
- HelpHander::success([],'修改成功');
- }else {
- HelpHander::error('修改失败');
- }
- }
- public function add(){
- // $jsonArray = [
- // 'userId'=>$this->userId,
- // 'orgId'=>$this->orgId,
- // 'patrolTaskId'=>input('patrolTaskId'),
- // 'patrolAddrFormId'=>input('patrolAddrFormId'),
- // 'patrolAddrId'=>input('patrolAddrId'),
- // 'images'=>input('images'),
- // 'content'=>input('content'),
- // 'isNormal'=>input('isNormal'),
- // 'patrolMode'=>input('patrolMode'),
- // 'checkJson'=>input('checkJson'),
- // ];
- // if(!$jsonArray['checkJson']){
- // HelpHander::error('检查结果不能为空');
- // }
- // checkJson新格式:[{"id":1,"title":"表单名称","forms":[{"remark":"","result":"1","title":"测试","type":"0"}]}]
- $json = input('json');
- if (!$json) {
- HelpHander::error('参数不正确');
- }
- $jsonArray=json_decode($json,true);
- if(!$jsonArray['checkJson']){
- HelpHander::error('检查结果不能为空');
- }
- $patrolTask = Db::name('patrol_task')
- ->where('id',$jsonArray['patrolTaskId'])
- ->where('del',0)
- ->find();
- $task = $patrolTask;
- if(!$patrolTask){
- HelpHander::error('任务不存在');
- }
- if(!in_array($patrolTask['status'],[0,1])){
- HelpHander::error('任务已处理');
- }
- $curTime = time();
- $startTime = strtotime($patrolTask['start_time']);
- $endTime = strtotime($patrolTask['end_time']);
- if($curTime < $startTime || $curTime > $endTime){
- HelpHander::error('不在任务时间范围内,无法提交');
- }
- // 检查该任务是已保存
- $res = Db::name('patrol_record')
- ->where('patrol_task_id',$jsonArray['patrolTaskId'])
- ->where('patrol_addr_form_id',$jsonArray['patrolAddrFormId'])
- ->find();
- if($res){
- HelpHander::error('该地点已上传记录');
- }
- $countrecord = Db::name('patrol_record')
- ->where('patrol_task_id',$jsonArray['patrolTaskId'])
- ->count();
- $time = 0; // 0=正常 1=异常
- $rmax = 0;
- $rmin = 0;
- $wctime = 0;
- if($task['in_order'] == 1){ // 按顺序巡更,检查地点循序
- $fcount = Db::name('patrol_task_addr')
- ->where('id','<',$jsonArray['patrolAddrFormId'])
- ->where('patrol_task_id',$jsonArray['patrolTaskId'])
- ->count();
- if($fcount > 0 && ($countrecord == 0 || ($countrecord > 0 && $countrecord != $fcount))){
- HelpHander::error('请按正确顺序执行计划');
- }
- if($task['lines']){ // 路线时间设置
- $lines = json_decode($task['lines'],true);
- if($countrecord > 0 && $fcount > 0){ // 说明不是第一个点,获取路线开始节点
- $saddrid = Db::name('patrol_task_addr')
- ->where('id','<',$jsonArray['patrolAddrFormId'])
- ->where('patrol_task_id',$jsonArray['patrolTaskId'])
- ->value('address_id');
- $eaddrid = $jsonArray['patrolAddrId'];
- $lastTime = Db::name('patrol_record')
- ->where('patrol_task_id',$jsonArray['patrolTaskId'])
- ->order('id desc')
- ->value('create_time');
- $ctime = round((time() - strtotime($lastTime))/60);
- foreach ($lines as $k=>$v){
- if($v['s'] = $saddrid && $v['e'] == $eaddrid){ // 是同一路线,计算时长
- $min = round($v['kd'] - $v['fd']);
- $max = round($v['kd'] + $v['fd']);
- $min = $min > 0 ?$min:0;
- $max = $max > 0 ?$max:0;
- if($ctime < $min || $max < $ctime){
- $time = 1;
- }
- $rmin = $min;
- $rmax = $max;
- $wctime = $ctime;
- break;
- }
- }
- }
- }
- }
- Db::startTrans();
- try {
- $data=[
- 'org_id'=>$jsonArray['orgId'],
- 'user_id'=>$jsonArray['userId'],
- 'patrol_task_id'=>$jsonArray['patrolTaskId'],
- 'patrol_addr_id'=>$jsonArray['patrolAddrId'],
- 'images'=>$jsonArray['images'],
- 'content'=>isset($jsonArray['content'])?$jsonArray['content']:'',
- 'check_json'=>$jsonArray['checkJson'],
- 'is_normal'=>$jsonArray['isNormal'],
- 'create_time'=>date('Y-m-d H:i:s'),
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- 'patrol_mode'=>$jsonArray['patrolMode'],
- 'patrol_addr_form_id'=>$jsonArray['patrolAddrFormId'],
- 'time' => $time,
- 'min' => $rmin,
- 'max' => $rmax,
- 'wc_time' => $wctime
- ];
- $addRecord=Db::name('patrol_record')->insert($data);
- if(!$addRecord){
- exception('保存失败');
- }
- $countaddr = Db::name('patrol_task_addr')
- ->where('patrol_task_id',$jsonArray['patrolTaskId'])->count();
- if($countaddr == $countrecord + 1){
- $status = 2;
- }else{
- $status = 1;
- }
- if($jsonArray['isNormal']==1 && $task['is_exception'] == 0){
- Db::name('patrol_task')->where('id',$jsonArray['patrolTaskId'])->setField('is_exception',1);
- }
- if($task['status'] != $status){
- $a = [
- 'status'=>$status,
- 'update_time'=>getTime(),
- ];
- $save = Db::name('patrol_task')
- ->where('id',$jsonArray['patrolTaskId'])
- ->update($a);
- if(!$save){
- exception('操作失败');
- }
- if(in_array($status,[2,3,5])){
- Db::name('task')
- ->where('bus_id', $jsonArray['patrolTaskId'])
- ->where('type', (new Task())::TASK_TYPE_PATROL)
- ->delete();
- }
- }
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- HelpHander::error( $e->getMessage());
- }
- HelpHander::success([],'操作成功');
- }
- public function recordByAddr(){
- $id = input('id/d',0);
- $mode = input('mode/d',1);
- $page = input('page/d',1);
- $size = input('size/d',10);
- $lists = Db::name('patrol_record')
- ->alias('a')
- ->join('user b','a.user_id = b.id')
- ->join('patrol_task c','a.patrol_task_id = c.id')
- ->where('a.patrol_addr_id',$id)
- ->where('a.patrol_mode',$mode)
- ->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')
- ->order('a.id desc')
- ->page($page,$size)
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $kk=>$val){
- $status = 0;
- if($val['order_id'] > 0 ){
- $order = Db::name('orders')->where('id',$val['order_id'])->whereIn('order_mode',[5,6])->where('del',0)->find();
- if($order){
- $status = 2;
- }else{
- $status = 1;
- }
- }
- $lists[$kk]['zg_type'] = $status;
- }
- HelpHander::success($lists?$lists:[],'成功');
- }
- }
|