enterprise.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package db
  2. import (
  3. "context"
  4. "github.com/issue9/conv"
  5. "log"
  6. "time"
  7. "youngee_b_api/model/gorm_model"
  8. "youngee_b_api/model/http_model"
  9. "youngee_b_api/util"
  10. "gorm.io/gorm"
  11. )
  12. func CreateEnterprise(ctx context.Context, newEnterprise gorm_model.Enterprise) (*int64, error) {
  13. db := GetReadDB(ctx)
  14. err := db.Create(&newEnterprise).Error
  15. if err != nil {
  16. return nil, err
  17. }
  18. return &newEnterprise.EnterpriseID, nil
  19. }
  20. //用户ID查找
  21. func GetEnterpriseByUID(ctx context.Context, userID int64) (*gorm_model.Enterprise, error) {
  22. db := GetReadDB(ctx)
  23. enterprise := gorm_model.Enterprise{}
  24. err := db.Where("user_id = ?", userID).First(&enterprise).Error
  25. if err != nil {
  26. if err == gorm.ErrRecordNotFound {
  27. return nil, nil
  28. } else {
  29. return nil, err
  30. }
  31. }
  32. return &enterprise, nil
  33. }
  34. //企业ID查找
  35. func GetEnterpriseByEnterpriseID(ctx context.Context, EnterpriseID int64) (*gorm_model.Enterprise, error) {
  36. db := GetReadDB(ctx)
  37. enterprise := gorm_model.Enterprise{}
  38. err := db.Where("enterprise_id = ?", EnterpriseID).First(&enterprise).Error
  39. if err != nil {
  40. if err == gorm.ErrRecordNotFound {
  41. return nil, nil
  42. } else {
  43. return nil, err
  44. }
  45. }
  46. return &enterprise, nil
  47. }
  48. // GetEnterpriseBalance 获取企业可用余额等信息
  49. func GetEnterpriseBalance(ctx context.Context, EnterpriseID int64) (*http_model.EnterpriseBalanceData, error) {
  50. db := GetReadDB(ctx)
  51. enterprise := gorm_model.Enterprise{}
  52. err := db.Where("enterprise_id = ?", EnterpriseID).First(&enterprise).Error
  53. if err != nil {
  54. if err == gorm.ErrRecordNotFound {
  55. return nil, nil
  56. } else {
  57. return nil, err
  58. }
  59. }
  60. var Invoicings []float64
  61. var Invoicing float64
  62. var BillableAmounts []float64
  63. var BillableAmount float64
  64. db1 := GetReadDB(ctx)
  65. db1.Model(gorm_model.YounggeeInvoiceRecord{}).Select("invoice_amount").
  66. Where("enterprise_id = ? AND status = 1", EnterpriseID).Find(&Invoicings)
  67. for _, v := range Invoicings {
  68. Invoicing += v
  69. }
  70. db2 := GetReadDB(ctx)
  71. db2.Model(gorm_model.YounggeeRechargeRecord{}).Select("recharge_amount").
  72. Where("enterprise_id = ? AND status != 3", EnterpriseID).Find(&BillableAmounts)
  73. for _, v := range BillableAmounts {
  74. BillableAmount += v
  75. }
  76. res := &http_model.EnterpriseBalanceData{
  77. Balance: enterprise.Balance,
  78. FrozenBalance: enterprise.FrozenBalance,
  79. AvailableBalance: enterprise.AvailableBalance,
  80. Recharging: enterprise.Recharging,
  81. BillableAmount: BillableAmount,
  82. Invoicing: Invoicing,
  83. }
  84. return res, nil
  85. }
  86. // 支付-修改企业账户余额
  87. func UpdateEnterpriseBalance(ctx context.Context, EnterpriseID int64, balance float64, availableBalance float64, frozenBalance float64) (*float64, error) {
  88. db := GetReadDB(ctx)
  89. err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).
  90. Updates(map[string]interface{}{"balance": gorm.Expr("balance + ?", balance), "available_balance": gorm.Expr("available_balance + ?", availableBalance), "frozen_balance": gorm.Expr("frozen_balance + ?", frozenBalance)}).Error
  91. if err != nil {
  92. return nil, err
  93. }
  94. enterprise := gorm_model.Enterprise{}
  95. err = db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).Scan(&enterprise).Error
  96. if err != nil {
  97. return nil, err
  98. }
  99. return &enterprise.Balance, nil
  100. }
  101. func MakeRechargeId(ctx context.Context, EnterpriseID int64) string {
  102. db := GetReadDB(ctx)
  103. // 1、年月日
  104. year := time.Now().Year()
  105. month := time.Now().Month()
  106. day := time.Now().Day()
  107. yearData, _ := util.GetDayNum("year", year)
  108. monthData, _ := util.GetDayNum("month", int(month))
  109. dayData, _ := util.GetDayNum("day", day)
  110. sum := 0
  111. sum += dayData + monthData
  112. leap := 0
  113. if (yearData%400 == 0) || ((yearData%4 == 0) && (yearData%100 != 0)) {
  114. leap = 1
  115. }
  116. if (leap == 1) && (monthData > 2) {
  117. sum += 1
  118. }
  119. last := ""
  120. rechargeIdPrefix := "8" + conv.MustString(EnterpriseID)[len(conv.MustString(EnterpriseID))-2:] + conv.MustString(sum)
  121. var rechargeIdLast string
  122. err := db.Model(gorm_model.YounggeeRechargeRecord{}).Select("recharge_id").Where("recharge_id like ?", rechargeIdPrefix+"%").
  123. Last(&rechargeIdLast).Error
  124. if err != nil {
  125. last = "0"
  126. } else {
  127. last = rechargeIdLast[len(rechargeIdLast)-2:]
  128. }
  129. var lastInt int
  130. lastInt = conv.MustInt(last)
  131. if lastInt+1 < 10 {
  132. last = "0" + conv.MustString(conv.MustInt(last)+1)
  133. } else {
  134. last = conv.MustString(conv.MustInt(last) + 1)
  135. }
  136. return rechargeIdPrefix + last
  137. }
  138. func RechargeAmount(ctx context.Context, EnterpriseID int64, Amount float64) error {
  139. db := GetReadDB(ctx)
  140. err := db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).
  141. Updates(map[string]interface{}{"balance": gorm.Expr("balance + ?", Amount), "available_balance": gorm.Expr("available_balance + ?", Amount)}).Error
  142. if err != nil {
  143. return err
  144. }
  145. //enterprise := gorm_model.Enterprise{}
  146. //err = db.Model(gorm_model.Enterprise{}).Where("enterprise_id", EnterpriseID).Scan(&enterprise).Error
  147. //if err != nil {
  148. // return err
  149. //}
  150. rechargeId := MakeRechargeId(ctx, EnterpriseID)
  151. err = db.Model(gorm_model.YounggeeRechargeRecord{}).Create(&gorm_model.YounggeeRechargeRecord{
  152. RechargeID: rechargeId,
  153. RechargeAmount: Amount,
  154. EnterpriseID: EnterpriseID,
  155. Status: 2,
  156. InvoiceStatus: 1,
  157. CommitAt: time.Now(),
  158. RechargeMethod: 3,
  159. TransferVoucherUrl: "--",
  160. ConfirmAt: time.Now(),
  161. }).Error
  162. if err != nil {
  163. return err
  164. }
  165. return nil
  166. }
  167. func TransferToPublic(ctx context.Context, Amount float64, phone string, EnterpriseID int64, transferVoucherUrl string) error {
  168. db := GetReadDB(ctx)
  169. rechargeId := MakeRechargeId(ctx, EnterpriseID)
  170. err := db.Model(gorm_model.YounggeeRechargeRecord{}).Create(&gorm_model.YounggeeRechargeRecord{
  171. RechargeID: rechargeId,
  172. RechargeAmount: Amount,
  173. EnterpriseID: EnterpriseID,
  174. Status: 1,
  175. InvoiceStatus: 1,
  176. CommitAt: time.Now(),
  177. Phone: phone,
  178. RechargeMethod: 1,
  179. TransferVoucherUrl: transferVoucherUrl,
  180. ConfirmAt: time.Now(),
  181. }).Error
  182. if err != nil {
  183. return err
  184. }
  185. db1 := GetReadDB(ctx)
  186. err = db1.Model(gorm_model.Enterprise{}).Where("enterprise_id = ?", EnterpriseID).Updates(
  187. map[string]interface{}{"recharging": gorm.Expr("recharging + ?", Amount)}).Error
  188. if err != nil {
  189. log.Println("[TransferToPublic] recharging modify failed:", err)
  190. return err
  191. }
  192. return nil
  193. }