|
- package hdl
-
- import (
- "applet/app/cfg"
- "applet/app/db"
- "applet/app/e"
- "applet/app/md"
- "applet/app/utils"
- "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"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
- md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
- md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/svc"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/shopspring/decimal"
- "time"
- )
-
- // PointsCenterGetBasic
- // @Summary 蛋蛋星球-积分中心-基础信息(获取)
- // @Tags 积分中心
- // @Description 基础信息(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {object} md.PointsCenterGetBasicResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/pointsCenter/getBasic [GET]
- func PointsCenterGetBasic(c *gin.Context) {
- val, exists := c.Get("user")
- if !exists {
- e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
- return
- }
- user, ok := val.(*model.User)
- if !ok {
- e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
- return
- }
-
- // 1. 获取蛋蛋能量货币类型
- settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
- setting, err := settingDb.EggEnergyBasicSettingGetOne()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- coinID := setting.PersonEggEnergyCoinId
-
- // 2. 获取可兑蛋蛋能量
- virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
- eggEnergyAmount, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, coinID)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- // 3. 可用现金
- walletDb := implement.NewUserWalletDb(db.Db)
- wallet, err := walletDb.GetUserVirtualWallet(user.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- resp := md.PointsCenterGetBasicResp{
- AvailableEnergy: eggEnergyAmount.Amount,
- AvailableCash: wallet.Amount,
- }
-
- e.OutSuc(c, resp, nil)
- }
-
- // GetPriceCurve
- // @Summary 蛋蛋星球-积分中心-价格趋势(获取)
- // @Tags 积分中心
- // @Description 价格趋势(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param kind query string false "1:按天 2:按小时 3:按周"
- // @Success 200 {object} md.GetPriceCurveResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/pointsCenter/priceCurve [GET]
- func GetPriceCurve(c *gin.Context) {
- kind := c.DefaultQuery("kind", "1")
- now := time.Now()
-
- priceDb := implement.NewEggEnergyPriceDb(db.Db)
- m, has, err := priceDb.EggEnergyPriceGetLastOne()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if has == false {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到数据")
- return
- }
-
- var yData []interface{}
- var xData []interface{}
- switch kind {
- case "1":
- var date = now.AddDate(0, 0, -30).Format("2006-01-02")
- var sql = fmt.Sprintf("SELECT price,date FROM `egg_energy_price` WHERE HOUR = 23 AND DATE >= \"%s\" AND DATE != \"%s\" ORDER BY DATE ASC ", date, now.Format("2006-01-02"))
- results, _ := db.Db.QueryString(sql)
- for _, v := range results {
- tmpDate := utils.TimeParseStd(v["date"])
- yData = append(yData, v["price"])
- xData = append(xData, tmpDate.Format("01-02"))
- }
- yData = append(yData, m.Price)
- tmpDate := utils.TimeParseStd(m.Date)
- xData = append(xData, tmpDate.Format("01-02"))
- break
- case "2":
- for i := 29; i >= 1; i-- {
- date := now.Add(-time.Hour * 4 * time.Duration(i)).Format("2006-01-02")
- hour := now.Add(-time.Hour * 4 * time.Duration(i)).Hour()
- var sql = "SELECT price,date,hour FROM `egg_energy_price` WHERE HOUR = %d AND DATE = \"%s\" "
- sql = fmt.Sprintf(sql, hour, date)
- results, _ := db.Db.QueryString(sql)
- if results != nil {
- //if results[0]["date"] != now.Format("2006-01-02") {
- // continue
- //}
- yData = append(yData, results[0]["price"])
- xData = append(xData, results[0]["hour"]+":00")
- }
- }
- yData = append(yData, m.Price)
- xData = append(xData, m.Hour+":00")
- break
- case "3":
- var nums = 29
- for i := nums; i >= 1; i-- {
- var date = now.AddDate(0, 0, -7*i).Format("2006-01-02")
- var sql = "SELECT price, date FROM `egg_energy_price` WHERE HOUR = 23 AND DATE = \"%s\" "
- sql = fmt.Sprintf(sql, date)
- results, _ := db.Db.QueryString(sql)
- if results != nil {
- tmpDate := utils.TimeParseStd(results[0]["date"])
- yData = append(yData, results[0]["price"])
- xData = append(xData, tmpDate.Format("01-02"))
- }
- }
- yData = append(yData, m.Price)
- tmpDate := utils.TimeParseStd(m.Date)
- xData = append(xData, tmpDate.Format("01-02"))
- break
- }
-
- e.OutSuc(c, md.GetPriceCurveResp{
- YData: yData,
- XData: xData,
- }, nil)
- return
- }
-
- // ExchangeEnergy
- // @Summary 蛋蛋星球-积分中心-能量兑换
- // @Tags 积分中心
- // @Description 能量兑换
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.ExchangeEnergyReq true "需要兑换的能量值"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/pointsCenter/exchangeEnergy [POST]
- func ExchangeEnergy(c *gin.Context) {
- val, exists := c.Get("user")
- if !exists {
- e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
- return
- }
- user, ok := val.(*model.User)
- if !ok {
- e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
- return
- }
-
- var req *md.ExchangeEnergyReq
- if err1 := c.ShouldBindJSON(&req); err1 != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
- return
- }
-
- // 1. 获取蛋蛋能量货币类型
- settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
- setting, err := settingDb.EggEnergyBasicSettingGetOne()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- coinID := setting.PersonEggEnergyCoinId
-
- session := db.Db.NewSession()
- defer session.Close()
-
- // 2. 计算蛋蛋能量兑换的金额
- egg_system_rules.Init(cfg.RedisAddr)
- eggEnergyCoreData, cb, err1 := svc.GetEggEnergyCoreData(db.Db)
- if err1 != nil {
- fmt.Println("EggEnergyAutoRecordPrices_ERR:::::", err1.Error())
- return
- }
- if cb != nil {
- defer cb() // 释放锁
- }
- energyAmount, err := decimal.NewFromString(req.EnergyAmount)
- if err != nil {
- e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
- return
- }
- nowPrice, err := decimal.NewFromString(eggEnergyCoreData.NowPrice)
- if err != nil {
- e.OutErr(c, e.ERR_UNMARSHAL, err.Error())
- return
- }
- amount, ok := energyAmount.Div(nowPrice).Float64()
- if !ok {
- e.OutErr(c, e.ERR_UNMARSHAL, nil)
- return
- }
-
- // 3. 获取用户蛋蛋能量余额
- eggEnergyAmount, err := rule.GetUserCoinAmount(session, coinID, user.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- // 4. 判断蛋蛋能量是否足够兑换
- if utils.StrToFloat64(eggEnergyAmount) < utils.StrToFloat64(req.EnergyAmount) {
- e.OutErr(c, e.ERR_BALANCE_NOT_ENOUGH, nil)
- }
-
- // 5. 调用降价公式
- err, calcPriceReductionFormula := egg_energy.CalcPriceReductionFormula(req.EnergyAmount, eggEnergyCoreData)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- // 6. 更改动态数据
- err = egg_energy.DealAvailableEggEnergyCoin(session, int(enum.EggEnergyExchangeAccountBalance), eggEnergyCoreData, md3.DealAvailableEggEnergyCoinReq{
- Amount: calcPriceReductionFormula.GetEggEnergyAmount,
- AmountFee: "",
- BeforePrice: calcPriceReductionFormula.BeforePrice,
- AfterPrice: calcPriceReductionFormula.AfterPrice,
- BeforePlanetTotalValue: calcPriceReductionFormula.BeforePlanetTotalValue,
- AfterPlanetTotalValue: calcPriceReductionFormula.AfterPlanetTotalValue,
- BeforeEnergyTotalNums: calcPriceReductionFormula.BeforeEnergyTotalNums,
- AfterEnergyTotalNums: calcPriceReductionFormula.AfterEnergyTotalNums,
- })
- if err != nil {
- fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err)
- _ = session.Rollback()
- return
- }
-
- // 7. 扣除蛋蛋能量
- dealUserVirtualCoinReq := md2.DealUserVirtualCoinReq{
- Kind: "sub",
- Title: enum.EggEnergyToExchangeToAmount.String(),
- TransferType: int(enum.EggEnergyToExchangeToAmount),
- CoinId: coinID,
- Uid: user.Id,
- Amount: utils.StrToFloat64(req.EnergyAmount),
- }
- err = rule.DealUserVirtualCoin(session, dealUserVirtualCoinReq)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- session.Rollback()
- return
- }
-
- // 8. 增加账户余额
- dealUserWalletReq := md2.DealUserWalletReq{
- Direction: "add",
- Kind: int(enum.EggEnergyExchangeAccountBalance),
- Title: enum.EggEnergyExchangeAccountBalance.String(),
- Uid: user.Id,
- Amount: amount,
- }
- err = rule.DealUserWallet(session, dealUserWalletReq)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- session.Rollback()
- return
- }
-
- err = session.Commit()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- }
-
- // GetContributionValue
- // @Summary 蛋蛋星球-积分中心-贡献值(获取)
- // @Tags 积分中心
- // @Description 贡献值(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {object} md.GetContributionValueResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/pointsCenter/contributionValue [GET]
- func GetContributionValue(c *gin.Context) {
- val, exists := c.Get("user")
- if !exists {
- e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
- return
- }
- user, ok := val.(*model.User)
- if !ok {
- e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
- return
- }
-
- settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
- setting, err := settingDb.EggEnergyBasicSettingGetOne()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- coinID := setting.ContributionCoinId
-
- virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
- virtualAmount, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, coinID)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- var contributionValue string
- if virtualAmount != nil {
- contributionValue = virtualAmount.Amount
- }
-
- coinDb := implement.NewVirtualCoinDb(db.Db)
- coin, err := coinDb.VirtualCoinGetOneByParams(map[string]interface{}{
- "key": "id",
- "value": coinID,
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- var ratio string
- if coin != nil {
- ratio = coin.ExchangeRatio
- }
-
- resp := md.GetContributionValueResp{
- ContributionValue: contributionValue,
- Ratio: ratio,
- }
- e.OutSuc(c, resp, nil)
- }
-
- // GetContributionValueFlow
- // @Summary 蛋蛋星球-积分中心-贡献值明细(获取)
- // @Tags 积分中心
- // @Description 贡献值明细(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param limit query int true "每页大小"
- // @Param page query int true "页数"
- // @Success 200 {object} md.GetContributionValueFlowResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/pointsCenter/contributionValueFlow [GET]
- func GetContributionValueFlow(c *gin.Context) {
- page := c.DefaultQuery("page", "1")
- limit := c.DefaultQuery("limit", "10")
- val, exists := c.Get("user")
- if !exists {
- e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
- return
- }
- user, ok := val.(*model.User)
- if !ok {
- e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
- return
- }
-
- settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
- setting, err := settingDb.EggEnergyBasicSettingGetOne()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- coinID := setting.ContributionCoinId
-
- flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
- flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(utils.StrToInt(page), utils.StrToInt(limit), coinID, user.Id, "", "", 0, false, 0)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- list := make([]md.ContributionValueFlowNode, len(flows))
- for i, flow := range flows {
- list[i].Title = flow.Title
- list[i].Amount = flow.Amount
- list[i].Direction = utils.IntToStr(flow.Direction)
- list[i].CreateAt = flow.CreateAt
- }
-
- resp := md.GetContributionValueFlowResp{
- List: list,
- Paginate: md.Paginate{
- Limit: utils.StrToInt(limit),
- Page: utils.StrToInt(page),
- Total: total,
- },
- }
-
- e.OutSuc(c, resp, nil)
- }
-
- func GetEggPointRecord(c *gin.Context) {
-
- }
|