selection.go 7.8 KB

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