123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace app\common\model;
- use app\common\util\AppAuth;
- use app\hander\HelpHander;
- use think\Db;
- class DailyRecord extends Base {
- public function addSave($jsonArray){
- $data=[
- 'org_id'=>$jsonArray['orgId'],
- 'user_id'=>$jsonArray['userId'],
- 'images'=>$jsonArray['images'],
- 'content'=>$jsonArray['content'],
- 'check_json'=>$jsonArray['checkJson'],
- 'create_time'=>getTime(),
- 'create_yyyy'=>date('Y'),
- 'create_yyyymm'=>date('Ym'),
- 'create_yyyymmdd'=>date('Ymd'),
- 'daily_id'=>$jsonArray['dailyId'],
- ];
- $taskId = $jsonArray['taskId'];
- if(!$data['check_json']){
- $this->error = '检查结果不能为空';
- return false;
- }
- if(!$data['daily_id']){
- $this->error = '任务名称不能为空';
- return false;
- }
- if($taskId >0){
- $data['task_id'] = $taskId;
- $dailyTask = Db::name('daily_task')
- ->where('id',$taskId)
- ->where('del',0)
- ->find();
- if(!$dailyTask){
- $this->error = '任务不存在';
- return false;
- }
- if(!in_array($dailyTask['status'],[0,1])){
- $this->error = '任务已处理';
- return false;
- }
- $curTime = time();
- $startTime = strtotime($dailyTask['start_time']);
- $endTime = strtotime($dailyTask['end_time']);
- if($curTime < $startTime || $curTime > $endTime){
- $this->error = '不在任务范围内,无法提交';
- return false;
- }
- // 检查该任务是已保存
- $res = Db::name('daily_record')
- ->where('task_id',$taskId)
- ->where('daily_id',$data['daily_id'])
- ->find();
- if($res){
- $this->error ='该地点已上传记录';
- return false;
- }
- $task = Db::name('daily_task')
- ->where('id',$taskId)
- ->find();
- $countrecord = Db::name('daily_record')
- ->where('task_id',$taskId)
- ->count();
- Db::startTrans();
- try {
- $addRecord=Db::name('daily_record')->insert($data);
- if(!$addRecord){
- exception('保存失败');
- }
- $countaddr = Db::name('daily_task_addr')
- ->where('task_id',$taskId)->count();
- if($countaddr == $countrecord + 1){
- $status = 2;
- }else{
- $status = 1;
- }
- if($task['status'] != $status){
- $a = [
- 'status'=>$status
- ];
- $save = Db::name('daily_task')
- ->where('id',$taskId)
- ->update($a);
- if(!$save){
- exception('操作失败');
- }
- if($status == 2){ // 删除任务栏任务
- Db::name('task')
- ->where('bus_id', $taskId)
- ->where('type', (new Task())::TASK_TYPE_DAILY)
- ->delete();
- }
- }
- Db::commit();
- return true;
- } catch (\Exception $e) {
- // 回滚事务
- $this->error = $e->getMessage();
- Db::rollback();
- return false;
- }
- }else{
- $addRecord=Db::name('daily_record')->insert($data);
- if($addRecord){
- return true;
- }
- $this->error = '保存失败';
- return false;
- }
- }
- public function dailyRecordlist($page,$size,$orgId,$dailyId,$userId,$type=0){
- $m[] = ['org_id','=',$orgId];
- $m[] = ['daily_id','=',$dailyId];
- if($type == 1){ // 查看全部记录
- // $auth = (new Roles())->getAppAuth($userId,AppAuth::DAILY_LOOK_ALL_AUTH);
- // if(!$auth){ // 无权限
- // $m[] = ['user_id','=',-1];
- // }
- }else{ // 查看个人记录
- $m[] = ['user_id','=',$userId];
- }
- $ret=$this->field('id,create_time,daily_id,user_id,order_id')
- ->where($m)
- ->page($page,$size)
- ->order('id desc')
- ->select();
- foreach ($ret as $kk=>$val){
- $ret[$kk]['real_name']=Db::name('user')
- ->where('id',$val['user_id'])
- ->value('real_name');
- unset($ret[$kk]['user_id']);
- $comment = Db::name('comment')->where('from_id',$val['id'])->where('type',100)->find();
- $ret[$kk]['comment_string'] = $comment?'已评价':'未评价';
- $status = 0;
- if($val['order_id'] > 0 ){
- $order = Db::name('orders')
- ->where('id',$val['order_id'])
- ->where('quality_type',0)
- ->whereIn('order_mode',[5,6])->where('del',0)->find();
- if($order){
- $status = 2;
- }else{
- $status = 1;
- }
- }
- $ret[$kk]['zg_type'] = $status;
- }
- return $ret?$ret:[];
- }
- public function dailyRecordDetails($dailyRecordId,$userId){
- $ret = Db::name('daily_record')
- ->alias('dr')
- ->join('daily d','d.id = dr.daily_id')
- ->where('dr.id',$dailyRecordId)
- ->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')
- ->find();
- $ret['title'] = Db::name('address')
- ->where('id',$ret['address_id'])
- ->value('title');
- $ret['real_name'] = Db::name('user')->where('id',$ret['user_id'])->value('real_name');
- $comment_auth = (new Roles())->getAppAuth($userId,AppAuth::DAILY_COMMENT_AUTH)?1:0;
- $reform_auth = (new Roles())->getAppAuth($userId,AppAuth::DAILY_REFORM)?1:0;
- $comment = Db::name('comment')
- ->alias('c')
- ->join('user u','u.id = c.user_id')
- ->where('c.from_id',$dailyRecordId)
- ->where('c.type',100)
- ->field('c.id,c.score,c.content,c.create_time,u.real_name,u.head_image')
- ->order('c.id desc')
- ->select();
- $ret['is_reform'] = $reform_auth && $ret['order_id'] == 0?1:0;
- $ret['comment'] = $comment?$comment:[];
- $ret['is_comment'] = $comment_auth && empty($comment)?1:0; // 暂时只能评价一条
- return $ret;
- }
- }
|