finance.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "github.com/sirupsen/logrus"
  7. "gorm.io/gorm"
  8. "reflect"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "youngee_m_api/consts"
  13. "youngee_m_api/model/common_model"
  14. "youngee_m_api/model/gorm_model"
  15. "youngee_m_api/model/http_model"
  16. "youngee_m_api/util"
  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("talent_wx_nickname = ? ", 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 ConfirmWithdrawal(ctx context.Context, withdrawId string) error {
  118. db := GetReadDB(ctx)
  119. db2 := GetReadDB(ctx)
  120. var task_id_list string
  121. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Select("task_id_list").Where("withdraw_id = ?", withdrawId).Find(&task_id_list)
  122. db2.Debug().Where("withdraw_id = ?", withdrawId).Updates(gorm_model.YounggeeWithdrawRecord{Status: 2, WithdrawAt: time.Now()})
  123. taskIdLists := strings.Split(task_id_list, ",")
  124. for _, taskIdList := range taskIdLists {
  125. db1 := GetReadDB(ctx)
  126. db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", conv.MustInt(taskIdList, 0)).Updates(gorm_model.YoungeeTaskInfo{WithdrawStatus: 4})
  127. }
  128. return nil
  129. }
  130. func GetBankInfo(ctx context.Context, req *http_model.GetBankInfoRequest) (*http_model.BankInfo, error) {
  131. db := GetReadDB(ctx)
  132. var infoBank *gorm_model.InfoBank
  133. db.Debug().Model(gorm_model.InfoBank{}).Where("id = ?", req.BankId).First(&infoBank)
  134. db1 := GetReadDB(ctx)
  135. var infoRegion *gorm_model.InfoRegion
  136. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(req.BankOpenAddress, 0)).First(&infoRegion)
  137. provinceCode := conv.MustString(req.BankOpenAddress, "")[0:2] + "0000"
  138. var province *gorm_model.InfoRegion
  139. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(provinceCode, 0)).First(&province)
  140. cityCode := conv.MustString(req.BankOpenAddress, "")[0:4] + "00"
  141. var city *gorm_model.InfoRegion
  142. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(cityCode, 0)).First(&city)
  143. data := new(http_model.BankInfo)
  144. data.BankName = infoBank.Name
  145. data.BankOpenAddress = province.RegionName + city.RegionName + infoRegion.RegionName
  146. //db.Model(gorm_model.InfoBank{}).Where("")
  147. return data, nil
  148. }
  149. // GetEnterpriseIDByBusiness 根据企业名称查找企业ID
  150. func GetEnterpriseIDByBusiness(ctx context.Context, BusinessName string) int64 {
  151. db := GetReadDB(ctx)
  152. var EnterpriseID int64
  153. db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("business_name", BusinessName).First(&EnterpriseID)
  154. return EnterpriseID
  155. }
  156. // GetEnterpriseIDByUserId 根据企业名称查找企业ID
  157. func GetEnterpriseIDByUserId(ctx context.Context, UserId int64) int64 {
  158. db := GetReadDB(ctx)
  159. var EnterpriseID int64
  160. db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("user_id", UserId).First(&EnterpriseID)
  161. return EnterpriseID
  162. }
  163. func GetInvoiceRecords(ctx context.Context, req *http_model.InvoiceRecordsRequest, condition *common_model.InvoiceRecordsCondition) (*http_model.InvoiceRecordsData, error) {
  164. db := GetReadDB(ctx)
  165. var invoiceRecords []*gorm_model.YounggeeInvoiceRecord
  166. db = db.Debug().Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", req.InvoiceStatus)
  167. conditionType := reflect.TypeOf(condition).Elem()
  168. conditionValue := reflect.ValueOf(condition).Elem()
  169. for i := 0; i < conditionType.NumField(); i++ {
  170. field := conditionType.Field(i)
  171. tag := field.Tag.Get("condition")
  172. value := conditionValue.FieldByName(field.Name)
  173. if tag == "submit_at" && value.Interface() != "" {
  174. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  175. }
  176. if tag == "billing_at" && value.Interface() != "" {
  177. db = db.Where(fmt.Sprintf("billing_at like '%s%%'", value.Interface()))
  178. }
  179. }
  180. if req.BusinessName != "" {
  181. enterpriseId := GetEnterpriseIDByBusiness(ctx, req.BusinessName)
  182. db = db.Where("enterprise_id = ?", enterpriseId)
  183. }
  184. if req.UserId != 0 {
  185. enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
  186. db = db.Where("enterprise_id = ?", enterpriseId)
  187. }
  188. // 查询总数
  189. var total int64
  190. if err := db.Count(&total).Error; err != nil {
  191. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql total, err:%+v", err)
  192. return nil, err
  193. }
  194. if req.InvoiceStatus == 1 {
  195. db.Order("submit_at")
  196. } else {
  197. db.Order("billing_at desc")
  198. }
  199. // 查询该页数据
  200. limit := req.PageSize
  201. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  202. err := db.Limit(int(limit)).Offset(int(offset)).Find(&invoiceRecords).Error
  203. if err != nil {
  204. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql limit, err:%+v", err)
  205. return nil, err
  206. }
  207. var enterpriseIds []int64
  208. for _, invoiceRecord := range invoiceRecords {
  209. enterpriseIds = append(enterpriseIds, invoiceRecord.EnterpriseID)
  210. }
  211. util.RemoveRepByMap(enterpriseIds)
  212. enterpriseIdToUserInfoMap := make(map[int64]gorm_model.Enterprise)
  213. db1 := GetReadDB(ctx)
  214. for _, v := range enterpriseIds {
  215. enterpriseInfo := gorm_model.Enterprise{}
  216. db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", v).Find(&enterpriseInfo)
  217. enterpriseIdToUserInfoMap[v] = enterpriseInfo
  218. }
  219. var InvoiceRecords []*http_model.InvoiceRecordsPreviews
  220. for _, invoiceRecord := range invoiceRecords {
  221. InvoiceRecord := new(http_model.InvoiceRecordsPreviews)
  222. InvoiceRecord.BillingId = invoiceRecord.BillingID
  223. InvoiceRecord.InvoiceInfo = invoiceRecord.InvoiceSnap
  224. InvoiceRecord.AddressInfo = invoiceRecord.AddressSnap
  225. InvoiceRecord.InvoiceType = invoiceRecord.InvoiceType
  226. InvoiceRecord.Amount = invoiceRecord.InvoiceAmount
  227. InvoiceRecord.Phone = invoiceRecord.Phone
  228. InvoiceRecord.ShipmentNumber = invoiceRecord.ShipmentNumber
  229. InvoiceRecord.BusinessName = enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].BusinessName
  230. InvoiceRecord.UserId = enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].UserID
  231. InvoiceRecord.SubmitAt = conv.MustString(invoiceRecord.SubmitAt, "")[:19]
  232. InvoiceRecord.BillingAt = conv.MustString(invoiceRecord.BillingAt, "")[:19]
  233. InvoiceRecords = append(InvoiceRecords, InvoiceRecord)
  234. }
  235. var InvoiceRecordsData http_model.InvoiceRecordsData
  236. InvoiceRecordsData.InvoiceRecordsPreviews = InvoiceRecords
  237. InvoiceRecordsData.Total = strconv.FormatInt(total, 10)
  238. return &InvoiceRecordsData, nil
  239. }
  240. func ConfirmInvoice(ctx context.Context, request *http_model.ConfirmInvoiceRequest) error {
  241. db := GetReadDB(ctx)
  242. return db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id = ?", request.BillingId).Updates(
  243. gorm_model.YounggeeInvoiceRecord{
  244. BillingAt: time.Now(),
  245. ShipmentNumber: request.ShipmentNumber,
  246. Status: 2,
  247. }).Error
  248. }
  249. func GetRechargeRecords(ctx context.Context, req *http_model.GetRechargeRecordsRequest, condition *common_model.RechargeRecordsCondition) (*http_model.RechargeRecordsData, error) {
  250. db := GetReadDB(ctx)
  251. var rechargeRecords []*gorm_model.YounggeeRechargeRecord
  252. db = db.Debug().Model(gorm_model.YounggeeRechargeRecord{}).Where("status = ?", req.Status)
  253. conditionType := reflect.TypeOf(condition).Elem()
  254. conditionValue := reflect.ValueOf(condition).Elem()
  255. for i := 0; i < conditionType.NumField(); i++ {
  256. field := conditionType.Field(i)
  257. tag := field.Tag.Get("condition")
  258. value := conditionValue.FieldByName(field.Name)
  259. if tag == "commit_at" && value.Interface() != "" {
  260. db = db.Where(fmt.Sprintf("commit_at like '%s%%'", value.Interface()))
  261. }
  262. if tag == "confirm_at" && value.Interface() != "" {
  263. db = db.Where(fmt.Sprintf("confirm_at like '%s%%'", value.Interface()))
  264. }
  265. }
  266. if req.BusinessName != "" {
  267. enterpriseId := GetEnterpriseIDByBusiness(ctx, req.BusinessName)
  268. db = db.Where("enterprise_id = ?", enterpriseId)
  269. }
  270. if req.UserId != 0 {
  271. enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
  272. db = db.Where("enterprise_id = ?", enterpriseId)
  273. }
  274. if req.RechargeMethod == 1 {
  275. db = db.Where("recharge_method = ?", 1)
  276. } else if req.RechargeMethod == 2 {
  277. db = db.Where("recharge_method = ?", 2)
  278. } else if req.RechargeMethod == 3 {
  279. db = db.Where("recharge_method = ?", 3)
  280. }
  281. // 查询总数
  282. var total int64
  283. if err := db.Count(&total).Error; err != nil {
  284. logrus.WithContext(ctx).Errorf("[GetRechargeRecords] error query mysql total, err:%+v", err)
  285. return nil, err
  286. }
  287. if req.Status == 1 {
  288. db = db.Order("commit_at")
  289. } else {
  290. db = db.Order("confirm_at desc")
  291. }
  292. // 查询该页数据
  293. limit := req.PageSize
  294. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  295. err := db.Limit(int(limit)).Offset(int(offset)).Find(&rechargeRecords).Error
  296. if err != nil {
  297. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql limit, err:%+v", err)
  298. return nil, err
  299. }
  300. var enterpriseIds []int64
  301. for _, rechargeRecord := range rechargeRecords {
  302. enterpriseIds = append(enterpriseIds, rechargeRecord.EnterpriseID)
  303. }
  304. util.RemoveRepByMap(enterpriseIds)
  305. enterpriseIdToUserInfoMap := make(map[int64]gorm_model.Enterprise)
  306. db1 := GetReadDB(ctx)
  307. for _, v := range enterpriseIds {
  308. enterpriseInfo := gorm_model.Enterprise{}
  309. db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", v).Find(&enterpriseInfo)
  310. enterpriseIdToUserInfoMap[v] = enterpriseInfo
  311. }
  312. var RechargeRecords []*http_model.RechargeRecordsPreview
  313. for _, rechargeRecord := range rechargeRecords {
  314. RechargeRecord := new(http_model.RechargeRecordsPreview)
  315. RechargeRecord.RechargeId = rechargeRecord.RechargeID
  316. RechargeRecord.EnterpriseID = rechargeRecord.EnterpriseID
  317. RechargeRecord.RechargeAmount = rechargeRecord.RechargeAmount
  318. RechargeRecord.ConfirmAt = conv.MustString(rechargeRecord.ConfirmAt, "")[:19]
  319. RechargeRecord.CommitAt = conv.MustString(rechargeRecord.CommitAt, "")[:19]
  320. RechargeRecord.Phone = rechargeRecord.Phone
  321. RechargeRecord.TransferVoucher = rechargeRecord.TransferVoucherUrl
  322. RechargeRecord.RechargeMethod = consts.GetRechargeMethod(rechargeRecord.RechargeMethod)
  323. RechargeRecord.UserId = enterpriseIdToUserInfoMap[rechargeRecord.EnterpriseID].UserID
  324. RechargeRecord.BusinessName = enterpriseIdToUserInfoMap[rechargeRecord.EnterpriseID].BusinessName
  325. RechargeRecords = append(RechargeRecords, RechargeRecord)
  326. }
  327. var RechargeRecordsData http_model.RechargeRecordsData
  328. RechargeRecordsData.RechargeRecordsPreview = RechargeRecords
  329. RechargeRecordsData.Total = conv.MustString(total, "")
  330. return &RechargeRecordsData, nil
  331. }
  332. func OperateRecharge(ctx context.Context, req *http_model.OperateRechargeRequest) error {
  333. db := GetReadDB(ctx)
  334. db1 := GetReadDB(ctx)
  335. //rechargeInfo := gorm_model.YounggeeRechargeRecord{}
  336. //db = db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Find(&rechargeInfo)
  337. //enterpriseInfo := gorm_model.Enterprise{}
  338. //db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", req.EnterpriseId).Find(&enterpriseInfo)
  339. err := db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", req.EnterpriseId).Updates(map[string]interface{}{
  340. "balance": gorm.Expr("balance + ?", req.RechargeAmount),
  341. "available_balance": gorm.Expr("available_balance + ?", req.RechargeAmount)}).Error
  342. if err != nil {
  343. logrus.WithContext(ctx).Errorf("[OperateRecharge] error Updates balance, err:%+v", err)
  344. return err
  345. }
  346. err1 := db.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Updates(gorm_model.YounggeeRechargeRecord{
  347. Status: 2,
  348. ConfirmAt: time.Now(),
  349. }).Error
  350. if err1 != nil {
  351. logrus.WithContext(ctx).Errorf("[OperateRecharge] error Updates Status, err:%+v", err)
  352. return err1
  353. }
  354. if req.Method == 1 {
  355. db2 := GetReadDB(ctx)
  356. db2.Model(gorm_model.YounggeeRechargeRecord{}).Where("recharge_id = ?", req.RechargeId).Updates(gorm_model.YounggeeRechargeRecord{
  357. RechargeAmount: req.RechargeAmount,
  358. })
  359. }
  360. return nil
  361. }