123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\Base;
- use app\common\model\Roles;
- use app\common\model\Task;
- use app\common\util\AppAuth;
- use app\hander\HelpHander;
- use think\Db;
- class GreenTaskRecord extends Base
- {
- public function add(){
- $jsonArray = [
- 'userId'=>$this->userId,
- 'orgId'=>$this->orgId,
- 'greenTaskId'=>input('greenTaskId',0),
- 'greenTaskAddrId'=>input('greenTaskAddrId',0),
- 'greenAddrId'=>input('greenAddrId',0),
- 'images'=>input('images','','trim'),
- 'content'=>input('content','','trim'),
- 'isNormal'=>input('isNormal',0),
- 'checkJson'=>input('checkJson',''),
- ];
- $arr = json_decode($jsonArray['checkJson'],true);
- if(!$arr){
- HelpHander::error('检查结果不能为空');
- }
- $greenTask = Db::name('green_task')
- ->where('id',$jsonArray['greenTaskId'])
- ->where('del',0)
- ->find();
- if(!$greenTask){
- HelpHander::error('任务不存在');
- }
- if(!in_array($greenTask['status'],[0,1])){
- HelpHander::error('任务已处理');
- }
- $curTime = time();
- $startTime = strtotime($greenTask['start_time']);
- $endTime = strtotime($greenTask['end_time']);
- if($curTime < $startTime || $curTime > $endTime){
- HelpHander::error('不在任务范围内,无法提交');
- }
- // 检查该任务是已保存
- $res = Db::name('green_task_record')
- ->where('green_task_id',$jsonArray['greenTaskId'])
- ->where('green_task_addr_id',$jsonArray['greenTaskAddrId'])
- ->find();
- if($res){
- HelpHander::error('该地点已上传记录');
- }
- $task = Db::name('green_task')
- ->where('id',$jsonArray['greenTaskId'])
- ->find();
- $countrecord = Db::name('green_task_record')
- ->where('green_task_id',$jsonArray['greenTaskId'])
- ->count();
- Db::startTrans();
- try {
- $data=[
- 'org_id'=>$jsonArray['orgId'],
- 'user_id'=>$jsonArray['userId'],
- 'green_task_id'=>$jsonArray['greenTaskId'],
- 'green_addr_id'=>$jsonArray['greenAddrId'],
- 'images'=>$jsonArray['images'],
- 'content'=>isset($jsonArray['content'])?$jsonArray['content']:'',
- 'check_json'=>$jsonArray['checkJson'],
- 'is_normal'=>$jsonArray['isNormal'],
- 'create_time'=>date('Y-m-d H:i:s'),
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- 'green_task_addr_id'=>$jsonArray['greenTaskAddrId'],
- ];
- $addRecord=Db::name('green_task_record')->insert($data);
- if(!$addRecord){
- exception('保存失败');
- }
- $countaddr = Db::name('green_task_addr')
- ->where('green_task_id',$jsonArray['greenTaskId'])->count();
- if($countaddr == $countrecord + 1){
- $status = 2;
- }else{
- $status = 1;
- }
- if($task['status'] != $status){
- if($jsonArray['isNormal']==1){
- $a = [
- 'is_exception'=>1,
- 'status'=>$status,
- 'update_time'=>getTime(),
- ];
- }else{
- $a = [
- 'status'=>$status,
- 'update_time'=>getTime(),
- ];
- }
- $save = Db::name('green_task')
- ->where('id',$jsonArray['greenTaskId'])
- ->update($a);
- if(!$save){
- exception('操作失败');
- }
- if(in_array($status,[2,3,5])){
- Db::name('task')
- ->where('bus_id', $jsonArray['greenTaskId'])
- ->where('type', (new Task())::TASK_TYPE_GREEN)
- ->delete();
- }
- }
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- HelpHander::error( $e->getMessage());
- }
- HelpHander::success([],'操作成功');
- }
- public function taskAddrList(){
- $page = input('page',1);
- $size = input('size',10);
- $type = input('type/d',0);
- $addId = input('greenAddrId');
- if(!$addId){
- HelpHander::error('地点不存在');
- }
- if($type == 1){ // 查看全部记录
- $auth = (new Roles())->getAppAuth($this->userId,AppAuth::GREEN_LOOK_ALL_AUTH);
- if(!$auth){ // 无权限
- $m[] = ['user_id','=',-1];
- }
- }else{ // 查看个人记录
- $m[] = ['user_id','=',$this->userId];
- }
- $m[] = ['org_id','=',$this->orgId];
- $m[] = ['green_addr_id','=',$addId];
- $list = Db::name('green_task_record')
- ->field('id,user_id,green_task_id,create_time,green_addr_id,green_task_addr_id')
- ->where($m)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- foreach ($list as $k=>$v){
- $list[$k]['task_name'] = Db::name('green_task')
- ->where('id',$v['green_task_id'])
- ->value('title');
- $list[$k]['user_name'] = Db::name('user')
- ->where('id',$v['user_id'])
- ->value('real_name');
- $list[$k]['addr_name'] = Db::name('green_addr')
- ->where('id',$v['green_addr_id'])
- ->value('title');
- }
- HelpHander::success($list);
- }
- }
|