Comment.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class Comment extends Base
  6. {
  7. public function addSave($jsonArray){
  8. $this->startTrans();
  9. try{
  10. $data=[
  11. 'score'=>$jsonArray['score'],
  12. 'content'=>isset($jsonArray['content'])?$jsonArray['content']:'',
  13. 'user_id'=>$jsonArray['userId'],
  14. 'type'=>$jsonArray['type'],
  15. 'from_id'=>$jsonArray['fromId'],
  16. 'org_id'=>$jsonArray['orgId'],
  17. 'create_time'=>date('Y-m-d H:i:s'),
  18. 'create_yyyymm'=>date('Ym'),
  19. 'create_yyyy'=>date('Y'),
  20. 'create_yyyymmdd'=>date('Ymd'),
  21. ];
  22. if(!$data['score']){
  23. $this->error = '得分不能为空';
  24. return false;
  25. }
  26. $res = $this->insert($data);
  27. if (!$res) {
  28. exception('保存失败');
  29. return false;
  30. }
  31. if(in_array($jsonArray['type'],[0,1,2,3,4,15])){ // 改变订单状态
  32. $ret = Db::name('orders')
  33. ->where('id',$jsonArray['fromId'])
  34. ->update(['order_mode' => 6]);
  35. if(!$ret){
  36. exception('更新订单失败');
  37. return false;
  38. }
  39. }
  40. $userIds = [];
  41. switch ($jsonArray['type']){
  42. case 100: // 日常工作记录
  43. $uid = Db::name('daily_record')->where('id',$jsonArray['fromId'])->value('user_id');
  44. $userIds[] = $uid;
  45. break;
  46. case 0:
  47. case 1:
  48. case 2:
  49. case 3:
  50. case 4:
  51. case 15: // 订单评价
  52. $uids = Db::name('todo')->where('order_id',$jsonArray['fromId'])->where('todo_mode',3)->column('to_user_id');
  53. $userIds = $uids;
  54. break;
  55. case 16: // 投诉订单
  56. Db::name('complain')
  57. ->where('id',$jsonArray['fromId'])
  58. ->update(['status'=>2,'finish_time'=>getTime()]);
  59. break;
  60. }
  61. $users = Db::name('user')->where('id','in',$userIds)->field('id,score,score_json')->select();
  62. foreach ($users as $k=>$v){
  63. $score = $data['score'];
  64. if($v['score_json']){
  65. $scoreJson = json_decode($v['score_json'],true);
  66. $scoreJson['score'] = $scoreJson['score'] + $data['score'];
  67. $scoreJson['count'] = $scoreJson['count'] + 1;
  68. $score = round($scoreJson['score']/$scoreJson['count'],1);
  69. }else{
  70. $scoreJson = [
  71. 'score' => $score,
  72. 'count' => 1
  73. ];
  74. }
  75. $ret = Db::name('user')->where('id',$v['id'])->update(['score'=>$score,'score_json'=>json_encode($scoreJson)]);
  76. if(!$ret){
  77. \exception('操作失败');
  78. }
  79. }
  80. $this->commit();
  81. return true;
  82. }catch (Exception $e){
  83. $this->error = $e->getMessage();
  84. $this->rollback();
  85. return false;
  86. }
  87. }
  88. public function details($id){
  89. $ret = Db::name('comment')
  90. ->field('create_time as comment_create_time,score,content,type,id,user_id,from_id')
  91. ->where('id',$id)
  92. ->find();
  93. $ret['real_name'] = Db::name('user')->where('id',$ret['user_id'])->value('real_name');
  94. if($ret['type']==100){
  95. $ret['description']='日常工作评价';
  96. $ret['create_time'] = Db::name('daily_record')
  97. ->where('id',$ret['from_id'])
  98. ->value('create_time');
  99. }
  100. $isCreateTime = Db::name('orders')
  101. ->where('id',$ret['from_id'])
  102. ->value('create_time');
  103. $ret['create_time']=$isCreateTime;
  104. if($ret['type']==1){
  105. $ret['description']='报修工作评价';
  106. }
  107. if($ret['type']==2){
  108. $ret['description']='保洁工作评价';
  109. }
  110. if($ret['type']==3){
  111. $ret['description']='运送工作评价';
  112. }
  113. if($ret['type']==4){
  114. $ret['description']='安保工作评价';
  115. }
  116. if($ret['type']==15){
  117. $ret['description']='品质整改工作评价';
  118. }
  119. if($ret['type']==0){
  120. $ret['description']='一键呼叫工作评价';
  121. }
  122. return $ret;
  123. }
  124. public function lists($userId,$orgId,$page,$size){
  125. $lists = Db::name('comment_reply')
  126. ->where('org_id',$orgId)
  127. ->where('to_user_id',$userId)
  128. ->order('comment_id desc')
  129. ->page($page,$size)
  130. ->select();
  131. return $lists;
  132. }
  133. }