QualityRecord.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\common\model\Task;
  5. use app\hander\HelpHander;
  6. use think\Db;
  7. use think\Exception;
  8. class QualityRecord extends Base
  9. {
  10. public function add(){
  11. $jsonArray = [
  12. 'userId'=>$this->userId,
  13. 'orgId'=>$this->orgId,
  14. 'qualityTaskId'=>input('qualityTaskId'),
  15. 'qualityAddrId'=>input('qualityAddrId'),
  16. 'images'=>input('images'),
  17. 'content'=>input('content'),
  18. 'checkJson'=>input('checkJson'),
  19. ];
  20. // checkJson新格式:[{"id":1,"title":"表单名称","forms":[{"remark":"","result":"1","title":"测试","type":"0"}]}]
  21. // $json = input('json');
  22. // if (!$json) {
  23. // HelpHander::error('参数不正确');
  24. // }
  25. // $jsonArray=json_decode($json,true);
  26. if(!$jsonArray['checkJson']){
  27. HelpHander::error('检查结果不能为空');
  28. }
  29. if(!$jsonArray['qualityTaskId']){
  30. HelpHander::error('参数错误');
  31. }
  32. if(!$jsonArray['qualityAddrId']){
  33. HelpHander::error('参数错误');
  34. }
  35. $patrolTask = Db::name('quality_task')
  36. ->where('id',$jsonArray['qualityTaskId'])
  37. ->where('del',0)
  38. ->find();
  39. if(!$patrolTask){
  40. HelpHander::error('任务不存在');
  41. }
  42. if(!in_array($patrolTask['status'],[0,1])){
  43. HelpHander::error('任务已处理');
  44. }
  45. $curTime = time();
  46. $startTime = strtotime($patrolTask['start_time']);
  47. $endTime = strtotime($patrolTask['end_time']);
  48. if($curTime < $startTime || $curTime > $endTime){
  49. HelpHander::error('不在任务时间范围内,无法提交');
  50. }
  51. // 检查该任务是已保存
  52. $res = Db::name('quality_record')
  53. ->where('quality_task_id',$jsonArray['qualityTaskId'])
  54. ->where('id',$jsonArray['qualityAddrId'])
  55. ->find();
  56. if($res){
  57. HelpHander::error('该检查内容已上传记录');
  58. }
  59. $countrecord = Db::name('quality_record')
  60. ->where('quality_task_id',$jsonArray['qualityTaskId'])
  61. ->count();
  62. $quality_group_id = Db::name('quality_task_addr')->where('id',$jsonArray['qualityAddrId'])->value('quality_group_id');
  63. Db::startTrans();
  64. try {
  65. $data=[
  66. 'org_id'=>$jsonArray['orgId'],
  67. 'user_id'=>$jsonArray['userId'],
  68. 'quality_task_id'=>$jsonArray['qualityTaskId'],
  69. 'quality_addr_id'=>$jsonArray['qualityAddrId'],
  70. 'quality_group_id'=>$quality_group_id,
  71. 'images'=>$jsonArray['images'],
  72. 'content'=>isset($jsonArray['content'])?$jsonArray['content']:'',
  73. 'check_json'=>$jsonArray['checkJson'],
  74. 'create_time'=>date('Y-m-d H:i:s'),
  75. 'create_yyyy'=>date('Y'),
  76. 'create_yyyymm'=>date('Ym'),
  77. 'create_yyyymmdd'=>date('Ymd'),
  78. ];
  79. $addRecord=Db::name('quality_record')->insert($data);
  80. if(!$addRecord){
  81. exception('保存失败');
  82. }
  83. $countaddr = Db::name('quality_task_addr')->where('quality_task_id',$jsonArray['qualityTaskId'])->count();
  84. if($countaddr == $countrecord + 1){
  85. $status = 2;
  86. }else{
  87. $status = 1;
  88. }
  89. if($patrolTask['status'] != $status){
  90. $a = [
  91. 'status'=>$status,
  92. 'update_time'=>getTime(),
  93. ];
  94. $save = Db::name('quality_task')
  95. ->where('id',$jsonArray['qualityTaskId'])
  96. ->update($a);
  97. if(!$save){
  98. exception('操作失败');
  99. }
  100. if(in_array($status,[2,3,5])){
  101. Db::name('task')
  102. ->where('bus_id', $jsonArray['qualityTaskId'])
  103. ->where('type', (new Task())::TASK_TYPE_QUALITY)
  104. ->delete();
  105. }
  106. }
  107. Db::commit();
  108. } catch (\Exception $e) {
  109. // 回滚事务
  110. Db::rollback();
  111. HelpHander::error( $e->getMessage());
  112. }
  113. HelpHander::success([],'操作成功');
  114. }
  115. public function detail(){
  116. $id = input('id/d',0);
  117. $record = Db::name('quality_record')
  118. ->where('quality_addr_id',$id)
  119. ->field('id,user_id,images,content,check_json,create_time')
  120. ->find();
  121. if(!$record){
  122. HelpHander::error('记录不存在');
  123. }
  124. $uname = Db::name('user')->where('id',$record['user_id'])->value('real_name');
  125. $record['uname'] = $uname?$uname:'';
  126. unset($record['user_id']);
  127. $record['check_json'] = json_decode($record['check_json'],true);
  128. HelpHander::success($record,'成功');
  129. }
  130. }