蛋蛋星球-制度模式
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

community_dividends.go 5.2 KiB

před 2 týdny
před 2 týdny
před 2 týdny
před 2 týdny
před 2 týdny
před 2 týdny
před 2 týdny
před 2 týdny
před 2 týdny
před 2 týdny
před 2 týdny
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package egg_energy
  2. import (
  3. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  5. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  6. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  8. enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
  9. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
  11. egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
  12. zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
  13. "errors"
  14. "fmt"
  15. "github.com/shopspring/decimal"
  16. "time"
  17. "xorm.io/xorm"
  18. )
  19. // AddCommunityDividends 新增社区分红
  20. func AddCommunityDividends(engine *xorm.Engine, amount, name string, basic model.EggEnergyBasicSetting) (err error) {
  21. //1、获取核心数据
  22. session := engine.NewSession()
  23. defer func() {
  24. session.Close()
  25. if err := recover(); err != nil {
  26. _ = zhios_order_relate_logx.Error(err)
  27. }
  28. }()
  29. session.Begin()
  30. eggEnergyCoreData, cb, err := svc.GetEggEnergyCoreData(engine)
  31. if err != nil {
  32. return err
  33. }
  34. if cb != nil {
  35. defer cb() // 释放锁
  36. }
  37. //2、计算涨价公式
  38. err1, calcPriceIncreaseFormulaResp := CalcPriceIncreaseFormula(amount, eggEnergyCoreData)
  39. if err1 != nil {
  40. _ = session.Rollback()
  41. return err1
  42. }
  43. //3、更改动态数据
  44. err = DealAvailableEggEnergyCoin(session, int(enum2.CommunityDividends), nil, md2.DealAvailableEggEnergyCoinReq{
  45. Amount: calcPriceIncreaseFormulaResp.GetEggEnergyNums,
  46. AmountFee: "",
  47. BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice,
  48. AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice,
  49. BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue,
  50. AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue,
  51. BeforeEnergyTotalNums: calcPriceIncreaseFormulaResp.BeforeEnergyTotalNums,
  52. AfterEnergyTotalNums: calcPriceIncreaseFormulaResp.AfterEnergyTotalNums,
  53. })
  54. if err != nil {
  55. fmt.Println("AddCommunityDividends:::::err111:::", err)
  56. _ = session.Rollback()
  57. return err
  58. }
  59. //4、新增 egg_energy_community_dividends 记录
  60. now := time.Now()
  61. eggEnergyCommunityDividendsDb := implement.NewEggEnergyCommunityDividendsDb(engine)
  62. _, err = eggEnergyCommunityDividendsDb.EggEnergyCommunityDividendsInsertBySession(session, &model.EggEnergyCommunityDividends{
  63. Nums: calcPriceIncreaseFormulaResp.GetEggEnergyNums,
  64. Amount: amount,
  65. Name: name,
  66. CoinId: basic.ContributionCoinId,
  67. PersonsNum: 0,
  68. IsOver: 0,
  69. CreateAt: now.Format("2006-01-02 15:04:05"),
  70. UpdateAt: now.Format("2006-01-02 15:04:05"),
  71. })
  72. if err != nil {
  73. return err
  74. }
  75. if err != nil {
  76. fmt.Println("AddCommunityDividends:::::err222:::", err)
  77. _ = session.Rollback()
  78. return err
  79. }
  80. return session.Commit()
  81. }
  82. // StartCommunityDividends 开始社区分红
  83. func StartCommunityDividends(engine *xorm.Engine) (err error) {
  84. //1、查找当前是否有待分红的记录
  85. var m model.EggEnergyCommunityDividends
  86. has, err := engine.Where("is_over = 0").Get(&m)
  87. if err != nil {
  88. fmt.Println("err::::::1111", err.Error())
  89. return
  90. }
  91. if has {
  92. //3、查找 `egg_energy_community_dividends_with_user` 数据
  93. var list []model.EggEnergyCommunityDividendsWithUser
  94. err = engine.Where("1 = 1").Find(&list)
  95. totalValue, _ := decimal.NewFromString(m.Nums)
  96. singleValue, _ := totalValue.Div(decimal.NewFromInt(int64(len(list)))).Float64()
  97. session := engine.NewSession()
  98. defer func() {
  99. session.Close()
  100. if err := recover(); err != nil {
  101. _ = zhios_order_relate_logx.Error(err)
  102. }
  103. }()
  104. session.Begin()
  105. now := time.Now()
  106. for _, v := range list {
  107. //4、 给相应的用户加上分红权益
  108. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  109. Kind: "add",
  110. Title: enum.EggEnergyCommunityDividends.String(),
  111. TransferType: int(enum.EggEnergyCommunityDividends),
  112. CoinId: m.CoinId,
  113. Uid: v.Uid,
  114. Amount: singleValue,
  115. })
  116. if err != nil {
  117. _ = session.Rollback()
  118. fmt.Println("err:::::33333", err)
  119. return err
  120. }
  121. //5、增加 egg_energy_community_dividends_with_user_records 记录
  122. _, err = session.InsertOne(&model.EggEnergyCommunityDividendsWithUserRecords{
  123. Uid: v.Uid,
  124. Value: egg_system_rules.Float64ToStr(singleValue),
  125. RecordsId: m.Id,
  126. CoinId: m.CoinId,
  127. CreateAt: now.Format("2006-01-02 15:04:05"),
  128. UpdateAt: now.Format("2006-01-02 15:04:05"),
  129. })
  130. if err != nil {
  131. _ = session.Rollback()
  132. fmt.Println("err:::::insert", err)
  133. return err
  134. }
  135. }
  136. //6、修改 egg_energy_community_dividends 记录
  137. m.IsOver = 1
  138. m.PersonsNum = len(list)
  139. affected, err := session.Where("id=?", m.Id).Cols("is_over", "persons_num").Update(m)
  140. if err != nil {
  141. _ = session.Rollback()
  142. fmt.Println("err:::::5555555", err)
  143. return err
  144. }
  145. if affected == 0 {
  146. _ = session.Rollback()
  147. fmt.Println("err:::::6666666", errors.New("更新 egg_energy_community_dividends 记录失败"))
  148. return err
  149. }
  150. err = session.Commit()
  151. if err != nil {
  152. _ = session.Rollback()
  153. return errors.New("事务提交失败")
  154. }
  155. }
  156. return
  157. }