DailyRecord.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace app\common\model;
  3. use app\common\util\AppAuth;
  4. use app\hander\HelpHander;
  5. use think\Db;
  6. class DailyRecord extends Base {
  7. public function addSave($jsonArray){
  8. $data=[
  9. 'org_id'=>$jsonArray['orgId'],
  10. 'user_id'=>$jsonArray['userId'],
  11. 'images'=>$jsonArray['images'],
  12. 'content'=>$jsonArray['content'],
  13. 'check_json'=>$jsonArray['checkJson'],
  14. 'create_time'=>getTime(),
  15. 'create_yyyy'=>date('Y'),
  16. 'create_yyyymm'=>date('Ym'),
  17. 'create_yyyymmdd'=>date('Ymd'),
  18. 'daily_id'=>$jsonArray['dailyId'],
  19. ];
  20. $taskId = $jsonArray['taskId'];
  21. if(!$data['check_json']){
  22. $this->error = '检查结果不能为空';
  23. return false;
  24. }
  25. if(!$data['daily_id']){
  26. $this->error = '任务名称不能为空';
  27. return false;
  28. }
  29. if($taskId >0){
  30. $data['task_id'] = $taskId;
  31. $dailyTask = Db::name('daily_task')
  32. ->where('id',$taskId)
  33. ->where('del',0)
  34. ->find();
  35. if(!$dailyTask){
  36. $this->error = '任务不存在';
  37. return false;
  38. }
  39. if(!in_array($dailyTask['status'],[0,1])){
  40. $this->error = '任务已处理';
  41. return false;
  42. }
  43. $curTime = time();
  44. $startTime = strtotime($dailyTask['start_time']);
  45. $endTime = strtotime($dailyTask['end_time']);
  46. if($curTime < $startTime || $curTime > $endTime){
  47. $this->error = '不在任务范围内,无法提交';
  48. return false;
  49. }
  50. // 检查该任务是已保存
  51. $res = Db::name('daily_record')
  52. ->where('task_id',$taskId)
  53. ->where('daily_id',$data['daily_id'])
  54. ->find();
  55. if($res){
  56. $this->error ='该地点已上传记录';
  57. return false;
  58. }
  59. $task = Db::name('daily_task')
  60. ->where('id',$taskId)
  61. ->find();
  62. $countrecord = Db::name('daily_record')
  63. ->where('task_id',$taskId)
  64. ->count();
  65. Db::startTrans();
  66. try {
  67. $addRecord=Db::name('daily_record')->insert($data);
  68. if(!$addRecord){
  69. exception('保存失败');
  70. }
  71. $countaddr = Db::name('daily_task_addr')
  72. ->where('task_id',$taskId)->count();
  73. if($countaddr == $countrecord + 1){
  74. $status = 2;
  75. }else{
  76. $status = 1;
  77. }
  78. if($task['status'] != $status){
  79. $a = [
  80. 'status'=>$status
  81. ];
  82. $save = Db::name('daily_task')
  83. ->where('id',$taskId)
  84. ->update($a);
  85. if(!$save){
  86. exception('操作失败');
  87. }
  88. if($status == 2){ // 删除任务栏任务
  89. Db::name('task')
  90. ->where('bus_id', $taskId)
  91. ->where('type', (new Task())::TASK_TYPE_DAILY)
  92. ->delete();
  93. }
  94. }
  95. Db::commit();
  96. return true;
  97. } catch (\Exception $e) {
  98. // 回滚事务
  99. $this->error = $e->getMessage();
  100. Db::rollback();
  101. return false;
  102. }
  103. }else{
  104. $addRecord=Db::name('daily_record')->insert($data);
  105. if($addRecord){
  106. return true;
  107. }
  108. $this->error = '保存失败';
  109. return false;
  110. }
  111. }
  112. public function dailyRecordlist($page,$size,$orgId,$dailyId,$userId,$type=0){
  113. $m[] = ['org_id','=',$orgId];
  114. $m[] = ['daily_id','=',$dailyId];
  115. if($type == 1){ // 查看全部记录
  116. // $auth = (new Roles())->getAppAuth($userId,AppAuth::DAILY_LOOK_ALL_AUTH);
  117. // if(!$auth){ // 无权限
  118. // $m[] = ['user_id','=',-1];
  119. // }
  120. }else{ // 查看个人记录
  121. $m[] = ['user_id','=',$userId];
  122. }
  123. $ret=$this->field('id,create_time,daily_id,user_id,order_id')
  124. ->where($m)
  125. ->page($page,$size)
  126. ->order('id desc')
  127. ->select();
  128. foreach ($ret as $kk=>$val){
  129. $ret[$kk]['real_name']=Db::name('user')
  130. ->where('id',$val['user_id'])
  131. ->value('real_name');
  132. unset($ret[$kk]['user_id']);
  133. $comment = Db::name('comment')->where('from_id',$val['id'])->where('type',100)->find();
  134. $ret[$kk]['comment_string'] = $comment?'已评价':'未评价';
  135. $status = 0;
  136. if($val['order_id'] > 0 ){
  137. $order = Db::name('orders')
  138. ->where('id',$val['order_id'])
  139. ->where('quality_type',0)
  140. ->whereIn('order_mode',[5,6])->where('del',0)->find();
  141. if($order){
  142. $status = 2;
  143. }else{
  144. $status = 1;
  145. }
  146. }
  147. $ret[$kk]['zg_type'] = $status;
  148. }
  149. return $ret?$ret:[];
  150. }
  151. public function dailyRecordDetails($dailyRecordId,$userId){
  152. $ret = Db::name('daily_record')
  153. ->alias('dr')
  154. ->join('daily d','d.id = dr.daily_id')
  155. ->where('dr.id',$dailyRecordId)
  156. ->field('d.address_id,dr.user_id,dr.check_json,dr.create_time,dr.content as record_content,dr.daily_id,dr.id,dr.order_id,dr.images,d.title,d.content as daily_content')
  157. ->find();
  158. $ret['title'] = Db::name('address')
  159. ->where('id',$ret['address_id'])
  160. ->value('title');
  161. $ret['real_name'] = Db::name('user')->where('id',$ret['user_id'])->value('real_name');
  162. $comment_auth = (new Roles())->getAppAuth($userId,AppAuth::DAILY_COMMENT_AUTH)?1:0;
  163. $reform_auth = (new Roles())->getAppAuth($userId,AppAuth::DAILY_REFORM)?1:0;
  164. $comment = Db::name('comment')
  165. ->alias('c')
  166. ->join('user u','u.id = c.user_id')
  167. ->where('c.from_id',$dailyRecordId)
  168. ->where('c.type',100)
  169. ->field('c.id,c.score,c.content,c.create_time,u.real_name,u.head_image')
  170. ->order('c.id desc')
  171. ->select();
  172. $ret['is_reform'] = $reform_auth && $ret['order_id'] == 0?1:0;
  173. $ret['comment'] = $comment?$comment:[];
  174. $ret['is_comment'] = $comment_auth && empty($comment)?1:0; // 暂时只能评价一条
  175. return $ret;
  176. }
  177. }