Comment.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Comment as CommentModel;
  4. use addons\qingdongams\model\Aftermarket;
  5. use addons\qingdongams\model\Message;
  6. use addons\qingdongams\model\Event;
  7. use addons\qingdongams\model\StaffSignIn;
  8. use addons\qingdongams\model\WorkReport;
  9. use think\Db;
  10. use think\Exception;
  11. use function EasyWeChat\Kernel\Support\get_client_ip;
  12. /**
  13. * 评论
  14. */
  15. class Comment extends StaffApi
  16. {
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = [];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. try{
  23. \think\Db::execute("SET @@sql_mode='';");
  24. }catch (Exception $e){
  25. }
  26. }
  27. //添加评论
  28. public function addComment()
  29. {
  30. $content = input('content');
  31. $files = input('file_ids');
  32. $record_id = input('record_id');
  33. $relation_type = input('relation_type', 'sign');
  34. if (empty($content)) {
  35. $this->error('评论内容不能为空');
  36. }
  37. $data = [
  38. 'relation_type' => $relation_type,
  39. 'relation_id' => $record_id,
  40. 'staff_id' => $this->auth->id,
  41. 'content' => $content,
  42. 'file_ids' => $files,
  43. 'status' => 1,
  44. 'ip' => get_client_ip(),
  45. ];
  46. $commentModel = new CommentModel();
  47. Db::startTrans();
  48. try {
  49. $commentModel->save($data);
  50. if ($relation_type ==$commentModel::SIGN_TYPE) {
  51. $record = StaffSignIn::where(['id' => $record_id])->find();
  52. $record->create_staff_id=$record['staff_id'];
  53. $type = Message::RECORD_TYPE;
  54. $content = $this->auth->name . '评论了您提交的跟进记录!';
  55. $content2 = $this->auth->name . '评论了您关注的跟进记录!';
  56. } elseif ($relation_type ==$commentModel::EVENT_TYPE) {
  57. $record = Event::where(['id' => $record_id])->find();
  58. $type = Message::EVENT_TYPE;
  59. $content = $this->auth->name . '评论了您的日程!';
  60. $content2 = $this->auth->name . '评论了您的日程!';
  61. } elseif ($relation_type ==$commentModel::ASSIST_EVENT_TYPE) {
  62. $record = Event::where(['id' => $record_id])->find();
  63. $type = Message::ASSIST_EVENT_TYPE;
  64. $content = $this->auth->name . '评论了您的协助任务!';
  65. $content2 = $this->auth->name . '评论了您的协助任务!';
  66. } elseif($relation_type ==$commentModel::ARTICLE_TYPE) {
  67. Aftermarket::where(['id'=>$record_id])->setInc('comment_num');
  68. $record = Aftermarket::where(['id' => $record_id])->find();
  69. $type = Message::ARTICLE_TYPE;
  70. $content = $this->auth->name . '增加了文章评论!';
  71. $content2 = $this->auth->name . '增加了文章评论!';
  72. }elseif($relation_type ==$commentModel::WORKREPORT_TYPE) {
  73. $record = WorkReport::where(['id' => $record_id])->find();
  74. $type = Message::WORKREPORT_TYPE;
  75. $content = $this->auth->name . '评论了您的工作报告!';
  76. $content2 = $this->auth->name . '评论了您关注的工作报告!';
  77. $record['create_staff_id']=$record['staff_id'];
  78. }elseif($relation_type ==$commentModel::WORKORDER_TYPE) {
  79. $record = \addons\qingdongams\model\Workorder::where(['id' => $record_id])->find();
  80. $type = Message::WORKORDER_TYPE;
  81. $content = $this->auth->name . '评论了您的工单!';
  82. $content2 = $this->auth->name . '评论了您关注的工单!';
  83. }elseif($relation_type ==$commentModel::DISCUSS_TYPE) {
  84. $record = \addons\qingdongams\model\Discuss::where(['id' => $record_id])->find();
  85. $type = Message::DISCUSS_TYPE;
  86. $content = $this->auth->name . '评论了您的讨论!';
  87. $content2 = $this->auth->name . '评论了您关注的讨论!';
  88. }
  89. if(empty($record)){
  90. $this->error('记录不存在');
  91. }
  92. if ($relation_type ==$commentModel::ASSIST_EVENT_TYPE) {
  93. Message::addMessage($type, $record_id, $record['create_staff_id'], $this->auth->id, $content);
  94. Message::addMessage($type, $record_id, $record['staff_id'], $this->auth->id, $content2);
  95. }else{
  96. Message::addMessage($type, $record_id, $record['create_staff_id'], $this->auth->id, $content);
  97. $staff_ids = $commentModel->where(['relation_type' => $relation_type, 'relation_id' => $record_id])->group('staff_id')->column('staff_id');
  98. foreach ($staff_ids as $staff_id) {
  99. //发送通知
  100. if ($staff_id != $this->auth->id) {
  101. Message::addMessage($type, $record_id, $staff_id, $this->auth->id, $content2);
  102. }
  103. }
  104. }
  105. Db::commit();
  106. } catch (Exception $e) {
  107. Db::rollback();
  108. $this->error($e->getMessage());
  109. }
  110. $this->success('评论成功');
  111. }
  112. //评论列表
  113. public function commentList()
  114. {
  115. $record_id = input('record_id');
  116. $relation_type = input('relation_type', 'sign');
  117. $comments = CommentModel::where([
  118. 'relation_type' => $relation_type,
  119. 'relation_id' => $record_id,
  120. 'status' => 1
  121. ])->field('id,staff_id,content,createtime')->order('id desc')->with(['staff'])->select();
  122. $this->success('请求成功', $comments);
  123. }
  124. // 文章评论点赞
  125. public function goodComment(){
  126. $id = input('id');
  127. if(!$id){
  128. $this->error('评论id呢?');
  129. }
  130. $commentModel = new CommentModel();
  131. $row = $commentModel->get($id);
  132. if(!$row){
  133. $this->error('评论不存在');
  134. }
  135. if($row->setInc('praise')){
  136. $this->success('成功');
  137. }else{
  138. $this->error('失败');
  139. }
  140. }
  141. }