selection.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. db = db.Debug().Model(gorm_model.YounggeeSelectionInfo{})
  121. conditionType := reflect.TypeOf(conditions).Elem()
  122. conditionValue := reflect.ValueOf(conditions).Elem()
  123. selectionStatus := ""
  124. searchValue := ""
  125. for i := 0; i < conditionType.NumField(); i++ {
  126. field := conditionType.Field(i)
  127. tag := field.Tag.Get("condition")
  128. value := conditionValue.FieldByName(field.Name)
  129. if tag == "selection_status" {
  130. selectionStatus = fmt.Sprintf("%v", conv.MustInt(value.Interface(), 0))
  131. if selectionStatus != "0" {
  132. db = db.Where("selection_status = ?", selectionStatus)
  133. }
  134. } else if tag == "search_value" {
  135. searchValue = fmt.Sprintf("%v", value.Interface())
  136. } else if tag == "submit_at" && value.Interface() != "" {
  137. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  138. } else if tag == "task_ddl" && value.Interface() != "" {
  139. db = db.Where(fmt.Sprintf("task_ddl like '%s%%'", value.Interface()))
  140. } else if !util.IsBlank(value) && tag != "task_ddl" && tag != "submit_at" && tag != "search_value" {
  141. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  142. }
  143. }
  144. // 查询总数
  145. var total int64
  146. var selectionInfos []*gorm_model.YounggeeSelectionInfo
  147. if err := db.Count(&total).Error; err != nil {
  148. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  149. return nil, 0, err
  150. }
  151. // 查询该页数据
  152. limit := pageSize
  153. offset := pageSize * pageNum // assert pageNum start with 0
  154. if selectionStatus == "1" {
  155. err := db.Order("submit_at desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
  156. if err != nil {
  157. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  158. return nil, 0, err
  159. }
  160. } else {
  161. err := db.Order("task_ddl desc").Limit(int(limit)).Offset(int(offset)).Find(&selectionInfos).Error
  162. if err != nil {
  163. logrus.WithContext(ctx).Errorf("[GetSelectionList] error query mysql total, err:%+v", err)
  164. return nil, 0, err
  165. }
  166. }
  167. var newSelectionInfos []*gorm_model.YounggeeSelectionInfo
  168. for _, v := range selectionInfos {
  169. if searchValue == "" {
  170. newSelectionInfos = append(newSelectionInfos, v)
  171. } else if strings.Contains(v.SelectionID, searchValue) {
  172. newSelectionInfos = append(newSelectionInfos, v)
  173. } else if strings.Contains(v.SelectionName, searchValue) {
  174. newSelectionInfos = append(newSelectionInfos, v)
  175. } else {
  176. total--
  177. }
  178. }
  179. return newSelectionInfos, total, nil
  180. }
  181. func GetSelectionBriefInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeSecBrief, error) {
  182. db := GetReadDB(ctx)
  183. var selectionBriefInfos []*gorm_model.YounggeeSecBrief
  184. err := db.Model(gorm_model.YounggeeSecBrief{}).Where("selection_id = ?", selectionId).Find(&selectionBriefInfos).Error
  185. if err != nil {
  186. logrus.WithContext(ctx).Errorf("[GetSelectionBriefInfo] error query mysql, err:%+v", err)
  187. return nil, err
  188. }
  189. return selectionBriefInfos, nil
  190. }
  191. func GetSelectionExampleInfo(ctx context.Context, selectionId string) ([]*gorm_model.YounggeeSecExample, error) {
  192. db := GetReadDB(ctx)
  193. var selectionExampleInfos []*gorm_model.YounggeeSecExample
  194. err := db.Model(gorm_model.YounggeeSecExample{}).Where("selection_id = ?", selectionId).Find(&selectionExampleInfos).Error
  195. if err != nil {
  196. logrus.WithContext(ctx).Errorf("[GetSelectionExampleInfo] error query, err:%+v", err)
  197. return nil, err
  198. }
  199. return selectionExampleInfos, nil
  200. }
  201. func PaySelection(ctx context.Context, enterpriseId string, payMoney float64, selectionId string) error {
  202. db := GetWriteDB(ctx)
  203. err := db.Transaction(func(tx *gorm.DB) error {
  204. // 1. 冻结账户余额
  205. whereCondition := gorm_model.Enterprise{
  206. EnterpriseID: enterpriseId,
  207. }
  208. updateData := map[string]interface{}{
  209. "frozen_balance": gorm.Expr("frozen_balance + ?", payMoney),
  210. "available_balance": gorm.Expr("available_balance - ?", payMoney)}
  211. if err := tx.Model(gorm_model.Enterprise{}).Where(whereCondition).Updates(updateData).Error; err != nil {
  212. return err
  213. }
  214. // 2. 更新选品项目状态
  215. whereCondition1 := gorm_model.YounggeeSelectionInfo{SelectionID: selectionId, SelectionStatus: 4}
  216. updateData1 := gorm_model.YounggeeSelectionInfo{SelectionStatus: 6}
  217. if err := tx.Model(gorm_model.YounggeeSelectionInfo{}).Where(whereCondition1).Updates(updateData1).Error; err != nil {
  218. return err
  219. }
  220. // 返回 nil 提交事务
  221. return nil
  222. })
  223. if err != nil {
  224. return err
  225. }
  226. return nil
  227. }
  228. func CreateSecBrief(ctx context.Context, briefInfo gorm_model.YounggeeSecBrief) error {
  229. db := GetWriteDB(ctx)
  230. err := db.Create(&briefInfo).Error
  231. if err != nil {
  232. return err
  233. }
  234. return nil
  235. }
  236. func DeleteSecBriefBySelectionId(ctx context.Context, selectionId string) error {
  237. db := GetWriteDB(ctx)
  238. deleteCondition := gorm_model.YounggeeSecBrief{
  239. SelectionID: selectionId,
  240. }
  241. err := db.Where(deleteCondition).Delete(gorm_model.YounggeeSecBrief{}).Error
  242. if err != nil {
  243. return err
  244. }
  245. return nil
  246. }
  247. func CreateSecExample(ctx context.Context, ExampleInfo gorm_model.YounggeeSecExample) error {
  248. db := GetWriteDB(ctx)
  249. err := db.Create(&ExampleInfo).Error
  250. if err != nil {
  251. return err
  252. }
  253. return nil
  254. }
  255. func DeleteSecExampleBySelectionId(ctx context.Context, selectionId string) error {
  256. db := GetWriteDB(ctx)
  257. deleteCondition := gorm_model.YounggeeSecExample{
  258. SelectionID: selectionId,
  259. }
  260. err := db.Where(deleteCondition).Delete(gorm_model.YounggeeSecExample{}).Error
  261. if err != nil {
  262. return err
  263. }
  264. return nil
  265. }