Discuss.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Discuss as DiscussModel;
  4. use addons\qingdongams\model\Message;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  9. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  10. * @desc 售后微信:qingdong_crm
  11. */
  12. /**
  13. * 讨论
  14. */
  15. class Discuss extends StaffApi
  16. {
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = [];
  19. //添加讨论
  20. public function addDiscuss()
  21. {
  22. $title = input('title');
  23. $content = input('content');
  24. $file_ids = input('file_ids');
  25. $relation_id = input('relation_id');
  26. $relation_type = input('relation_type');
  27. $show_staff_id = input('show_staff_id');
  28. if (empty($content)) {
  29. $this->error('评论内容不能为空');
  30. }
  31. $data = [
  32. 'relation_type' => $relation_type,
  33. 'relation_id' => $relation_id,
  34. 'create_staff_id' => $this->auth->id,
  35. 'title' => $title,
  36. 'file_ids' => $file_ids,
  37. 'content' => $content,
  38. 'show_staff_id' => $show_staff_id,
  39. ];
  40. $discussModel = new DiscussModel();
  41. Db::startTrans();
  42. try {
  43. $discussModel->save($data);
  44. $lastid=$discussModel->getLastInsID();
  45. $content2 = $this->auth->name . '发起《'.$title.'》讨论,邀请您参加!';
  46. $show_staff_id=explode(',',$show_staff_id);
  47. foreach ($show_staff_id as $staff_id) {
  48. //发送通知
  49. if ($staff_id != $this->auth->id) {
  50. Message::addMessage(Message::DISCUSS_TYPE, $lastid, $staff_id, $this->auth->id, $content2);
  51. }
  52. }
  53. Db::commit();
  54. } catch (Exception $e) {
  55. Db::rollback();
  56. $this->error($e->getMessage());
  57. }
  58. $this->success('发起讨论成功');
  59. }
  60. //列表
  61. public function getList()
  62. {
  63. $relation_id = input('relation_id');
  64. $relation_type = input('relation_type');
  65. $comments = DiscussModel::where([
  66. 'relation_type' => $relation_type,
  67. 'relation_id' => $relation_id,
  68. ])->order('id desc')->with(['staff'])->paginate();
  69. $this->success('请求成功', $comments);
  70. }
  71. //详情
  72. public function getDetail()
  73. {
  74. $id = input('id');
  75. $discuss = DiscussModel::where([
  76. 'id' => $id,
  77. ])->with(['staff'])->find();
  78. if(empty($discuss)){
  79. $this->error('讨论信息不存在');
  80. }
  81. $discuss['is_edit']=0;
  82. if($discuss['create_staff_id'] == $this->auth->id){
  83. $discuss['is_edit']=1;
  84. }
  85. Message::setRead(Message::DISCUSS_TYPE,$id,$this->auth->id);
  86. $this->success('请求成功', $discuss);
  87. }
  88. //修改讨论
  89. public function editDiscuss()
  90. {
  91. $id = input('id');
  92. $title = input('title');
  93. $content = input('content');
  94. $file_ids = input('file_ids');
  95. $show_staff_id = input('show_staff_id');
  96. if (empty($content)) {
  97. $this->error('评论内容不能为空');
  98. }
  99. $row=DiscussModel::get($id);
  100. if(empty($row)){
  101. $this->error('参数错误');
  102. }
  103. $save=[];
  104. if($title){
  105. $save['title']=$title;
  106. }
  107. if($file_ids){
  108. $save['file_ids']=$file_ids;
  109. }
  110. if($content){
  111. $save['content']=$content;
  112. }
  113. if($show_staff_id){
  114. $save['show_staff_id']=$show_staff_id;
  115. }
  116. $data = [
  117. 'title' => $title,
  118. 'file_ids' => $file_ids,
  119. 'content' => $content,
  120. 'show_staff_id' => $show_staff_id,
  121. ];
  122. $discussModel = new DiscussModel();
  123. Db::startTrans();
  124. try {
  125. $discussModel->save($data,['id'=>$id]);
  126. $content2 = $this->auth->name . '发起《'.$title.'》讨论,邀请您参加!';
  127. $show_staff_id=explode(',',$show_staff_id);
  128. foreach ($show_staff_id as $staff_id) {
  129. //发送通知
  130. if ($staff_id != $this->auth->id) {
  131. Message::addMessage(Message::DISCUSS_TYPE, $id, $staff_id, $this->auth->id, $content2);
  132. }
  133. }
  134. Db::commit();
  135. } catch (Exception $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. }
  139. $this->success('修改讨论信息成功');
  140. }
  141. }