123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace app\common\model;
- use think\Db;
- use think\Exception;
- class Comment extends Base
- {
- public function addSave($jsonArray){
- $this->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;
- }
- }
|