news.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package message
  2. import (
  3. "spaceofcheng/services/models"
  4. "spaceofcheng/services/models/model"
  5. "spaceofcheng/services/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/go-xorm/builder"
  10. )
  11. // CmsNewsInfo 咨询信息
  12. type CmsNewsInfo struct {
  13. model.TaCmsNews `xorm:"extends"`
  14. LocationNames string
  15. LocationIds string
  16. }
  17. // GetNewsList 获取咨询列表
  18. func (m *MessageDAO) GetNewsList(locationid, title, orgid string, page int, pageSize int) ([]CmsNewsInfo, error) {
  19. var news []CmsNewsInfo
  20. sql := `select * from ta_cms_news new left join (
  21. select b.news_id,GROUP_CONCAT(a.location_name) location_names,GROUP_CONCAT(a.location_id) location_ids
  22. from td_cms_image_location a inner join ta_cms_location b on a.location_id=b.location_id and a.status>` + strconv.Itoa(models.STATUS_DEL) +
  23. ` and a.org_id='` + orgid + `' group by b.news_id
  24. ) c on new.news_id = c.news_id WHERE new.status>` + strconv.Itoa(models.STATUS_DEL) + ` and new.org_id='` + orgid + `'`
  25. if locationid != "" {
  26. sql += ` and new.news_id in (select news_id from ta_cms_location where location_id='` + locationid + `')`
  27. }
  28. if title != "" {
  29. sql += ` and new.title like '%` + title + `%'`
  30. }
  31. sql = sql + " order by new.create_date desc limit " + strconv.Itoa((page-1)*pageSize) + ", " + strconv.Itoa(pageSize)
  32. err := m.db.Sql(sql).Find(&news)
  33. return news, err
  34. }
  35. // GetNewsListCount 获取咨询列表count
  36. func (m *MessageDAO) GetNewsListCount(locationid, title, orgid string) (int, error) {
  37. var news []model.TaCmsNews
  38. dao := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).And("org_id=?", orgid)
  39. if locationid != "" {
  40. dao.In("news_id", builder.Select("news_id").From("ta_cms_location").Where(builder.Eq{"location_id": locationid}))
  41. }
  42. if title != "" {
  43. dao.And("title like '%" + title + "%'")
  44. }
  45. err := dao.Find(&news)
  46. if err != nil {
  47. return 0, nil
  48. }
  49. return len(news), nil
  50. }
  51. // GetNewsByLocation 根据位置获取咨询信息
  52. func (m *MessageDAO) GetNewsByLocation(locationid, orgid string) ([]model.TaCmsNews, error) {
  53. var news []model.TaCmsNews
  54. err := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).
  55. And("org_id=?", orgid).
  56. In("news_id", builder.Select("news_id").From("ta_cms_location").Where(builder.Eq{"location_id": locationid})).
  57. Desc("create_date").
  58. Find(&news)
  59. return news, err
  60. }
  61. // GetNewsByLocationShow 根据位置获取咨询信息
  62. func (m *MessageDAO) GetNewsByLocationShow(locationid, orgid string) ([]model.TaCmsNews, error) {
  63. var news []model.TaCmsNews
  64. // 2018年10月9日 yansen 默认限制 20 条
  65. err := m.db.Where("status = ?", models.STATUS_NORMAL).
  66. And("org_id=?", orgid).
  67. In("news_id", builder.Select("news_id").From("ta_cms_location").Where(builder.Eq{"location_id": locationid})).
  68. Desc("public_date").
  69. Limit(20).
  70. Find(&news)
  71. return news, err
  72. }
  73. // CmsNews 咨询详情
  74. type CmsNews struct {
  75. model.TaCmsNews `xorm:"extends"`
  76. NewsLocations []model.TaCmsLocation
  77. }
  78. // GetNewsByID 获取咨询详情
  79. func (m *MessageDAO) GetNewsByID(newid string) (*CmsNews, error) {
  80. var news []CmsNews
  81. sql := `select * from ta_cms_news where news_id='` + newid + `' and status>` + strconv.Itoa(models.STATUS_DEL)
  82. err := m.db.Sql(sql).Find(&news)
  83. if err != nil {
  84. return nil, err
  85. }
  86. if len(news) > 0 {
  87. return &news[0], err
  88. }
  89. return nil, nil
  90. }
  91. // GetNewsLocationByID 根据咨询id获取咨询位置
  92. func (m *MessageDAO) GetNewsLocationByID(newsid string) ([]model.TaCmsLocation, error) {
  93. var newslocation []model.TaCmsLocation
  94. err := m.db.Where("news_id=?", newsid).Find(&newslocation)
  95. return newslocation, err
  96. }
  97. // AddNews 新增咨询详细
  98. func (m *MessageDAO) AddNews(news model.TaCmsNews) (*model.TaCmsNews, error) {
  99. news.NewsId = utils.GetGUID()
  100. news.CreateDate = time.Now()
  101. user := m.ctx.Get("user").(model.SysUser)
  102. news.CreateUser = user.UserId
  103. _, err := m.db.Insert(news)
  104. return &news, err
  105. }
  106. // DelNewsLocation 删除咨询与位置映射
  107. func (m *MessageDAO) DelNewsLocation(newsid string) error {
  108. sql := "delete from ta_cms_location where news_id='" + newsid + "'"
  109. _, err := m.db.Exec(sql)
  110. return err
  111. }
  112. // SaveNewsLocation 保存咨询与位置映射
  113. func (m *MessageDAO) SaveNewsLocation(newsid, locationsid string) error {
  114. sql := `insert into ta_cms_location(news_id,location_id) select '` + newsid + `', location_id from td_cms_image_location where location_id in ('` + strings.Replace(locationsid, ",", "','", -1) + `') and status > ` + strconv.Itoa(models.STATUS_DEL)
  115. _, err := m.db.Exec(sql)
  116. return err
  117. }
  118. // UpdateNews 编辑咨询详情
  119. func (m *MessageDAO) UpdateNews(news model.TaCmsNews) error {
  120. var col = []string{
  121. "image_url",
  122. "forward_type",
  123. "forward_url",
  124. "forward_course_id",
  125. "status",
  126. "title",
  127. "public_date",
  128. }
  129. _, err := m.db.Cols(col...).Where("news_id=?", news.NewsId).Update(news)
  130. return err
  131. }
  132. // DelNews 删除咨询
  133. func (m *MessageDAO) DelNews(newsid string) error {
  134. var news = model.TaCmsNews{}
  135. news.Status = models.STATUS_DEL
  136. news.NewsId = newsid
  137. var cols = []string{
  138. "status",
  139. }
  140. _, err := m.db.Cols(cols...).Where("news_id=?", news.NewsId).Update(news)
  141. return err
  142. }