|
- package hdl
-
- import (
- "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"
- es2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
- "context"
- "encoding/json"
- "github.com/gin-gonic/gin"
- "github.com/olivere/elastic/v7"
- "github.com/shopspring/decimal"
- "time"
- )
-
- // MemberCenterGetBasic
- // @Summary 蛋蛋星球-会员中心-基础数据(获取)
- // @Tags 会员中心
- // @Description 基础数据(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {object} md.MemberCenterGetBasicResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/memberCenter/getBasic [GET]
- func MemberCenterGetBasic(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. 获取会员等级名称
- userLevelDb := implement.NewUserLevelDb(db.Db)
- level, err := userLevelDb.UserLevelByID(user.Level)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
-
- // 2. 获取基础设置
- settingDb := implement.NewEggEnergyBasicSettingDb(db.Db)
- setting, err := settingDb.EggEnergyBasicSettingGetOne()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
-
- // 3. 查询余额
- virtualAmountDb := implement.NewUserVirtualAmountDb(db.Db)
- // 3.1 活跃积分(蛋蛋积分 = 团队 + 个人)
- eggPersonalPoint, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.PersonEggPointsCoinId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
- eggTeamPoint, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.TeamEggPointsCoinId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
- eggPoint := utils.StrToFloat64(eggPersonalPoint.Amount) + utils.StrToFloat64(eggTeamPoint.Amount)
- // 3.2 蛋蛋能量 (个人 + 团队)
- eggPersonalEnergy, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.PersonEggEnergyCoinId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
- eggTeamEnergy, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.TeamEggEnergyCoinId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
- eggEnergy := utils.StrToFloat64(eggPersonalEnergy.Amount) + utils.StrToFloat64(eggTeamEnergy.Amount)
- // 3.3 钱包余额
- walletDb := implement.NewUserWalletDb(db.Db)
- wallet, err := walletDb.GetUserVirtualWallet(user.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
- // 3.4 贡献值
- contributionValue, err := virtualAmountDb.GetUserVirtualWalletBySession(user.Id, setting.ContributionCoinId)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
-
- coreDataDb := implement.NewEggEnergyCoreDataDb(db.Db)
- coreData, err := coreDataDb.EggEnergyCoreDataGet()
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
-
- // 4. 预估蛋蛋能量价值
- nowPrice, err := decimal.NewFromString(coreData.NowPrice)
- if err != nil {
- e.OutErr(c, e.ERR_UNMARSHAL, nil)
- return
- }
- eggEnergyValue := nowPrice.Mul(decimal.NewFromFloat(eggEnergy))
-
- // 5. 查询蛋蛋分
- now := time.Now()
- esIndexName := es2.GetLatestEffectiveIndexFromAlias(now)
- // 构建查询条件
- boolQuery := elastic.NewBoolQuery()
- boolQuery.Filter(elastic.NewTermQuery("uid", user.Id))
- searchResult, err := es.EsClient.Search().
- Index(esIndexName).
- Query(boolQuery).
- Pretty(true).
- Do(context.Background())
-
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- var results []md.UserEggFlowReqRespList
- // 检查是否有结果
- if searchResult.Hits.TotalHits.Value != 0 {
- // 解析结果
- for _, hit := range searchResult.Hits.Hits {
- var doc md.UserEggFlowReqRespList
- err = json.Unmarshal(hit.Source, &doc)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
- results = append(results, doc)
- }
- }
- score := results[0].ScoreValue
-
- resp := md.MemberCenterGetBasicResp{
- Nickname: user.Nickname,
- LevelName: level.LevelName,
- ActiveNum: utils.Float64ToStr(eggPoint),
- EggScore: utils.Float64ToStr(score),
- EggEnergy: utils.Float64ToStr(eggEnergy),
- EggEnergyValue: eggEnergyValue.String(),
- ContributionValue: contributionValue.Amount,
- Date: "29",
- Ratio: "1:1000",
- Amount: wallet.Amount,
- }
- e.OutSuc(c, resp, nil)
- return
- }
|