0
0

DeviceRecord.php 6.0 KB

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