123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479 |
- package db
- import (
- "context"
- "encoding/json"
- "fmt"
- "reflect"
- "strconv"
- "strings"
- "time"
- "youngee_m_api/consts"
- "youngee_m_api/model/common_model"
- "youngee_m_api/model/gorm_model"
- "youngee_m_api/model/http_model"
- "youngee_m_api/util"
- "github.com/caixw/lib.go/conv"
- "github.com/sirupsen/logrus"
- "gorm.io/gorm"
- )
- 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(fmt.Sprintf("talent_wx_nickname like '%%%s%%'", 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.WithdrawId = withdrawRecord.WithdrawID
- 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.BankType = withdrawRecord.BankType
- 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
- }
- func GetWithdrawNums(ctx context.Context) (*http_model.WithdrawNums, error) {
- var withdrawNums int64
- db := GetReadDB(ctx)
- err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("status = 1").Count(&withdrawNums).Error
- if err != nil {
- return nil, err
- }
- WithdrawNums := new(http_model.WithdrawNums)
- WithdrawNums.WithdrawNums = int(withdrawNums)
- return WithdrawNums, err
- }
- func GetRechargeValue(ctx context.Context, request http_model.GetRechargeValueRequest) (*http_model.RechargeValue, error) {
- var rechargeValue float64
- db := GetReadDB(ctx)
- var rechargelist []gorm_model.YounggeeRechargeRecord
- err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("status = ?", request.Status).Find(rechargelist).Error
- if err != nil {
- return nil, err
- }
- for _, recharge := range rechargelist {
- rechargeValue += recharge.RechargeAmount
- }
- RechargeValue := new(http_model.RechargeValue)
- RechargeValue.RechargeValue = rechargeValue
- return RechargeValue, err
- }
- func GetInvoiceValue(ctx context.Context, request http_model.GetInvoiceValueRequest) (*http_model.InvoiceValue, error) {
- var invoicevalue float64
- db := GetReadDB(ctx)
- var invoicelist []gorm_model.YounggeeInvoiceRecord
- err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", request.Status).Find(invoicelist).Error
- if err != nil {
- return nil, err
- }
- for _, invoice := range invoicelist {
- invoicevalue += invoice.InvoiceAmount
- }
- InvoiceValue := new(http_model.InvoiceValue)
- InvoiceValue.InvoiceValue = invoicevalue
- return InvoiceValue, err
- }
- func GetSupplierWithdrawPrevalue(ctx context.Context, request http_model.GetSupplierWithdrawPrevalueRequest) (*http_model.WithdrawValue, error) {
- var withdrawvalue float64
- db := GetReadDB(ctx)
- var withdrawlist []gorm_model.YounggeeSupplierWithdraw
- // 获取所有符合条件的发票
- err := db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status =2").Find(&withdrawlist).Error
- if err != nil {
- return nil, err
- }
- for _, withdraw := range withdrawlist {
- withdrawvalue += withdraw.WithdrawAmount
- }
- return &http_model.WithdrawValue{
- withdrawvalue,
- }, err
- }
- func GetTalentWithdrawPrevalue(ctx context.Context, request http_model.GetTalentWithdrawPrevalueRequest) (*http_model.TalentWithdrawValue, error) {
- var withdrawvalue float64
- db := GetReadDB(ctx)
- var withdrawlist []gorm_model.YounggeeWithdrawRecord
- // 获取所有符合条件的发票
- err := db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status =1").Find(&withdrawlist).Error
- if err != nil {
- return nil, err
- }
- for _, withdraw := range withdrawlist {
- withdrawvalue += withdraw.WithdrawAmount
- }
- return &http_model.TalentWithdrawValue{
- withdrawvalue,
- }, err
- }
- func GetWithdrawValue(ctx context.Context, request http_model.GetWithdrawValueRequest) (*http_model.ALlWithdrawValue, error) {
- var withdrawvalue float64
- db := GetReadDB(ctx)
- var withdrawlist []gorm_model.YounggeeWithdrawRecord
- // 获取所有符合条件的发票
- err := db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status =2").Find(&withdrawlist).Error
- if err != nil {
- return nil, err
- }
- for _, withdraw := range withdrawlist {
- withdrawvalue += withdraw.WithdrawAmount
- }
- var swithdrawlist []gorm_model.YounggeeSupplierWithdraw
- // 获取所有符合条件的发票
- err = db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status =3").Find(&swithdrawlist).Error
- if err != nil {
- return nil, err
- }
- for _, withdraw := range withdrawlist {
- withdrawvalue += withdraw.WithdrawAmount
- }
- return &http_model.ALlWithdrawValue{
- withdrawvalue,
- }, err
- }
- func GetSupplierInvoiceValue(ctx context.Context, request http_model.GetSupplierInvoiceValueRequest) (*http_model.SupplierInvoiceValue, error) {
- var invoicevalue float64
- db := GetReadDB(ctx)
- var invoicelist []gorm_model.YounggeeSupplierInvoice
- // 获取所有符合条件的发票
- err := db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("status = ?", request.Status).Find(&invoicelist).Error
- if err != nil {
- return nil, err
- }
- // 收集所有的 TaskIDs
- var taskIDList []int
- for _, invoice := range invoicelist {
- TaskIDs := strings.Split(invoice.IncomeIds, ",")
- for _, taskId := range TaskIDs {
- // 将 taskId 转换为 int 并添加到 taskIDList 中
- if id, err := strconv.Atoi(taskId); err == nil {
- taskIDList = append(taskIDList, id)
- }
- }
- }
- // 批量查询所有相关的收入数据
- var supplierIncome []gorm_model.YounggeeSupplierIncome
- err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id IN ?", taskIDList).Find(&supplierIncome).Error
- if err != nil {
- return &http_model.SupplierInvoiceValue{}, err
- }
- // 将查询到的 SupplierIncome 存入 map,以便快速查找
- incomeMap := make(map[int]gorm_model.YounggeeSupplierIncome)
- for _, income := range supplierIncome {
- incomeMap[income.IncomeId] = income
- }
- // 根据每个发票的 IncomeIds 查找对应的收入数据,并累加 SupplierChargeActual
- for _, invoice := range invoicelist {
- TaskIDs := strings.Split(invoice.IncomeIds, ",")
- for _, taskId := range TaskIDs {
- // 将 taskId 转换为 int
- if id, err := strconv.Atoi(taskId); err == nil {
- if income, exists := incomeMap[id]; exists {
- invoicevalue += income.SupplierChargeActual
- }
- }
- }
- }
- // 返回最终的发票值
- InvoiceValue := new(http_model.SupplierInvoiceValue)
- InvoiceValue.InvoiceValue = invoicevalue
- return InvoiceValue, nil
- }
- func GetInvoiceNums(ctx context.Context) (*http_model.InvoiceNums, error) {
- var invoiceNums int64
- db := GetReadDB(ctx)
- err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = 1").Count(&invoiceNums).Error
- if err != nil {
- return nil, err
- }
- InvoiceNums := new(http_model.InvoiceNums)
- InvoiceNums.InvoiceNums = int(invoiceNums)
- return InvoiceNums, err
- }
- func GetRechargeNums(ctx context.Context) (*http_model.RechargeNums, error) {
- var rechargeNums int64
- db := GetReadDB(ctx)
- err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("status = 1").Count(&rechargeNums).Error
- if err != nil {
- return nil, err
- }
- RechargeNums := new(http_model.RechargeNums)
- RechargeNums.RechargeNums = rechargeNums
- return RechargeNums, err
- }
- func AcceptRecharge(ctx context.Context, req http_model.AcceptRechargeRequest) (*http_model.AcceptRechargeData, error) {
- db := GetReadDB(ctx)
- err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id=?", req.Id).Updates(map[string]interface{}{"status": 2, "agree_at": time.Now()}).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call AcceptRecharge error,err:%+v", err)
- return nil, err
- }
- res := &http_model.AcceptRechargeData{
- Id: req.Id,
- }
- return res, nil
- }
- func RefuseRecharge(ctx context.Context, req http_model.RefuseRechargeRequest) (*http_model.RefuseRechargeData, error) {
- db := GetReadDB(ctx)
- err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id=?", req.Id).Updates(map[string]interface{}{"status": 3, "refuse_at": time.Now(), "fail_reason": req.FailReason}).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call AcceptRecharge error,err:%+v", err)
- return nil, err
- }
- res := &http_model.RefuseRechargeData{
- Id: req.Id,
- }
- return res, nil
- }
- func GetPreRechargeList(ctx context.Context, req http_model.GetPreRechargeListRequest) (*http_model.PreRechargeListData, error) {
- db := GetReadDB(ctx)
- // 获取分页参数
- page := req.PageNum
- pageSize := req.PageSize
- if page < 1 {
- page = 1
- }
- if pageSize < 1 {
- pageSize = 10 // 设置默认每页记录数为 10
- }
- var prerechargelist []gorm_model.YounggeeRechargeRecord
- var total int64
- // 计算总记录数
- err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("status = ?", req.Status).Count(&total).Error
- if err != nil {
- return &http_model.PreRechargeListData{}, err
- }
- // 查询当前页的数据
- err = db.Model(gorm_model.YounggeeRechargeRecord{}).
- Where("status = 1").
- Offset((page - 1) * pageSize). // 计算偏移量
- Limit(pageSize). // 设置每页限制
- Find(&prerechargelist).Error
- if err != nil {
- return &http_model.PreRechargeListData{}, err
- }
- rechargePointers := make([]*http_model.PreRechargeListResponse, 0, len(prerechargelist))
- for _, recharge := range prerechargelist {
- subaccountinfo := gorm_model.YounggeeSubAccount{}
- userResult := db.Where(gorm_model.YounggeeSubAccount{SubAccountId: recharge.SubAccountID, SubAccountType: 1}).First(&subaccountinfo)
- if userResult.Error != nil {
- return nil, userResult.Error
- }
- enterprise := gorm_model.Enterprise{}
- result := db.Where(gorm_model.Enterprise{EnterpriseID: recharge.EnterpriseID}).First(&enterprise)
- if result.Error != nil {
- return nil, result.Error
- }
- response := &http_model.PreRechargeListResponse{
- ID: recharge.RechargeID,
- EnterPrise: enterprise.BusinessName,
- Operator: subaccountinfo.SubAccountName,
- RechargeAmount: recharge.RechargeAmount,
- RechargeMethod: recharge.RechargeMethod,
- TransferVoucherUrl: recharge.TransferVoucherUrl,
- Phone: recharge.Phone,
- CommitAt: recharge.CommitAt,
- ConfirmAt: recharge.ConfirmAt,
- RefuseAt: recharge.RefuseAt,
- FailReason: recharge.FailReason,
- }
- rechargePointers = append(rechargePointers, response)
- }
- return &http_model.PreRechargeListData{
- PreRechargeListinfo: rechargePointers,
- Total: total,
- }, nil
- }
- func GetTalentWithdrawList(ctx context.Context, req *http_model.GetTalentWithdrawListRequest) (*http_model.TalentWithdrawListData, error) {
- db := GetReadDB(ctx)
- page := req.PageNum
- pageSize := req.PageSize
- if page < 1 {
- page = 1
- }
- if pageSize < 1 {
- pageSize = 10 // 设置默认每页记录数为 10
- }
- var withdrawlist []gorm_model.YounggeeWithdrawRecord
- var total int64
- // 计算总记录数
- err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("status = ?", req.Status).Count(&total).Error
- if err != nil {
- return &http_model.TalentWithdrawListData{}, err
- }
- // 查询当前页的数据
- err = db.Model(gorm_model.YounggeeWithdrawRecord{}).
- Offset((page - 1) * pageSize). // 计算偏移量
- Limit(pageSize). // 设置每页限制
- Find(&withdrawlist).Error
- if err != nil {
- return &http_model.TalentWithdrawListData{}, err
- }
- withdrawPointers := make([]*http_model.TalentWithdrawListResponse, 0, len(withdrawlist))
- for _, withdraw := range withdrawlist {
- response := &http_model.TalentWithdrawListResponse{
- WithdrawId: withdraw.WithdrawID,
- TalentId: withdraw.TalentID,
- WithdrawAmount: withdraw.WithdrawAmount,
- ActualAmount: withdraw.AmountPayable,
- ReajectAt: withdraw.RejectAt,
- SubmitAt: withdraw.SubmitAt,
- WithdrawAt: withdraw.WithdrawAt,
- RejectReason: withdraw.RejectReason,
- Name: withdraw.Name,
- PhoneNumber: withdraw.PhoneNum,
- IdcardNumber: withdraw.IdcardNum,
- BankNumber: withdraw.BankNum,
- }
- withdrawPointers = append(withdrawPointers, response)
- }
- return &http_model.TalentWithdrawListData{
- TalentwithdrawList: withdrawPointers,
- Total: total,
- }, nil
- }
- func ConfirmWithdrawal(ctx context.Context, withdrawId string) error {
- db := GetReadDB(ctx)
- db2 := GetReadDB(ctx)
- withdrawInfo := gorm_model.YounggeeWithdrawRecord{}
- db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Where("withdraw_id = ?", withdrawId).Find(&withdrawInfo)
- db2.Debug().Where("withdraw_id = ?", withdrawId).Updates(gorm_model.YounggeeWithdrawRecord{Status: 2, WithdrawAt: time.Now()})
- IncomeIdLists := strings.Split(withdrawInfo.IncomeIdList, ",")
- for _, income := range IncomeIdLists {
- var incomeinfo gorm_model.YounggeeTalentIncome
- err := db.Debug().Model(gorm_model.YounggeeTalentIncome{}).
- Where("id = ?", income).
- First(&incomeinfo).Error // 使用 First 而不是 Updates 来查询数据
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Query YounggeeTalentIncome error, err:%+v", err)
- return err
- }
- err = db.Model(gorm_model.YounggeeTalentIncome{}).Where("id = ?", income).Updates(gorm_model.YounggeeTalentIncome{WithdrawStatus: 1}).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Updates YounggeeTalentIncome error, err:%+v", err)
- return err
- }
- switch incomeinfo.Type {
- case 1:
- {
- err = db.Debug().Model(gorm_model.YounggeeSecTaskInfo{}).Where("task_id = ?", incomeinfo.SectaskID).Updates(gorm_model.YounggeeSecTaskInfo{WithdrawStatus: 4}).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Updates YounggeeSecTaskInfo error, err:%+v", err)
- return err
- }
- }
- case 2:
- {
- err = db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", incomeinfo.TaskID).Updates(gorm_model.YoungeeTaskInfo{WithdrawStatus: 4}).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Updates YoungeeTaskInfo error, err:%+v", err)
- return err
- }
- }
- case 3:
- {
- err = db.Debug().Model(gorm_model.YoungeeLocalTaskInfo{}).Where("task_id = ?", incomeinfo.LocalTaskId).Updates(gorm_model.YoungeeLocalTaskInfo{WithdrawStatus: 4}).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Updates YoungeeLocalTaskInfo error, err:%+v", err)
- return err
- }
- }
- }
- }
- //for _, taskId := range taskIdLists {
- // db1 := GetReadDB(ctx)
- // err := db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", taskId).Updates(gorm_model.YoungeeTaskInfo{WithdrawStatus: 4}).Error
- // if err != nil {
- // logrus.WithContext(ctx).Errorf("[finance db] Update YoungeeTaskInfo error,err:%+v", err)
- // return err
- // }
- //
- // err = CreateMessageByTaskId(ctx, 6, 1, taskId)
- // if err != nil {
- // logrus.WithContext(ctx).Errorf("[ConfirmWithdrawal] call CreateMessageByTaskId error,err:%+v", err)
- // return err
- // }
- //}
- db3 := GetReadDB(ctx)
- db3.Debug().Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", withdrawInfo.TalentID).Updates(
- map[string]interface{}{
- "withdrawing": gorm.Expr("withdrawing - ?", withdrawInfo.WithdrawAmount),
- "withdrawed": gorm.Expr("withdrawed + ?", withdrawInfo.WithdrawAmount)})
- return nil
- }
- func RefuseTalentWithdrawal(ctx context.Context, req *http_model.RefuseTalentWithdrawalRequest) error {
- db := GetReadDB(ctx)
- // 获取撤回记录
- var withdrwainfo gorm_model.YounggeeWithdrawRecord
- err := db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).
- Where("withdraw_id = ?", req.WithdrawId).
- First(&withdrwainfo).Error // 使用 First 而不是 Updates 来查询数据
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Query YounggeeWithdrawRecord error, err:%+v", err)
- return err
- }
- // 更新 YounggeeWithdrawRecord
- err = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).
- Where("withdraw_id = ?", req.WithdrawId).
- Updates(gorm_model.YounggeeWithdrawRecord{
- Status: 3,
- RejectAt: time.Now(),
- RejectReason: req.Reason,
- }).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Update YounggeeWithdrawRecord error, err:%+v", err)
- return err
- }
- // 更新 YoungeeTalentInfo
- err = db.Debug().Model(gorm_model.YoungeeTalentInfo{}).
- Where("id = ?", withdrwainfo.TalentID).
- Updates(map[string]interface{}{
- "withdrawing": gorm.Expr("withdrawing + ?", withdrwainfo.WithdrawAmount),
- "canwithdraw": gorm.Expr("canwithdraw - ?", withdrwainfo.WithdrawAmount),
- }).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Update YoungeeTalentInfo error, err:%+v", err)
- return err
- }
- return nil
- }
- func GetBankInfo(ctx context.Context, req *http_model.GetBankInfoRequest) (*http_model.BankInfo, error) {
- //db := GetReadDB(ctx)
- //if req.BankId == "" {
- // return nil, nil
- //}
- //var infoBank *gorm_model.InfoBank
- //db.Debug().Model(gorm_model.InfoBank{}).Where("id = ?", req.BankId).First(&infoBank)
- db1 := GetReadDB(ctx)
- var infoRegion *gorm_model.InfoRegion
- db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(req.BankOpenAddress, 0)).First(&infoRegion)
- provinceCode := conv.MustString(req.BankOpenAddress, "")[0:2] + "0000"
- var province *gorm_model.InfoRegion
- db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(provinceCode, 0)).First(&province)
- cityCode := conv.MustString(req.BankOpenAddress, "")[0:4] + "00"
- var city *gorm_model.InfoRegion
- db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(cityCode, 0)).First(&city)
- data := new(http_model.BankInfo)
- //data.BankName = infoBank.Name
- data.BankOpenAddress = province.RegionName + city.RegionName + infoRegion.RegionName
- //db.Model(gorm_model.InfoBank{}).Where("")
- return data, nil
- }
- // GetEnterpriseIDByBusiness 根据企业名称查找企业ID
- func GetEnterpriseIDByBusiness(ctx context.Context, BusinessName string) int64 {
- db := GetReadDB(ctx)
- var EnterpriseID int64
- db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("business_name", BusinessName).First(&EnterpriseID)
- return EnterpriseID
- }
- // GetEnterpriseIDByUserId 根据企业名称查找企业ID
- func GetEnterpriseIDByUserId(ctx context.Context, UserId int64) int64 {
- db := GetReadDB(ctx)
- var EnterpriseID int64
- db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("user_id", UserId).First(&EnterpriseID)
- return EnterpriseID
- }
- // GetUserIDByUsername 根据用户名称查UserID
- func GetUserIDByUsername(ctx context.Context, Username string) int64 {
- db := GetReadDB(ctx)
- var UserID int64
- db = db.Model([]gorm_model.YounggeeUser{}).Select("id").Where(fmt.Sprintf("username like '%%%s%%'", Username)).First(&UserID)
- return UserID
- }
- func GetInvoiceRecords(ctx context.Context, req *http_model.InvoiceRecordsRequest, condition *common_model.InvoiceRecordsCondition) (*http_model.InvoiceRecordsData, error) {
- db := GetReadDB(ctx)
- var invoiceRecords []*gorm_model.YounggeeInvoiceRecord
- db = db.Debug().Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", req.InvoiceStatus)
- 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 == "billing_at" && value.Interface() != "" {
- db = db.Where(fmt.Sprintf("billing_at like '%s%%'", value.Interface()))
- }
- }
- if req.Username != "" {
- UserID := GetUserIDByUsername(ctx, req.Username)
- enterpriseId := GetEnterpriseIDByUserId(ctx, UserID)
- db = db.Where("enterprise_id = ?", enterpriseId)
- }
- if req.UserId != 0 {
- enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
- db = db.Where("enterprise_id = ?", enterpriseId)
- }
- // 查询总数
- var total int64
- if err := db.Count(&total).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql total, err:%+v", err)
- return nil, err
- }
- if req.InvoiceStatus != 3 {
- db.Order("submit_at")
- } else {
- db.Order("billing_at desc")
- }
- // 查询该页数据
- limit := req.PageSize
- offset := req.PageSize * req.PageNum // assert pageNum start with 0
- err := db.Limit(int(limit)).Offset(int(offset)).Find(&invoiceRecords).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql limit, err:%+v", err)
- return nil, err
- }
- enterpriseIdToUserInfoMap := make(map[string]gorm_model.Enterprise)
- regionAddressMap := make(map[string]string)
- for _, invoiceRecord := range invoiceRecords {
- if _, ok := enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID]; !ok {
- db1 := GetReadDB(ctx)
- enterpriseInfo := gorm_model.Enterprise{}
- db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", invoiceRecord.EnterpriseID).Find(&enterpriseInfo)
- enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID] = enterpriseInfo
- }
- if _, ok := regionAddressMap[invoiceRecord.EnterpriseID]; !ok {
- db1 := GetReadDB(ctx)
- var regionCode string
- db1.Model(gorm_model.YounggeeInvoiceAddress{}).Select("region_code").Where("enterprise_id = ?", invoiceRecord.EnterpriseID).Find(®ionCode)
- regionAddressMap[invoiceRecord.EnterpriseID] = GetRegion(ctx, conv.MustInt(regionCode, 0))
- }
- }
- var InvoiceRecords []*http_model.InvoiceRecordsPreviews
- for _, invoiceRecord := range invoiceRecords {
- InvoiceRecord := new(http_model.InvoiceRecordsPreviews)
- InvoiceRecord.BillingId = invoiceRecord.BillingID
- InvoiceRecord.InvoiceInfo = invoiceRecord.InvoiceSnap
- InvoiceRecord.AddressInfo = invoiceRecord.AddressSnap
- InvoiceRecord.InvoiceAddress = regionAddressMap[invoiceRecord.EnterpriseID]
- InvoiceRecord.InvoiceType = invoiceRecord.InvoiceType
- InvoiceRecord.Amount = invoiceRecord.InvoiceAmount
- InvoiceRecord.Phone = invoiceRecord.Phone
- InvoiceRecord.ShipmentNumber = invoiceRecord.ShipmentNumber
- InvoiceRecord.BusinessName = enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].BusinessName
- InvoiceRecord.UserId = invoiceRecord.EnterpriseID
- InvoiceRecord.Username = GetUsernameByUserID(ctx, enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].UserID)
- InvoiceRecord.SubmitAt = conv.MustString(invoiceRecord.SubmitAt, "")[:19]
- InvoiceRecord.BillingAt = conv.MustString(invoiceRecord.BillingAt, "")[:19]
- InvoiceRecords = append(InvoiceRecords, InvoiceRecord)
- }
- var InvoiceRecordsData http_model.InvoiceRecordsData
- InvoiceRecordsData.InvoiceRecordsPreviews = InvoiceRecords
- InvoiceRecordsData.Total = strconv.FormatInt(total, 10)
- return &InvoiceRecordsData, nil
- }
- func GetInvoiceList(ctx context.Context, req http_model.GetInvoiceListRequest) (*http_model.InvoiceListData, error) {
- db := GetReadDB(ctx)
- // 获取分页参数
- page := req.PageNum
- pageSize := req.PageSize
- if page < 1 {
- page = 1
- }
- if pageSize < 1 {
- pageSize = 10 // 设置默认每页记录数为 10
- }
- var invoicelist []gorm_model.YounggeeInvoiceRecord
- var total int64
- // 计算总记录数
- err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", req.Status).Count(&total).Error
- if err != nil {
- return &http_model.InvoiceListData{}, err
- }
- // 查询当前页的数据
- err = db.Model(gorm_model.YounggeeInvoiceRecord{}).
- Offset((page - 1) * pageSize). // 计算偏移量
- Limit(pageSize). // 设置每页限制
- Find(&invoicelist).Error
- if err != nil {
- return &http_model.InvoiceListData{}, err
- }
- invoicePointers := make([]*http_model.InvoiceListResponse, 0, len(invoicelist))
- for _, invoice := range invoicelist {
- var operator string
- if invoice.SubAccountID == 0 {
- operator = GetEnterprisenameByEnterpriseID(ctx, invoice.EnterpriseID)
- } else {
- operator = GetOperatorBySubacountID(ctx, invoice.SubAccountID)
- }
- response := &http_model.InvoiceListResponse{
- BillingID: invoice.BillingID,
- InvoiceAmount: invoice.InvoiceAmount,
- Operator: operator,
- SubmitAt: invoice.SubmitAt,
- AgreeAt: invoice.BillingAt,
- TaskNumber: GetTasknum(invoice.TaskIds),
- InvoiceType: invoice.InvoiceType,
- }
- invoicePointers = append(invoicePointers, response)
- }
- return &http_model.InvoiceListData{
- InvoiceListinfo: invoicePointers,
- Total: total,
- }, nil
- }
- func GetSupplierWithdrawList(ctx context.Context, req http_model.GetSupplierWithdrawListRequest) (*http_model.SupplierWithdrawListData, error) {
- db := GetReadDB(ctx)
- // 获取分页参数
- page := req.PageNum
- pageSize := req.PageSize
- if page < 1 {
- page = 1
- }
- if pageSize < 1 {
- pageSize = 10 // 设置默认每页记录数为 10
- }
- var withdrawlist []gorm_model.YounggeeSupplierWithdraw
- var total int64
- // 计算总记录数
- err := db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status = ?", req.Status).Count(&total).Error
- if err != nil {
- return &http_model.SupplierWithdrawListData{}, err
- }
- // 查询当前页的数据
- err = db.Model(gorm_model.YounggeeSupplierWithdraw{}).
- Offset((page - 1) * pageSize). // 计算偏移量
- Limit(pageSize). // 设置每页限制
- Find(&withdrawlist).Error
- if err != nil {
- return &http_model.SupplierWithdrawListData{}, err
- }
- withdrawPointers := make([]*http_model.SupplierWithdrawListResponse, 0, len(withdrawlist))
- for _, withdraw := range withdrawlist {
- var supplier gorm_model.YounggeeSupplier
- err = db.Model(gorm_model.YounggeeSupplier{}).Where("supplier_id = ?", withdraw.SupplierId).First(&supplier).Error
- if err != nil {
- return &http_model.SupplierWithdrawListData{}, err
- }
- var name string
- if supplier.SupplierType == 1 {
- name = supplier.Name
- } else {
- name = supplier.CompanyName
- }
- var InvoiceIDList []string
- TaskIDs := strings.Split(withdraw.InvoiceIds, ",")
- for _, taskId := range TaskIDs {
- InvoiceIDList = append(InvoiceIDList, taskId)
- }
- var invoiceIncome []gorm_model.YounggeeSupplierInvoice
- err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id IN ?", InvoiceIDList).Find(&invoiceIncome).Error
- if err != nil {
- return &http_model.SupplierWithdrawListData{}, err
- }
- // 收集所有的 incomeIDs
- var incomelist []string
- for _, invoice := range invoiceIncome {
- TaskIDs := strings.Split(invoice.IncomeIds, ",")
- for _, taskId := range TaskIDs {
- incomelist = append(incomelist, taskId)
- }
- }
- var supplier_income []gorm_model.YounggeeSupplierIncome
- err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomelist).Find(&supplier_income).Error
- if err != nil {
- return &http_model.SupplierWithdrawListData{}, err
- }
- var taskinfo []http_model.Tasklist
- taskinfo = make([]http_model.Tasklist, 0, len(supplier_income)) // 预分配足够的空间
- for _, income := range supplier_income {
- var info http_model.Tasklist
- // 根据 IncomeType 判断信息的来源
- switch income.IncomeType {
- case 1:
- info.Id = income.SProjectId
- case 3:
- info.Id = income.SLocalLifeId
- }
- info.Fee = income.SupplierChargeActual
- // 添加到任务信息列表
- taskinfo = append(taskinfo, info)
- }
- withdrawinfo := &http_model.Withdrawinfo{
- Name: withdraw.Name,
- IdNumber: withdraw.IdNumber,
- BankName: withdraw.BankName,
- BankNumber: withdraw.BankNumber,
- Phone: withdraw.Phone,
- }
- response := &http_model.SupplierWithdrawListResponse{
- WithDrawId: withdraw.SupplierWithdrawId,
- WithdrawAmount: withdraw.WithdrawAmount,
- ActualAmount: withdraw.AmountPayable,
- PhoneNumber: withdraw.Phone,
- SupplierName: supplier.SupplierName,
- Name: name,
- WithDrawinfo: withdrawinfo,
- SubmitAt: withdraw.SupplyTime,
- AggreeAt: withdraw.AgreeTime,
- ReajectAt: withdraw.RejectTime,
- ReajectReason: withdraw.FailReson,
- Taskinfo: &taskinfo,
- SupplierId: supplier.SupplierId,
- Avater: supplier.Avater,
- }
- withdrawPointers = append(withdrawPointers, response)
- }
- return &http_model.SupplierWithdrawListData{
- WithdrawListinfo: withdrawPointers,
- Total: total,
- }, nil
- }
- func GetSupplierInvoiceList(ctx context.Context, req http_model.GetSupplierInvoiceListRequest) (*http_model.SupplierInvoiceListData, error) {
- db := GetReadDB(ctx)
- // 获取分页参数
- page := req.PageNum
- pageSize := req.PageSize
- if page < 1 {
- page = 1
- }
- if pageSize < 1 {
- pageSize = 10 // 设置默认每页记录数为 10
- }
- var invoicelist []gorm_model.YounggeeSupplierInvoice
- var total int64
- // 计算总记录数
- err := db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_status = ?", req.Status).Count(&total).Error
- if err != nil {
- return &http_model.SupplierInvoiceListData{}, err
- }
- // 查询当前页的数据
- err = db.Model(gorm_model.YounggeeSupplierInvoice{}).
- Offset((page - 1) * pageSize). // 计算偏移量
- Limit(pageSize). // 设置每页限制
- Find(&invoicelist).Error
- if err != nil {
- return &http_model.SupplierInvoiceListData{}, err
- }
- invoicePointers := make([]*http_model.SupplierInvoiceListResponse, 0, len(invoicelist))
- for _, invoice := range invoicelist {
- var supplier gorm_model.YounggeeSupplier
- err = db.Model(gorm_model.YounggeeSupplier{}).Where("supplier_id = ?", invoice.SupplierId).First(&supplier).Error
- if err != nil {
- return &http_model.SupplierInvoiceListData{}, err
- }
- var name string
- if supplier.SupplierType == 1 {
- name = supplier.Name
- } else {
- name = supplier.CompanyName
- }
- var TaskIDList []string
- TaskIDs := strings.Split(invoice.IncomeIds, ",")
- for _, taskId := range TaskIDs {
- TaskIDList = append(TaskIDList, taskId)
- }
- var supplier_income []gorm_model.YounggeeSupplierIncome
- err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", TaskIDs).Find(&supplier_income).Error
- if err != nil {
- return &http_model.SupplierInvoiceListData{}, err
- }
- var taskinfo []http_model.Tasklist
- taskinfo = make([]http_model.Tasklist, 0, len(supplier_income)) // 预分配足够的空间
- var AllFee float64
- for _, income := range supplier_income {
- var info http_model.Tasklist
- // 根据 IncomeType 判断信息的来源
- switch income.IncomeType {
- case 1:
- info.Id = income.SProjectId
- case 3:
- info.Id = income.SLocalLifeId
- }
- info.Fee = income.SupplierChargeActual
- AllFee += income.SupplierChargeActual
- // 添加到任务信息列表
- taskinfo = append(taskinfo, info)
- }
- response := &http_model.SupplierInvoiceListResponse{
- InvoiceId: invoice.InvoiceId,
- SubmitAt: invoice.UploadInvoiceTime,
- AggreeAt: invoice.AgreeTime,
- ReajectAt: invoice.RejectTime,
- SupplierId: invoice.SupplierId,
- ReajectReason: invoice.FailReason,
- PhoneNumber: supplier.PhoneNumber,
- Name: name,
- SupplierName: supplier.SupplierName,
- CompanyName: invoice.Company,
- Taskinfo: &taskinfo,
- InvoiceAmount: AllFee,
- Avater: supplier.Avater,
- InvoiceUrl: invoice.InvoiceUrl,
- }
- invoicePointers = append(invoicePointers, response)
- }
- return &http_model.SupplierInvoiceListData{
- InvoiceListinfo: invoicePointers,
- Total: total,
- }, nil
- }
- func GetInvoiceInfo(ctx context.Context, request http_model.GetInvoiceInfoRequest) (*http_model.InvoiceInfoResponse, error) {
- db := GetReadDB(ctx)
- var invoice gorm_model.YounggeeInvoiceRecord
- err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id=?", request.BillingID).First(&invoice).Error
- if err != nil {
- return nil, err
- }
- res := &http_model.InvoiceInfoResponse{
- Businessname: GetEnterprisenameByEnterpriseID(ctx, invoice.EnterpriseID),
- TaxCode: invoice.TaxCode,
- RegisteredAddress: invoice.RegisteredAddress,
- RegisteredPhone: invoice.RegisteredPhone,
- Bank: invoice.Bank,
- BankCardNumber: invoice.BankCardNumber,
- InvoiceType: invoice.InvoiceType,
- }
- return res, nil
- }
- func ConfirmInvoice(ctx context.Context, request *http_model.ConfirmInvoiceRequest) error {
- db := GetReadDB(ctx)
- return db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id = ?", request.BillingId).Updates(
- gorm_model.YounggeeInvoiceRecord{
- BillingAt: time.Now(),
- InvoiceUrl: request.Url,
- Status: 2,
- }).Error
- }
- func SetInvoiceInfo(ctx context.Context, request *http_model.SetInvoiceInfoRequest) error {
- db := GetReadDB(ctx)
- invoiceInfo := gorm_model.YounggeePlatformInvoiceInfo{
- EnterpriseName: request.EnterpriseName,
- EnterpriseTax: request.EnterpriseTax,
- Phone: request.Phone,
- Address: request.Address,
- BankAccount: request.BankAccount,
- BankName: request.BankName,
- Project: request.Project,
- }
- return db.Create(&invoiceInfo).Error
- }
- func RefuseSupplierInvoice(ctx context.Context, request *http_model.RefuseSupplierInvoiceRequest) error {
- db := GetReadDB(ctx)
- return db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id = ?", request.InvoiceId).Updates(
- gorm_model.YounggeeSupplierInvoice{
- RejectTime: time.Now(),
- InvoiceStatus: 4,
- FailReason: request.Reason,
- }).Error
- }
- func ConfirmSupplierInvoice(ctx context.Context, request *http_model.ConfirmSupplierInvoiceRequest) error {
- db := GetReadDB(ctx)
- return db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id = ?", request.InvoiceId).Updates(
- gorm_model.YounggeeSupplierInvoice{
- AgreeTime: time.Now(),
- InvoiceStatus: 3,
- }).Error
- }
- func ConfirmWithdraw(ctx context.Context, request *http_model.ConfirmWithdrawRequest) error {
- db := GetReadDB(ctx)
- // 获取撤回记录
- var supplierincomeinfo gorm_model.YounggeeSupplierWithdraw
- err := db.Debug().Model(gorm_model.YounggeeSupplierWithdraw{}).
- Where("supplier_withdraw_id = ?", request.InvoiceId).
- First(&supplierincomeinfo).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Query YounggeeWithdrawRecord error, err:%+v", err)
- return err
- }
- err = db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("supplier_withdraw_id = ?", request.InvoiceId).Updates(
- gorm_model.YounggeeSupplierWithdraw{
- AgreeTime: time.Now(),
- WithdrawStatus: 3,
- }).Error
- if err != nil {
- return err
- }
- if supplierincomeinfo.SupplierType == 2 {
- var InvoiceIDList []string
- InvoiceIDs := strings.Split(supplierincomeinfo.InvoiceIds, ",")
- for _, taskId := range InvoiceIDs {
- InvoiceIDList = append(InvoiceIDList, taskId)
- }
- var invoiceIncome []gorm_model.YounggeeSupplierInvoice
- err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id IN ?", InvoiceIDList).Find(&invoiceIncome).Error
- if err != nil {
- return err
- }
- err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id in ?", InvoiceIDList).Updates(
- gorm_model.YounggeeSupplierInvoice{
- WithdrawStatus: 3,
- }).Error
- if err != nil {
- return err
- }
- // 收集所有的 incomeIDs
- var incomelist []string
- for _, invoice := range invoiceIncome {
- IncomeIds := strings.Split(invoice.IncomeIds, ",")
- for _, IncomeId := range IncomeIds {
- incomelist = append(incomelist, IncomeId)
- }
- }
- err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomelist).Updates(gorm_model.YounggeeSupplierIncome{WithdrawStatus: 3}).Error
- if err != nil {
- return err
- }
- } else {
- incomeids := strings.Split(supplierincomeinfo.IncomeIds, ",")
- err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomeids).Updates(gorm_model.YounggeeSupplierIncome{WithdrawStatus: 3}).Error
- if err != nil {
- return err
- }
- }
- return nil
- }
- func RefuseWithdraw(ctx context.Context, request *http_model.RefuseWithdrawRequest) error {
- db := GetReadDB(ctx)
- // 获取撤回记录
- var supplierincomeinfo gorm_model.YounggeeSupplierWithdraw
- err := db.Debug().Model(gorm_model.YounggeeSupplierWithdraw{}).
- Where("supplier_withdraw_id = ?", request.InvoiceId).
- First(&supplierincomeinfo).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[finance db] Query YounggeeWithdrawRecord error, err:%+v", err)
- return err
- }
- err = db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("supplier_withdraw_id = ?", request.InvoiceId).Updates(
- gorm_model.YounggeeSupplierWithdraw{
- RejectTime: time.Now(),
- WithdrawStatus: 4,
- FailReson: request.Reason,
- }).Error
- if err != nil {
- return err
- }
- if supplierincomeinfo.SupplierType == 2 {
- var InvoiceIDList []string
- InvoiceIDs := strings.Split(supplierincomeinfo.InvoiceIds, ",")
- for _, taskId := range InvoiceIDs {
- InvoiceIDList = append(InvoiceIDList, taskId)
- }
- var invoiceIncome []gorm_model.YounggeeSupplierInvoice
- err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id IN ?", InvoiceIDList).Find(&invoiceIncome).Error
- if err != nil {
- return err
- }
- err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id in ?", InvoiceIDList).Updates(
- gorm_model.YounggeeSupplierInvoice{
- WithdrawStatus: 1,
- }).Error
- if err != nil {
- return err
- }
- // 收集所有的 incomeIDs
- var incomelist []string
- for _, invoice := range invoiceIncome {
- IncomeIds := strings.Split(invoice.IncomeIds, ",")
- for _, IncomeId := range IncomeIds {
- incomelist = append(incomelist, IncomeId)
- }
- }
- err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomelist).Updates(gorm_model.YounggeeSupplierIncome{WithdrawStatus: 1}).Error
- if err != nil {
- return err
- }
- } else {
- incomeids := strings.Split(supplierincomeinfo.IncomeIds, ",")
- err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomeids).Updates(gorm_model.YounggeeSupplierIncome{WithdrawStatus: 1}).Error
- if err != nil {
- return err
- }
- }
- return nil
- }
- func GetTasknum(taskIds string) int {
- var ids [][]string // 修改为二维字符串切片
- if err := json.Unmarshal([]byte(taskIds), &ids); err != nil {
- return 0
- }
- count := 0
- for _, pair := range ids {
- count += len(pair)
- }
- return count
- }
- func GetOperatorBySubacountID(ctx context.Context, SubaccountID int) (username string) {
- db := GetReadDB(ctx)
- db = db.Model([]gorm_model.YounggeeSubAccount{}).Select("sub_account_name").Where("sub_account_id = ?", SubaccountID).First(&username)
- return username
- }
- func GetEnterprisenameByEnterpriseID(ctx context.Context, EnterpriseID string) (username string) {
- db := GetReadDB(ctx)
- db = db.Model([]gorm_model.Enterprise{}).Select("business_name").Where("enterprise_id = ?", EnterpriseID).First(&username)
- return username
- }
- func GetUsernameByUserID(ctx context.Context, UserID int64) (username string) {
- db := GetReadDB(ctx)
- db = db.Model([]gorm_model.YounggeeUser{}).Select("username").Where("id", UserID).First(&username)
- return username
- }
- func GetRechargeRecords(ctx context.Context, req *http_model.GetRechargeRecordsRequest, condition *common_model.RechargeRecordsCondition) (*http_model.RechargeRecordsData, error) {
- db := GetReadDB(ctx)
- var rechargeRecords []*gorm_model.YounggeeRechargeRecord
- db = db.Debug().Model(gorm_model.YounggeeRechargeRecord{}).Where("status = ?", req.Status)
- 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 == "commit_at" && value.Interface() != "" {
- db = db.Where(fmt.Sprintf("commit_at like '%s%%'", value.Interface()))
- }
- if tag == "confirm_at" && value.Interface() != "" {
- db = db.Where(fmt.Sprintf("confirm_at like '%s%%'", value.Interface()))
- }
- }
- if req.Username != "" {
- UserID := GetUserIDByUsername(ctx, req.Username)
- enterpriseId := GetEnterpriseIDByUserId(ctx, UserID)
- db = db.Where("enterprise_id = ?", enterpriseId)
- }
- if req.UserId != 0 {
- enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
- db = db.Where("enterprise_id = ?", enterpriseId)
- }
- if req.RechargeMethod == 1 {
- db = db.Where("recharge_method = ?", 1)
- } else if req.RechargeMethod == 2 {
- db = db.Where("recharge_method = ?", 2)
- } else if req.RechargeMethod == 3 {
- db = db.Where("recharge_method = ?", 3)
- }
- // 查询总数
- var total int64
- if err := db.Count(&total).Error; err != nil {
- logrus.WithContext(ctx).Errorf("[GetRechargeRecords] error query mysql total, err:%+v", err)
- return nil, err
- }
- if req.Status == 1 {
- db = db.Order("commit_at")
- } else {
- db = db.Order("confirm_at desc")
- }
- // 查询该页数据
- limit := req.PageSize
- offset := req.PageSize * req.PageNum // assert pageNum start with 0
- err := db.Limit(int(limit)).Offset(int(offset)).Find(&rechargeRecords).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[GetRechargeRecords] error query mysql limit, err:%+v", err)
- return nil, err
- }
- var enterpriseIds []string
- for _, rechargeRecord := range rechargeRecords {
- enterpriseIds = append(enterpriseIds, rechargeRecord.EnterpriseID)
- }
- util.RemoveStrRepByMap(enterpriseIds)
- enterpriseIdToUserInfoMap := make(map[string]gorm_model.Enterprise)
- db1 := GetReadDB(ctx)
- for _, v := range enterpriseIds {
- enterpriseInfo := gorm_model.Enterprise{}
- db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", v).Find(&enterpriseInfo)
- enterpriseIdToUserInfoMap[v] = enterpriseInfo
- }
- var RechargeRecords []*http_model.RechargeRecordsPreview
- for _, rechargeRecord := range rechargeRecords {
- RechargeRecord := new(http_model.RechargeRecordsPreview)
- RechargeRecord.RechargeId = rechargeRecord.RechargeID
- RechargeRecord.EnterpriseID = rechargeRecord.EnterpriseID
- RechargeRecord.RechargeAmount = rechargeRecord.RechargeAmount
- RechargeRecord.ConfirmAt = conv.MustString(rechargeRecord.ConfirmAt, "")[:19]
- RechargeRecord.CommitAt = conv.MustString(rechargeRecord.CommitAt, "")[:19]
- RechargeRecord.Phone = rechargeRecord.Phone
- RechargeRecord.TransferVoucher = rechargeRecord.TransferVoucherUrl
- RechargeRecord.RechargeMethod = consts.GetRechargeMethod(rechargeRecord.RechargeMethod)
- RechargeRecord.UserId = rechargeRecord.EnterpriseID
- RechargeRecord.Username = GetUsernameByUserID(ctx, enterpriseIdToUserInfoMap[rechargeRecord.EnterpriseID].UserID)
- RechargeRecord.BusinessName = enterpriseIdToUserInfoMap[rechargeRecord.EnterpriseID].BusinessName
- RechargeRecords = append(RechargeRecords, RechargeRecord)
- }
- var RechargeRecordsData http_model.RechargeRecordsData
- RechargeRecordsData.RechargeRecordsPreview = RechargeRecords
- RechargeRecordsData.Total = conv.MustString(total, "")
- return &RechargeRecordsData, nil
- }
- func OperateRecharge(ctx context.Context, req *http_model.OperateRechargeRequest) error {
- db := GetReadDB(ctx)
- db1 := GetReadDB(ctx)
- err := db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", req.EnterpriseId).Updates(map[string]interface{}{
- "balance": gorm.Expr("balance + ?", req.RechargeAmount),
- "available_balance": gorm.Expr("available_balance + ?", req.RechargeAmount)}).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[OperateRecharge] error Updates balance, err:%+v", err)
- return err
- }
- err1 := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Updates(gorm_model.YounggeeRechargeRecord{
- Status: 2,
- InvoiceStatus: 2,
- ConfirmAt: time.Now(),
- }).Error
- if err1 != nil {
- logrus.WithContext(ctx).Errorf("[OperateRecharge] error Updates Status, err:%+v", err)
- return err1
- }
- if req.Method == 1 {
- db2 := GetReadDB(ctx)
- db2.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Updates(gorm_model.YounggeeRechargeRecord{
- RechargeAmount: req.RechargeAmount,
- })
- }
- return nil
- }
- func GetBillTaskList(ctx context.Context, req http_model.GetBillTaskListRequest) (*http_model.BillTaskData, error) {
- db := GetReadDB(ctx)
- // 获取分页参数
- page := req.PageNum
- pageSize := req.PageSize
- if page < 1 {
- page = 1
- }
- if pageSize < 1 {
- pageSize = 10 // 设置默认每页记录数为 10
- }
- var invoice gorm_model.YounggeeInvoiceRecord
- err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id = ?", req.BillingID).First(&invoice).Error
- if err != nil {
- return nil, err
- }
- var ids [][]string // 二维切片用于存储二元组
- if err = json.Unmarshal([]byte(invoice.TaskIds), &ids); err != nil {
- return nil, err // 返回错误信息
- }
- var results []*http_model.BillTaskDataResponse // 用于存储合并后的结果
- // 检查ID组的有效性
- if len(ids) != 3 {
- return nil, fmt.Errorf("task_ids must contain three groups")
- }
- // 第一组ID对应 selection 表
- if len(ids[0]) > 0 {
- var selections []gorm_model.YounggeeSelectionInfo
- if err := db.Where("selection_id IN ?", ids[0]).Find(&selections).Error; err != nil {
- return nil, err
- }
- for _, selection := range selections {
- var productName string
- var productPrice float64
- var mainImage string
- var creater string
- product, err := GetProductByID(ctx, int64(selection.ProductID))
- if err == nil && product != nil {
- productName = product.ProductName
- productPrice = product.ProductPrice
- }
- if selection.SubAccountID == 0 {
- creater = GetEnterprisenameByEnterpriseID(ctx, selection.EnterpriseID)
- } else {
- creater = GetOperatorBySubacountID(ctx, selection.SubAccountID)
- }
- mainImage, err = GetMainPhotoByProductID(ctx, int64(selection.ProductID))
- results = append(results, &http_model.BillTaskDataResponse{
- TaskType: 1,
- TaskForm: selection.TaskMode,
- Platform: selection.Platform,
- ContentType: selection.ContentType,
- SettleAmount: selection.SettlementAmount,
- Operator: creater,
- PhotoUrl: mainImage,
- Price: productPrice,
- ProductName: productName,
- Taskid: selection.SelectionID,
- })
- }
- }
- // 第二组ID对应 project 表
- if len(ids[1]) > 0 {
- var projects []gorm_model.ProjectInfo
- if err := db.Where("project_id IN ?", ids[1]).Find(&projects).Error; err != nil {
- return nil, err
- }
- for _, project := range projects {
- var productName string
- var productPrice float64
- var mainImage string
- var creater string
- product, err := GetProductByID(ctx, project.ProductID)
- if err == nil && product != nil {
- productName = product.ProductName
- productPrice = product.ProductPrice
- }
- if project.SubAccountID == 0 {
- creater = GetEnterprisenameByEnterpriseID(ctx, project.EnterpriseID)
- } else {
- creater = GetOperatorBySubacountID(ctx, int(project.SubAccountID))
- }
- mainImage, err = GetMainPhotoByProductID(ctx, project.ProductID)
- results = append(results, &http_model.BillTaskDataResponse{
- TaskType: 2,
- TaskForm: int(project.ProjectForm),
- ContentType: int(project.ContentType),
- SettleAmount: conv.MustString(project.SettlementAmount, ""),
- Operator: creater,
- PhotoUrl: mainImage,
- Price: productPrice,
- ProductName: productName,
- Taskid: project.ProjectID,
- })
- }
- }
- // 第三组ID对应 llocalife 表
- if len(ids[2]) > 0 {
- var localifes []gorm_model.YounggeeLocalLifeInfo
- if err := db.Where("local_id IN ?", ids[2]).Find(&localifes).Error; err != nil {
- return nil, err
- }
- for _, localife := range localifes {
- var productName string
- var address string
- var mainImage string
- var creater string
- store, err := GetStoreByID(ctx, int64(localife.StoreId))
- if err == nil && store != nil {
- productName = store.StoreName
- address = store.StoreLocation
- }
- if localife.SubAccountId == 0 {
- creater = GetEnterprisenameByEnterpriseID(ctx, localife.EnterpriseId)
- } else {
- creater = GetOperatorBySubacountID(ctx, localife.SubAccountId)
- }
- mainImage, err = GetMainPhotoByStoreID(ctx, int64(localife.StoreId))
- results = append(results, &http_model.BillTaskDataResponse{
- TaskType: 3,
- TaskForm: localife.TaskForm,
- ContentType: localife.ContentType,
- SettleAmount: conv.MustString(localife.SettlementAmount, ""),
- Operator: creater,
- Taskid: localife.LocalId,
- ProductName: productName,
- Address: address,
- PhotoUrl: mainImage,
- })
- }
- }
- // 计算总数
- total := len(results)
- // 分页处理
- start := (page - 1) * pageSize
- if start >= total {
- return &http_model.BillTaskData{InvoiceListinfo: nil, Total: total}, nil // 返回空结果
- }
- end := start + pageSize
- if end > total {
- end = total
- }
- // 返回分页后的结果
- return &http_model.BillTaskData{
- InvoiceListinfo: results[start:end],
- Total: total,
- }, nil
- }
- func GetMainPhotoByStoreID(ctx context.Context, storeID int64) (string, error) {
- db := GetReadDB(ctx)
- var productPhoto gorm_model.YounggeeProductPhoto
- err := db.Where("store_id = ? AND symbol = ?", storeID, 1).First(&productPhoto).Error
- if err != nil {
- return "", err
- }
- return productPhoto.PhotoUrl, nil
- }
- func GetMainPhotoByProductID(ctx context.Context, productId int64) (string, error) {
- db := GetReadDB(ctx)
- var productPhoto gorm_model.YounggeeProductPhoto
- err := db.Where("product_id = ? AND symbol = ?", productId, 1).First(&productPhoto).Error
- if err != nil {
- return "", err
- }
- return productPhoto.PhotoUrl, nil
- }
- func GetStoreByID(ctx context.Context, stroeid int64) (*gorm_model.YounggeeStore, error) {
- db := GetReadDB(ctx)
- store := &gorm_model.YounggeeStore{}
- err := db.First(&store, stroeid).Error
- if err != nil {
- if err == gorm.ErrRecordNotFound {
- return nil, nil
- } else {
- return nil, err
- }
- }
- return store, nil
- }
|