|
- package hdl
-
- import (
- "applet/app/db"
- "applet/app/e"
- "applet/app/md"
- "applet/app/svc"
- "applet/app/utils"
- "applet/app/utils/cache"
- "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"
- md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
- "fmt"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- // GetAmountFlow
- // @Summary 蛋蛋星球-钱包-余额明细(获取)
- // @Tags 钱包
- // @Description 余额明细(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param limit query string true "每页大小"
- // @Param page query string true "页数"
- // @Param startAt query string false "开始时间"
- // @Param endAt query string false "结束时间"
- // @Param direction query string false "流水方向(1.收入 2.支出 0.全部)"
- // @Success 200 {object} md.GetAmountFlowResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/wallet/amountFlow [GET]
- func GetAmountFlow(c *gin.Context) {
- pageStr := c.DefaultQuery("page", "1")
- limitStr := c.DefaultQuery("limit", "10")
- startAt := c.Query("startAt")
- endAt := c.Query("endAt")
- directionStr := c.Query("direction")
-
- 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
- }
-
- direction := 0
- switch directionStr {
- case "1":
- direction = 1
- case "2":
- direction = 2
- }
-
- page := utils.StrToInt(pageStr)
- limit := utils.StrToInt(limitStr)
-
- flowDb := implement.NewUserWalletFlowDb(db.Db)
- flows, total, err := flowDb.UserWalletFlowFindByCoinAndUser(page, limit, user.Id, startAt, endAt, direction, false, 0, 0)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- list := make([]md.WalletFlowNode, 0, len(flows))
- for _, flow := range flows {
- temp := md.WalletFlowNode{
- Id: flow.Id,
- Uid: flow.Uid,
- Direction: flow.Direction,
- Amount: flow.Amount,
- BeforeAmount: flow.BeforeAmount,
- AfterAmount: flow.AfterAmount,
- SysFee: flow.SysFee,
- OrdId: flow.OrdId,
- Title: flow.Title,
- Kind: flow.Kind,
- State: flow.State,
- Memo: flow.Memo,
- CreateTime: flow.CreateAt,
- UpdateTime: flow.UpdateAt,
- }
- list = append(list, temp)
- }
-
- resp := md.GetAmountFlowResp{
- List: list,
- Paginate: md.Paginate{
- Limit: limit,
- Page: page,
- Total: total,
- },
- }
- e.OutSuc(c, resp, nil)
- }
-
- // WithdrawGetAmount
- // @Summary 蛋蛋星球-钱包-提现余额(获取)
- // @Tags 钱包
- // @Description 提现余额(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Success 200 {object} md.WithdrawGetAmountResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/wallet/withdraw/index [GET]
- func WithdrawGetAmount(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
- }
-
- 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.WithdrawGetAmountResp{
- Amount: wallet.Amount,
- }
- e.OutSuc(c, resp, nil)
- }
-
- // WithdrawApply
- // @Summary 蛋蛋星球-钱包-发起提现
- // @Tags 钱包
- // @Description 发起提现
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.WithdrawApplyReq true "具体参数"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/v1/wallet/withdraw/apply [POST]
- func WithdrawApply(c *gin.Context) {
- var req md.WithdrawApplyReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = svc.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- user := svc.GetUser(c)
- var userId, openId string
- //sysCfgDb := implement.NewSysCfgDb(db.Db, cache.GetPool().Get())
- //sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.WxAppId)
- if req.Kind == int(enum.FinWithdrawApplyWithdrawKindForAli) {
- alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
- aliInfo, err := alipayUserInfoDb.GetAlipayUserInfo(user.Id)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- if aliInfo == nil {
- e.OutErr(c, e.ERR, "支付宝用户信息未授权")
- return
- }
- //appId = sysCfgMap[enum.AlipayAppId]
- userId = aliInfo.UserId
- openId = aliInfo.OpenId
- } else if req.Kind == int(enum.FinWithdrawApplyWithdrawKindForWx) {
- wxUserInfoDb := implement.NewWxUserInfoDb(db.Db)
- wxInfo, err := wxUserInfoDb.GetWxUserInfo(user.Id)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
- if wxInfo == nil {
- e.OutErr(c, e.ERR, "微信用户信息未授权")
- return
- }
- //appId = sysCfgMap[enum.WxAppId]
- userId = wxInfo.UserId
- openId = wxInfo.OpenId
- } else {
- e.OutErr(c, e.ERR, "未知的提现类型")
- return
- }
-
- //1、判断是否可以提现
- err, realAmount, fee, isAuto, isFirst := svc.CheckWithdraw(c, req.Amount)
- if err != nil {
- e.OutErr(c, e.ERR, err.Error())
- return
- }
-
- // 2、加锁 防止并发提取
- mutexKey := fmt.Sprintf("egg_app_withdraw_apply:%s", utils.Int64ToStr(user.Id))
- withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
- if err != nil {
- e.OutErr(c, e.ERR, err)
- return
- }
- if withdrawAvailable != "OK" {
- e.OutErr(c, e.ERR, e.NewErr(400000, "请求过于频繁,请稍后再试"))
- return
- }
-
- // 开启事务
- session := db.Db.NewSession()
- defer session.Close()
- err = session.Begin()
- if err != nil {
- session.Rollback()
- e.OutErr(c, e.ERR_DB_ORM, err)
- return
- }
-
- //3、处理用户余额
- dealUserWalletReq := md2.DealUserWalletReq{
- Direction: "sub",
- Kind: int(enum.UserWithdrawApply),
- Title: enum.UserWithdrawApply.String(),
- Uid: user.Id,
- Amount: utils.StrToFloat64(req.Amount),
- }
- err = rule.DealUserWallet(session, dealUserWalletReq)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- session.Rollback()
- return
- }
-
- //4、新增提现记录
- now := time.Now()
- finWithdrawApplyDb := implement.NewFinWithdrawApplyDb(db.Db)
- insertAffected, err := finWithdrawApplyDb.FinWithdrawApplyInsertOneBySession(session, &model.FinWithdrawApply{
- Uid: user.Id,
- AdmId: 0,
- Amount: req.Amount,
- RealAmount: realAmount,
- Fee: fee,
- Type: func(isAuto bool) int {
- if isAuto {
- return 2
- }
- return 1
- }(isAuto),
- WithdrawAccount: userId,
- WithdrawName: openId,
- Reason: 0,
- PaymentDate: "",
- State: 0,
- WithdrawKind: req.Kind,
- IsFirst: func(isFirst bool) int {
- if isFirst {
- return 1
- }
- return 0
- }(isFirst),
- Memo: "",
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- CreateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- session.Rollback()
- e.OutErr(c, e.ERR_DB_ORM, err)
- return
- }
- if insertAffected <= 0 {
- session.Rollback()
- e.OutErr(c, e.ERR_DB_ORM, "生成提现单失败")
- return
- }
- err = session.Begin()
- if err != nil {
- session.Rollback()
- e.OutErr(c, e.ERR_DB_ORM, err)
- return
- }
-
- //5、推入mq
- if isAuto {
-
- }
- e.OutSuc(c, "success", nil)
- }
|