comment.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package yb
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hz_crm_api/utils"
  5. "time"
  6. )
  7. // 研报 用户留言表
  8. type Comment struct {
  9. CommentId uint64 `orm:"column(comment_id);pk;" description:"留言ID"`
  10. UserId uint64 `orm:"column(user_id)" description:"用户id"`
  11. AdminId uint64 `orm:"column(admin_id)" description:"发布留言回复的管理员ID"`
  12. ReportId int `orm:"column(report_id)" description:"报告ID"`
  13. ReportChapterId int `orm:"column(report_chapter_id)" description:"报告章节ID"`
  14. Content string `orm:"column(content)" description:"留言内容"`
  15. SysIsRead int8 `orm:"column(sys_is_read)" description:"管理员是否已读 0-未读,1-已读"`
  16. SysReadTime time.Time `orm:"column(sys_read_time)" description:"管理员已读时间"`
  17. SysReadAdminId int64 `orm:"column(sys_read_admin_id)" description:"已读的管理员id"`
  18. ReplyCommentId uint64 `orm:"column(reply_comment_id)" description:"回复的留言ID"`
  19. IsTop int8 `orm:"column(is_top)" description:"是否置顶(0-未置顶,1-置顶)"`
  20. IsHot int8 `orm:"column(is_hot)" description:"是否设置精选(0-未设置,1-已设置)"`
  21. HotTopTime time.Time `orm:"column(hot_top_time)" description:"设置精选或者设置置顶的时间"`
  22. TopTime time.Time `orm:"column(top_time)" description:"设置置顶的时间"`
  23. HotTime time.Time `orm:"column(hot_time)" description:"设置精选的时间"`
  24. Type int8 `orm:"column(type)" description:"留言类型 1-评论 2-回复"`
  25. Enabled int8 `orm:"column(enabled)" description:"是否有效, 0-无效留言 1-有效留言"`
  26. IsShowName int8 `orm:"column(is_show_name)" description:"是否匿名 0-匿名,1-不匿名"`
  27. SourceAgent int `orm:"column(source_agent)" description:"留言入口来源,1:小程序,2:小程序pc, 4:web pc"`
  28. CreateTime time.Time `orm:"column(create_time)" description:"创建时间"`
  29. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  30. OldReportId int `orm:"column(old_report_id)" description:"老报告ID"`
  31. OldReportChapterId int `orm:"column(old_report_chapter_id)" description:"老报告章节ID"`
  32. }
  33. type CommentUser struct {
  34. Comment
  35. NickName string `description:"昵称"`
  36. Headimgurl string `description:"用户头像"`
  37. }
  38. func (yc *Comment) TableName() string {
  39. return "yb_comment"
  40. }
  41. // CommentIdList 评论id列表
  42. type CommentIdList struct {
  43. CommentId int
  44. }
  45. // GetCommentReportIdsByCondition 根据条件查询相关的 report
  46. func GetCommentReportIdsByCondition(condition string, pars []interface{}) (list []*Comment, err error) {
  47. o := orm.NewOrm()
  48. var commentIdList []*CommentIdList
  49. //groupSql := `SELECT * FROM (`
  50. //查找最大评论的id
  51. sql := `SELECT MAX(c.comment_id) comment_id FROM yb_comment c
  52. LEFT JOIN wx_user u ON c.user_id = u.user_id
  53. WHERE 1=1 and report_id > 0 and c.type = 1 `
  54. if condition != "" {
  55. sql += condition
  56. }
  57. sql += ` GROUP BY c.report_id, c.report_chapter_id `
  58. _, err = o.Raw(sql, pars).QueryRows(&commentIdList)
  59. if err != nil {
  60. return
  61. }
  62. commentIdArr := make([]int, 0)
  63. for _, v := range commentIdList {
  64. commentIdArr = append(commentIdArr, v.CommentId)
  65. }
  66. lenCommentIdArr := len(commentIdArr)
  67. if lenCommentIdArr <= 0 {
  68. return
  69. }
  70. groupSql := `SELECT c.report_id, c.report_chapter_id FROM yb_comment AS c
  71. WHERE comment_id in (` + utils.GetOrmInReplace(lenCommentIdArr) + `) ORDER BY c.sys_is_read asc,c.comment_id desc `
  72. _, err = o.Raw(groupSql, commentIdArr).QueryRows(&list)
  73. return
  74. }
  75. // GetCommentReportIdsByConditionBak 根据条件查询相关的 report
  76. func GetCommentReportIdsByConditionBak(condition string, pars []interface{}) (list []*Comment, err error) {
  77. o := orm.NewOrm()
  78. sql := `SELECT DISTINCT c.report_id, c.report_chapter_id
  79. FROM
  80. yb_comment c
  81. LEFT JOIN wx_user u ON c.user_id = u.user_id
  82. WHERE 1=1 and report_id > 0`
  83. if condition != "" {
  84. sql += condition
  85. }
  86. sql += ` ORDER BY c.sys_is_read asc,c.create_time desc`
  87. _, err = o.Raw(sql, pars).QueryRows(&list)
  88. return
  89. }
  90. // GetCommentIdsByCondition 根据条件查询相关的 留言ID
  91. func GetCommentIdsByCondition(condition string, pars []interface{}) (list []*Comment, err error) {
  92. o := orm.NewOrm()
  93. sql := `SELECT comment_id
  94. FROM
  95. yb_comment c
  96. LEFT JOIN wx_user u ON c.user_id = u.user_id
  97. WHERE 1=1 `
  98. if condition != "" {
  99. sql += condition
  100. }
  101. _, err = o.Raw(sql, pars).QueryRows(&list)
  102. return
  103. }
  104. type ReportCommentNum struct {
  105. ReportId int `description:"报告Id"`
  106. ReportChapterId int `description:"报告章节ID"`
  107. SysIsRead int8 `description:"管理员是否已读 0-未读,1-已读"`
  108. Num int `description:"汇总数"`
  109. }
  110. // StatReportCommentNum 统计报告的留言数
  111. func StatReportCommentNum(reportIds, reportChapterIds string, condition string, pars []interface{}) (list []*ReportCommentNum, err error) {
  112. o := orm.NewOrm()
  113. sql := `SELECT c.report_id, c.report_chapter_id, c.sys_is_read, COUNT(c.comment_id) as num
  114. FROM
  115. yb_comment c
  116. LEFT JOIN wx_user u ON c.user_id = u.user_id
  117. WHERE type=1 `
  118. if reportIds != "" && reportChapterIds != "" {
  119. sql += ` and (c.report_id in (` + reportIds + `) or c.report_chapter_id in (` + reportChapterIds + `) )`
  120. } else if reportIds != "" {
  121. sql += ` and c.report_id in (` + reportIds + `)`
  122. } else if reportChapterIds != "" {
  123. sql += ` and c.report_chapter_id in (` + reportChapterIds + `)`
  124. } else {
  125. return
  126. }
  127. if condition != "" {
  128. sql += condition
  129. }
  130. sql += ` GROUP BY report_id,report_chapter_id,sys_is_read`
  131. _, err = o.Raw(sql, pars).QueryRows(&list)
  132. return
  133. }
  134. // GetCommentListByByCondition 根据报告ID,查询留言列表
  135. func GetCommentListByByCondition(condition string, pars []interface{}, orderStr string, startSize, pageSize int) (list []*Comment, err error) {
  136. o := orm.NewOrm()
  137. sql := `SELECT c.*
  138. FROM
  139. yb_comment c
  140. LEFT JOIN wx_user u ON c.user_id = u.user_id
  141. WHERE 1=1 and c.type=1`
  142. if condition != "" {
  143. sql += condition
  144. }
  145. if orderStr != "" {
  146. sql += orderStr
  147. } else {
  148. sql += " ORDER BY create_time DESC , comment_id DESC"
  149. }
  150. sql += ` LIMIT ?,?`
  151. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  152. return
  153. }
  154. // GetCommentListTotalByByCondition 根据报告ID,查询留言列表总数
  155. func GetCommentListTotalByByCondition(condition string, pars []interface{}) (total int64, err error) {
  156. o := orm.NewOrm()
  157. sql := `select count(*) from (SELECT c.comment_id
  158. FROM
  159. yb_comment c
  160. LEFT JOIN wx_user u ON c.user_id = u.user_id
  161. WHERE 1=1 and c.type=1`
  162. if condition != "" {
  163. sql += condition
  164. }
  165. sql += ") f"
  166. err = o.Raw(sql, pars).QueryRow(&total)
  167. return
  168. }
  169. // GetReplyListByReplyCommentIds 获取报告的精选留言的回复列表
  170. func GetReplyListByReplyCommentIds(reportId, reportChapterId int, replyCommentIds string) (list []*Comment, err error) {
  171. o := orm.NewOrm()
  172. sql := `SELECT *
  173. FROM
  174. yb_comment
  175. WHERE report_id =? and report_chapter_id=? and type=2 and reply_comment_id in` + replyCommentIds
  176. sql += ` ORDER BY comment_id ASC`
  177. _, err = o.Raw(sql, reportId, reportChapterId).QueryRows(&list)
  178. return
  179. }
  180. // GetReplyByReplyCommentIds 获取报告的精选留言的回复列表
  181. func GetReplyByReplyCommentIds(replyCommentIds string) (list []*Comment, err error) {
  182. o := orm.NewOrm()
  183. sql := `SELECT *
  184. FROM
  185. yb_comment
  186. WHERE type=2 and reply_comment_id in` + replyCommentIds
  187. sql += ` ORDER BY comment_id ASC`
  188. _, err = o.Raw(sql).QueryRows(&list)
  189. return
  190. }
  191. // UpdateCommentSysIsReadByCommentIds 设置报告相关的留言为已读状态
  192. func UpdateCommentSysIsReadByCommentIds(adminId int, commentIds string) (err error) {
  193. o := orm.NewOrm()
  194. sql := `UPDATE yb_comment SET sys_is_read=1, sys_read_admin_id=?, sys_read_time = NOW(), modify_time=NOW() WHERE sys_is_read=0 and comment_id in (` + commentIds + `)`
  195. _, err = o.Raw(sql, adminId).Exec()
  196. return
  197. }
  198. // UpdateCommentSysIsRead 设置所有留言为已读状态
  199. func UpdateCommentSysIsRead(adminId int) (err error) {
  200. o := orm.NewOrm()
  201. sql := `UPDATE yb_comment SET sys_is_read=1, sys_read_admin_id=?, sys_read_time = NOW(), modify_time=NOW() WHERE sys_is_read=0`
  202. _, err = o.Raw(sql, adminId).Exec()
  203. return
  204. }
  205. // AddComment 新增回复
  206. func AddComment(item *Comment) (id int64, err error) {
  207. o := orm.NewOrm()
  208. id, err = o.Insert(item)
  209. return
  210. }
  211. // DeleteComment 标记处理用户所有待处理申请记录
  212. func DeleteComment(commentId int) (err error) {
  213. o := orm.NewOrm()
  214. to, err := o.Begin()
  215. if err != nil {
  216. return
  217. }
  218. defer func() {
  219. if err != nil {
  220. _ = to.Rollback()
  221. } else {
  222. _ = to.Commit()
  223. }
  224. }()
  225. //删除评论
  226. sql := "delete from yb_comment where comment_id=? and type=1"
  227. _, err = to.Raw(sql, commentId).Exec()
  228. if err != nil {
  229. return
  230. }
  231. //删除回复
  232. sql = "delete from yb_comment where reply_comment_id=? and type=2"
  233. _, err = to.Raw(sql, commentId).Exec()
  234. return
  235. }
  236. // Update 更新留言状态
  237. func (c *Comment) Update(cols []string) (err error) {
  238. o := orm.NewOrm()
  239. _, err = o.Update(c, cols...)
  240. return
  241. }
  242. // DeleteCommentReply 删除管理员回复
  243. func DeleteCommentReply(commentId int) (err error) {
  244. sql := `DELETE FROM yb_comment WHERE comment_id=? and type=2 `
  245. o := orm.NewOrm()
  246. _, err = o.Raw(sql, commentId).Exec()
  247. return
  248. }
  249. // GetCommentByCommentId 根据留言ID,查询留言详情
  250. func GetCommentByCommentId(commentId int) (item *Comment, err error) {
  251. sql := `SELECT
  252. *
  253. FROM
  254. yb_comment
  255. where comment_id=?`
  256. err = orm.NewOrm().Raw(sql, commentId).QueryRow(&item)
  257. return
  258. }
  259. // GetCommentUserByCommentId 根据留言ID,查询留言详情
  260. func GetCommentUserByCommentId(commentId int) (item *CommentUser, err error) {
  261. sql := `SELECT
  262. y.*,u.nick_name,u.headimgurl
  263. FROM
  264. yb_comment y left join wx_user u on y.user_id=u.user_id
  265. where y.comment_id=?`
  266. err = orm.NewOrm().Raw(sql, commentId).QueryRow(&item)
  267. return
  268. }
  269. // GetReplyTotalByCommentId 查询该留言的回复次数
  270. func GetReplyTotalByCommentId(commentId int) (total int64, err error) {
  271. o := orm.NewOrm()
  272. sql := `SELECT count(*)
  273. FROM
  274. yb_comment
  275. WHERE reply_comment_id=? and type=2`
  276. err = o.Raw(sql, commentId).QueryRow(&total)
  277. return
  278. }
  279. // GetReplyByReplyCommentId 查询该留言的回复次数
  280. func GetReplyByReplyCommentId(commentId uint64) (list []*Comment, err error) {
  281. o := orm.NewOrm()
  282. sql := `SELECT *
  283. FROM
  284. yb_comment
  285. WHERE reply_comment_id=? and type=2 ORDER BY comment_id ASC`
  286. _, err = o.Raw(sql, commentId).QueryRows(&list)
  287. return
  288. }
  289. type ProductCommentItem struct {
  290. CommentId int `description:"研报留言ID或者问答社区评论ID"`
  291. UserId int `description:"用户ID"`
  292. ReportId int `description:"报告Id"`
  293. ReportChapterId int `description:"报告章节Id"`
  294. OldReportId int `description:"老报告Id"`
  295. CommunityQuestionId int `description:"问答ID"`
  296. Content string `description:"留言内容"`
  297. CreateTime time.Time `description:"创建时间"`
  298. ProductType int `description:"留言板块ID: 1-研报留言,2-问答社区评论"`
  299. IsTop int8 `description:"是否置顶(0-未置顶,1-置顶)"`
  300. RealName string `description:"用户姓名"`
  301. }
  302. // GetProductCommentList 获取研报留言和问答社区评论列表
  303. func GetProductCommentList(condition string, pars []interface{}, startSize, pageSize int) (list []*ProductCommentItem, total int64, err error) {
  304. o := orm.NewOrm()
  305. sql := `SELECT * from (( SELECT
  306. comment_id,
  307. user_id,
  308. report_id,
  309. report_chapter_id,
  310. 0 AS community_question_id,
  311. content,
  312. create_time,
  313. 1 AS product_type,
  314. is_top,
  315. real_name,
  316. old_report_id
  317. FROM
  318. yb_comment
  319. WHERE
  320. type = 1)
  321. UNION
  322. (
  323. SELECT
  324. community_question_comment_id AS comment_id,
  325. user_id,
  326. 0 AS report_id,
  327. 0 AS report_chapter_id,
  328. community_question_id,
  329. content,
  330. create_time,
  331. source+1 AS product_type,
  332. 0 as is_top,
  333. real_name,
  334. 0 as old_report_id
  335. FROM
  336. yb_community_question_comment
  337. WHERE
  338. type = 1 and enabled=1
  339. )
  340. ) as t
  341. where 1=1`
  342. if condition != "" {
  343. sql += condition
  344. }
  345. //汇总数据
  346. totalSQl := `SELECT COUNT(1) total FROM (` + sql + `) as a`
  347. if err = o.Raw(totalSQl, pars).QueryRow(&total); err != nil {
  348. return
  349. }
  350. sql += ` ORDER BY t.create_time DESC,t.comment_id DESC`
  351. sql += ` LIMIT ?,?`
  352. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  353. return
  354. }