version.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "go-common/app/common/openplatform/random"
  7. "go-common/app/service/openplatform/ticket-item/model"
  8. "go-common/library/database/elastic"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. // AddVersion 添加新版本
  13. func (d *Dao) AddVersion(c context.Context, requiredTx *gorm.DB, verInfo *model.Version, verExtInfo *model.VersionExt) (err error) {
  14. // 此处ver_id调用订单号生成器
  15. verID := uint64(random.Uniqid(19))
  16. // 版本号赋值与信息
  17. verInfo.VerID = verID
  18. verExtInfo.VerID = verID
  19. var tx *gorm.DB
  20. if requiredTx == nil {
  21. // 开启事务
  22. tx = d.db.Begin()
  23. } else {
  24. tx = requiredTx
  25. }
  26. // 插入新数据
  27. if verError := tx.Create(&verInfo).Error; verError != nil {
  28. log.Error("version insertion failed:%s", verError)
  29. tx.Rollback()
  30. return ecode.TicketAddVersionFailed
  31. }
  32. if extError := tx.Create(&verExtInfo).Error; extError != nil {
  33. log.Error("version ext insertion failed:%s", extError)
  34. tx.Rollback()
  35. return ecode.TicketAddVerExtFailed
  36. }
  37. if requiredTx == nil {
  38. tx.Commit()
  39. }
  40. return nil
  41. }
  42. // UpdateVersion 编辑版本信息
  43. func (d *Dao) UpdateVersion(c context.Context, verInfo *model.Version) (bool, error) {
  44. // update guest with new info (using map can update the column with empty string)
  45. updateErr := d.db.Model(&model.Version{}).Where("ver_id = ?", verInfo.VerID).Updates(
  46. map[string]interface{}{
  47. "type": verInfo.Type,
  48. "status": verInfo.Status,
  49. "item_name": verInfo.ItemName,
  50. "ver": verInfo.Ver,
  51. "target_item": verInfo.TargetItem,
  52. "auto_pub": verInfo.AutoPub,
  53. "parent_id": verInfo.ParentID,
  54. }).Error
  55. if updateErr != nil {
  56. log.Error("VERSION UPDATE FAILED:%s", updateErr)
  57. return false, ecode.NotModified
  58. }
  59. return true, nil
  60. }
  61. // GetVersion 获取版本信息外加详情
  62. func (d *Dao) GetVersion(c context.Context, verID uint64, needExt bool) (*model.Version, *model.VersionExt, error) {
  63. var verInfo model.Version
  64. var verExtInfo model.VersionExt
  65. if dbErr := d.db.Where("ver_id = ?", verID).First(&verInfo).Error; dbErr != nil {
  66. log.Error("verinfo:(%v) not found with err:%s", verID, dbErr)
  67. return nil, nil, ecode.NothingFound
  68. }
  69. if needExt {
  70. if dbErr := d.db.Where("ver_id = ?", verID).First(&verExtInfo).Error; dbErr != nil {
  71. log.Error("ver_ext_info:(%v) not found with err:%s", verID, dbErr)
  72. return nil, nil, ecode.NothingFound
  73. }
  74. }
  75. return &verInfo, &verExtInfo, nil
  76. }
  77. // RejectVersion 驳回版本
  78. func (d *Dao) RejectVersion(c context.Context, verID uint64, verType int32) (bool, error) {
  79. var newStatus int32
  80. switch verType {
  81. case model.VerTypeBanner:
  82. newStatus = model.VerStatusNotReviewed
  83. default:
  84. newStatus = model.VerStatusRejected
  85. }
  86. updateErr := d.db.Where("ver_id = ? and type = ?", verID, verType).Model(&model.Version{}).Update("status", newStatus).Error
  87. if updateErr != nil {
  88. log.Error("更新版本状态失败:%s", updateErr)
  89. return false, ecode.NotModified
  90. }
  91. return true, nil
  92. }
  93. // AddVersionLog 新建版本审核记录
  94. func (d *Dao) AddVersionLog(c context.Context, info *model.VersionLog) error {
  95. if insertErr := d.db.Create(&info).Error; insertErr != nil {
  96. log.Error("新建版本审核记录失败:%s", insertErr)
  97. return ecode.NotModified
  98. }
  99. return nil
  100. }
  101. // VersionSearch 项目版本查询
  102. func (d *Dao) VersionSearch(c context.Context, in *model.VersionSearchParam) (versions *model.VersionSearchList, err error) {
  103. r := d.es.NewRequest("ticket_version").Index("ticket_version")
  104. if in.TargetItem > 0 {
  105. r.WhereEq("target_item", in.TargetItem)
  106. } else if in.ItemName != "" {
  107. r.WhereLike([]string{"item_name"}, []string{in.ItemName}, false, elastic.LikeLevelLow)
  108. }
  109. if in.Type > 0 {
  110. r.WhereEq("type", in.Type)
  111. }
  112. if length := len(in.Status); length == 1 {
  113. r.WhereEq("status", in.Status[0])
  114. } else if length > 1 {
  115. r.WhereIn("status", in.Status)
  116. }
  117. r.Order("ctime", elastic.OrderDesc).Ps(in.Ps).Pn(in.Pn)
  118. log.Info(fmt.Sprintf("%s/x/admin/search/query?%s", d.c.URL.ElasticHost, r.Params()))
  119. versions = new(model.VersionSearchList)
  120. err = r.Scan(c, versions)
  121. if err != nil {
  122. log.Error("VersionSearch(%v) r.Query(%s) error(%s)", in, r.Params(), err)
  123. return
  124. }
  125. return
  126. }