selection.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package db
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/sirupsen/logrus"
  7. "gorm.io/gorm"
  8. "reflect"
  9. "strings"
  10. "youngee_b_api/model/common_model"
  11. "youngee_b_api/model/gorm_model"
  12. "youngee_b_api/util"
  13. )
  14. func CreateSelection(ctx context.Context, selectionInfo gorm_model.YounggeeSelectionInfo) error {
  15. db := GetWriteDB(ctx)
  16. err := db.Create(&selectionInfo).Error
  17. if err != nil {
  18. return err
  19. }
  20. return nil
  21. }
  22. func UpdateSelection(ctx context.Context, selectionInfo gorm_model.YounggeeSelectionInfo) error {
  23. db := GetWriteDB(ctx)
  24. whereCondition := gorm_model.YounggeeSelectionInfo{SelectionID: selectionInfo.SelectionID}
  25. err := db.Model(&gorm_model.YounggeeSelectionInfo{}).Where(whereCondition).Updates(selectionInfo).Error
  26. if err != nil {
  27. return err
  28. }
  29. return nil
  30. }
  31. func DeleteSelection(ctx context.Context, SelectionId string) error {
  32. db := GetReadDB(ctx)
  33. err := db.Where("selection_id = ?", SelectionId).Delete(&gorm_model.YounggeeSelectionInfo{}).Error
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. func GetSelectionById(ctx context.Context, selectionId string) (*gorm_model.YounggeeSelectionInfo, error) {
  40. db := GetWriteDB(ctx)
  41. selectionInfo := gorm_model.YounggeeSelectionInfo{}
  42. whereCondition := gorm_model.YounggeeSelectionInfo{SelectionID: selectionId}
  43. result := db.Where(&whereCondition).First(&selectionInfo)
  44. if result.Error != nil {
  45. if errors.Is(result.Error, gorm.ErrRecordNotFound) {
  46. return nil, nil
  47. } else {
  48. return nil, result.Error
  49. }
  50. }
  51. return &selectionInfo, nil
  52. }
  53. func GetSelectionByEnterpiseIdAndProductId(ctx context.Context, enterpriseId string, productId int) (*gorm_model.YounggeeSelectionInfo, error) {
  54. db := GetWriteDB(ctx)
  55. selectionInfo := gorm_model.YounggeeSelectionInfo{}
  56. whereCondition := gorm_model.YounggeeSelectionInfo{EnterpriseID: enterpriseId, ProductID: productId}
  57. result := db.Where(&whereCondition).First(&selectionInfo)
  58. if result.Error != nil {
  59. if errors.Is(result.Error, gorm.ErrRecordNotFound) {
  60. return nil, nil
  61. } else {
  62. return nil, result.Error
  63. }
  64. }
  65. return &selectionInfo, nil
  66. }
  67. func GetSelectionList(ctx context.Context, enterpriseID string, pageSize, pageNum int64, conditions *common_model.SelectionConditions) ([]*gorm_model.YounggeeSelectionInfo, int64, error) {
  68. db := GetReadDB(ctx)
  69. db = db.Debug().Model(gorm_model.YounggeeSelectionInfo{}).Where("enterprise_id = ?", enterpriseID)
  70. conditionType := reflect.TypeOf(conditions).Elem()
  71. conditionValue := reflect.ValueOf(conditions).Elem()
  72. selectionStatus := ""
  73. searchValue := ""
  74. for i := 0; i < conditionType.NumField(); i++ {
  75. field := conditionType.Field(i)
  76. tag := field.Tag.Get("condition")
  77. value := conditionValue.FieldByName(field.Name)
  78. if tag == "selection_status" {
  79. selectionStatus = fmt.Sprintf("%v", value.Interface())
  80. } else if tag == "search_value" {
  81. searchValue = fmt.Sprintf("%v", value.Interface())
  82. } else if tag == "submit_at" && value.Interface() != "" {
  83. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  84. } else if tag == "task_ddl" && value.Interface() != "" {
  85. db = db.Where(fmt.Sprintf("task_ddl like '%s%%'", value.Interface()))
  86. } else if !util.IsBlank(value) && tag != "task_ddl" && tag != "submit_at" && tag != "search_value" {
  87. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  88. }
  89. }
  90. // 查询总数
  91. var total int64
  92. var selectionInfos []*gorm_model.YounggeeSelectionInfo
  93. if err := db.Count(&total).Error; err != nil {
  94. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  95. return nil, 0, err
  96. }
  97. // 查询该页数据
  98. limit := pageSize
  99. offset := pageSize * pageNum // assert pageNum start with 0
  100. if selectionStatus == "1" {
  101. err := db.Order("submit_at desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
  102. if err != nil {
  103. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  104. return nil, 0, err
  105. }
  106. } else {
  107. err := db.Order("task_ddl desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
  108. if err != nil {
  109. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  110. return nil, 0, err
  111. }
  112. }
  113. var newSelectionInfos []*gorm_model.YounggeeSelectionInfo
  114. for _, v := range selectionInfos {
  115. if searchValue == "" {
  116. newSelectionInfos = append(newSelectionInfos, v)
  117. } else if strings.Contains(v.SelectionID, searchValue) {
  118. newSelectionInfos = append(newSelectionInfos, v)
  119. } else if strings.Contains(v.SelectionName, searchValue) {
  120. newSelectionInfos = append(newSelectionInfos, v)
  121. } else {
  122. total--
  123. }
  124. }
  125. return newSelectionInfos, total, nil
  126. }
  127. func GetSelectionBriefInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeSecBrief, error) {
  128. db := GetReadDB(ctx)
  129. var selectionBriefInfos []*gorm_model.YounggeeSecBrief
  130. err := db.Model(gorm_model.YounggeeSecBrief{}).Where("selection_id = ?", selectionId).Find(&selectionBriefInfos).Error
  131. if err != nil {
  132. logrus.WithContext(ctx).Errorf("[GetSelectionBriefInfo] error query mysql, err:%+v", err)
  133. return nil, err
  134. }
  135. return selectionBriefInfos, nil
  136. }
  137. func GetSelectionExampleInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeSecExample, error) {
  138. db := GetReadDB(ctx)
  139. var selectionExampleInfos []*gorm_model.YounggeeSecExample
  140. err := db.Model(gorm_model.YounggeeSecExample{}).Where("selection_id = ?", selectionId).Find(&selectionExampleInfos).Error
  141. if err != nil {
  142. logrus.WithContext(ctx).Errorf("[GetSelectionExampleInfo] error query, err:%+v", err)
  143. return nil, err
  144. }
  145. return selectionExampleInfos, nil
  146. }
  147. func PaySelection(ctx context.Context, enterpriseId string, payMoney float64, selectionId string) error {
  148. db := GetWriteDB(ctx)
  149. err := db.Transaction(func(tx *gorm.DB) error {
  150. // 1. 冻结账户余额
  151. whereCondition := gorm_model.Enterprise{
  152. EnterpriseID: enterpriseId,
  153. }
  154. updateData := map[string]interface{}{
  155. "frozen_balance": gorm.Expr("frozen_balance + ?", payMoney),
  156. "available_balance": gorm.Expr("available_balance - ?", payMoney)}
  157. if err := tx.Model(gorm_model.Enterprise{}).Where(whereCondition).Updates(updateData).Error; err != nil {
  158. return err
  159. }
  160. // 2. 更新选品项目状态
  161. whereCondition1 := gorm_model.YounggeeSelectionInfo{SelectionID: selectionId, SelectionStatus: 4}
  162. updateData1 := gorm_model.YounggeeSelectionInfo{SelectionStatus: 6}
  163. if err := tx.Model(gorm_model.YounggeeSelectionInfo{}).Where(whereCondition1).Updates(updateData1).Error; err != nil {
  164. return err
  165. }
  166. // 返回 nil 提交事务
  167. return nil
  168. })
  169. if err != nil {
  170. return err
  171. }
  172. return nil
  173. }