GreenTaskRecord.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\common\model\Roles;
  5. use app\common\model\Task;
  6. use app\common\util\AppAuth;
  7. use app\hander\HelpHander;
  8. use think\Db;
  9. class GreenTaskRecord extends Base
  10. {
  11. public function add(){
  12. $jsonArray = [
  13. 'userId'=>$this->userId,
  14. 'orgId'=>$this->orgId,
  15. 'greenTaskId'=>input('greenTaskId',0),
  16. 'greenTaskAddrId'=>input('greenTaskAddrId',0),
  17. 'greenAddrId'=>input('greenAddrId',0),
  18. 'images'=>input('images','','trim'),
  19. 'content'=>input('content','','trim'),
  20. 'isNormal'=>input('isNormal',0),
  21. 'checkJson'=>input('checkJson',''),
  22. ];
  23. $arr = json_decode($jsonArray['checkJson'],true);
  24. if(!$arr){
  25. HelpHander::error('检查结果不能为空');
  26. }
  27. $greenTask = Db::name('green_task')
  28. ->where('id',$jsonArray['greenTaskId'])
  29. ->where('del',0)
  30. ->find();
  31. if(!$greenTask){
  32. HelpHander::error('任务不存在');
  33. }
  34. if(!in_array($greenTask['status'],[0,1])){
  35. HelpHander::error('任务已处理');
  36. }
  37. $curTime = time();
  38. $startTime = strtotime($greenTask['start_time']);
  39. $endTime = strtotime($greenTask['end_time']);
  40. if($curTime < $startTime || $curTime > $endTime){
  41. HelpHander::error('不在任务范围内,无法提交');
  42. }
  43. // 检查该任务是已保存
  44. $res = Db::name('green_task_record')
  45. ->where('green_task_id',$jsonArray['greenTaskId'])
  46. ->where('green_task_addr_id',$jsonArray['greenTaskAddrId'])
  47. ->find();
  48. if($res){
  49. HelpHander::error('该地点已上传记录');
  50. }
  51. $task = Db::name('green_task')
  52. ->where('id',$jsonArray['greenTaskId'])
  53. ->find();
  54. $countrecord = Db::name('green_task_record')
  55. ->where('green_task_id',$jsonArray['greenTaskId'])
  56. ->count();
  57. Db::startTrans();
  58. try {
  59. $data=[
  60. 'org_id'=>$jsonArray['orgId'],
  61. 'user_id'=>$jsonArray['userId'],
  62. 'green_task_id'=>$jsonArray['greenTaskId'],
  63. 'green_addr_id'=>$jsonArray['greenAddrId'],
  64. 'images'=>$jsonArray['images'],
  65. 'content'=>isset($jsonArray['content'])?$jsonArray['content']:'',
  66. 'check_json'=>$jsonArray['checkJson'],
  67. 'is_normal'=>$jsonArray['isNormal'],
  68. 'create_time'=>date('Y-m-d H:i:s'),
  69. 'create_yyyy'=>date('Y'),
  70. 'create_yyyymm'=>date('Ym'),
  71. 'create_yyyymmdd'=>date('Ymd'),
  72. 'green_task_addr_id'=>$jsonArray['greenTaskAddrId'],
  73. ];
  74. $addRecord=Db::name('green_task_record')->insert($data);
  75. if(!$addRecord){
  76. exception('保存失败');
  77. }
  78. $countaddr = Db::name('green_task_addr')
  79. ->where('green_task_id',$jsonArray['greenTaskId'])->count();
  80. if($countaddr == $countrecord + 1){
  81. $status = 2;
  82. }else{
  83. $status = 1;
  84. }
  85. if($task['status'] != $status){
  86. if($jsonArray['isNormal']==1){
  87. $a = [
  88. 'is_exception'=>1,
  89. 'status'=>$status,
  90. 'update_time'=>getTime(),
  91. ];
  92. }else{
  93. $a = [
  94. 'status'=>$status,
  95. 'update_time'=>getTime(),
  96. ];
  97. }
  98. $save = Db::name('green_task')
  99. ->where('id',$jsonArray['greenTaskId'])
  100. ->update($a);
  101. if(!$save){
  102. exception('操作失败');
  103. }
  104. if(in_array($status,[2,3,5])){
  105. Db::name('task')
  106. ->where('bus_id', $jsonArray['greenTaskId'])
  107. ->where('type', (new Task())::TASK_TYPE_GREEN)
  108. ->delete();
  109. }
  110. }
  111. Db::commit();
  112. } catch (\Exception $e) {
  113. // 回滚事务
  114. Db::rollback();
  115. HelpHander::error( $e->getMessage());
  116. }
  117. HelpHander::success([],'操作成功');
  118. }
  119. public function taskAddrList(){
  120. $page = input('page',1);
  121. $size = input('size',10);
  122. $type = input('type/d',0);
  123. $addId = input('greenAddrId');
  124. if(!$addId){
  125. HelpHander::error('地点不存在');
  126. }
  127. if($type == 1){ // 查看全部记录
  128. $auth = (new Roles())->getAppAuth($this->userId,AppAuth::GREEN_LOOK_ALL_AUTH);
  129. if(!$auth){ // 无权限
  130. $m[] = ['user_id','=',-1];
  131. }
  132. }else{ // 查看个人记录
  133. $m[] = ['user_id','=',$this->userId];
  134. }
  135. $m[] = ['org_id','=',$this->orgId];
  136. $m[] = ['green_addr_id','=',$addId];
  137. $list = Db::name('green_task_record')
  138. ->field('id,user_id,green_task_id,create_time,green_addr_id,green_task_addr_id')
  139. ->where($m)
  140. ->page($page,$size)
  141. ->order('id desc')
  142. ->select();
  143. foreach ($list as $k=>$v){
  144. $list[$k]['task_name'] = Db::name('green_task')
  145. ->where('id',$v['green_task_id'])
  146. ->value('title');
  147. $list[$k]['user_name'] = Db::name('user')
  148. ->where('id',$v['user_id'])
  149. ->value('real_name');
  150. $list[$k]['addr_name'] = Db::name('green_addr')
  151. ->where('id',$v['green_addr_id'])
  152. ->value('title');
  153. }
  154. HelpHander::success($list);
  155. }
  156. }