bulletin.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "go-common/app/common/openplatform/random"
  6. "go-common/app/service/openplatform/ticket-item/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. item "go-common/app/service/openplatform/ticket-item/api/grpc/v1"
  10. )
  11. // BulletinMainInfo 公告版本内容
  12. type BulletinMainInfo struct {
  13. Name string `json:"name"`
  14. Introduction string `json:"introduction"`
  15. Content string `json:"content"`
  16. ProjectName string `json:"project_name"`
  17. Pid int64 `json:"pid"`
  18. }
  19. // GetBulletins 获取项目下所有公告
  20. func (d *Dao) GetBulletins(c context.Context, pid int64) (res []*item.BulletinInfo, err error) {
  21. // 获取项目下所有bulletin基础信息
  22. var projectBulletins []*model.Bulletin
  23. if dbErr := d.db.Order("ctime desc").Where("project_id = ? and status = 1", pid).Find(&projectBulletins).Error; dbErr != nil {
  24. log.Error("获取项目公告基础信息失败: %s", dbErr)
  25. return nil, ecode.NothingFound
  26. }
  27. var bulIDs []int64
  28. for _, value := range projectBulletins {
  29. bulIDs = append(bulIDs, value.ID)
  30. }
  31. // 获取公告详情并用id当键值组成map
  32. var bulletinDetail []*model.BulletinExtra
  33. if dbErr := d.db.Where("id in (?)", bulIDs).Find(&bulletinDetail).Error; dbErr != nil {
  34. log.Error("获取项目公告详情失败: %s", dbErr)
  35. return nil, ecode.NothingFound
  36. }
  37. bulletinDetailMap := make(map[int64]*model.BulletinExtra)
  38. for _, value := range bulletinDetail {
  39. bulletinDetailMap[value.ID] = value
  40. }
  41. // 添加详情信息到公告信息中
  42. for _, value := range projectBulletins {
  43. tmpBul := &item.BulletinInfo{
  44. ID: value.ID,
  45. Title: value.Title,
  46. Content: value.Content,
  47. Ctime: value.Ctime.Time().Format("2006-01-02 15:04:05"),
  48. Mtime: value.Mtime.Time().Format("2006-01-02 15:04:05"),
  49. VerID: value.VerID,
  50. }
  51. detailInfo, ok := bulletinDetailMap[value.ID]
  52. if ok {
  53. tmpBul.Detail = detailInfo.Detail
  54. } else {
  55. tmpBul.Detail = ""
  56. }
  57. res = append(res, tmpBul)
  58. }
  59. return
  60. }
  61. // AddBulletin 添加公告
  62. func (d *Dao) AddBulletin(c context.Context, info *item.BulletinInfoRequest) (bool, error) {
  63. pid := info.ParentID
  64. mainInfo, jsonErr := d.GenBulMainInfo(pid, info.Title, info.Content, info.Detail)
  65. if jsonErr != nil {
  66. log.Error("获取整合maininfo失败: %s", jsonErr)
  67. return false, ecode.NothingFound
  68. }
  69. // add version and version ext
  70. verErr := d.AddVersion(c, nil, &model.Version{
  71. Type: model.VerTypeBulletin,
  72. Status: 1, // 审核中
  73. ItemName: info.Title,
  74. ParentID: pid,
  75. TargetItem: info.TargetItem,
  76. AutoPub: 1, // 自动上架
  77. PubStart: model.TimeNull,
  78. PubEnd: model.TimeNull,
  79. }, &model.VersionExt{
  80. Type: model.VerTypeBulletin,
  81. MainInfo: string(mainInfo),
  82. })
  83. if verErr != nil {
  84. log.Error("创建公告版本失败: %s", verErr)
  85. return false, ecode.NothingFound
  86. }
  87. return true, nil
  88. }
  89. // PassBulletin 审核通过公告
  90. func (d *Dao) PassBulletin(c context.Context, verID uint64) (bool, error) {
  91. verInfo, verExtInfo, err := d.GetVersion(c, verID, true)
  92. if err != nil {
  93. return false, ecode.NothingFound
  94. }
  95. targetItem := verInfo.TargetItem
  96. var decodedMainInfo BulletinMainInfo
  97. err = json.Unmarshal([]byte(verExtInfo.MainInfo), &decodedMainInfo)
  98. if err != nil {
  99. return false, err
  100. }
  101. var finalTargetItem int64
  102. // 开启事务
  103. tx := d.db.Begin()
  104. if targetItem == 0 {
  105. // 没新建过bulletin信息
  106. bulletinID := random.Uniqid(19)
  107. bulData := &model.Bulletin{
  108. Status: 1,
  109. Title: decodedMainInfo.Name,
  110. Content: decodedMainInfo.Introduction,
  111. ProjectID: decodedMainInfo.Pid,
  112. VerID: verID,
  113. BulletinID: bulletinID,
  114. }
  115. insertErr := tx.Save(&bulData).Error
  116. if insertErr != nil {
  117. tx.Rollback()
  118. log.Error("新建bulletin失败: %s", insertErr)
  119. return false, ecode.NotModified
  120. }
  121. // 获取新建的bulletin自增id
  122. bulPrimID := bulData.ID
  123. if bulPrimID == 0 {
  124. tx.Rollback()
  125. log.Error("获取新建bulletin自增id失败")
  126. return false, ecode.NothingFound
  127. }
  128. // 新建bulletin_extra
  129. insertExtErr := tx.Create(&model.BulletinExtra{
  130. ID: bulPrimID,
  131. Detail: decodedMainInfo.Content,
  132. BulletinID: bulletinID,
  133. }).Error
  134. if insertExtErr != nil {
  135. tx.Rollback()
  136. log.Error("新建bulletin_extra失败:%s", insertExtErr)
  137. return false, ecode.NotModified
  138. }
  139. finalTargetItem = bulPrimID
  140. } else {
  141. // 已建过的直接更新bulletin
  142. updateErr := tx.Where("id = ?", targetItem).Model(&model.Bulletin{}).Updates(
  143. map[string]interface{}{
  144. "status": 1,
  145. "title": decodedMainInfo.Name,
  146. "content": decodedMainInfo.Introduction,
  147. "ver_id": verID,
  148. }).Error
  149. if updateErr != nil {
  150. tx.Rollback()
  151. log.Error("UPDATE BULLETIN FAILED")
  152. return false, ecode.NotModified
  153. }
  154. // 更新bulletin_extra
  155. updateExtErr := tx.Where("id = ?", targetItem).Model(&model.BulletinExtra{}).Updates(
  156. map[string]interface{}{
  157. "detail": decodedMainInfo.Content,
  158. }).Error
  159. if updateExtErr != nil {
  160. tx.Rollback()
  161. log.Error("UPDATE BULLETIN_EXTRA FAILED")
  162. return false, ecode.NotModified
  163. }
  164. finalTargetItem = targetItem
  165. }
  166. // 更新版本为已审核状态
  167. updateVerErr := tx.Where("ver_id = ?", verID).Model(&model.Version{}).Updates(
  168. map[string]interface{}{
  169. "status": 4, // 已上架状态
  170. "ver": "1.0",
  171. "target_item": finalTargetItem,
  172. }).Error
  173. if updateVerErr != nil {
  174. tx.Rollback()
  175. log.Error("UPDATE BULLETIN VERSION FAILED")
  176. return false, ecode.NotModified
  177. }
  178. tx.Commit()
  179. return true, nil
  180. }
  181. // UpdateBulletin 编辑公告版本
  182. func (d *Dao) UpdateBulletin(c context.Context, info *item.BulletinInfoRequest) (bool, error) {
  183. // 获取JSONEncode好的maininfo
  184. pid := info.ParentID
  185. mainInfo, jsonErr := d.GenBulMainInfo(pid, info.Title, info.Content, info.Detail)
  186. if jsonErr != nil || mainInfo == "" {
  187. log.Error("获取maininfo失败: %s", jsonErr)
  188. return false, ecode.NothingFound
  189. }
  190. // 开启事务
  191. tx := d.db.Begin()
  192. // 编辑version_ext
  193. updateExtErr := tx.Model(&model.VersionExt{}).Where("ver_id = ? and type = ?", info.VerID, model.VerTypeBulletin).Update("main_info", mainInfo).Error
  194. if updateExtErr != nil {
  195. tx.Rollback()
  196. log.Error("更新versionext失败:%s", updateExtErr)
  197. return false, ecode.NotModified
  198. }
  199. // 编辑version
  200. updateErr := d.db.Model(&model.Version{}).Where("ver_id = ? and type = ?", info.VerID, model.VerTypeBulletin).Updates(
  201. map[string]interface{}{
  202. "status": 1, // 审核中
  203. "item_name": info.Title,
  204. }).Error
  205. if updateErr != nil {
  206. tx.Rollback()
  207. log.Error("VERSION UPDATE FAILED:%s", updateErr)
  208. return false, ecode.NotModified
  209. }
  210. tx.Commit()
  211. return true, nil
  212. }
  213. // GenBulMainInfo 整合公告的详情maininfo字段
  214. func (d *Dao) GenBulMainInfo(pid int64, name string, introduction string, content string) (string, error) {
  215. var projectInfo []model.Item
  216. if dbErr := d.db.Select("name").Where("id = ?", pid).Find(&projectInfo).Error; dbErr != nil {
  217. log.Error("获取项目信息失败:%s", dbErr)
  218. return "", ecode.NothingFound
  219. }
  220. extInfo := BulletinMainInfo{
  221. Name: name,
  222. Introduction: introduction,
  223. Content: content,
  224. ProjectName: projectInfo[0].Name,
  225. Pid: pid,
  226. }
  227. mainInfo, jsonErr := json.Marshal(extInfo)
  228. if jsonErr != nil {
  229. log.Error("JSONEncode失败: %s", jsonErr)
  230. return "", ecode.NothingFound
  231. }
  232. return string(mainInfo), nil
  233. }
  234. // UnpublishBulletin 下架版本
  235. func (d *Dao) UnpublishBulletin(c context.Context, verID uint64, status int8) (bool, error) {
  236. // 获取版本信息
  237. verInfo, _, err := d.GetVersion(c, verID, false)
  238. if err != nil {
  239. return false, ecode.NothingFound
  240. }
  241. bulletinID := verInfo.TargetItem
  242. // 开启事务
  243. tx := d.db.Begin()
  244. // 取消激活公告
  245. updateErr := tx.Model(&model.Bulletin{}).Where("id = ? and ver_id = ?", bulletinID, verID).Updates(
  246. map[string]interface{}{
  247. "status": 0,
  248. }).Error
  249. if updateErr != nil {
  250. tx.Rollback()
  251. log.Error("取消激活公告失败:%s", updateErr)
  252. return false, ecode.NotModified
  253. }
  254. // 更新版本为已下架状态
  255. updateVerErr := tx.Model(&model.Version{}).Where("ver_id = ? and type = ?", verID, model.VerTypeBulletin).Updates(
  256. map[string]interface{}{
  257. "status": status,
  258. }).Error
  259. if updateVerErr != nil {
  260. tx.Rollback()
  261. log.Error("版本更新状态失败:%s", updateVerErr)
  262. }
  263. tx.Commit()
  264. return true, nil
  265. }