finance.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "github.com/sirupsen/logrus"
  7. "reflect"
  8. "youngee_m_api/model/common_model"
  9. "youngee_m_api/model/gorm_model"
  10. "youngee_m_api/model/http_model"
  11. "youngee_m_api/util"
  12. )
  13. func GetWithdrawRecords(ctx context.Context, pageSize, pageNum int32, req *http_model.WithdrawalRecordsRequest, condition *common_model.WithdrawRecordsCondition) (*http_model.WithdrawalRecordsPreview, error) {
  14. db := GetReadDB(ctx)
  15. var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
  16. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{})
  17. conditionType := reflect.TypeOf(condition).Elem()
  18. conditionValue := reflect.ValueOf(condition).Elem()
  19. for i := 0; i < conditionType.NumField(); i++ {
  20. field := conditionType.Field(i)
  21. tag := field.Tag.Get("condition")
  22. value := conditionValue.FieldByName(field.Name)
  23. if tag == "submit_at" && value.Interface() != "" {
  24. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  25. }
  26. if tag == "withdraw_at" && value.Interface() != "" {
  27. db = db.Where(fmt.Sprintf("withdraw_at like '%s%%'", value.Interface()))
  28. }
  29. if !util.IsBlank(value) && tag != "submit_at" && tag != "withdraw_at" {
  30. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  31. }
  32. }
  33. if req.TalentName != "" {
  34. fmt.Println("TalentName:", req.TalentName)
  35. db1 := GetReadDB(ctx)
  36. var talentId string
  37. db1.Model(gorm_model.YoungeeTalentInfo{}).Select("id").Where("talent_wx_nickname = ? ", req.TalentName).Find(&talentId)
  38. db = db.Where("talent_id = ?", talentId)
  39. }
  40. // 查询总数
  41. var total int64
  42. if err := db.Count(&total).Error; err != nil {
  43. logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql total, err:%+v", err)
  44. return nil, err
  45. }
  46. // 查询该页数据
  47. limit := pageSize
  48. offset := pageSize * pageNum // assert pageNum start with 0
  49. err := db.Order("submit_at").Limit(int(limit)).Offset(int(offset)).Find(&withdrawRecords).Error
  50. if err != nil {
  51. logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql limit, err:%+v", err)
  52. return nil, err
  53. }
  54. var talentIds []string
  55. for _, withdrawRecord := range withdrawRecords {
  56. talentIds = append(talentIds, withdrawRecord.TalentID)
  57. }
  58. talentIdToTalentInfoMap := make(map[string]gorm_model.YoungeeTalentInfo)
  59. for _, talentId := range talentIds {
  60. db1 := GetReadDB(ctx)
  61. talentInfo := gorm_model.YoungeeTalentInfo{}
  62. db1.Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", talentId).Find(&talentInfo)
  63. talentIdToTalentInfoMap[talentId] = talentInfo
  64. }
  65. var withdrawRecordsDatas []*http_model.WithdrawalRecordsData
  66. for _, withdrawRecord := range withdrawRecords {
  67. withdrawRecordsData := new(http_model.WithdrawalRecordsData)
  68. withdrawRecordsData.TalentId = withdrawRecord.TalentID
  69. withdrawRecordsData.TalentName = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentWxNickname
  70. withdrawRecordsData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
  71. withdrawRecordsData.AmountPayable = float32(withdrawRecord.AmountPayable)
  72. withdrawRecordsData.ReceiveInfo = withdrawRecord.ReceiveInfo
  73. withdrawRecordsData.Phone = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentPhoneNumber
  74. withdrawRecordsData.SubmitAt = conv.MustString(withdrawRecord.SubmitAt, "")[0:19]
  75. withdrawRecordsData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
  76. withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordsData)
  77. }
  78. var withdrawRecordsPreview http_model.WithdrawalRecordsPreview
  79. withdrawRecordsPreview.WithdrawalRecordsData = withdrawRecordsDatas
  80. withdrawRecordsPreview.Total = total
  81. return &withdrawRecordsPreview, nil
  82. }
  83. func GetWithdrawRecord(ctx context.Context, req *http_model.GetWithdrawalRecordRequest) (*http_model.WithdrawalRecordPreview, error) {
  84. db := GetReadDB(ctx)
  85. fmt.Println("talentId:", req.TalentId)
  86. var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
  87. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Where("talent_id = ? AND status = ?", req.TalentId, 2).Find(&withdrawRecords)
  88. var withdrawRecordsPreview http_model.WithdrawalRecordPreview
  89. var withdrawRecordsDatas []*http_model.WithdrawalRecordData
  90. for _, withdrawRecord := range withdrawRecords {
  91. withdrawRecordData := new(http_model.WithdrawalRecordData)
  92. withdrawRecordData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
  93. withdrawRecordData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
  94. withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordData)
  95. }
  96. withdrawRecordsPreview.WithdrawalRecordData = withdrawRecordsDatas
  97. return &withdrawRecordsPreview, nil
  98. }