selection.go 8.6 KB

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