finance.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/caixw/lib.go/conv"
  6. "github.com/sirupsen/logrus"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "time"
  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. )
  16. func GetWithdrawRecords(ctx context.Context, pageSize, pageNum int32, req *http_model.WithdrawalRecordsRequest, condition *common_model.WithdrawRecordsCondition) (*http_model.WithdrawalRecordsPreview, error) {
  17. db := GetReadDB(ctx)
  18. var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
  19. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{})
  20. conditionType := reflect.TypeOf(condition).Elem()
  21. conditionValue := reflect.ValueOf(condition).Elem()
  22. for i := 0; i < conditionType.NumField(); i++ {
  23. field := conditionType.Field(i)
  24. tag := field.Tag.Get("condition")
  25. value := conditionValue.FieldByName(field.Name)
  26. if tag == "submit_at" && value.Interface() != "" {
  27. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  28. }
  29. if tag == "withdraw_at" && value.Interface() != "" {
  30. db = db.Where(fmt.Sprintf("withdraw_at like '%s%%'", value.Interface()))
  31. }
  32. if !util.IsBlank(value) && tag != "submit_at" && tag != "withdraw_at" {
  33. db = db.Where(fmt.Sprintf("%s = ?", tag), value.Interface())
  34. }
  35. }
  36. if req.TalentName != "" {
  37. fmt.Println("TalentName:", req.TalentName)
  38. db1 := GetReadDB(ctx)
  39. var talentId string
  40. db1.Model(gorm_model.YoungeeTalentInfo{}).Select("id").Where("talent_wx_nickname = ? ", req.TalentName).Find(&talentId)
  41. db = db.Where("talent_id = ?", talentId)
  42. }
  43. // 查询总数
  44. var total int64
  45. if err := db.Count(&total).Error; err != nil {
  46. logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql total, err:%+v", err)
  47. return nil, err
  48. }
  49. // 查询该页数据
  50. limit := pageSize
  51. offset := pageSize * pageNum // assert pageNum start with 0
  52. err := db.Order("submit_at").Limit(int(limit)).Offset(int(offset)).Find(&withdrawRecords).Error
  53. if err != nil {
  54. logrus.WithContext(ctx).Errorf("[GetWithdrawRecords] error query mysql limit, err:%+v", err)
  55. return nil, err
  56. }
  57. var talentIds []string
  58. for _, withdrawRecord := range withdrawRecords {
  59. talentIds = append(talentIds, withdrawRecord.TalentID)
  60. }
  61. talentIdToTalentInfoMap := make(map[string]gorm_model.YoungeeTalentInfo)
  62. for _, talentId := range talentIds {
  63. db1 := GetReadDB(ctx)
  64. talentInfo := gorm_model.YoungeeTalentInfo{}
  65. db1.Model(gorm_model.YoungeeTalentInfo{}).Where("id = ?", talentId).Find(&talentInfo)
  66. talentIdToTalentInfoMap[talentId] = talentInfo
  67. }
  68. var withdrawRecordsDatas []*http_model.WithdrawalRecordsData
  69. for _, withdrawRecord := range withdrawRecords {
  70. withdrawRecordsData := new(http_model.WithdrawalRecordsData)
  71. withdrawRecordsData.WithdrawId = withdrawRecord.WithdrawID
  72. withdrawRecordsData.TalentId = withdrawRecord.TalentID
  73. withdrawRecordsData.TalentName = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentWxNickname
  74. withdrawRecordsData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
  75. withdrawRecordsData.AmountPayable = float32(withdrawRecord.AmountPayable)
  76. withdrawRecordsData.ReceiveInfo = withdrawRecord.ReceiveInfo
  77. withdrawRecordsData.BankType = withdrawRecord.BankType
  78. withdrawRecordsData.Phone = talentIdToTalentInfoMap[withdrawRecord.TalentID].TalentPhoneNumber
  79. withdrawRecordsData.SubmitAt = conv.MustString(withdrawRecord.SubmitAt, "")[0:19]
  80. withdrawRecordsData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
  81. withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordsData)
  82. }
  83. var withdrawRecordsPreview http_model.WithdrawalRecordsPreview
  84. withdrawRecordsPreview.WithdrawalRecordsData = withdrawRecordsDatas
  85. withdrawRecordsPreview.Total = total
  86. return &withdrawRecordsPreview, nil
  87. }
  88. func GetWithdrawRecord(ctx context.Context, req *http_model.GetWithdrawalRecordRequest) (*http_model.WithdrawalRecordPreview, error) {
  89. db := GetReadDB(ctx)
  90. fmt.Println("talentId:", req.TalentId)
  91. var withdrawRecords []*gorm_model.YounggeeWithdrawRecord
  92. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Where("talent_id = ? AND status = ?", req.TalentId, 2).Find(&withdrawRecords)
  93. var withdrawRecordsPreview http_model.WithdrawalRecordPreview
  94. var withdrawRecordsDatas []*http_model.WithdrawalRecordData
  95. for _, withdrawRecord := range withdrawRecords {
  96. withdrawRecordData := new(http_model.WithdrawalRecordData)
  97. withdrawRecordData.WithdrawAmount = float32(withdrawRecord.WithdrawAmount)
  98. withdrawRecordData.WithdrawAt = conv.MustString(withdrawRecord.WithdrawAt, "")[0:19]
  99. withdrawRecordsDatas = append(withdrawRecordsDatas, withdrawRecordData)
  100. }
  101. withdrawRecordsPreview.WithdrawalRecordData = withdrawRecordsDatas
  102. return &withdrawRecordsPreview, nil
  103. }
  104. func GetWithdrawNums(ctx context.Context) (*http_model.WithdrawNums, error) {
  105. var withdrawNums int64
  106. db := GetReadDB(ctx)
  107. err := db.Model(gorm_model.YounggeeWithdrawRecord{}).Where("status = 1").Count(&withdrawNums).Error
  108. if err != nil {
  109. return nil, err
  110. }
  111. WithdrawNums := new(http_model.WithdrawNums)
  112. WithdrawNums.WithdrawNums = int(withdrawNums)
  113. return WithdrawNums, err
  114. }
  115. func ConfirmWithdrawal(ctx context.Context, withdrawId string) error {
  116. db := GetReadDB(ctx)
  117. db2 := GetReadDB(ctx)
  118. var task_id_list string
  119. db = db.Debug().Model(gorm_model.YounggeeWithdrawRecord{}).Select("task_id_list").Where("withdraw_id = ?", withdrawId).Find(&task_id_list)
  120. db2.Debug().Where("withdraw_id = ?", withdrawId).Updates(gorm_model.YounggeeWithdrawRecord{Status: 2, WithdrawAt: time.Now()})
  121. taskIdLists := strings.Split(task_id_list, ",")
  122. for _, taskIdList := range taskIdLists {
  123. db1 := GetReadDB(ctx)
  124. db1.Debug().Model(gorm_model.YoungeeTaskInfo{}).Where("task_id = ?", conv.MustInt(taskIdList, 0)).Updates(gorm_model.YoungeeTaskInfo{WithdrawStatus: 4})
  125. }
  126. return nil
  127. }
  128. func GetBankInfo(ctx context.Context, req *http_model.GetBankInfoRequest) (*http_model.BankInfo, error) {
  129. db := GetReadDB(ctx)
  130. var infoBank *gorm_model.InfoBank
  131. db.Debug().Model(gorm_model.InfoBank{}).Where("id = ?", req.BankId).First(&infoBank)
  132. db1 := GetReadDB(ctx)
  133. var infoRegion *gorm_model.InfoRegion
  134. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(req.BankOpenAddress, 0)).First(&infoRegion)
  135. provinceCode := conv.MustString(req.BankOpenAddress, "")[0:2] + "0000"
  136. var province *gorm_model.InfoRegion
  137. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(provinceCode, 0)).First(&province)
  138. cityCode := conv.MustString(req.BankOpenAddress, "")[0:4] + "00"
  139. var city *gorm_model.InfoRegion
  140. db1.Debug().Model(gorm_model.InfoRegion{}).Where("self_code = ?", conv.MustInt(cityCode, 0)).First(&city)
  141. data := new(http_model.BankInfo)
  142. data.BankName = infoBank.Name
  143. data.BankOpenAddress = province.RegionName + city.RegionName + infoRegion.RegionName
  144. db.Model(gorm_model.InfoBank{}).Where("")
  145. return data, nil
  146. }
  147. // GetEnterpriseIDByBusiness 根据企业名称查找企业ID
  148. func GetEnterpriseIDByBusiness(ctx context.Context, BusinessName string) int64 {
  149. db := GetReadDB(ctx)
  150. var EnterpriseID int64
  151. db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("business_name", BusinessName).First(&EnterpriseID)
  152. return EnterpriseID
  153. }
  154. // GetEnterpriseIDByUserId 根据企业名称查找企业ID
  155. func GetEnterpriseIDByUserId(ctx context.Context, UserId int64) int64 {
  156. db := GetReadDB(ctx)
  157. var EnterpriseID int64
  158. db = db.Model([]gorm_model.Enterprise{}).Select("enterprise_id").Where("user_id", UserId).First(&EnterpriseID)
  159. return EnterpriseID
  160. }
  161. func GetInvoiceRecords(ctx context.Context, req *http_model.InvoiceRecordsRequest, condition *common_model.InvoiceRecordsCondition) (*http_model.InvoiceRecordsData, error) {
  162. db := GetReadDB(ctx)
  163. var invoiceRecords []*gorm_model.YounggeeInvoiceRecord
  164. db = db.Debug().Model(gorm_model.YounggeeInvoiceRecord{}).Where("status = ?", req.InvoiceStatus)
  165. conditionType := reflect.TypeOf(condition).Elem()
  166. conditionValue := reflect.ValueOf(condition).Elem()
  167. for i := 0; i < conditionType.NumField(); i++ {
  168. field := conditionType.Field(i)
  169. tag := field.Tag.Get("condition")
  170. value := conditionValue.FieldByName(field.Name)
  171. if tag == "submit_at" && value.Interface() != "" {
  172. db = db.Where(fmt.Sprintf("submit_at like '%s%%'", value.Interface()))
  173. }
  174. if tag == "billing_at" && value.Interface() != "" {
  175. db = db.Where(fmt.Sprintf("billing_at like '%s%%'", value.Interface()))
  176. }
  177. }
  178. if req.BusinessName != "" {
  179. enterpriseId := GetEnterpriseIDByBusiness(ctx, req.BusinessName)
  180. db = db.Where("enterprise_id = ?", enterpriseId)
  181. }
  182. if req.UserId != 0 {
  183. enterpriseId := GetEnterpriseIDByUserId(ctx, req.UserId)
  184. db = db.Where("enterprise_id = ?", enterpriseId)
  185. }
  186. // 查询总数
  187. var total int64
  188. if err := db.Count(&total).Error; err != nil {
  189. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql total, err:%+v", err)
  190. return nil, err
  191. }
  192. if req.InvoiceStatus == 1 {
  193. db.Order("submit_at")
  194. } else {
  195. db.Order("submit_at desc")
  196. }
  197. // 查询该页数据
  198. limit := req.PageSize
  199. offset := req.PageSize * req.PageNum // assert pageNum start with 0
  200. err := db.Limit(int(limit)).Offset(int(offset)).Find(&invoiceRecords).Error
  201. if err != nil {
  202. logrus.WithContext(ctx).Errorf("[GetInvoiceRecords] error query mysql limit, err:%+v", err)
  203. return nil, err
  204. }
  205. var enterpriseIds []int64
  206. for _, invoiceRecord := range invoiceRecords {
  207. enterpriseIds = append(enterpriseIds, invoiceRecord.EnterpriseID)
  208. }
  209. util.RemoveRepByMap(enterpriseIds)
  210. enterpriseIdToUserInfoMap := make(map[int64]gorm_model.Enterprise)
  211. db1 := GetReadDB(ctx)
  212. for _, v := range enterpriseIds {
  213. enterpriseInfo := gorm_model.Enterprise{}
  214. db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", v).Find(&enterpriseInfo)
  215. enterpriseIdToUserInfoMap[v] = enterpriseInfo
  216. }
  217. var InvoiceRecords []*http_model.InvoiceRecordsPreviews
  218. for _, invoiceRecord := range invoiceRecords {
  219. InvoiceRecord := new(http_model.InvoiceRecordsPreviews)
  220. InvoiceRecord.BillingId = invoiceRecord.BillingID
  221. InvoiceRecord.InvoiceInfo = invoiceRecord.InvoiceSnap
  222. InvoiceRecord.AddressInfo = invoiceRecord.AddressSnap
  223. InvoiceRecord.InvoiceType = invoiceRecord.InvoiceType
  224. InvoiceRecord.Amount = invoiceRecord.InvoiceAmount
  225. InvoiceRecord.Phone = invoiceRecord.Phone
  226. InvoiceRecord.ShipmentNumber = invoiceRecord.ShipmentNumber
  227. InvoiceRecord.BusinessName = enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].BusinessName
  228. InvoiceRecord.UserId = enterpriseIdToUserInfoMap[invoiceRecord.EnterpriseID].UserID
  229. InvoiceRecord.SubmitAt = conv.MustString(invoiceRecord.SubmitAt, "")[:19]
  230. InvoiceRecord.BillingAt = conv.MustString(invoiceRecord.BillingAt, "")[:19]
  231. InvoiceRecords = append(InvoiceRecords, InvoiceRecord)
  232. }
  233. var InvoiceRecordsData http_model.InvoiceRecordsData
  234. InvoiceRecordsData.InvoiceRecordsPreviews = InvoiceRecords
  235. InvoiceRecordsData.Total = strconv.FormatInt(total, 10)
  236. return &InvoiceRecordsData, nil
  237. }
  238. func ConfirmInvoice(ctx context.Context, request *http_model.ConfirmInvoiceRequest) error {
  239. db := GetReadDB(ctx)
  240. return db.Model(gorm_model.YounggeeInvoiceRecord{}).Where("billing_id = ?", request.BillingId).Updates(
  241. gorm_model.YounggeeInvoiceRecord{
  242. BillingAt: time.Now(),
  243. ShipmentNumber: request.ShipmentNumber,
  244. Status: 2,
  245. }).Error
  246. }