12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730 |
- 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) (*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) (*http_model.TalentWithdrawValue, error) {
- var withdrawvalue float64
- db := GetReadDB(ctx)
- var withdrawlist []gorm_model.YounggeeWithdrawRecord
- // 获取所有符合条件的发票
- err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("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 GetWithDrawInfo(ctx context.Context, request http_model.GetWithDrawInfoRequest) (*http_model.WithDrawInfoResponse, error) {
- db := GetReadDB(ctx)
- var withdrawinfo gorm_model.YounggeeWithdrawRecord
- fmt.Println(request.Withdrawid)
- err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("withdraw_id = ?", request.Withdrawid).Find(&withdrawinfo).Error
- if err != nil {
- return nil, err
- }
- res := &http_model.WithDrawInfoResponse{
- Operator: withdrawinfo.Name,
- WithdrawAmount: withdrawinfo.WithdrawAmount,
- AmountPayable: withdrawinfo.AmountPayable,
- }
- return res, nil
- }
- func GetAllwithdrawValue(ctx context.Context) (*http_model.GetAllwithdrawValueData, error) {
- allvalues, err := GetWithdrawValue(ctx)
- if err != nil {
- return nil, err
- }
- talentvalue, err := GetTalentWithdrawPrevalue(ctx)
- if err != nil {
- return nil, err
- }
- suppliervalue, err := GetSupplierWithdrawPrevalue(ctx)
- if err != nil {
- return nil, err
- }
- return &http_model.GetAllwithdrawValueData{
- WithdrawValue: suppliervalue.WithdrawValue,
- TalentWithdrawvalue: talentvalue.TalentWithdrawValue,
- AccumulateValue: allvalues.AllWithdrawValue,
- }, err
- }
- func GetWithdrawValue(ctx context.Context) (*http_model.ALlWithdrawValue, error) {
- var withdrawvalue float64
- db := GetReadDB(ctx)
- var withdrawlist []gorm_model.YounggeeWithdrawRecord
- // 获取所有符合条件的发票
- err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("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("invoice_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 RefuseRecharge error,err:%+v", err)
- return nil, err
- }
- res := &http_model.RefuseRechargeData{
- Id: req.Id,
- }
- return res, nil
- }
- func GetTalentWithdrawInfo(ctx context.Context, req http_model.GetTalentWithdrawInfoRequest) (*http_model.TalentWithdrawResponse, error) {
- db := GetReadDB(ctx)
- var withdrawinfo gorm_model.YounggeeWithdrawRecord
- err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("withdraw_id = ?", req.WithdrawId).First(&withdrawinfo).Error
- if err != nil {
- return &http_model.TalentWithdrawResponse{}, err
- }
- res := &http_model.TalentWithdrawResponse{
- Name: withdrawinfo.Name,
- IdcardNumber: withdrawinfo.IdcardNum,
- PhoneNumber: withdrawinfo.PhoneNum,
- BankNumber: withdrawinfo.BankNum,
- }
- return res, nil
- }
- func GetRechargeInfo(ctx context.Context, req http_model.GetRechargeInfoRequest) (*http_model.RechargeInfoResponse, error) {
- db := GetReadDB(ctx)
- var rechargeinfo gorm_model.YounggeeRechargeRecord
- err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).First(&rechargeinfo).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call GetRechargeInfo error,err:%+v", err)
- return nil, err
- }
- var operator string
- enterprise := gorm_model.Enterprise{}
- result := db.Where(gorm_model.Enterprise{EnterpriseID: rechargeinfo.EnterpriseID}).First(&enterprise)
- if result.Error != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call Enterprise error,err:%+v", err)
- return nil, result.Error
- }
- if rechargeinfo.SubAccountID != 0 {
- subaccountinfo := gorm_model.YounggeeSubAccount{}
- userResult := db.Where(gorm_model.YounggeeSubAccount{SubAccountId: rechargeinfo.SubAccountID, SubAccountType: 1}).First(&subaccountinfo)
- if userResult.Error != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call YounggeeSubAccount error,err:%+v", err)
- return nil, userResult.Error
- }
- operator = subaccountinfo.SubAccountName
- } else {
- operator = enterprise.BusinessName
- }
- res := &http_model.RechargeInfoResponse{
- RechargeAmount: rechargeinfo.RechargeAmount,
- Operator: operator,
- MainAccount: enterprise.BusinessName,
- }
- 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
- // 先查询当前页的数据,不考虑 "Others" 筛选条件
- err := db.Model(gorm_model.YounggeeRechargeRecord{}).
- Where("status = ?", req.Status).
- Offset((page - 1) * pageSize). // 计算偏移量
- Limit(pageSize). // 设置每页限制
- Find(&prerechargelist).Error
- if err != nil {
- return &http_model.PreRechargeListData{}, err
- }
- // 在查询到的数据上进行筛选 (比如 "Others" 筛选条件)
- filteredList := make([]gorm_model.YounggeeRechargeRecord, 0)
- for _, recharge := range prerechargelist {
- var operator string
- enterprise := gorm_model.Enterprise{}
- result := db.Where(gorm_model.Enterprise{EnterpriseID: recharge.EnterpriseID}).First(&enterprise)
- if result.Error != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call Enterprise error,err:%+v", err)
- return nil, result.Error
- }
- if recharge.SubAccountID != 0 {
- subaccountinfo := gorm_model.YounggeeSubAccount{}
- userResult := db.Where(gorm_model.YounggeeSubAccount{SubAccountId: recharge.SubAccountID, SubAccountType: 1}).First(&subaccountinfo)
- if userResult.Error != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call YounggeeSubAccount error,err:%+v", err)
- return nil, userResult.Error
- }
- operator = subaccountinfo.SubAccountName
- } else {
- operator = enterprise.BusinessName
- }
- // 进行 "Others" 筛选条件
- if req.Others != "" {
- if operator != req.Others && req.Others != recharge.RechargeID {
- continue
- }
- }
- // 满足筛选条件的记录添加到 filteredList
- filteredList = append(filteredList, recharge)
- }
- // 计算筛选后的总记录数
- total = int64(len(filteredList))
- // 构建返回结果
- rechargePointers := make([]*http_model.PreRechargeListResponse, 0, len(filteredList))
- for _, recharge := range filteredList {
- var operator string
- enterprise := gorm_model.Enterprise{}
- result := db.Where(gorm_model.Enterprise{EnterpriseID: recharge.EnterpriseID}).First(&enterprise)
- if result.Error != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call Enterprise error,err:%+v", err)
- return nil, result.Error
- }
- if recharge.SubAccountID != 0 {
- subaccountinfo := gorm_model.YounggeeSubAccount{}
- userResult := db.Where(gorm_model.YounggeeSubAccount{SubAccountId: recharge.SubAccountID, SubAccountType: 1}).First(&subaccountinfo)
- if userResult.Error != nil {
- logrus.WithContext(ctx).Errorf("[Recharge service] call YounggeeSubAccount error,err:%+v", err)
- return nil, userResult.Error
- }
- operator = subaccountinfo.SubAccountName
- } else {
- operator = enterprise.BusinessName
- }
- // 构建返回响应数据
- response := &http_model.PreRechargeListResponse{
- ID: recharge.RechargeID,
- EnterPrise: enterprise.BusinessName,
- Operator: operator,
- RechargeAmount: recharge.RechargeAmount,
- RechargeMethod: recharge.RechargeMethod,
- TransferVoucherUrl: recharge.TransferVoucherUrl,
- Phone: recharge.Phone,
- CommitAt: recharge.CommitAt.Format("2006-01-02 15:04:05"),
- ConfirmAt: recharge.ConfirmAt.Format("2006-01-02 15:04:05"),
- RefuseAt: recharge.RefuseAt.Format("2006-01-02 15:04:05"),
- FailReason: recharge.FailReason,
- }
- rechargePointers = append(rechargePointers, response)
- }
- return &http_model.PreRechargeListData{
- PreRechargeListinfo: rechargePointers,
- Total: total, // 返回筛选后的总记录数
- }, nil
- }
- func GetRechargenums(db *gorm.DB, status int) (int64, error) {
- query := db.Model(&gorm_model.YounggeeRechargeRecord{}).Where("status = ?", status)
- // 计算总数
- var total int64
- if err := query.Count(&total).Error; err != nil {
- return 0, err
- }
- return total, nil
- }
- func GetRechargeCount(ctx context.Context) (*http_model.RechargeCountResponse, error) {
- db := GetReadDB(ctx)
- pre, _ := GetRechargenums(db, 1)
- ing, _ := GetRechargenums(db, 2)
- ed, _ := GetRechargenums(db, 3)
- res := &http_model.RechargeCountResponse{
- PreRechargeNums: pre,
- RechargeNums: ing,
- FailNums: ed,
- }
- return res, nil
- }
- func GetWithdrawnums(db *gorm.DB, status int) (int64, error) {
- query := db.Model(&gorm_model.YounggeeWithdrawRecord{}).Where("status = ?", status)
- // 计算总数
- var total int64
- if err := query.Count(&total).Error; err != nil {
- return 0, err
- }
- return total, nil
- }
- func GetWithdrawCount(ctx context.Context) (*http_model.WithdrawCountResponse, error) {
- db := GetReadDB(ctx)
- pre, _ := GetWithdrawnums(db, 1)
- ing, _ := GetWithdrawnums(db, 2)
- ed, _ := GetWithdrawnums(db, 3)
- res := &http_model.WithdrawCountResponse{
- PreWithdrawNums: pre,
- WithdrawNums: ing,
- FailNums: ed,
- }
- return res, nil
- }
- func GetTalentWithdrawList(ctx context.Context, req *http_model.GetTalentWithdrawListRequest) (*http_model.TalentWithdrawListData, error) {
- db := GetReadDB(ctx)
- pageSize := req.PageSize
- var withdrawlist []gorm_model.YounggeeWithdrawRecord
- var total int64
- // 计算总记录数
- query := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("status = ?", req.Status)
- if req.Others != "" {
- query = query.Where("withdraw_id LIKE ? OR Name LIKE ?", "%"+req.Others+"%", "%"+req.Others+"%")
- }
- // 添加 during 条件(时间范围筛选)
- if req.During != "" {
- // 解析 during 参数为时间
- startTime, err := time.Parse("2006-01-02", req.During)
- if err != nil {
- return nil, fmt.Errorf("invalid during format, expected 'YYYY-MM-DD'")
- }
- endTime := startTime.Add(24 * time.Hour).Add(-1 * time.Second) // 当天的 23:59:59
- // 添加时间范围条件
- var timeField string
- switch req.Status {
- case 1:
- timeField = "submit_at"
- case 2:
- timeField = "withdraw_at"
- case 3:
- timeField = "reject_at"
- }
- query = query.Where(
- fmt.Sprintf("%s BETWEEN ? AND ?", timeField), // 字段名安全拼接
- startTime,
- endTime,
- )
- }
- if err := query.Count(&total).Error; err != nil {
- return nil, err
- }
- // 添加分页逻辑
- if pageSize == 0 {
- pageSize = 10
- }
- pageNum := req.PageNum
- if pageNum == 0 {
- pageNum = 1
- }
- offset := (pageNum - 1) * pageSize
- if err := query.Offset(offset).Limit(pageSize).Find(&withdrawlist).Error; err != nil {
- return nil, 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.Format("2006-01-02 15:04:05"),
- SubmitAt: withdraw.SubmitAt.Format("2006-01-02 15:04:05"),
- WithdrawAt: withdraw.WithdrawAt.Format("2006-01-02 15:04:05"),
- 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, withdrawIdsStr string) (*http_model.ConfirmWithdrawalResponse, error) {
- // 分割 withdrawId 列表
- withdrawIds := strings.Split(withdrawIdsStr, ",")
- res := http_model.ConfirmWithdrawalResponse{
- WithdrawId: withdrawIdsStr,
- }
- if len(withdrawIds) == 0 {
- return &res, fmt.Errorf("empty withdrawIds list")
- }
- db := GetReadDB(ctx) // 使用统一的 DB 实例
- // 开启事务
- return &res, db.Transaction(func(tx *gorm.DB) error {
- // 1. 批量更新提现记录状态
- if err := tx.Model(&gorm_model.YounggeeWithdrawRecord{}).
- Where("withdraw_id IN (?)", withdrawIds).
- Updates(map[string]interface{}{
- "status": 2,
- "withdraw_at": time.Now(),
- }).Error; err != nil {
- return fmt.Errorf("批量更新提现记录失败: %v", err)
- }
- // 2. 获取所有提现记录的 IncomeIdList(保留重复项)
- var withdrawRecords []gorm_model.YounggeeWithdrawRecord
- if err := tx.Where("withdraw_id IN (?)", withdrawIds).
- Find(&withdrawRecords).Error; err != nil {
- return fmt.Errorf("查询提现记录失败: %v", err)
- }
- // 3. 收集所有 IncomeId(包含重复项)
- var rawIncomeIds []string
- talentAmountMap := make(map[string]float64) // talentID -> 总提现金额
- for _, record := range withdrawRecords {
- // 按原始顺序保留所有 IncomeId(含重复)
- ids := strings.Split(record.IncomeIdList, ",")
- rawIncomeIds = append(rawIncomeIds, ids...)
- // 累加提现金额
- talentAmountMap[record.TalentID] += record.WithdrawAmount
- }
- // 4. 更新所有 Income 记录状态(含重复项)
- if len(rawIncomeIds) > 0 {
- if err := tx.Model(&gorm_model.YounggeeTalentIncome{}).
- Where("id IN (?)", rawIncomeIds).
- Update("withdraw_status", 1).Error; err != nil {
- return fmt.Errorf("更新收入状态失败: %v", err)
- }
- }
- // 5. 查询所有 Income 记录详情(含重复项)
- var incomeList []gorm_model.YounggeeTalentIncome
- if err := tx.Where("id IN (?)", rawIncomeIds).
- Find(&incomeList).Error; err != nil {
- return fmt.Errorf("查询收入详情失败: %v", err)
- }
- // 6. 按类型收集所有任务ID(含重复项)
- type TaskRef struct {
- Type int
- TaskIDs []string
- }
- var taskRefs []TaskRef
- for _, income := range incomeList {
- var taskID string
- switch income.Type {
- case 1:
- taskID = income.SectaskID
- case 2:
- taskID = income.TaskID
- case 3:
- taskID = income.LocalTaskId
- default:
- continue
- }
- // 允许重复的 TaskID
- taskRefs = append(taskRefs, TaskRef{
- Type: income.Type,
- TaskIDs: []string{taskID},
- })
- }
- // 7. 批量更新任务状态(允许重复更新)
- for _, ref := range taskRefs {
- var model interface{}
- switch ref.Type {
- case 1:
- model = &gorm_model.YounggeeSecTaskInfo{}
- case 2:
- model = &gorm_model.YoungeeTaskInfo{}
- case 3:
- model = &gorm_model.YoungeeLocalTaskInfo{}
- default:
- continue
- }
- if err := tx.Model(model).
- Where("task_id IN (?)", ref.TaskIDs).
- Update("withdraw_status", 4).Error; err != nil {
- return fmt.Errorf("更新任务状态失败: %v", err)
- }
- }
- // 8. 更新人才账户金额
- for talentID, amount := range talentAmountMap {
- if err := tx.Model(&gorm_model.YoungeeTalentInfo{}).
- Where("id = ?", talentID).
- Updates(map[string]interface{}{
- "withdrawing": gorm.Expr("withdrawing - ?", amount),
- "withdrawed": gorm.Expr("withdrawed + ?", amount),
- }).Error; err != nil {
- return fmt.Errorf("更新人才账户失败: %v", err)
- }
- }
- 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.Supplier
- err = db.Model(gorm_model.Supplier{}).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.Format("2006-01-02 15:04:05"),
- AggreeAt: withdraw.AgreeTime.Format("2006-01-02 15:04:05"),
- ReajectAt: withdraw.RejectTime.Format("2006-01-02 15:04:05"),
- 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.Supplier
- err = db.Model(gorm_model.Supplier{}).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
- }
|