DeviceRecord.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppAuth;
  4. use think\Db;
  5. class DeviceRecord extends Base {
  6. public function add($json){
  7. if(!$json){
  8. $this->error = '参数不正确';
  9. return false;
  10. }
  11. $jsonArr=$json;
  12. if(!$jsonArr['checkJson']){
  13. $this->error = '检查结果不能为空';
  14. return false;
  15. }
  16. if(!$jsonArr['deviceId']){
  17. $this->error = '设备不能为空';
  18. return false;
  19. }
  20. $data=[
  21. 'org_id'=>$jsonArr['orgId'],
  22. 'user_id'=>$jsonArr['userId'],
  23. 'images'=>$jsonArr['images'],
  24. 'content'=>$jsonArr['content'],
  25. 'device_id'=>$jsonArr['deviceId'],
  26. 'check_json'=>$jsonArr['checkJson'],
  27. 'create_time'=>date('Y-m-d H:i:s'),
  28. 'create_yyyy'=>date('Y'),
  29. 'create_yyyymm'=>date('Ym'),
  30. 'create_yyyymmdd'=>date('Ymd'),
  31. 'video'=>$jsonArr['video']
  32. ];
  33. $taskId = $jsonArr['taskId'];
  34. if($taskId >0){
  35. $data['task_id'] = $taskId;
  36. $deviceTask = Db::name('device_task')
  37. ->where('id',$taskId)
  38. ->where('del',0)
  39. ->find();
  40. if(!$deviceTask){
  41. $this->error = '任务不存在';
  42. return false;
  43. }
  44. if(!in_array($deviceTask['status'],[0,1])){
  45. $this->error = '任务已处理';
  46. return false;
  47. }
  48. $curTime = time();
  49. $startTime = strtotime($deviceTask['start_time']);
  50. $endTime = strtotime($deviceTask['end_time']);
  51. if($curTime < $startTime || $curTime > $endTime){
  52. $this->error = '不在任务范围内,无法提交';
  53. return false;
  54. }
  55. // 检查该任务是已保存
  56. $res = Db::name('device_record')
  57. ->where('task_id',$taskId)
  58. ->where('device_id',$data['device_id'])
  59. ->find();
  60. if($res){
  61. $this->error ='该地点已上传记录';
  62. return false;
  63. }
  64. $task = Db::name('device_task')
  65. ->where('id',$taskId)
  66. ->find();
  67. $countrecord = Db::name('device_record')
  68. ->where('task_id',$taskId)
  69. ->count();
  70. Db::startTrans();
  71. try {
  72. $addRecord=Db::name('device_record')->insert($data);
  73. if(!$addRecord){
  74. exception('保存失败');
  75. }
  76. $countaddr = Db::name('device_task_addr')
  77. ->where('task_id',$taskId)->count();
  78. if($countaddr == $countrecord + 1){
  79. $status = 2;
  80. }else{
  81. $status = 1;
  82. }
  83. if($task['status'] != $status){
  84. $a = [
  85. 'status'=>$status
  86. ];
  87. $save = Db::name('device_task')
  88. ->where('id',$taskId)
  89. ->update($a);
  90. if(!$save){
  91. exception('操作失败');
  92. }
  93. if($status == 2){ // 删除任务栏任务
  94. Db::name('task')
  95. ->where('bus_id', $taskId)
  96. ->where('type', (new Task())::TASK_TYPE_DEVICE)
  97. ->delete();
  98. }
  99. }
  100. Db::commit();
  101. return $status;
  102. } catch (\Exception $e) {
  103. // 回滚事务
  104. $this->error = $e->getMessage();
  105. Db::rollback();
  106. return false;
  107. }
  108. }else{
  109. $addRecord=Db::name('device_record')->insert($data);
  110. if(!$addRecord){
  111. $this->error = '保存失败';
  112. return false;
  113. }
  114. return true;
  115. }
  116. }
  117. public function lists($page,$size,$deviceId,$userId,$orgId,$type=0){
  118. $m[] = ['org_id','=',$orgId];
  119. $m[] = ['device_id','=',$deviceId];
  120. if($type == 1){ // 查看全部记录
  121. $auth = (new Roles())->getAppAuth($userId,AppAuth::DEVICE_LOOK_ALL_AUTH);
  122. if(!$auth){ // 无权限
  123. $m[] = ['user_id','=',-1];
  124. }
  125. }else{ // 查看个人记录
  126. $m[] = ['user_id','=',$userId];
  127. }
  128. $ret = Db::name('device_record')
  129. ->field('id,device_id,create_time,user_id,order_id')
  130. ->where($m)
  131. ->page($page,$size)
  132. ->order('id desc')
  133. ->select();
  134. foreach ($ret as $k=>$v){
  135. $ret[$k]['real_name'] = Db::name('user')
  136. ->where('id',$v['user_id'])
  137. ->value('real_name');
  138. $status = 0;
  139. if($v['order_id'] > 0 ){
  140. $order = Db::name('orders')
  141. ->where('id',$v['order_id'])
  142. ->where('quality_type',1)
  143. ->whereIn('order_mode',[5,6])->where('del',0)->find();
  144. if($order){
  145. $status = 2;
  146. }else{
  147. $status = 1;
  148. }
  149. }
  150. $ret[$k]['zg_type'] = $status;
  151. }
  152. return $ret?$ret:[];
  153. }
  154. public function details($id){
  155. $ret = Db::name('device_record')
  156. ->field('id,check_json,device_id,create_time,user_id,images as record_img,content as record_content,order_id,video')
  157. ->where('id',$id)
  158. ->find();
  159. $device = Db::name('device')
  160. ->where('id',$ret['device_id'])
  161. ->find();
  162. $ret['device_content'] = $device['content'];
  163. $ret['device_form'] = $device['device_form'];
  164. $ret['device_img'] = $device['imgs'];
  165. $ret['device_title'] = $device['title'];
  166. $ret['real_name'] = Db::name('user')->where('id',$ret['user_id'])->value('real_name');
  167. return $ret;
  168. }
  169. }