123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace app\common\model;
- use app\common\util\AppAuth;
- use think\Db;
- class DeviceRecord extends Base {
- public function add($json){
- if(!$json){
- $this->error = '参数不正确';
- return false;
- }
- $jsonArr=$json;
- if(!$jsonArr['checkJson']){
- $this->error = '检查结果不能为空';
- return false;
- }
- if(!$jsonArr['deviceId']){
- $this->error = '设备不能为空';
- return false;
- }
- $data=[
- 'org_id'=>$jsonArr['orgId'],
- 'user_id'=>$jsonArr['userId'],
- 'images'=>$jsonArr['images'],
- 'content'=>$jsonArr['content'],
- 'device_id'=>$jsonArr['deviceId'],
- 'check_json'=>$jsonArr['checkJson'],
- 'create_time'=>date('Y-m-d H:i:s'),
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- ];
- $taskId = $jsonArr['taskId'];
- if($taskId >0){
- $data['task_id'] = $taskId;
- $deviceTask = Db::name('device_task')
- ->where('id',$taskId)
- ->where('del',0)
- ->find();
- if(!$deviceTask){
- $this->error = '任务不存在';
- return false;
- }
- if(!in_array($deviceTask['status'],[0,1])){
- $this->error = '任务已处理';
- return false;
- }
- $curTime = time();
- $startTime = strtotime($deviceTask['start_time']);
- $endTime = strtotime($deviceTask['end_time']);
- if($curTime < $startTime || $curTime > $endTime){
- $this->error = '不在任务范围内,无法提交';
- return false;
- }
- // 检查该任务是已保存
- $res = Db::name('device_record')
- ->where('task_id',$taskId)
- ->where('device_id',$data['device_id'])
- ->find();
- if($res){
- $this->error ='该地点已上传记录';
- return false;
- }
- $task = Db::name('device_task')
- ->where('id',$taskId)
- ->find();
- $countrecord = Db::name('device_record')
- ->where('task_id',$taskId)
- ->count();
- Db::startTrans();
- try {
- $addRecord=Db::name('device_record')->insert($data);
- if(!$addRecord){
- exception('保存失败');
- }
- $countaddr = Db::name('device_task_addr')
- ->where('task_id',$taskId)->count();
- if($countaddr == $countrecord + 1){
- $status = 2;
- }else{
- $status = 1;
- }
- if($task['status'] != $status){
- $a = [
- 'status'=>$status
- ];
- $save = Db::name('device_task')
- ->where('id',$taskId)
- ->update($a);
- if(!$save){
- exception('操作失败');
- }
- if($status == 2){ // 删除任务栏任务
- Db::name('task')
- ->where('bus_id', $taskId)
- ->where('type', (new Task())::TASK_TYPE_DEVICE)
- ->delete();
- }
- }
- Db::commit();
- return $status;
- } catch (\Exception $e) {
- // 回滚事务
- $this->error = $e->getMessage();
- Db::rollback();
- return false;
- }
- }else{
- $addRecord=Db::name('device_record')->insert($data);
- if(!$addRecord){
- $this->error = '保存失败';
- return false;
- }
- return true;
- }
- }
- public function lists($page,$size,$deviceId,$userId,$orgId,$type=0){
- $m[] = ['org_id','=',$orgId];
- $m[] = ['device_id','=',$deviceId];
- if($type == 1){ // 查看全部记录
- $auth = (new Roles())->getAppAuth($userId,AppAuth::DEVICE_LOOK_ALL_AUTH);
- if(!$auth){ // 无权限
- $m[] = ['user_id','=',-1];
- }
- }else{ // 查看个人记录
- $m[] = ['user_id','=',$userId];
- }
- $ret = Db::name('device_record')
- ->field('id,device_id,create_time,user_id,order_id')
- ->where($m)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- foreach ($ret as $k=>$v){
- $ret[$k]['real_name'] = Db::name('user')
- ->where('id',$v['user_id'])
- ->value('real_name');
- $status = 0;
- if($v['order_id'] > 0 ){
- $order = Db::name('orders')
- ->where('id',$v['order_id'])
- ->where('quality_type',1)
- ->whereIn('order_mode',[5,6])->where('del',0)->find();
- if($order){
- $status = 2;
- }else{
- $status = 1;
- }
- }
- $ret[$k]['zg_type'] = $status;
- }
- return $ret?$ret:[];
- }
- public function details($id){
- $ret = Db::name('device_record')
- ->field('id,check_json,device_id,create_time,user_id,images as record_img,content as record_content,order_id')
- ->where('id',$id)
- ->find();
- $device = Db::name('device')
- ->where('id',$ret['device_id'])
- ->find();
- $ret['device_content'] = $device['content'];
- $ret['device_form'] = $device['device_form'];
- $ret['device_img'] = $device['imgs'];
- $ret['device_title'] = $device['title'];
- $ret['real_name'] = Db::name('user')->where('id',$ret['user_id'])->value('real_name');
- return $ret;
- }
- }
|