123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package db
- import (
- "context"
- "fmt"
- "github.com/caixw/lib.go/conv"
- "github.com/sirupsen/logrus"
- "reflect"
- "youngee_m_api/model/common_model"
- "youngee_m_api/model/gorm_model"
- "youngee_m_api/model/http_model"
- "youngee_m_api/util"
- )
- func GetWithdrawRecords(ctx context.Context, pageSize, pageNum int32, req *http_model.WithdrawalRecordsRequest, condition *common_model.WithdrawRecordsCondition) (*http_model.WithdrawalRecordsPreview, error) {
- db := GetReadDB(ctx)
- var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
- db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{})
- conditionType := reflect.TypeOf(condition).Elem()
- conditionValue := reflect.ValueOf(condition).Elem()
- for i := 0; i < conditionType.NumField(); i++ {
- field := conditionType.Field(i)
- tag := field.Tag.Get("condition")
- value := conditionValue.FieldByName(field.Name)
- if tag == "submit_at" && value.Interface() != "" {
- db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
- }
- if tag == "withdraw_at" && value.Interface() != "" {
- db = db.Where(fmt.Sprintf("withdraw_at like '%s%%'", value.Interface()))
- }
- if !util.IsBlank(value) && tag != "submit_at" && tag != "withdraw_at" {
- db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
- }
- }
- if req.TalentName != "" {
- fmt.Println("TalentName:", req.TalentName)
- db1 := GetReadDB(ctx)
- var talentId string
- db1.Model(gorm_model.YoungeeTalentInfo{}).Select("id").Where("talent_wx_nickname = ? ", req.TalentName).Find(&talentId)
- db = db.Where("talent_id = ?", talentId)
- }
- // 查询总数
- var total int64
- if err := db.Count(&total).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql total, err:%+v", err)
- return nil, err
- }
- // 查询该页数据
- limit := pageSize
- offset := pageSize * pageNum // assert pageNum start with 0
- err := db.Order("submit_at").Limit(int(limit)).Offset(int(offset)).Find(&withdrawRecords).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql limit, err:%+v", err)
- return nil, err
- }
- var talentIds []string
- for _, withdrawRecord := range withdrawRecords {
- talentIds = append(talentIds, withdrawRecord.TalentID)
- }
- talentIdToTalentInfoMap := make(map[string]gorm_model.YoungeeTalentInfo)
- for _, talentId := range talentIds {
- db1 := GetReadDB(ctx)
- talentInfo := gorm_model.YoungeeTalentInfo{}
- db1.Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", talentId).Find(&talentInfo)
- talentIdToTalentInfoMap[talentId] = talentInfo
- }
- var withdrawRecordsDatas []*http_model.WithdrawalRecordsData
- for _, withdrawRecord := range withdrawRecords {
- withdrawRecordsData := new(http_model.WithdrawalRecordsData)
- withdrawRecordsData.TalentId = withdrawRecord.TalentID
- withdrawRecordsData.TalentName = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentWxNickname
- withdrawRecordsData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
- withdrawRecordsData.AmountPayable = float32(withdrawRecord.AmountPayable)
- withdrawRecordsData.ReceiveInfo = withdrawRecord.ReceiveInfo
- withdrawRecordsData.Phone = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentPhoneNumber
- withdrawRecordsData.SubmitAt = conv.MustString(withdrawRecord.SubmitAt, "")[0:19]
- withdrawRecordsData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
- withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordsData)
- }
- var withdrawRecordsPreview http_model.WithdrawalRecordsPreview
- withdrawRecordsPreview.WithdrawalRecordsData = withdrawRecordsDatas
- withdrawRecordsPreview.Total = total
- return &withdrawRecordsPreview, nil
- }
- func GetWithdrawRecord(ctx context.Context, req *http_model.GetWithdrawalRecordRequest) (*http_model.WithdrawalRecordPreview, error) {
- db := GetReadDB(ctx)
- fmt.Println("talentId:", req.TalentId)
- var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
- db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Where("talent_id = ? AND status = ?", req.TalentId, 2).Find(&withdrawRecords)
- var withdrawRecordsPreview http_model.WithdrawalRecordPreview
- var withdrawRecordsDatas []*http_model.WithdrawalRecordData
- for _, withdrawRecord := range withdrawRecords {
- withdrawRecordData := new(http_model.WithdrawalRecordData)
- withdrawRecordData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
- withdrawRecordData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
- withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordData)
- }
- withdrawRecordsPreview.WithdrawalRecordData = withdrawRecordsDatas
- return &withdrawRecordsPreview, nil
- }
|