startTrans(); try{ $data=[ 'score'=>$jsonArray['score'], 'content'=>isset($jsonArray['content'])?$jsonArray['content']:'', 'user_id'=>$jsonArray['userId'], 'type'=>$jsonArray['type'], 'from_id'=>$jsonArray['fromId'], 'org_id'=>$jsonArray['orgId'], 'create_time'=>date('Y-m-d H:i:s'), 'create_yyyymm'=>date('Ym'), 'create_yyyy'=>date('Y'), 'create_yyyymmdd'=>date('Ymd'), ]; if(!$data['score']){ $this->error = '得分不能为空'; return false; } $res = $this->insert($data); if (!$res) { exception('保存失败'); return false; } if(in_array($jsonArray['type'],[0,1,2,3,4,15])){ // 改变订单状态 $ret = Db::name('orders') ->where('id',$jsonArray['fromId']) ->update(['order_mode' => 6]); if(!$ret){ exception('更新订单失败'); return false; } } $userIds = []; switch ($jsonArray['type']){ case 100: // 日常工作记录 $uid = Db::name('daily_record')->where('id',$jsonArray['fromId'])->value('user_id'); $userIds[] = $uid; break; case 0: case 1: case 2: case 3: case 4: case 15: // 订单评价 $uids = Db::name('todo')->where('order_id',$jsonArray['fromId'])->where('todo_mode',3)->column('to_user_id'); $userIds = $uids; break; case 16: // 投诉订单 Db::name('complain') ->where('id',$jsonArray['fromId']) ->update(['status'=>2,'finish_time'=>getTime()]); break; } $users = Db::name('user')->where('id','in',$userIds)->field('id,score,score_json')->select(); foreach ($users as $k=>$v){ $score = $data['score']; if($v['score_json']){ $scoreJson = json_decode($v['score_json'],true); $scoreJson['score'] = $scoreJson['score'] + $data['score']; $scoreJson['count'] = $scoreJson['count'] + 1; $score = round($scoreJson['score']/$scoreJson['count'],1); }else{ $scoreJson = [ 'score' => $score, 'count' => 1 ]; } $ret = Db::name('user')->where('id',$v['id'])->update(['score'=>$score,'score_json'=>json_encode($scoreJson)]); if(!$ret){ \exception('操作失败'); } } $this->commit(); return true; }catch (Exception $e){ $this->error = $e->getMessage(); $this->rollback(); return false; } } public function details($id){ $ret = Db::name('comment') ->field('create_time as comment_create_time,score,content,type,id,user_id,from_id') ->where('id',$id) ->find(); $ret['real_name'] = Db::name('user')->where('id',$ret['user_id'])->value('real_name'); if($ret['type']==100){ $ret['description']='日常工作评价'; $ret['create_time'] = Db::name('daily_record') ->where('id',$ret['from_id']) ->value('create_time'); } $isCreateTime = Db::name('orders') ->where('id',$ret['from_id']) ->value('create_time'); $ret['create_time']=$isCreateTime; if($ret['type']==1){ $ret['description']='报修工作评价'; } if($ret['type']==2){ $ret['description']='保洁工作评价'; } if($ret['type']==3){ $ret['description']='运送工作评价'; } if($ret['type']==4){ $ret['description']='安保工作评价'; } if($ret['type']==15){ $ret['description']='品质整改工作评价'; } if($ret['type']==0){ $ret['description']='一键呼叫工作评价'; } return $ret; } public function lists($userId,$orgId,$page,$size){ $lists = Db::name('comment_reply') ->where('org_id',$orgId) ->where('to_user_id',$userId) ->order('comment_id desc') ->page($page,$size) ->select(); return $lists; } }