finance.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "youngee_m_api/consts"
  10. "youngee_m_api/model/common_model"
  11. "youngee_m_api/model/gorm_model"
  12. "youngee_m_api/model/http_model"
  13. "youngee_m_api/util"
  14. "github.com/caixw/lib.go/conv"
  15. "github.com/sirupsen/logrus"
  16. "gorm.io/gorm"
  17. )
  18. func GetWithdrawRecords(ctx context.Context, pageSize, pageNum int32, req *http_model.WithdrawalRecordsRequest, condition *common_model.WithdrawRecordsCondition) (*http_model.WithdrawalRecordsPreview, error) {
  19. db := GetReadDB(ctx)
  20. var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
  21. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{})
  22. conditionType := reflect.TypeOf(condition).Elem()
  23. conditionValue := reflect.ValueOf(condition).Elem()
  24. for i := 0; i < conditionType.NumField(); i++ {
  25. field := conditionType.Field(i)
  26. tag := field.Tag.Get("condition")
  27. value := conditionValue.FieldByName(field.Name)
  28. if tag == "submit_at" && value.Interface() != "" {
  29. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  30. }
  31. if tag == "withdraw_at" && value.Interface() != "" {
  32. db = db.Where(fmt.Sprintf("withdraw_at like '%s%%'", value.Interface()))
  33. }
  34. if !util.IsBlank(value) && tag != "submit_at" && tag != "withdraw_at" {
  35. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  36. }
  37. }
  38. if req.TalentName != "" {
  39. fmt.Println("TalentName:", req.TalentName)
  40. db1 := GetReadDB(ctx)
  41. var talentId string
  42. db1.Model(gorm_model.YoungeeTalentInfo{}).Select("id").Where(fmt.Sprintf("talent_wx_nickname like '%%%s%%'", req.TalentName)).Find(&talentId)
  43. db = db.Where("talent_id = ?", talentId)
  44. }
  45. // 查询总数
  46. var total int64
  47. if err := db.Count(&total).Error; err != nil {
  48. logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql total, err:%+v", err)
  49. return nil, err
  50. }
  51. // 查询该页数据
  52. limit := pageSize
  53. offset := pageSize * pageNum // assert pageNum start with 0
  54. err := db.Order("submit_at").Limit(int(limit)).Offset(int(offset)).Find(&withdrawRecords).Error
  55. if err != nil {
  56. logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql limit, err:%+v", err)
  57. return nil, err
  58. }
  59. var talentIds []string
  60. for _, withdrawRecord := range withdrawRecords {
  61. talentIds = append(talentIds, withdrawRecord.TalentID)
  62. }
  63. talentIdToTalentInfoMap := make(map[string]gorm_model.YoungeeTalentInfo)
  64. for _, talentId := range talentIds {
  65. db1 := GetReadDB(ctx)
  66. talentInfo := gorm_model.YoungeeTalentInfo{}
  67. db1.Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", talentId).Find(&talentInfo)
  68. talentIdToTalentInfoMap[talentId] = talentInfo
  69. }
  70. var withdrawRecordsDatas []*http_model.WithdrawalRecordsData
  71. for _, withdrawRecord := range withdrawRecords {
  72. withdrawRecordsData := new(http_model.WithdrawalRecordsData)
  73. withdrawRecordsData.WithdrawId = withdrawRecord.WithdrawID
  74. withdrawRecordsData.TalentId = withdrawRecord.TalentID
  75. withdrawRecordsData.TalentName = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentWxNickname
  76. withdrawRecordsData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
  77. withdrawRecordsData.AmountPayable = float32(withdrawRecord.AmountPayable)
  78. withdrawRecordsData.ReceiveInfo = withdrawRecord.ReceiveInfo
  79. withdrawRecordsData.BankType = withdrawRecord.BankType
  80. withdrawRecordsData.Phone = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentPhoneNumber
  81. withdrawRecordsData.SubmitAt = conv.MustString(withdrawRecord.SubmitAt, "")[0:19]
  82. withdrawRecordsData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
  83. withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordsData)
  84. }
  85. var withdrawRecordsPreview http_model.WithdrawalRecordsPreview
  86. withdrawRecordsPreview.WithdrawalRecordsData = withdrawRecordsDatas
  87. withdrawRecordsPreview.Total = total
  88. return &withdrawRecordsPreview, nil
  89. }
  90. func GetWithdrawRecord(ctx context.Context, req *http_model.GetWithdrawalRecordRequest) (*http_model.WithdrawalRecordPreview, error) {
  91. db := GetReadDB(ctx)
  92. //fmt.Println("talentId:", req.TalentId)
  93. var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
  94. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Where("talent_id = ? AND status = ?", req.TalentId, 2).Find(&withdrawRecords)
  95. var withdrawRecordsPreview http_model.WithdrawalRecordPreview
  96. var withdrawRecordsDatas []*http_model.WithdrawalRecordData
  97. for _, withdrawRecord := range withdrawRecords {
  98. withdrawRecordData := new(http_model.WithdrawalRecordData)
  99. withdrawRecordData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
  100. withdrawRecordData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
  101. withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordData)
  102. }
  103. withdrawRecordsPreview.WithdrawalRecordData = withdrawRecordsDatas
  104. return &withdrawRecordsPreview, nil
  105. }
  106. func GetWithdrawNums(ctx context.Context) (*http_model.WithdrawNums, error) {
  107. var withdrawNums int64
  108. db := GetReadDB(ctx)
  109. err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("status = 1").Count(&withdrawNums).Error
  110. if err != nil {
  111. return nil, err
  112. }
  113. WithdrawNums := new(http_model.WithdrawNums)
  114. WithdrawNums.WithdrawNums = int(withdrawNums)
  115. return WithdrawNums, err
  116. }
  117. func GetInvoiceNums(ctx context.Context) (*http_model.InvoiceNums, error) {
  118. var invoiceNums int64
  119. db := GetReadDB(ctx)
  120. err := db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = 1").Count(&invoiceNums).Error
  121. if err != nil {
  122. return nil, err
  123. }
  124. InvoiceNums := new(http_model.InvoiceNums)
  125. InvoiceNums.InvoiceNums = int(invoiceNums)
  126. return InvoiceNums, err
  127. }
  128. func GetRechargeNums(ctx context.Context) (*http_model.RechargeNums, error) {
  129. var rechargeNums int64
  130. db := GetReadDB(ctx)
  131. err := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("status = 1").Count(&rechargeNums).Error
  132. if err != nil {
  133. return nil, err
  134. }
  135. RechargeNums := new(http_model.RechargeNums)
  136. RechargeNums.RechargeNums = rechargeNums
  137. return RechargeNums, err
  138. }
  139. func ConfirmWithdrawal(ctx context.Context, withdrawId string) error {
  140. db := GetReadDB(ctx)
  141. db2 := GetReadDB(ctx)
  142. withdrawInfo := gorm_model.YounggeeWithdrawRecord{}
  143. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Where("withdraw_id = ?", withdrawId).Find(&withdrawInfo)
  144. db2.Debug().Where("withdraw_id = ?", withdrawId).Updates(gorm_model.YounggeeWithdrawRecord{Status: 2, WithdrawAt: time.Now()})
  145. taskIdLists := strings.Split(withdrawInfo.TaskIDList, ",")
  146. for _, taskId := range taskIdLists {
  147. db1 := GetReadDB(ctx)
  148. err := db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", taskId).Updates(gorm_model.YoungeeTaskInfo{WithdrawStatus: 4}).Error
  149. if err != nil {
  150. logrus.WithContext(ctx).Errorf("[finance db] Update YoungeeTaskInfo error,err:%+v", err)
  151. return err
  152. }
  153. err = CreateMessageByTaskId(ctx, 6, 1, taskId)
  154. if err != nil {
  155. logrus.WithContext(ctx).Errorf("[ConfirmWithdrawal] call CreateMessageByTaskId error,err:%+v", err)
  156. return err
  157. }
  158. }
  159. db3 := GetReadDB(ctx)
  160. db3.Debug().Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", withdrawInfo.TalentID).Updates(
  161. map[string]interface{}{
  162. "withdrawing": gorm.Expr("withdrawing - ?", withdrawInfo.WithdrawAmount),
  163. "withdrawed": gorm.Expr("withdrawed + ?", withdrawInfo.WithdrawAmount)})
  164. return nil
  165. }
  166. func GetBankInfo(ctx context.Context, req *http_model.GetBankInfoRequest) (*http_model.BankInfo, error) {
  167. //db := GetReadDB(ctx)
  168. //if req.BankId == "" {
  169. // return nil, nil
  170. //}
  171. //var infoBank *gorm_model.InfoBank
  172. //db.Debug().Model(gorm_model.InfoBank{}).Where("id = ?", req.BankId).First(&infoBank)
  173. db1 := GetReadDB(ctx)
  174. var infoRegion *gorm_model.InfoRegion
  175. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(req.BankOpenAddress, 0)).First(&infoRegion)
  176. provinceCode := conv.MustString(req.BankOpenAddress, "")[0:2] + "0000"
  177. var province *gorm_model.InfoRegion
  178. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(provinceCode, 0)).First(&province)
  179. cityCode := conv.MustString(req.BankOpenAddress, "")[0:4] + "00"
  180. var city *gorm_model.InfoRegion
  181. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(cityCode, 0)).First(&city)
  182. data := new(http_model.BankInfo)
  183. //data.BankName = infoBank.Name
  184. data.BankOpenAddress = province.RegionName + city.RegionName + infoRegion.RegionName
  185. //db.Model(gorm_model.InfoBank{}).Where("")
  186. return data, nil
  187. }
  188. // GetEnterpriseIDByBusiness 根据企业名称查找企业ID
  189. func GetEnterpriseIDByBusiness(ctx context.Context, BusinessName string) int64 {
  190. db := GetReadDB(ctx)
  191. var EnterpriseID int64
  192. db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("business_name", BusinessName).First(&EnterpriseID)
  193. return EnterpriseID
  194. }
  195. // GetEnterpriseIDByUserId 根据企业名称查找企业ID
  196. func GetEnterpriseIDByUserId(ctx context.Context, UserId int64) int64 {
  197. db := GetReadDB(ctx)
  198. var EnterpriseID int64
  199. db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("user_id", UserId).First(&EnterpriseID)
  200. return EnterpriseID
  201. }
  202. // GetUserIDByUsername 根据用户名称查UserID
  203. func GetUserIDByUsername(ctx context.Context, Username string) int64 {
  204. db := GetReadDB(ctx)
  205. var UserID int64
  206. db = db.Model([]gorm_model.YounggeeUser{}).Select("id").Where(fmt.Sprintf("username like '%%%s%%'", Username)).First(&UserID)
  207. return UserID
  208. }
  209. func GetInvoiceRecords(ctx context.Context, req *http_model.InvoiceRecordsRequest, condition *common_model.InvoiceRecordsCondition) (*http_model.InvoiceRecordsData, error) {
  210. db := GetReadDB(ctx)
  211. var invoiceRecords []*gorm_model.YounggeeInvoiceRecord
  212. db = db.Debug().Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", req.InvoiceStatus)
  213. conditionType := reflect.TypeOf(condition).Elem()
  214. conditionValue := reflect.ValueOf(condition).Elem()
  215. for i := 0; i < conditionType.NumField(); i++ {
  216. field := conditionType.Field(i)
  217. tag := field.Tag.Get("condition")
  218. value := conditionValue.FieldByName(field.Name)
  219. if tag == "submit_at" && value.Interface() != "" {
  220. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  221. }
  222. if tag == "billing_at" && value.Interface() != "" {
  223. db = db.Where(fmt.Sprintf("billing_at like '%s%%'", value.Interface()))
  224. }
  225. }
  226. if req.Username != "" {
  227. UserID := GetUserIDByUsername(ctx, req.Username)
  228. enterpriseId := GetEnterpriseIDByUserId(ctx, UserID)
  229. db = db.Where("enterprise_id = ?", enterpriseId)
  230. }
  231. if req.UserId != 0 {
  232. enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
  233. db = db.Where("enterprise_id = ?", enterpriseId)
  234. }
  235. // 查询总数
  236. var total int64
  237. if err := db.Count(&total).Error; err != nil {
  238. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql total, err:%+v", err)
  239. return nil, err
  240. }
  241. if req.InvoiceStatus != 3 {
  242. db.Order("submit_at")
  243. } else {
  244. db.Order("billing_at desc")
  245. }
  246. // 查询该页数据
  247. limit := req.PageSize
  248. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  249. err := db.Limit(int(limit)).Offset(int(offset)).Find(&invoiceRecords).Error
  250. if err != nil {
  251. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql limit, err:%+v", err)
  252. return nil, err
  253. }
  254. enterpriseIdToUserInfoMap := make(map[string]gorm_model.Enterprise)
  255. regionAddressMap := make(map[string]string)
  256. for _, invoiceRecord := range invoiceRecords {
  257. if _, ok := enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID]; !ok {
  258. db1 := GetReadDB(ctx)
  259. enterpriseInfo := gorm_model.Enterprise{}
  260. db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", invoiceRecord.EnterpriseID).Find(&enterpriseInfo)
  261. enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID] = enterpriseInfo
  262. }
  263. if _, ok := regionAddressMap[invoiceRecord.EnterpriseID]; !ok {
  264. db1 := GetReadDB(ctx)
  265. var regionCode string
  266. db1.Model(gorm_model.YounggeeInvoiceAddress{}).Select("region_code").Where("enterprise_id = ?", invoiceRecord.EnterpriseID).Find(&regionCode)
  267. regionAddressMap[invoiceRecord.EnterpriseID] = GetRegion(ctx, conv.MustInt(regionCode, 0))
  268. }
  269. }
  270. var InvoiceRecords []*http_model.InvoiceRecordsPreviews
  271. for _, invoiceRecord := range invoiceRecords {
  272. InvoiceRecord := new(http_model.InvoiceRecordsPreviews)
  273. InvoiceRecord.BillingId = invoiceRecord.BillingID
  274. InvoiceRecord.InvoiceInfo = invoiceRecord.InvoiceSnap
  275. InvoiceRecord.AddressInfo = invoiceRecord.AddressSnap
  276. InvoiceRecord.InvoiceAddress = regionAddressMap[invoiceRecord.EnterpriseID]
  277. InvoiceRecord.InvoiceType = invoiceRecord.InvoiceType
  278. InvoiceRecord.Amount = invoiceRecord.InvoiceAmount
  279. InvoiceRecord.Phone = invoiceRecord.Phone
  280. InvoiceRecord.ShipmentNumber = invoiceRecord.ShipmentNumber
  281. InvoiceRecord.BusinessName = enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].BusinessName
  282. InvoiceRecord.UserId = invoiceRecord.EnterpriseID
  283. InvoiceRecord.Username = GetUsernameByUserID(ctx, enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].UserID)
  284. InvoiceRecord.SubmitAt = conv.MustString(invoiceRecord.SubmitAt, "")[:19]
  285. InvoiceRecord.BillingAt = conv.MustString(invoiceRecord.BillingAt, "")[:19]
  286. InvoiceRecords = append(InvoiceRecords, InvoiceRecord)
  287. }
  288. var InvoiceRecordsData http_model.InvoiceRecordsData
  289. InvoiceRecordsData.InvoiceRecordsPreviews = InvoiceRecords
  290. InvoiceRecordsData.Total = strconv.FormatInt(total, 10)
  291. return &InvoiceRecordsData, nil
  292. }
  293. func ConfirmInvoice(ctx context.Context, request *http_model.ConfirmInvoiceRequest) error {
  294. db := GetReadDB(ctx)
  295. return db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id = ?", request.BillingId).Updates(
  296. gorm_model.YounggeeInvoiceRecord{
  297. BillingAt: time.Now(),
  298. ShipmentNumber: request.ShipmentNumber,
  299. Status: 2,
  300. }).Error
  301. }
  302. func GetUsernameByUserID(ctx context.Context, UserID int64) (username string) {
  303. db := GetReadDB(ctx)
  304. db = db.Model([]gorm_model.YounggeeUser{}).Select("username").Where("id", UserID).First(&username)
  305. return username
  306. }
  307. func GetRechargeRecords(ctx context.Context, req *http_model.GetRechargeRecordsRequest, condition *common_model.RechargeRecordsCondition) (*http_model.RechargeRecordsData, error) {
  308. db := GetReadDB(ctx)
  309. var rechargeRecords []*gorm_model.YounggeeRechargeRecord
  310. db = db.Debug().Model(gorm_model.YounggeeRechargeRecord{}).Where("status = ?", req.Status)
  311. conditionType := reflect.TypeOf(condition).Elem()
  312. conditionValue := reflect.ValueOf(condition).Elem()
  313. for i := 0; i < conditionType.NumField(); i++ {
  314. field := conditionType.Field(i)
  315. tag := field.Tag.Get("condition")
  316. value := conditionValue.FieldByName(field.Name)
  317. if tag == "commit_at" && value.Interface() != "" {
  318. db = db.Where(fmt.Sprintf("commit_at like '%s%%'", value.Interface()))
  319. }
  320. if tag == "confirm_at" && value.Interface() != "" {
  321. db = db.Where(fmt.Sprintf("confirm_at like '%s%%'", value.Interface()))
  322. }
  323. }
  324. if req.Username != "" {
  325. UserID := GetUserIDByUsername(ctx, req.Username)
  326. enterpriseId := GetEnterpriseIDByUserId(ctx, UserID)
  327. db = db.Where("enterprise_id = ?", enterpriseId)
  328. }
  329. if req.UserId != 0 {
  330. enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
  331. db = db.Where("enterprise_id = ?", enterpriseId)
  332. }
  333. if req.RechargeMethod == 1 {
  334. db = db.Where("recharge_method = ?", 1)
  335. } else if req.RechargeMethod == 2 {
  336. db = db.Where("recharge_method = ?", 2)
  337. } else if req.RechargeMethod == 3 {
  338. db = db.Where("recharge_method = ?", 3)
  339. }
  340. // 查询总数
  341. var total int64
  342. if err := db.Count(&total).Error; err != nil {
  343. logrus.WithContext(ctx).Errorf("[GetRechargeRecords] error query mysql total, err:%+v", err)
  344. return nil, err
  345. }
  346. if req.Status == 1 {
  347. db = db.Order("commit_at")
  348. } else {
  349. db = db.Order("confirm_at desc")
  350. }
  351. // 查询该页数据
  352. limit := req.PageSize
  353. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  354. err := db.Limit(int(limit)).Offset(int(offset)).Find(&rechargeRecords).Error
  355. if err != nil {
  356. logrus.WithContext(ctx).Errorf("[GetRechargeRecords] error query mysql limit, err:%+v", err)
  357. return nil, err
  358. }
  359. var enterpriseIds []string
  360. for _, rechargeRecord := range rechargeRecords {
  361. enterpriseIds = append(enterpriseIds, rechargeRecord.EnterpriseID)
  362. }
  363. util.RemoveStrRepByMap(enterpriseIds)
  364. enterpriseIdToUserInfoMap := make(map[string]gorm_model.Enterprise)
  365. db1 := GetReadDB(ctx)
  366. for _, v := range enterpriseIds {
  367. enterpriseInfo := gorm_model.Enterprise{}
  368. db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", v).Find(&enterpriseInfo)
  369. enterpriseIdToUserInfoMap[v] = enterpriseInfo
  370. }
  371. var RechargeRecords []*http_model.RechargeRecordsPreview
  372. for _, rechargeRecord := range rechargeRecords {
  373. RechargeRecord := new(http_model.RechargeRecordsPreview)
  374. RechargeRecord.RechargeId = rechargeRecord.RechargeID
  375. RechargeRecord.EnterpriseID = rechargeRecord.EnterpriseID
  376. RechargeRecord.RechargeAmount = rechargeRecord.RechargeAmount
  377. RechargeRecord.ConfirmAt = conv.MustString(rechargeRecord.ConfirmAt, "")[:19]
  378. RechargeRecord.CommitAt = conv.MustString(rechargeRecord.CommitAt, "")[:19]
  379. RechargeRecord.Phone = rechargeRecord.Phone
  380. RechargeRecord.TransferVoucher = rechargeRecord.TransferVoucherUrl
  381. RechargeRecord.RechargeMethod = consts.GetRechargeMethod(rechargeRecord.RechargeMethod)
  382. RechargeRecord.UserId = rechargeRecord.EnterpriseID
  383. RechargeRecord.Username = GetUsernameByUserID(ctx, enterpriseIdToUserInfoMap[rechargeRecord.EnterpriseID].UserID)
  384. RechargeRecord.BusinessName = enterpriseIdToUserInfoMap[rechargeRecord.EnterpriseID].BusinessName
  385. RechargeRecords = append(RechargeRecords, RechargeRecord)
  386. }
  387. var RechargeRecordsData http_model.RechargeRecordsData
  388. RechargeRecordsData.RechargeRecordsPreview = RechargeRecords
  389. RechargeRecordsData.Total = conv.MustString(total, "")
  390. return &RechargeRecordsData, nil
  391. }
  392. func OperateRecharge(ctx context.Context, req *http_model.OperateRechargeRequest) error {
  393. db := GetReadDB(ctx)
  394. db1 := GetReadDB(ctx)
  395. err := db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", req.EnterpriseId).Updates(map[string]interface{}{
  396. "balance": gorm.Expr("balance + ?", req.RechargeAmount),
  397. "available_balance": gorm.Expr("available_balance + ?", req.RechargeAmount)}).Error
  398. if err != nil {
  399. logrus.WithContext(ctx).Errorf("[OperateRecharge] error Updates balance, err:%+v", err)
  400. return err
  401. }
  402. err1 := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Updates(gorm_model.YounggeeRechargeRecord{
  403. Status: 2,
  404. InvoiceStatus: 2,
  405. ConfirmAt: time.Now(),
  406. }).Error
  407. if err1 != nil {
  408. logrus.WithContext(ctx).Errorf("[OperateRecharge] error Updates Status, err:%+v", err)
  409. return err1
  410. }
  411. if req.Method == 1 {
  412. db2 := GetReadDB(ctx)
  413. db2.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Updates(gorm_model.YounggeeRechargeRecord{
  414. RechargeAmount: req.RechargeAmount,
  415. })
  416. }
  417. return nil
  418. }