finance.go 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. package db
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "reflect"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "youngee_m_api/consts"
  11. "youngee_m_api/model/common_model"
  12. "youngee_m_api/model/gorm_model"
  13. "youngee_m_api/model/http_model"
  14. "youngee_m_api/util"
  15. "github.com/caixw/lib.go/conv"
  16. "github.com/sirupsen/logrus"
  17. "gorm.io/gorm"
  18. )
  19. func GetWithdrawRecords(ctx context.Context, pageSize, pageNum int32, req *http_model.WithdrawalRecordsRequest, condition *common_model.WithdrawRecordsCondition) (*http_model.WithdrawalRecordsPreview, error) {
  20. db := GetReadDB(ctx)
  21. var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
  22. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{})
  23. conditionType := reflect.TypeOf(condition).Elem()
  24. conditionValue := reflect.ValueOf(condition).Elem()
  25. for i := 0; i < conditionType.NumField(); i++ {
  26. field := conditionType.Field(i)
  27. tag := field.Tag.Get("condition")
  28. value := conditionValue.FieldByName(field.Name)
  29. if tag == "submit_at" && value.Interface() != "" {
  30. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  31. }
  32. if tag == "withdraw_at" && value.Interface() != "" {
  33. db = db.Where(fmt.Sprintf("withdraw_at like '%s%%'", value.Interface()))
  34. }
  35. if !util.IsBlank(value) && tag != "submit_at" && tag != "withdraw_at" {
  36. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  37. }
  38. }
  39. if req.TalentName != "" {
  40. fmt.Println("TalentName:", req.TalentName)
  41. db1 := GetReadDB(ctx)
  42. var talentId string
  43. db1.Model(gorm_model.YoungeeTalentInfo{}).Select("id").Where(fmt.Sprintf("talent_wx_nickname like '%%%s%%'", req.TalentName)).Find(&talentId)
  44. db = db.Where("talent_id = ?", talentId)
  45. }
  46. // 查询总数
  47. var total int64
  48. if err := db.Count(&total).Error; err != nil {
  49. logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql total, err:%+v", err)
  50. return nil, err
  51. }
  52. // 查询该页数据
  53. limit := pageSize
  54. offset := pageSize * pageNum // assert pageNum start with 0
  55. err := db.Order("submit_at").Limit(int(limit)).Offset(int(offset)).Find(&withdrawRecords).Error
  56. if err != nil {
  57. logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql limit, err:%+v", err)
  58. return nil, err
  59. }
  60. var talentIds []string
  61. for _, withdrawRecord := range withdrawRecords {
  62. talentIds = append(talentIds, withdrawRecord.TalentID)
  63. }
  64. talentIdToTalentInfoMap := make(map[string]gorm_model.YoungeeTalentInfo)
  65. for _, talentId := range talentIds {
  66. db1 := GetReadDB(ctx)
  67. talentInfo := gorm_model.YoungeeTalentInfo{}
  68. db1.Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", talentId).Find(&talentInfo)
  69. talentIdToTalentInfoMap[talentId] = talentInfo
  70. }
  71. var withdrawRecordsDatas []*http_model.WithdrawalRecordsData
  72. for _, withdrawRecord := range withdrawRecords {
  73. withdrawRecordsData := new(http_model.WithdrawalRecordsData)
  74. withdrawRecordsData.WithdrawId = withdrawRecord.WithdrawID
  75. withdrawRecordsData.TalentId = withdrawRecord.TalentID
  76. withdrawRecordsData.TalentName = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentWxNickname
  77. withdrawRecordsData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
  78. withdrawRecordsData.AmountPayable = float32(withdrawRecord.AmountPayable)
  79. withdrawRecordsData.ReceiveInfo = withdrawRecord.ReceiveInfo
  80. withdrawRecordsData.BankType = withdrawRecord.BankType
  81. withdrawRecordsData.Phone = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentPhoneNumber
  82. withdrawRecordsData.SubmitAt = conv.MustString(withdrawRecord.SubmitAt, "")[0:19]
  83. withdrawRecordsData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
  84. withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordsData)
  85. }
  86. var withdrawRecordsPreview http_model.WithdrawalRecordsPreview
  87. withdrawRecordsPreview.WithdrawalRecordsData = withdrawRecordsDatas
  88. withdrawRecordsPreview.Total = total
  89. return &withdrawRecordsPreview, nil
  90. }
  91. func GetWithdrawRecord(ctx context.Context, req *http_model.GetWithdrawalRecordRequest) (*http_model.WithdrawalRecordPreview, error) {
  92. db := GetReadDB(ctx)
  93. //fmt.Println("talentId:", req.TalentId)
  94. var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
  95. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Where("talent_id = ? AND status = ?", req.TalentId, 2).Find(&withdrawRecords)
  96. var withdrawRecordsPreview http_model.WithdrawalRecordPreview
  97. var withdrawRecordsDatas []*http_model.WithdrawalRecordData
  98. for _, withdrawRecord := range withdrawRecords {
  99. withdrawRecordData := new(http_model.WithdrawalRecordData)
  100. withdrawRecordData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
  101. withdrawRecordData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
  102. withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordData)
  103. }
  104. withdrawRecordsPreview.WithdrawalRecordData = withdrawRecordsDatas
  105. return &withdrawRecordsPreview, nil
  106. }
  107. func GetWithdrawNums(ctx context.Context) (*http_model.WithdrawNums, error) {
  108. var withdrawNums int64
  109. db := GetReadDB(ctx)
  110. err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("status = 1").Count(&withdrawNums).Error
  111. if err != nil {
  112. return nil, err
  113. }
  114. WithdrawNums := new(http_model.WithdrawNums)
  115. WithdrawNums.WithdrawNums = int(withdrawNums)
  116. return WithdrawNums, err
  117. }
  118. func GetRechargeValue(ctx context.Context, request http_model.GetRechargeValueRequest) (*http_model.RechargeValue, error) {
  119. var rechargeValue float64
  120. db := GetReadDB(ctx)
  121. var rechargelist []gorm_model.YounggeeRechargeRecord
  122. err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("status = ?", request.Status).Find(rechargelist).Error
  123. if err != nil {
  124. return nil, err
  125. }
  126. for _, recharge := range rechargelist {
  127. rechargeValue += recharge.RechargeAmount
  128. }
  129. RechargeValue := new(http_model.RechargeValue)
  130. RechargeValue.RechargeValue = rechargeValue
  131. return RechargeValue, err
  132. }
  133. func GetInvoiceValue(ctx context.Context, request http_model.GetInvoiceValueRequest) (*http_model.InvoiceValue, error) {
  134. var invoicevalue float64
  135. db := GetReadDB(ctx)
  136. var invoicelist []gorm_model.YounggeeInvoiceRecord
  137. err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", request.Status).Find(invoicelist).Error
  138. if err != nil {
  139. return nil, err
  140. }
  141. for _, invoice := range invoicelist {
  142. invoicevalue += invoice.InvoiceAmount
  143. }
  144. InvoiceValue := new(http_model.InvoiceValue)
  145. InvoiceValue.InvoiceValue = invoicevalue
  146. return InvoiceValue, err
  147. }
  148. func GetSupplierWithdrawPrevalue(ctx context.Context, request http_model.GetSupplierWithdrawPrevalueRequest) (*http_model.WithdrawValue, error) {
  149. var withdrawvalue float64
  150. db := GetReadDB(ctx)
  151. var withdrawlist []gorm_model.YounggeeSupplierWithdraw
  152. // 获取所有符合条件的发票
  153. err := db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status =2").Find(&withdrawlist).Error
  154. if err != nil {
  155. return nil, err
  156. }
  157. for _, withdraw := range withdrawlist {
  158. withdrawvalue += withdraw.WithdrawAmount
  159. }
  160. return &http_model.WithdrawValue{
  161. withdrawvalue,
  162. }, err
  163. }
  164. func GetTalentWithdrawPrevalue(ctx context.Context, request http_model.GetTalentWithdrawPrevalueRequest) (*http_model.TalentWithdrawValue, error) {
  165. var withdrawvalue float64
  166. db := GetReadDB(ctx)
  167. var withdrawlist []gorm_model.YounggeeWithdrawRecord
  168. // 获取所有符合条件的发票
  169. err := db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status =1").Find(&withdrawlist).Error
  170. if err != nil {
  171. return nil, err
  172. }
  173. for _, withdraw := range withdrawlist {
  174. withdrawvalue += withdraw.WithdrawAmount
  175. }
  176. return &http_model.TalentWithdrawValue{
  177. withdrawvalue,
  178. }, err
  179. }
  180. func GetWithdrawValue(ctx context.Context, request http_model.GetWithdrawValueRequest) (*http_model.ALlWithdrawValue, error) {
  181. var withdrawvalue float64
  182. db := GetReadDB(ctx)
  183. var withdrawlist []gorm_model.YounggeeWithdrawRecord
  184. // 获取所有符合条件的发票
  185. err := db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status =2").Find(&withdrawlist).Error
  186. if err != nil {
  187. return nil, err
  188. }
  189. for _, withdraw := range withdrawlist {
  190. withdrawvalue += withdraw.WithdrawAmount
  191. }
  192. var swithdrawlist []gorm_model.YounggeeSupplierWithdraw
  193. // 获取所有符合条件的发票
  194. err = db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status =3").Find(&swithdrawlist).Error
  195. if err != nil {
  196. return nil, err
  197. }
  198. for _, withdraw := range withdrawlist {
  199. withdrawvalue += withdraw.WithdrawAmount
  200. }
  201. return &http_model.ALlWithdrawValue{
  202. withdrawvalue,
  203. }, err
  204. }
  205. func GetSupplierInvoiceValue(ctx context.Context, request http_model.GetSupplierInvoiceValueRequest) (*http_model.SupplierInvoiceValue, error) {
  206. var invoicevalue float64
  207. db := GetReadDB(ctx)
  208. var invoicelist []gorm_model.YounggeeSupplierInvoice
  209. // 获取所有符合条件的发票
  210. err := db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("status = ?", request.Status).Find(&invoicelist).Error
  211. if err != nil {
  212. return nil, err
  213. }
  214. // 收集所有的 TaskIDs
  215. var taskIDList []int
  216. for _, invoice := range invoicelist {
  217. TaskIDs := strings.Split(invoice.IncomeIds, ",")
  218. for _, taskId := range TaskIDs {
  219. // 将 taskId 转换为 int 并添加到 taskIDList 中
  220. if id, err := strconv.Atoi(taskId); err == nil {
  221. taskIDList = append(taskIDList, id)
  222. }
  223. }
  224. }
  225. // 批量查询所有相关的收入数据
  226. var supplierIncome []gorm_model.YounggeeSupplierIncome
  227. err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id IN ?", taskIDList).Find(&supplierIncome).Error
  228. if err != nil {
  229. return &http_model.SupplierInvoiceValue{}, err
  230. }
  231. // 将查询到的 SupplierIncome 存入 map,以便快速查找
  232. incomeMap := make(map[int]gorm_model.YounggeeSupplierIncome)
  233. for _, income := range supplierIncome {
  234. incomeMap[income.IncomeId] = income
  235. }
  236. // 根据每个发票的 IncomeIds 查找对应的收入数据,并累加 SupplierChargeActual
  237. for _, invoice := range invoicelist {
  238. TaskIDs := strings.Split(invoice.IncomeIds, ",")
  239. for _, taskId := range TaskIDs {
  240. // 将 taskId 转换为 int
  241. if id, err := strconv.Atoi(taskId); err == nil {
  242. if income, exists := incomeMap[id]; exists {
  243. invoicevalue += income.SupplierChargeActual
  244. }
  245. }
  246. }
  247. }
  248. // 返回最终的发票值
  249. InvoiceValue := new(http_model.SupplierInvoiceValue)
  250. InvoiceValue.InvoiceValue = invoicevalue
  251. return InvoiceValue, nil
  252. }
  253. func GetInvoiceNums(ctx context.Context) (*http_model.InvoiceNums, error) {
  254. var invoiceNums int64
  255. db := GetReadDB(ctx)
  256. err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = 1").Count(&invoiceNums).Error
  257. if err != nil {
  258. return nil, err
  259. }
  260. InvoiceNums := new(http_model.InvoiceNums)
  261. InvoiceNums.InvoiceNums = int(invoiceNums)
  262. return InvoiceNums, err
  263. }
  264. func GetRechargeNums(ctx context.Context) (*http_model.RechargeNums, error) {
  265. var rechargeNums int64
  266. db := GetReadDB(ctx)
  267. err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("status = 1").Count(&rechargeNums).Error
  268. if err != nil {
  269. return nil, err
  270. }
  271. RechargeNums := new(http_model.RechargeNums)
  272. RechargeNums.RechargeNums = rechargeNums
  273. return RechargeNums, err
  274. }
  275. func AcceptRecharge(ctx context.Context, req http_model.AcceptRechargeRequest) (*http_model.AcceptRechargeData, error) {
  276. db := GetReadDB(ctx)
  277. err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id=?", req.Id).Updates(map[string]interface{}{"status": 2, "agree_at": time.Now()}).Error
  278. if err != nil {
  279. logrus.WithContext(ctx).Errorf("[Recharge service] call AcceptRecharge error,err:%+v", err)
  280. return nil, err
  281. }
  282. res := &http_model.AcceptRechargeData{
  283. Id: req.Id,
  284. }
  285. return res, nil
  286. }
  287. func RefuseRecharge(ctx context.Context, req http_model.RefuseRechargeRequest) (*http_model.RefuseRechargeData, error) {
  288. db := GetReadDB(ctx)
  289. 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
  290. if err != nil {
  291. logrus.WithContext(ctx).Errorf("[Recharge service] call AcceptRecharge error,err:%+v", err)
  292. return nil, err
  293. }
  294. res := &http_model.RefuseRechargeData{
  295. Id: req.Id,
  296. }
  297. return res, nil
  298. }
  299. func GetPreRechargeList(ctx context.Context, req http_model.GetPreRechargeListRequest) (*http_model.PreRechargeListData, error) {
  300. db := GetReadDB(ctx)
  301. // 获取分页参数
  302. page := req.PageNum
  303. pageSize := req.PageSize
  304. if page < 1 {
  305. page = 1
  306. }
  307. if pageSize < 1 {
  308. pageSize = 10 // 设置默认每页记录数为 10
  309. }
  310. var prerechargelist []gorm_model.YounggeeRechargeRecord
  311. var total int64
  312. // 计算总记录数
  313. err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("status = ?", req.Status).Count(&total).Error
  314. if err != nil {
  315. return &http_model.PreRechargeListData{}, err
  316. }
  317. // 查询当前页的数据
  318. err = db.Model(gorm_model.YounggeeRechargeRecord{}).
  319. Where("status = 1").
  320. Offset((page - 1) * pageSize). // 计算偏移量
  321. Limit(pageSize). // 设置每页限制
  322. Find(&prerechargelist).Error
  323. if err != nil {
  324. return &http_model.PreRechargeListData{}, err
  325. }
  326. rechargePointers := make([]*http_model.PreRechargeListResponse, 0, len(prerechargelist))
  327. for _, recharge := range prerechargelist {
  328. subaccountinfo := gorm_model.YounggeeSubAccount{}
  329. userResult := db.Where(gorm_model.YounggeeSubAccount{SubAccountId: recharge.SubAccountID, SubAccountType: 1}).First(&subaccountinfo)
  330. if userResult.Error != nil {
  331. return nil, userResult.Error
  332. }
  333. enterprise := gorm_model.Enterprise{}
  334. result := db.Where(gorm_model.Enterprise{EnterpriseID: recharge.EnterpriseID}).First(&enterprise)
  335. if result.Error != nil {
  336. return nil, result.Error
  337. }
  338. response := &http_model.PreRechargeListResponse{
  339. ID: recharge.RechargeID,
  340. EnterPrise: enterprise.BusinessName,
  341. Operator: subaccountinfo.SubAccountName,
  342. RechargeAmount: recharge.RechargeAmount,
  343. RechargeMethod: recharge.RechargeMethod,
  344. TransferVoucherUrl: recharge.TransferVoucherUrl,
  345. Phone: recharge.Phone,
  346. CommitAt: recharge.CommitAt,
  347. ConfirmAt: recharge.ConfirmAt,
  348. RefuseAt: recharge.RefuseAt,
  349. FailReason: recharge.FailReason,
  350. }
  351. rechargePointers = append(rechargePointers, response)
  352. }
  353. return &http_model.PreRechargeListData{
  354. PreRechargeListinfo: rechargePointers,
  355. Total: total,
  356. }, nil
  357. }
  358. func GetTalentWithdrawList(ctx context.Context, req *http_model.GetTalentWithdrawListRequest) (*http_model.TalentWithdrawListData, error) {
  359. db := GetReadDB(ctx)
  360. page := req.PageNum
  361. pageSize := req.PageSize
  362. if page < 1 {
  363. page = 1
  364. }
  365. if pageSize < 1 {
  366. pageSize = 10 // 设置默认每页记录数为 10
  367. }
  368. var withdrawlist []gorm_model.YounggeeWithdrawRecord
  369. var total int64
  370. // 计算总记录数
  371. err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("status = ?", req.Status).Count(&total).Error
  372. if err != nil {
  373. return &http_model.TalentWithdrawListData{}, err
  374. }
  375. // 查询当前页的数据
  376. err = db.Model(gorm_model.YounggeeWithdrawRecord{}).
  377. Offset((page - 1) * pageSize). // 计算偏移量
  378. Limit(pageSize). // 设置每页限制
  379. Find(&withdrawlist).Error
  380. if err != nil {
  381. return &http_model.TalentWithdrawListData{}, err
  382. }
  383. withdrawPointers := make([]*http_model.TalentWithdrawListResponse, 0, len(withdrawlist))
  384. for _, withdraw := range withdrawlist {
  385. response := &http_model.TalentWithdrawListResponse{
  386. WithdrawId: withdraw.WithdrawID,
  387. TalentId: withdraw.TalentID,
  388. WithdrawAmount: withdraw.WithdrawAmount,
  389. ActualAmount: withdraw.AmountPayable,
  390. ReajectAt: withdraw.RejectAt,
  391. SubmitAt: withdraw.SubmitAt,
  392. WithdrawAt: withdraw.WithdrawAt,
  393. RejectReason: withdraw.RejectReason,
  394. Name: withdraw.Name,
  395. PhoneNumber: withdraw.PhoneNum,
  396. IdcardNumber: withdraw.IdcardNum,
  397. BankNumber: withdraw.BankNum,
  398. }
  399. withdrawPointers = append(withdrawPointers, response)
  400. }
  401. return &http_model.TalentWithdrawListData{
  402. TalentwithdrawList: withdrawPointers,
  403. Total: total,
  404. }, nil
  405. }
  406. func ConfirmWithdrawal(ctx context.Context, withdrawId string) error {
  407. db := GetReadDB(ctx)
  408. db2 := GetReadDB(ctx)
  409. withdrawInfo := gorm_model.YounggeeWithdrawRecord{}
  410. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Where("withdraw_id = ?", withdrawId).Find(&withdrawInfo)
  411. db2.Debug().Where("withdraw_id = ?", withdrawId).Updates(gorm_model.YounggeeWithdrawRecord{Status: 2, WithdrawAt: time.Now()})
  412. IncomeIdLists := strings.Split(withdrawInfo.IncomeIdList, ",")
  413. for _, income := range IncomeIdLists {
  414. var incomeinfo gorm_model.YounggeeTalentIncome
  415. err := db.Debug().Model(gorm_model.YounggeeTalentIncome{}).
  416. Where("id = ?", income).
  417. First(&incomeinfo).Error // 使用 First 而不是 Updates 来查询数据
  418. if err != nil {
  419. logrus.WithContext(ctx).Errorf("[finance db] Query YounggeeTalentIncome error, err:%+v", err)
  420. return err
  421. }
  422. err = db.Model(gorm_model.YounggeeTalentIncome{}).Where("id = ?", income).Updates(gorm_model.YounggeeTalentIncome{WithdrawStatus: 1}).Error
  423. if err != nil {
  424. logrus.WithContext(ctx).Errorf("[finance db] Updates YounggeeTalentIncome error, err:%+v", err)
  425. return err
  426. }
  427. switch incomeinfo.Type {
  428. case 1:
  429. {
  430. err = db.Debug().Model(gorm_model.YounggeeSecTaskInfo{}).Where("task_id = ?", incomeinfo.SectaskID).Updates(gorm_model.YounggeeSecTaskInfo{WithdrawStatus: 4}).Error
  431. if err != nil {
  432. logrus.WithContext(ctx).Errorf("[finance db] Updates YounggeeSecTaskInfo error, err:%+v", err)
  433. return err
  434. }
  435. }
  436. case 2:
  437. {
  438. err = db.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", incomeinfo.TaskID).Updates(gorm_model.YoungeeTaskInfo{WithdrawStatus: 4}).Error
  439. if err != nil {
  440. logrus.WithContext(ctx).Errorf("[finance db] Updates YoungeeTaskInfo error, err:%+v", err)
  441. return err
  442. }
  443. }
  444. case 3:
  445. {
  446. err = db.Debug().Model(gorm_model.YoungeeLocalTaskInfo{}).Where("task_id = ?", incomeinfo.LocalTaskId).Updates(gorm_model.YoungeeLocalTaskInfo{WithdrawStatus: 4}).Error
  447. if err != nil {
  448. logrus.WithContext(ctx).Errorf("[finance db] Updates YoungeeLocalTaskInfo error, err:%+v", err)
  449. return err
  450. }
  451. }
  452. }
  453. }
  454. //for _, taskId := range taskIdLists {
  455. // db1 := GetReadDB(ctx)
  456. // err := db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", taskId).Updates(gorm_model.YoungeeTaskInfo{WithdrawStatus: 4}).Error
  457. // if err != nil {
  458. // logrus.WithContext(ctx).Errorf("[finance db] Update YoungeeTaskInfo error,err:%+v", err)
  459. // return err
  460. // }
  461. //
  462. // err = CreateMessageByTaskId(ctx, 6, 1, taskId)
  463. // if err != nil {
  464. // logrus.WithContext(ctx).Errorf("[ConfirmWithdrawal] call CreateMessageByTaskId error,err:%+v", err)
  465. // return err
  466. // }
  467. //}
  468. db3 := GetReadDB(ctx)
  469. db3.Debug().Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", withdrawInfo.TalentID).Updates(
  470. map[string]interface{}{
  471. "withdrawing": gorm.Expr("withdrawing - ?", withdrawInfo.WithdrawAmount),
  472. "withdrawed": gorm.Expr("withdrawed + ?", withdrawInfo.WithdrawAmount)})
  473. return nil
  474. }
  475. func RefuseTalentWithdrawal(ctx context.Context, req *http_model.RefuseTalentWithdrawalRequest) error {
  476. db := GetReadDB(ctx)
  477. // 获取撤回记录
  478. var withdrwainfo gorm_model.YounggeeWithdrawRecord
  479. err := db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).
  480. Where("withdraw_id = ?", req.WithdrawId).
  481. First(&withdrwainfo).Error // 使用 First 而不是 Updates 来查询数据
  482. if err != nil {
  483. logrus.WithContext(ctx).Errorf("[finance db] Query YounggeeWithdrawRecord error, err:%+v", err)
  484. return err
  485. }
  486. // 更新 YounggeeWithdrawRecord
  487. err = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).
  488. Where("withdraw_id = ?", req.WithdrawId).
  489. Updates(gorm_model.YounggeeWithdrawRecord{
  490. Status: 3,
  491. RejectAt: time.Now(),
  492. RejectReason: req.Reason,
  493. }).Error
  494. if err != nil {
  495. logrus.WithContext(ctx).Errorf("[finance db] Update YounggeeWithdrawRecord error, err:%+v", err)
  496. return err
  497. }
  498. // 更新 YoungeeTalentInfo
  499. err = db.Debug().Model(gorm_model.YoungeeTalentInfo{}).
  500. Where("id = ?", withdrwainfo.TalentID).
  501. Updates(map[string]interface{}{
  502. "withdrawing": gorm.Expr("withdrawing + ?", withdrwainfo.WithdrawAmount),
  503. "canwithdraw": gorm.Expr("canwithdraw - ?", withdrwainfo.WithdrawAmount),
  504. }).Error
  505. if err != nil {
  506. logrus.WithContext(ctx).Errorf("[finance db] Update YoungeeTalentInfo error, err:%+v", err)
  507. return err
  508. }
  509. return nil
  510. }
  511. func GetBankInfo(ctx context.Context, req *http_model.GetBankInfoRequest) (*http_model.BankInfo, error) {
  512. //db := GetReadDB(ctx)
  513. //if req.BankId == "" {
  514. // return nil, nil
  515. //}
  516. //var infoBank *gorm_model.InfoBank
  517. //db.Debug().Model(gorm_model.InfoBank{}).Where("id = ?", req.BankId).First(&infoBank)
  518. db1 := GetReadDB(ctx)
  519. var infoRegion *gorm_model.InfoRegion
  520. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(req.BankOpenAddress, 0)).First(&infoRegion)
  521. provinceCode := conv.MustString(req.BankOpenAddress, "")[0:2] + "0000"
  522. var province *gorm_model.InfoRegion
  523. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(provinceCode, 0)).First(&province)
  524. cityCode := conv.MustString(req.BankOpenAddress, "")[0:4] + "00"
  525. var city *gorm_model.InfoRegion
  526. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(cityCode, 0)).First(&city)
  527. data := new(http_model.BankInfo)
  528. //data.BankName = infoBank.Name
  529. data.BankOpenAddress = province.RegionName + city.RegionName + infoRegion.RegionName
  530. //db.Model(gorm_model.InfoBank{}).Where("")
  531. return data, nil
  532. }
  533. // GetEnterpriseIDByBusiness 根据企业名称查找企业ID
  534. func GetEnterpriseIDByBusiness(ctx context.Context, BusinessName string) int64 {
  535. db := GetReadDB(ctx)
  536. var EnterpriseID int64
  537. db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("business_name", BusinessName).First(&EnterpriseID)
  538. return EnterpriseID
  539. }
  540. // GetEnterpriseIDByUserId 根据企业名称查找企业ID
  541. func GetEnterpriseIDByUserId(ctx context.Context, UserId int64) int64 {
  542. db := GetReadDB(ctx)
  543. var EnterpriseID int64
  544. db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("user_id", UserId).First(&EnterpriseID)
  545. return EnterpriseID
  546. }
  547. // GetUserIDByUsername 根据用户名称查UserID
  548. func GetUserIDByUsername(ctx context.Context, Username string) int64 {
  549. db := GetReadDB(ctx)
  550. var UserID int64
  551. db = db.Model([]gorm_model.YounggeeUser{}).Select("id").Where(fmt.Sprintf("username like '%%%s%%'", Username)).First(&UserID)
  552. return UserID
  553. }
  554. func GetInvoiceRecords(ctx context.Context, req *http_model.InvoiceRecordsRequest, condition *common_model.InvoiceRecordsCondition) (*http_model.InvoiceRecordsData, error) {
  555. db := GetReadDB(ctx)
  556. var invoiceRecords []*gorm_model.YounggeeInvoiceRecord
  557. db = db.Debug().Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", req.InvoiceStatus)
  558. conditionType := reflect.TypeOf(condition).Elem()
  559. conditionValue := reflect.ValueOf(condition).Elem()
  560. for i := 0; i < conditionType.NumField(); i++ {
  561. field := conditionType.Field(i)
  562. tag := field.Tag.Get("condition")
  563. value := conditionValue.FieldByName(field.Name)
  564. if tag == "submit_at" && value.Interface() != "" {
  565. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  566. }
  567. if tag == "billing_at" && value.Interface() != "" {
  568. db = db.Where(fmt.Sprintf("billing_at like '%s%%'", value.Interface()))
  569. }
  570. }
  571. if req.Username != "" {
  572. UserID := GetUserIDByUsername(ctx, req.Username)
  573. enterpriseId := GetEnterpriseIDByUserId(ctx, UserID)
  574. db = db.Where("enterprise_id = ?", enterpriseId)
  575. }
  576. if req.UserId != 0 {
  577. enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
  578. db = db.Where("enterprise_id = ?", enterpriseId)
  579. }
  580. // 查询总数
  581. var total int64
  582. if err := db.Count(&total).Error; err != nil {
  583. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql total, err:%+v", err)
  584. return nil, err
  585. }
  586. if req.InvoiceStatus != 3 {
  587. db.Order("submit_at")
  588. } else {
  589. db.Order("billing_at desc")
  590. }
  591. // 查询该页数据
  592. limit := req.PageSize
  593. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  594. err := db.Limit(int(limit)).Offset(int(offset)).Find(&invoiceRecords).Error
  595. if err != nil {
  596. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql limit, err:%+v", err)
  597. return nil, err
  598. }
  599. enterpriseIdToUserInfoMap := make(map[string]gorm_model.Enterprise)
  600. regionAddressMap := make(map[string]string)
  601. for _, invoiceRecord := range invoiceRecords {
  602. if _, ok := enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID]; !ok {
  603. db1 := GetReadDB(ctx)
  604. enterpriseInfo := gorm_model.Enterprise{}
  605. db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", invoiceRecord.EnterpriseID).Find(&enterpriseInfo)
  606. enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID] = enterpriseInfo
  607. }
  608. if _, ok := regionAddressMap[invoiceRecord.EnterpriseID]; !ok {
  609. db1 := GetReadDB(ctx)
  610. var regionCode string
  611. db1.Model(gorm_model.YounggeeInvoiceAddress{}).Select("region_code").Where("enterprise_id = ?", invoiceRecord.EnterpriseID).Find(&regionCode)
  612. regionAddressMap[invoiceRecord.EnterpriseID] = GetRegion(ctx, conv.MustInt(regionCode, 0))
  613. }
  614. }
  615. var InvoiceRecords []*http_model.InvoiceRecordsPreviews
  616. for _, invoiceRecord := range invoiceRecords {
  617. InvoiceRecord := new(http_model.InvoiceRecordsPreviews)
  618. InvoiceRecord.BillingId = invoiceRecord.BillingID
  619. InvoiceRecord.InvoiceInfo = invoiceRecord.InvoiceSnap
  620. InvoiceRecord.AddressInfo = invoiceRecord.AddressSnap
  621. InvoiceRecord.InvoiceAddress = regionAddressMap[invoiceRecord.EnterpriseID]
  622. InvoiceRecord.InvoiceType = invoiceRecord.InvoiceType
  623. InvoiceRecord.Amount = invoiceRecord.InvoiceAmount
  624. InvoiceRecord.Phone = invoiceRecord.Phone
  625. InvoiceRecord.ShipmentNumber = invoiceRecord.ShipmentNumber
  626. InvoiceRecord.BusinessName = enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].BusinessName
  627. InvoiceRecord.UserId = invoiceRecord.EnterpriseID
  628. InvoiceRecord.Username = GetUsernameByUserID(ctx, enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].UserID)
  629. InvoiceRecord.SubmitAt = conv.MustString(invoiceRecord.SubmitAt, "")[:19]
  630. InvoiceRecord.BillingAt = conv.MustString(invoiceRecord.BillingAt, "")[:19]
  631. InvoiceRecords = append(InvoiceRecords, InvoiceRecord)
  632. }
  633. var InvoiceRecordsData http_model.InvoiceRecordsData
  634. InvoiceRecordsData.InvoiceRecordsPreviews = InvoiceRecords
  635. InvoiceRecordsData.Total = strconv.FormatInt(total, 10)
  636. return &InvoiceRecordsData, nil
  637. }
  638. func GetInvoiceList(ctx context.Context, req http_model.GetInvoiceListRequest) (*http_model.InvoiceListData, error) {
  639. db := GetReadDB(ctx)
  640. // 获取分页参数
  641. page := req.PageNum
  642. pageSize := req.PageSize
  643. if page < 1 {
  644. page = 1
  645. }
  646. if pageSize < 1 {
  647. pageSize = 10 // 设置默认每页记录数为 10
  648. }
  649. var invoicelist []gorm_model.YounggeeInvoiceRecord
  650. var total int64
  651. // 计算总记录数
  652. err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", req.Status).Count(&total).Error
  653. if err != nil {
  654. return &http_model.InvoiceListData{}, err
  655. }
  656. // 查询当前页的数据
  657. err = db.Model(gorm_model.YounggeeInvoiceRecord{}).
  658. Offset((page - 1) * pageSize). // 计算偏移量
  659. Limit(pageSize). // 设置每页限制
  660. Find(&invoicelist).Error
  661. if err != nil {
  662. return &http_model.InvoiceListData{}, err
  663. }
  664. invoicePointers := make([]*http_model.InvoiceListResponse, 0, len(invoicelist))
  665. for _, invoice := range invoicelist {
  666. var operator string
  667. if invoice.SubAccountID == 0 {
  668. operator = GetEnterprisenameByEnterpriseID(ctx, invoice.EnterpriseID)
  669. } else {
  670. operator = GetOperatorBySubacountID(ctx, invoice.SubAccountID)
  671. }
  672. response := &http_model.InvoiceListResponse{
  673. BillingID: invoice.BillingID,
  674. InvoiceAmount: invoice.InvoiceAmount,
  675. Operator: operator,
  676. SubmitAt: invoice.SubmitAt,
  677. AgreeAt: invoice.BillingAt,
  678. TaskNumber: GetTasknum(invoice.TaskIds),
  679. InvoiceType: invoice.InvoiceType,
  680. }
  681. invoicePointers = append(invoicePointers, response)
  682. }
  683. return &http_model.InvoiceListData{
  684. InvoiceListinfo: invoicePointers,
  685. Total: total,
  686. }, nil
  687. }
  688. func GetSupplierWithdrawList(ctx context.Context, req http_model.GetSupplierWithdrawListRequest) (*http_model.SupplierWithdrawListData, error) {
  689. db := GetReadDB(ctx)
  690. // 获取分页参数
  691. page := req.PageNum
  692. pageSize := req.PageSize
  693. if page < 1 {
  694. page = 1
  695. }
  696. if pageSize < 1 {
  697. pageSize = 10 // 设置默认每页记录数为 10
  698. }
  699. var withdrawlist []gorm_model.YounggeeSupplierWithdraw
  700. var total int64
  701. // 计算总记录数
  702. err := db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("withdraw_status = ?", req.Status).Count(&total).Error
  703. if err != nil {
  704. return &http_model.SupplierWithdrawListData{}, err
  705. }
  706. // 查询当前页的数据
  707. err = db.Model(gorm_model.YounggeeSupplierWithdraw{}).
  708. Offset((page - 1) * pageSize). // 计算偏移量
  709. Limit(pageSize). // 设置每页限制
  710. Find(&withdrawlist).Error
  711. if err != nil {
  712. return &http_model.SupplierWithdrawListData{}, err
  713. }
  714. withdrawPointers := make([]*http_model.SupplierWithdrawListResponse, 0, len(withdrawlist))
  715. for _, withdraw := range withdrawlist {
  716. var supplier gorm_model.YounggeeSupplier
  717. err = db.Model(gorm_model.YounggeeSupplier{}).Where("supplier_id = ?", withdraw.SupplierId).First(&supplier).Error
  718. if err != nil {
  719. return &http_model.SupplierWithdrawListData{}, err
  720. }
  721. var name string
  722. if supplier.SupplierType == 1 {
  723. name = supplier.Name
  724. } else {
  725. name = supplier.CompanyName
  726. }
  727. var InvoiceIDList []string
  728. TaskIDs := strings.Split(withdraw.InvoiceIds, ",")
  729. for _, taskId := range TaskIDs {
  730. InvoiceIDList = append(InvoiceIDList, taskId)
  731. }
  732. var invoiceIncome []gorm_model.YounggeeSupplierInvoice
  733. err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id IN ?", InvoiceIDList).Find(&invoiceIncome).Error
  734. if err != nil {
  735. return &http_model.SupplierWithdrawListData{}, err
  736. }
  737. // 收集所有的 incomeIDs
  738. var incomelist []string
  739. for _, invoice := range invoiceIncome {
  740. TaskIDs := strings.Split(invoice.IncomeIds, ",")
  741. for _, taskId := range TaskIDs {
  742. incomelist = append(incomelist, taskId)
  743. }
  744. }
  745. var supplier_income []gorm_model.YounggeeSupplierIncome
  746. err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomelist).Find(&supplier_income).Error
  747. if err != nil {
  748. return &http_model.SupplierWithdrawListData{}, err
  749. }
  750. var taskinfo []http_model.Tasklist
  751. taskinfo = make([]http_model.Tasklist, 0, len(supplier_income)) // 预分配足够的空间
  752. for _, income := range supplier_income {
  753. var info http_model.Tasklist
  754. // 根据 IncomeType 判断信息的来源
  755. switch income.IncomeType {
  756. case 1:
  757. info.Id = income.SProjectId
  758. case 3:
  759. info.Id = income.SLocalLifeId
  760. }
  761. info.Fee = income.SupplierChargeActual
  762. // 添加到任务信息列表
  763. taskinfo = append(taskinfo, info)
  764. }
  765. withdrawinfo := &http_model.Withdrawinfo{
  766. Name: withdraw.Name,
  767. IdNumber: withdraw.IdNumber,
  768. BankName: withdraw.BankName,
  769. BankNumber: withdraw.BankNumber,
  770. Phone: withdraw.Phone,
  771. }
  772. response := &http_model.SupplierWithdrawListResponse{
  773. WithDrawId: withdraw.SupplierWithdrawId,
  774. WithdrawAmount: withdraw.WithdrawAmount,
  775. ActualAmount: withdraw.AmountPayable,
  776. PhoneNumber: withdraw.Phone,
  777. SupplierName: supplier.SupplierName,
  778. Name: name,
  779. WithDrawinfo: withdrawinfo,
  780. SubmitAt: withdraw.SupplyTime,
  781. AggreeAt: withdraw.AgreeTime,
  782. ReajectAt: withdraw.RejectTime,
  783. ReajectReason: withdraw.FailReson,
  784. Taskinfo: &taskinfo,
  785. SupplierId: supplier.SupplierId,
  786. Avater: supplier.Avater,
  787. }
  788. withdrawPointers = append(withdrawPointers, response)
  789. }
  790. return &http_model.SupplierWithdrawListData{
  791. WithdrawListinfo: withdrawPointers,
  792. Total: total,
  793. }, nil
  794. }
  795. func GetSupplierInvoiceList(ctx context.Context, req http_model.GetSupplierInvoiceListRequest) (*http_model.SupplierInvoiceListData, error) {
  796. db := GetReadDB(ctx)
  797. // 获取分页参数
  798. page := req.PageNum
  799. pageSize := req.PageSize
  800. if page < 1 {
  801. page = 1
  802. }
  803. if pageSize < 1 {
  804. pageSize = 10 // 设置默认每页记录数为 10
  805. }
  806. var invoicelist []gorm_model.YounggeeSupplierInvoice
  807. var total int64
  808. // 计算总记录数
  809. err := db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_status = ?", req.Status).Count(&total).Error
  810. if err != nil {
  811. return &http_model.SupplierInvoiceListData{}, err
  812. }
  813. // 查询当前页的数据
  814. err = db.Model(gorm_model.YounggeeSupplierInvoice{}).
  815. Offset((page - 1) * pageSize). // 计算偏移量
  816. Limit(pageSize). // 设置每页限制
  817. Find(&invoicelist).Error
  818. if err != nil {
  819. return &http_model.SupplierInvoiceListData{}, err
  820. }
  821. invoicePointers := make([]*http_model.SupplierInvoiceListResponse, 0, len(invoicelist))
  822. for _, invoice := range invoicelist {
  823. var supplier gorm_model.YounggeeSupplier
  824. err = db.Model(gorm_model.YounggeeSupplier{}).Where("supplier_id = ?", invoice.SupplierId).First(&supplier).Error
  825. if err != nil {
  826. return &http_model.SupplierInvoiceListData{}, err
  827. }
  828. var name string
  829. if supplier.SupplierType == 1 {
  830. name = supplier.Name
  831. } else {
  832. name = supplier.CompanyName
  833. }
  834. var TaskIDList []string
  835. TaskIDs := strings.Split(invoice.IncomeIds, ",")
  836. for _, taskId := range TaskIDs {
  837. TaskIDList = append(TaskIDList, taskId)
  838. }
  839. var supplier_income []gorm_model.YounggeeSupplierIncome
  840. err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", TaskIDs).Find(&supplier_income).Error
  841. if err != nil {
  842. return &http_model.SupplierInvoiceListData{}, err
  843. }
  844. var taskinfo []http_model.Tasklist
  845. taskinfo = make([]http_model.Tasklist, 0, len(supplier_income)) // 预分配足够的空间
  846. var AllFee float64
  847. for _, income := range supplier_income {
  848. var info http_model.Tasklist
  849. // 根据 IncomeType 判断信息的来源
  850. switch income.IncomeType {
  851. case 1:
  852. info.Id = income.SProjectId
  853. case 3:
  854. info.Id = income.SLocalLifeId
  855. }
  856. info.Fee = income.SupplierChargeActual
  857. AllFee += income.SupplierChargeActual
  858. // 添加到任务信息列表
  859. taskinfo = append(taskinfo, info)
  860. }
  861. response := &http_model.SupplierInvoiceListResponse{
  862. InvoiceId: invoice.InvoiceId,
  863. SubmitAt: invoice.UploadInvoiceTime,
  864. AggreeAt: invoice.AgreeTime,
  865. ReajectAt: invoice.RejectTime,
  866. SupplierId: invoice.SupplierId,
  867. ReajectReason: invoice.FailReason,
  868. PhoneNumber: supplier.PhoneNumber,
  869. Name: name,
  870. SupplierName: supplier.SupplierName,
  871. CompanyName: invoice.Company,
  872. Taskinfo: &taskinfo,
  873. InvoiceAmount: AllFee,
  874. Avater: supplier.Avater,
  875. InvoiceUrl: invoice.InvoiceUrl,
  876. }
  877. invoicePointers = append(invoicePointers, response)
  878. }
  879. return &http_model.SupplierInvoiceListData{
  880. InvoiceListinfo: invoicePointers,
  881. Total: total,
  882. }, nil
  883. }
  884. func GetInvoiceInfo(ctx context.Context, request http_model.GetInvoiceInfoRequest) (*http_model.InvoiceInfoResponse, error) {
  885. db := GetReadDB(ctx)
  886. var invoice gorm_model.YounggeeInvoiceRecord
  887. err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id=?", request.BillingID).First(&invoice).Error
  888. if err != nil {
  889. return nil, err
  890. }
  891. res := &http_model.InvoiceInfoResponse{
  892. Businessname: GetEnterprisenameByEnterpriseID(ctx, invoice.EnterpriseID),
  893. TaxCode: invoice.TaxCode,
  894. RegisteredAddress: invoice.RegisteredAddress,
  895. RegisteredPhone: invoice.RegisteredPhone,
  896. Bank: invoice.Bank,
  897. BankCardNumber: invoice.BankCardNumber,
  898. InvoiceType: invoice.InvoiceType,
  899. }
  900. return res, nil
  901. }
  902. func ConfirmInvoice(ctx context.Context, request *http_model.ConfirmInvoiceRequest) error {
  903. db := GetReadDB(ctx)
  904. return db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id = ?", request.BillingId).Updates(
  905. gorm_model.YounggeeInvoiceRecord{
  906. BillingAt: time.Now(),
  907. InvoiceUrl: request.Url,
  908. Status: 2,
  909. }).Error
  910. }
  911. func SetInvoiceInfo(ctx context.Context, request *http_model.SetInvoiceInfoRequest) error {
  912. db := GetReadDB(ctx)
  913. invoiceInfo := gorm_model.YounggeePlatformInvoiceInfo{
  914. EnterpriseName: request.EnterpriseName,
  915. EnterpriseTax: request.EnterpriseTax,
  916. Phone: request.Phone,
  917. Address: request.Address,
  918. BankAccount: request.BankAccount,
  919. BankName: request.BankName,
  920. Project: request.Project,
  921. }
  922. return db.Create(&invoiceInfo).Error
  923. }
  924. func RefuseSupplierInvoice(ctx context.Context, request *http_model.RefuseSupplierInvoiceRequest) error {
  925. db := GetReadDB(ctx)
  926. return db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id = ?", request.InvoiceId).Updates(
  927. gorm_model.YounggeeSupplierInvoice{
  928. RejectTime: time.Now(),
  929. InvoiceStatus: 4,
  930. FailReason: request.Reason,
  931. }).Error
  932. }
  933. func ConfirmSupplierInvoice(ctx context.Context, request *http_model.ConfirmSupplierInvoiceRequest) error {
  934. db := GetReadDB(ctx)
  935. return db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id = ?", request.InvoiceId).Updates(
  936. gorm_model.YounggeeSupplierInvoice{
  937. AgreeTime: time.Now(),
  938. InvoiceStatus: 3,
  939. }).Error
  940. }
  941. func ConfirmWithdraw(ctx context.Context, request *http_model.ConfirmWithdrawRequest) error {
  942. db := GetReadDB(ctx)
  943. // 获取撤回记录
  944. var supplierincomeinfo gorm_model.YounggeeSupplierWithdraw
  945. err := db.Debug().Model(gorm_model.YounggeeSupplierWithdraw{}).
  946. Where("supplier_withdraw_id = ?", request.InvoiceId).
  947. First(&supplierincomeinfo).Error
  948. if err != nil {
  949. logrus.WithContext(ctx).Errorf("[finance db] Query YounggeeWithdrawRecord error, err:%+v", err)
  950. return err
  951. }
  952. err = db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("supplier_withdraw_id = ?", request.InvoiceId).Updates(
  953. gorm_model.YounggeeSupplierWithdraw{
  954. AgreeTime: time.Now(),
  955. WithdrawStatus: 3,
  956. }).Error
  957. if err != nil {
  958. return err
  959. }
  960. if supplierincomeinfo.SupplierType == 2 {
  961. var InvoiceIDList []string
  962. InvoiceIDs := strings.Split(supplierincomeinfo.InvoiceIds, ",")
  963. for _, taskId := range InvoiceIDs {
  964. InvoiceIDList = append(InvoiceIDList, taskId)
  965. }
  966. var invoiceIncome []gorm_model.YounggeeSupplierInvoice
  967. err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id IN ?", InvoiceIDList).Find(&invoiceIncome).Error
  968. if err != nil {
  969. return err
  970. }
  971. err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id in ?", InvoiceIDList).Updates(
  972. gorm_model.YounggeeSupplierInvoice{
  973. WithdrawStatus: 3,
  974. }).Error
  975. if err != nil {
  976. return err
  977. }
  978. // 收集所有的 incomeIDs
  979. var incomelist []string
  980. for _, invoice := range invoiceIncome {
  981. IncomeIds := strings.Split(invoice.IncomeIds, ",")
  982. for _, IncomeId := range IncomeIds {
  983. incomelist = append(incomelist, IncomeId)
  984. }
  985. }
  986. err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomelist).Updates(gorm_model.YounggeeSupplierIncome{WithdrawStatus: 3}).Error
  987. if err != nil {
  988. return err
  989. }
  990. } else {
  991. incomeids := strings.Split(supplierincomeinfo.IncomeIds, ",")
  992. err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomeids).Updates(gorm_model.YounggeeSupplierIncome{WithdrawStatus: 3}).Error
  993. if err != nil {
  994. return err
  995. }
  996. }
  997. return nil
  998. }
  999. func RefuseWithdraw(ctx context.Context, request *http_model.RefuseWithdrawRequest) error {
  1000. db := GetReadDB(ctx)
  1001. // 获取撤回记录
  1002. var supplierincomeinfo gorm_model.YounggeeSupplierWithdraw
  1003. err := db.Debug().Model(gorm_model.YounggeeSupplierWithdraw{}).
  1004. Where("supplier_withdraw_id = ?", request.InvoiceId).
  1005. First(&supplierincomeinfo).Error
  1006. if err != nil {
  1007. logrus.WithContext(ctx).Errorf("[finance db] Query YounggeeWithdrawRecord error, err:%+v", err)
  1008. return err
  1009. }
  1010. err = db.Model(gorm_model.YounggeeSupplierWithdraw{}).Where("supplier_withdraw_id = ?", request.InvoiceId).Updates(
  1011. gorm_model.YounggeeSupplierWithdraw{
  1012. RejectTime: time.Now(),
  1013. WithdrawStatus: 4,
  1014. FailReson: request.Reason,
  1015. }).Error
  1016. if err != nil {
  1017. return err
  1018. }
  1019. if supplierincomeinfo.SupplierType == 2 {
  1020. var InvoiceIDList []string
  1021. InvoiceIDs := strings.Split(supplierincomeinfo.InvoiceIds, ",")
  1022. for _, taskId := range InvoiceIDs {
  1023. InvoiceIDList = append(InvoiceIDList, taskId)
  1024. }
  1025. var invoiceIncome []gorm_model.YounggeeSupplierInvoice
  1026. err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id IN ?", InvoiceIDList).Find(&invoiceIncome).Error
  1027. if err != nil {
  1028. return err
  1029. }
  1030. err = db.Model(gorm_model.YounggeeSupplierInvoice{}).Where("invoice_id in ?", InvoiceIDList).Updates(
  1031. gorm_model.YounggeeSupplierInvoice{
  1032. WithdrawStatus: 1,
  1033. }).Error
  1034. if err != nil {
  1035. return err
  1036. }
  1037. // 收集所有的 incomeIDs
  1038. var incomelist []string
  1039. for _, invoice := range invoiceIncome {
  1040. IncomeIds := strings.Split(invoice.IncomeIds, ",")
  1041. for _, IncomeId := range IncomeIds {
  1042. incomelist = append(incomelist, IncomeId)
  1043. }
  1044. }
  1045. err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomelist).Updates(gorm_model.YounggeeSupplierIncome{WithdrawStatus: 1}).Error
  1046. if err != nil {
  1047. return err
  1048. }
  1049. } else {
  1050. incomeids := strings.Split(supplierincomeinfo.IncomeIds, ",")
  1051. err = db.Model(gorm_model.YounggeeSupplierIncome{}).Where("income_id in ?", incomeids).Updates(gorm_model.YounggeeSupplierIncome{WithdrawStatus: 1}).Error
  1052. if err != nil {
  1053. return err
  1054. }
  1055. }
  1056. return nil
  1057. }
  1058. func GetTasknum(taskIds string) int {
  1059. var ids [][]string // 修改为二维字符串切片
  1060. if err := json.Unmarshal([]byte(taskIds), &ids); err != nil {
  1061. return 0
  1062. }
  1063. count := 0
  1064. for _, pair := range ids {
  1065. count += len(pair)
  1066. }
  1067. return count
  1068. }
  1069. func GetOperatorBySubacountID(ctx context.Context, SubaccountID int) (username string) {
  1070. db := GetReadDB(ctx)
  1071. db = db.Model([]gorm_model.YounggeeSubAccount{}).Select("sub_account_name").Where("sub_account_id = ?", SubaccountID).First(&username)
  1072. return username
  1073. }
  1074. func GetEnterprisenameByEnterpriseID(ctx context.Context, EnterpriseID string) (username string) {
  1075. db := GetReadDB(ctx)
  1076. db = db.Model([]gorm_model.Enterprise{}).Select("business_name").Where("enterprise_id = ?", EnterpriseID).First(&username)
  1077. return username
  1078. }
  1079. func GetUsernameByUserID(ctx context.Context, UserID int64) (username string) {
  1080. db := GetReadDB(ctx)
  1081. db = db.Model([]gorm_model.YounggeeUser{}).Select("username").Where("id", UserID).First(&username)
  1082. return username
  1083. }
  1084. func GetRechargeRecords(ctx context.Context, req *http_model.GetRechargeRecordsRequest, condition *common_model.RechargeRecordsCondition) (*http_model.RechargeRecordsData, error) {
  1085. db := GetReadDB(ctx)
  1086. var rechargeRecords []*gorm_model.YounggeeRechargeRecord
  1087. db = db.Debug().Model(gorm_model.YounggeeRechargeRecord{}).Where("status = ?", req.Status)
  1088. conditionType := reflect.TypeOf(condition).Elem()
  1089. conditionValue := reflect.ValueOf(condition).Elem()
  1090. for i := 0; i < conditionType.NumField(); i++ {
  1091. field := conditionType.Field(i)
  1092. tag := field.Tag.Get("condition")
  1093. value := conditionValue.FieldByName(field.Name)
  1094. if tag == "commit_at" && value.Interface() != "" {
  1095. db = db.Where(fmt.Sprintf("commit_at like '%s%%'", value.Interface()))
  1096. }
  1097. if tag == "confirm_at" && value.Interface() != "" {
  1098. db = db.Where(fmt.Sprintf("confirm_at like '%s%%'", value.Interface()))
  1099. }
  1100. }
  1101. if req.Username != "" {
  1102. UserID := GetUserIDByUsername(ctx, req.Username)
  1103. enterpriseId := GetEnterpriseIDByUserId(ctx, UserID)
  1104. db = db.Where("enterprise_id = ?", enterpriseId)
  1105. }
  1106. if req.UserId != 0 {
  1107. enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
  1108. db = db.Where("enterprise_id = ?", enterpriseId)
  1109. }
  1110. if req.RechargeMethod == 1 {
  1111. db = db.Where("recharge_method = ?", 1)
  1112. } else if req.RechargeMethod == 2 {
  1113. db = db.Where("recharge_method = ?", 2)
  1114. } else if req.RechargeMethod == 3 {
  1115. db = db.Where("recharge_method = ?", 3)
  1116. }
  1117. // 查询总数
  1118. var total int64
  1119. if err := db.Count(&total).Error; err != nil {
  1120. logrus.WithContext(ctx).Errorf("[GetRechargeRecords] error query mysql total, err:%+v", err)
  1121. return nil, err
  1122. }
  1123. if req.Status == 1 {
  1124. db = db.Order("commit_at")
  1125. } else {
  1126. db = db.Order("confirm_at desc")
  1127. }
  1128. // 查询该页数据
  1129. limit := req.PageSize
  1130. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  1131. err := db.Limit(int(limit)).Offset(int(offset)).Find(&rechargeRecords).Error
  1132. if err != nil {
  1133. logrus.WithContext(ctx).Errorf("[GetRechargeRecords] error query mysql limit, err:%+v", err)
  1134. return nil, err
  1135. }
  1136. var enterpriseIds []string
  1137. for _, rechargeRecord := range rechargeRecords {
  1138. enterpriseIds = append(enterpriseIds, rechargeRecord.EnterpriseID)
  1139. }
  1140. util.RemoveStrRepByMap(enterpriseIds)
  1141. enterpriseIdToUserInfoMap := make(map[string]gorm_model.Enterprise)
  1142. db1 := GetReadDB(ctx)
  1143. for _, v := range enterpriseIds {
  1144. enterpriseInfo := gorm_model.Enterprise{}
  1145. db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", v).Find(&enterpriseInfo)
  1146. enterpriseIdToUserInfoMap[v] = enterpriseInfo
  1147. }
  1148. var RechargeRecords []*http_model.RechargeRecordsPreview
  1149. for _, rechargeRecord := range rechargeRecords {
  1150. RechargeRecord := new(http_model.RechargeRecordsPreview)
  1151. RechargeRecord.RechargeId = rechargeRecord.RechargeID
  1152. RechargeRecord.EnterpriseID = rechargeRecord.EnterpriseID
  1153. RechargeRecord.RechargeAmount = rechargeRecord.RechargeAmount
  1154. RechargeRecord.ConfirmAt = conv.MustString(rechargeRecord.ConfirmAt, "")[:19]
  1155. RechargeRecord.CommitAt = conv.MustString(rechargeRecord.CommitAt, "")[:19]
  1156. RechargeRecord.Phone = rechargeRecord.Phone
  1157. RechargeRecord.TransferVoucher = rechargeRecord.TransferVoucherUrl
  1158. RechargeRecord.RechargeMethod = consts.GetRechargeMethod(rechargeRecord.RechargeMethod)
  1159. RechargeRecord.UserId = rechargeRecord.EnterpriseID
  1160. RechargeRecord.Username = GetUsernameByUserID(ctx, enterpriseIdToUserInfoMap[rechargeRecord.EnterpriseID].UserID)
  1161. RechargeRecord.BusinessName = enterpriseIdToUserInfoMap[rechargeRecord.EnterpriseID].BusinessName
  1162. RechargeRecords = append(RechargeRecords, RechargeRecord)
  1163. }
  1164. var RechargeRecordsData http_model.RechargeRecordsData
  1165. RechargeRecordsData.RechargeRecordsPreview = RechargeRecords
  1166. RechargeRecordsData.Total = conv.MustString(total, "")
  1167. return &RechargeRecordsData, nil
  1168. }
  1169. func OperateRecharge(ctx context.Context, req *http_model.OperateRechargeRequest) error {
  1170. db := GetReadDB(ctx)
  1171. db1 := GetReadDB(ctx)
  1172. err := db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", req.EnterpriseId).Updates(map[string]interface{}{
  1173. "balance": gorm.Expr("balance + ?", req.RechargeAmount),
  1174. "available_balance": gorm.Expr("available_balance + ?", req.RechargeAmount)}).Error
  1175. if err != nil {
  1176. logrus.WithContext(ctx).Errorf("[OperateRecharge] error Updates balance, err:%+v", err)
  1177. return err
  1178. }
  1179. err1 := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Updates(gorm_model.YounggeeRechargeRecord{
  1180. Status: 2,
  1181. InvoiceStatus: 2,
  1182. ConfirmAt: time.Now(),
  1183. }).Error
  1184. if err1 != nil {
  1185. logrus.WithContext(ctx).Errorf("[OperateRecharge] error Updates Status, err:%+v", err)
  1186. return err1
  1187. }
  1188. if req.Method == 1 {
  1189. db2 := GetReadDB(ctx)
  1190. db2.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Updates(gorm_model.YounggeeRechargeRecord{
  1191. RechargeAmount: req.RechargeAmount,
  1192. })
  1193. }
  1194. return nil
  1195. }
  1196. func GetBillTaskList(ctx context.Context, req http_model.GetBillTaskListRequest) (*http_model.BillTaskData, error) {
  1197. db := GetReadDB(ctx)
  1198. // 获取分页参数
  1199. page := req.PageNum
  1200. pageSize := req.PageSize
  1201. if page < 1 {
  1202. page = 1
  1203. }
  1204. if pageSize < 1 {
  1205. pageSize = 10 // 设置默认每页记录数为 10
  1206. }
  1207. var invoice gorm_model.YounggeeInvoiceRecord
  1208. err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id = ?", req.BillingID).First(&invoice).Error
  1209. if err != nil {
  1210. return nil, err
  1211. }
  1212. var ids [][]string // 二维切片用于存储二元组
  1213. if err = json.Unmarshal([]byte(invoice.TaskIds), &ids); err != nil {
  1214. return nil, err // 返回错误信息
  1215. }
  1216. var results []*http_model.BillTaskDataResponse // 用于存储合并后的结果
  1217. // 检查ID组的有效性
  1218. if len(ids) != 3 {
  1219. return nil, fmt.Errorf("task_ids must contain three groups")
  1220. }
  1221. // 第一组ID对应 selection 表
  1222. if len(ids[0]) > 0 {
  1223. var selections []gorm_model.YounggeeSelectionInfo
  1224. if err := db.Where("selection_id IN ?", ids[0]).Find(&selections).Error; err != nil {
  1225. return nil, err
  1226. }
  1227. for _, selection := range selections {
  1228. var productName string
  1229. var productPrice float64
  1230. var mainImage string
  1231. var creater string
  1232. product, err := GetProductByID(ctx, int64(selection.ProductID))
  1233. if err == nil && product != nil {
  1234. productName = product.ProductName
  1235. productPrice = product.ProductPrice
  1236. }
  1237. if selection.SubAccountID == 0 {
  1238. creater = GetEnterprisenameByEnterpriseID(ctx, selection.EnterpriseID)
  1239. } else {
  1240. creater = GetOperatorBySubacountID(ctx, selection.SubAccountID)
  1241. }
  1242. mainImage, err = GetMainPhotoByProductID(ctx, int64(selection.ProductID))
  1243. results = append(results, &http_model.BillTaskDataResponse{
  1244. TaskType: 1,
  1245. TaskForm: selection.TaskMode,
  1246. Platform: selection.Platform,
  1247. ContentType: selection.ContentType,
  1248. SettleAmount: selection.SettlementAmount,
  1249. Operator: creater,
  1250. PhotoUrl: mainImage,
  1251. Price: productPrice,
  1252. ProductName: productName,
  1253. Taskid: selection.SelectionID,
  1254. })
  1255. }
  1256. }
  1257. // 第二组ID对应 project 表
  1258. if len(ids[1]) > 0 {
  1259. var projects []gorm_model.ProjectInfo
  1260. if err := db.Where("project_id IN ?", ids[1]).Find(&projects).Error; err != nil {
  1261. return nil, err
  1262. }
  1263. for _, project := range projects {
  1264. var productName string
  1265. var productPrice float64
  1266. var mainImage string
  1267. var creater string
  1268. product, err := GetProductByID(ctx, project.ProductID)
  1269. if err == nil && product != nil {
  1270. productName = product.ProductName
  1271. productPrice = product.ProductPrice
  1272. }
  1273. if project.SubAccountID == 0 {
  1274. creater = GetEnterprisenameByEnterpriseID(ctx, project.EnterpriseID)
  1275. } else {
  1276. creater = GetOperatorBySubacountID(ctx, int(project.SubAccountID))
  1277. }
  1278. mainImage, err = GetMainPhotoByProductID(ctx, project.ProductID)
  1279. results = append(results, &http_model.BillTaskDataResponse{
  1280. TaskType: 2,
  1281. TaskForm: int(project.ProjectForm),
  1282. ContentType: int(project.ContentType),
  1283. SettleAmount: conv.MustString(project.SettlementAmount, ""),
  1284. Operator: creater,
  1285. PhotoUrl: mainImage,
  1286. Price: productPrice,
  1287. ProductName: productName,
  1288. Taskid: project.ProjectID,
  1289. })
  1290. }
  1291. }
  1292. // 第三组ID对应 llocalife 表
  1293. if len(ids[2]) > 0 {
  1294. var localifes []gorm_model.YounggeeLocalLifeInfo
  1295. if err := db.Where("local_id IN ?", ids[2]).Find(&localifes).Error; err != nil {
  1296. return nil, err
  1297. }
  1298. for _, localife := range localifes {
  1299. var productName string
  1300. var address string
  1301. var mainImage string
  1302. var creater string
  1303. store, err := GetStoreByID(ctx, int64(localife.StoreId))
  1304. if err == nil && store != nil {
  1305. productName = store.StoreName
  1306. address = store.StoreLocation
  1307. }
  1308. if localife.SubAccountId == 0 {
  1309. creater = GetEnterprisenameByEnterpriseID(ctx, localife.EnterpriseId)
  1310. } else {
  1311. creater = GetOperatorBySubacountID(ctx, localife.SubAccountId)
  1312. }
  1313. mainImage, err = GetMainPhotoByStoreID(ctx, int64(localife.StoreId))
  1314. results = append(results, &http_model.BillTaskDataResponse{
  1315. TaskType: 3,
  1316. TaskForm: localife.TaskForm,
  1317. ContentType: localife.ContentType,
  1318. SettleAmount: conv.MustString(localife.SettlementAmount, ""),
  1319. Operator: creater,
  1320. Taskid: localife.LocalId,
  1321. ProductName: productName,
  1322. Address: address,
  1323. PhotoUrl: mainImage,
  1324. })
  1325. }
  1326. }
  1327. // 计算总数
  1328. total := len(results)
  1329. // 分页处理
  1330. start := (page - 1) * pageSize
  1331. if start >= total {
  1332. return &http_model.BillTaskData{InvoiceListinfo: nil, Total: total}, nil // 返回空结果
  1333. }
  1334. end := start + pageSize
  1335. if end > total {
  1336. end = total
  1337. }
  1338. // 返回分页后的结果
  1339. return &http_model.BillTaskData{
  1340. InvoiceListinfo: results[start:end],
  1341. Total: total,
  1342. }, nil
  1343. }
  1344. func GetMainPhotoByStoreID(ctx context.Context, storeID int64) (string, error) {
  1345. db := GetReadDB(ctx)
  1346. var productPhoto gorm_model.YounggeeProductPhoto
  1347. err := db.Where("store_id = ? AND symbol = ?", storeID, 1).First(&productPhoto).Error
  1348. if err != nil {
  1349. return "", err
  1350. }
  1351. return productPhoto.PhotoUrl, nil
  1352. }
  1353. func GetMainPhotoByProductID(ctx context.Context, productId int64) (string, error) {
  1354. db := GetReadDB(ctx)
  1355. var productPhoto gorm_model.YounggeeProductPhoto
  1356. err := db.Where("product_id = ? AND symbol = ?", productId, 1).First(&productPhoto).Error
  1357. if err != nil {
  1358. return "", err
  1359. }
  1360. return productPhoto.PhotoUrl, nil
  1361. }
  1362. func GetStoreByID(ctx context.Context, stroeid int64) (*gorm_model.YounggeeStore, error) {
  1363. db := GetReadDB(ctx)
  1364. store := &gorm_model.YounggeeStore{}
  1365. err := db.First(&store, stroeid).Error
  1366. if err != nil {
  1367. if err == gorm.ErrRecordNotFound {
  1368. return nil, nil
  1369. } else {
  1370. return nil, err
  1371. }
  1372. }
  1373. return store, nil
  1374. }