|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package egg_energy
-
- import (
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
- enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/enum"
- md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
- egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
- zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
- "errors"
- "fmt"
- "github.com/shopspring/decimal"
- "time"
- "xorm.io/xorm"
- )
-
- // AddCommunityDividends 新增社区分红
- func AddCommunityDividends(engine *xorm.Engine, amount, name string, basic model.EggEnergyBasicSetting) (err error) {
- //1、获取核心数据
- session := engine.NewSession()
- defer func() {
- session.Close()
- if err := recover(); err != nil {
- _ = zhios_order_relate_logx.Error(err)
- }
- }()
- session.Begin()
-
- eggEnergyCoreData, cb, err := svc.GetEggEnergyCoreData(engine)
- if err != nil {
- return err
- }
- if cb != nil {
- defer cb() // 释放锁
- }
-
- //2、计算涨价公式
- err1, calcPriceIncreaseFormulaResp := CalcPriceIncreaseFormula(amount, eggEnergyCoreData)
- if err1 != nil {
- _ = session.Rollback()
- return err1
- }
-
- //3、更改动态数据
- err = DealAvailableEggEnergyCoin(session, int(enum2.CommunityDividends), nil, md2.DealAvailableEggEnergyCoinReq{
- Amount: calcPriceIncreaseFormulaResp.GetEggEnergyNums,
- AmountFee: "",
- BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice,
- AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice,
- BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue,
- AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue,
- BeforeEnergyTotalNums: calcPriceIncreaseFormulaResp.BeforeEnergyTotalNums,
- AfterEnergyTotalNums: calcPriceIncreaseFormulaResp.AfterEnergyTotalNums,
- })
- if err != nil {
- fmt.Println("AddCommunityDividends:::::err111:::", err)
- _ = session.Rollback()
- return err
- }
-
- //4、新增 egg_energy_community_dividends 记录
- now := time.Now()
- eggEnergyCommunityDividendsDb := implement.NewEggEnergyCommunityDividendsDb(engine)
- _, err = eggEnergyCommunityDividendsDb.EggEnergyCommunityDividendsInsertBySession(session, &model.EggEnergyCommunityDividends{
- Nums: calcPriceIncreaseFormulaResp.GetEggEnergyNums,
- Amount: amount,
- Name: name,
- CoinId: basic.ContributionCoinId,
- PersonsNum: 0,
- IsOver: 0,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- return err
- }
- if err != nil {
- fmt.Println("AddCommunityDividends:::::err222:::", err)
- _ = session.Rollback()
- return err
- }
- return session.Commit()
- }
-
- // StartCommunityDividends 开始社区分红
- func StartCommunityDividends(engine *xorm.Engine) (err error) {
- //1、查找当前是否有待分红的记录
- var m model.EggEnergyCommunityDividends
- has, err := engine.Where("is_over = 0").Get(&m)
- if err != nil {
- fmt.Println("err::::::1111", err.Error())
- return
- }
- if has {
- //3、查找 `egg_energy_community_dividends_with_user` 数据
- var list []model.EggEnergyCommunityDividendsWithUser
- err = engine.Where("1 = 1").Find(&list)
- totalValue, _ := decimal.NewFromString(m.Nums)
- singleValue, _ := totalValue.Div(decimal.NewFromInt(int64(len(list)))).Float64()
- session := engine.NewSession()
- defer func() {
- session.Close()
- if err := recover(); err != nil {
-
- _ = zhios_order_relate_logx.Error(err)
- }
- }()
- session.Begin()
- now := time.Now()
-
- for _, v := range list {
- //4、 给相应的用户加上分红权益
- err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
- Kind: "add",
- Title: enum.EggEnergyCommunityDividends.String(),
- TransferType: int(enum.EggEnergyCommunityDividends),
- CoinId: m.CoinId,
- Uid: v.Uid,
- Amount: singleValue,
- })
- if err != nil {
- _ = session.Rollback()
- fmt.Println("err:::::33333", err)
- return err
- }
-
- //5、增加 egg_energy_community_dividends_with_user_records 记录
- _, err = session.InsertOne(&model.EggEnergyCommunityDividendsWithUserRecords{
- Uid: v.Uid,
- Value: egg_system_rules.Float64ToStr(singleValue),
- RecordsId: m.Id,
- CoinId: m.CoinId,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- _ = session.Rollback()
- fmt.Println("err:::::insert", err)
- return err
- }
- }
-
- //6、修改 egg_energy_community_dividends 记录
- m.IsOver = 1
- m.PersonsNum = len(list)
- affected, err := session.Where("id=?", m.Id).Cols("is_over", "persons_num").Update(m)
- if err != nil {
- _ = session.Rollback()
- fmt.Println("err:::::5555555", err)
- return err
- }
- if affected == 0 {
- _ = session.Rollback()
- fmt.Println("err:::::6666666", errors.New("更新 egg_energy_community_dividends 记录失败"))
- return err
- }
-
- err = session.Commit()
- if err != nil {
- _ = session.Rollback()
- return errors.New("事务提交失败")
- }
- }
- return
- }
|