Comment.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use traits\model\SoftDelete;
  7. use function EasyWeChat\Kernel\Support\get_client_ip;
  8. /**
  9. *评论表
  10. */
  11. class Comment Extends Model {
  12. use SoftDelete;
  13. // 表名,不含前缀
  14. const SIGN_TYPE = 'sign';//签到
  15. const CUSTOMER_TYPE = 'customer';//客户
  16. const EVENT_TYPE = 'event';//日程
  17. const ASSIST_EVENT_TYPE = 'assist_event';//日程
  18. const ARTICLE_TYPE = 'article';// 文章评论
  19. const WORKORDER_TYPE = 'workorder';// 工单评论
  20. const DISCUSS_TYPE = 'discuss';// 工单评论
  21. const WORKREPORT_TYPE = 'workreport';// 工单评论
  22. const DAILY_TYPE = 5;//工作报告
  23. // 表名,不含前缀
  24. protected $name = 'qingdongams_comment';
  25. // 开启自动写入时间戳字段
  26. protected $autoWriteTimestamp = 'int';
  27. // 定义时间戳字段名
  28. protected $createTime = 'createtime';
  29. protected $updateTime = 'updatetime';
  30. protected $deleteTime = 'deletetime';
  31. public function staff() {
  32. return $this->belongsTo(Staff::class, 'staff_id', 'id')->field('id,name,img,post');
  33. }
  34. public function getFileIdsAttr($value) {
  35. $files = explode(',', $value);
  36. $result = [];
  37. foreach ($files as $fid) {
  38. if ($fid) {
  39. $result[] = cdnurl(File::getUrl($fid), true);
  40. }
  41. }
  42. return $result;
  43. }
  44. public function getCreatetimeAttr($value) {
  45. return date('Y-m-d H:i', $value);
  46. }
  47. //跟进
  48. public function record() {
  49. return $this->hasOne(Record::class, 'id', 'relation_id')->field('id,content');
  50. }
  51. //工作报告
  52. public function daily() {
  53. return $this->hasOne(Daily::class, 'id', 'relation_id')->field('id,type');
  54. }
  55. //添加评论
  56. public static function addComment($content,$files,$record_id,$relation_type,$staff_id)
  57. {
  58. $data = [
  59. 'relation_type' => $relation_type,
  60. 'relation_id' => $record_id,
  61. 'staff_id' => $staff_id,
  62. 'content' => $content,
  63. 'file_ids' => $files,
  64. 'status' => 1,
  65. 'ip' => get_client_ip(),
  66. ];
  67. $staff = Staff::info();
  68. $commentModel = new self();
  69. Db::startTrans();
  70. try {
  71. $commentModel->save($data);
  72. if ($relation_type ==$commentModel::SIGN_TYPE) {
  73. $record = StaffSignIn::where(['id' => $record_id])->find();
  74. $record->create_staff_id=$record['staff_id'];
  75. $type = Message::RECORD_TYPE;
  76. $content = $staff->name . '评论了您提交的跟进记录!';
  77. $content2 = $staff->name . '评论了您关注的跟进记录!';
  78. } elseif ($relation_type ==$commentModel::EVENT_TYPE) {
  79. $record = Event::where(['id' => $record_id])->find();
  80. $type = Message::EVENT_TYPE;
  81. $content = $staff->name . '评论了您的日程!';
  82. $content2 = $staff->name . '评论了您的日程!';
  83. } elseif ($relation_type ==$commentModel::ASSIST_EVENT_TYPE) {
  84. $record = Event::where(['id' => $record_id])->find();
  85. $type = Message::ASSIST_EVENT_TYPE;
  86. $content = $staff->name . '评论了您的协助任务!';
  87. $content2 = $staff->name . '评论了您的协助任务!';
  88. } elseif($relation_type ==$commentModel::ARTICLE_TYPE) {
  89. Aftermarket::where(['id'=>$record_id])->setInc('comment_num');
  90. $record = Aftermarket::where(['id' => $record_id])->find();
  91. $type = Message::ARTICLE_TYPE;
  92. $content = $staff->name . '增加了文章评论!';
  93. $content2 = $staff->name . '增加了文章评论!';
  94. }elseif($relation_type ==$commentModel::WORKREPORT_TYPE) {
  95. $record = WorkReport::where(['id' => $record_id])->find();
  96. $type = Message::WORKREPORT_TYPE;
  97. $content = $staff->name . '评论了您的工作报告!';
  98. $content2 = $staff->name . '评论了您关注的工作报告!';
  99. $record['create_staff_id']=$record['staff_id'];
  100. }elseif($relation_type ==$commentModel::WORKORDER_TYPE) {
  101. $record = \addons\qingdongams\model\Workorder::where(['id' => $record_id])->find();
  102. $type = Message::WORKORDER_TYPE;
  103. $content = $staff->name . '评论了您的工单!';
  104. $content2 = $staff->name . '评论了您关注的工单!';
  105. }elseif($relation_type ==$commentModel::DISCUSS_TYPE) {
  106. $record = \addons\qingdongams\model\Discuss::where(['id' => $record_id])->find();
  107. $type = Message::DISCUSS_TYPE;
  108. $content = $staff->name . '评论了您的讨论!';
  109. $content2 = $staff->name . '评论了您关注的讨论!';
  110. }
  111. if(empty($record)){
  112. throw new Exception('记录不存在');
  113. }
  114. if ($relation_type ==$commentModel::ASSIST_EVENT_TYPE) {
  115. Message::addMessage($type, $record_id, $record['create_staff_id'], $staff->id, $content);
  116. Message::addMessage($type, $record_id, $record['staff_id'], $staff->id, $content2);
  117. }else{
  118. Message::addMessage($type, $record_id, $record['create_staff_id'], $staff->id, $content);
  119. $staff_ids = $commentModel->where(['relation_type' => $relation_type, 'relation_id' => $record_id])->group('staff_id')->column('staff_id');
  120. foreach ($staff_ids as $staff_id) {
  121. //发送通知
  122. if ($staff_id != $staff->id) {
  123. Message::addMessage($type, $record_id, $staff_id, $staff->id, $content2);
  124. }
  125. }
  126. }
  127. Db::commit();
  128. } catch (Exception $e) {
  129. Db::rollback();
  130. throw new Exception($e->getMessage());
  131. }
  132. return true;
  133. }
  134. }