蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

293 lines
7.6 KiB

  1. package hdl
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md"
  6. "applet/app/svc"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  12. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  13. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  14. "fmt"
  15. "github.com/gin-gonic/gin"
  16. "time"
  17. )
  18. // GetAmountFlow
  19. // @Summary 蛋蛋星球-钱包-余额明细(获取)
  20. // @Tags 钱包
  21. // @Description 余额明细(获取)
  22. // @Accept json
  23. // @Produce json
  24. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  25. // @Param limit query string true "每页大小"
  26. // @Param page query string true "页数"
  27. // @Param startAt query string false "开始时间"
  28. // @Param endAt query string false "结束时间"
  29. // @Param direction query string false "流水方向(1.收入 2.支出 0.全部)"
  30. // @Success 200 {object} md.GetAmountFlowResp "具体数据"
  31. // @Failure 400 {object} md.Response "具体错误"
  32. // @Router /api/v1/wallet/amountFlow [GET]
  33. func GetAmountFlow(c *gin.Context) {
  34. pageStr := c.DefaultQuery("page", "1")
  35. limitStr := c.DefaultQuery("limit", "10")
  36. startAt := c.Query("startAt")
  37. endAt := c.Query("endAt")
  38. directionStr := c.Query("direction")
  39. val, exists := c.Get("user")
  40. if !exists {
  41. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  42. return
  43. }
  44. user, ok := val.(*model.User)
  45. if !ok {
  46. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  47. return
  48. }
  49. direction := 0
  50. switch directionStr {
  51. case "1":
  52. direction = 1
  53. case "2":
  54. direction = 2
  55. }
  56. page := utils.StrToInt(pageStr)
  57. limit := utils.StrToInt(limitStr)
  58. flowDb := implement.NewUserWalletFlowDb(db.Db)
  59. flows, total, err := flowDb.UserWalletFlowFindByCoinAndUser(page, limit, user.Id, startAt, endAt, direction, false, 0, 0)
  60. if err != nil {
  61. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  62. return
  63. }
  64. list := make([]md.WalletFlowNode, 0, len(flows))
  65. for _, flow := range flows {
  66. temp := md.WalletFlowNode{
  67. Id: flow.Id,
  68. Uid: flow.Uid,
  69. Direction: flow.Direction,
  70. Amount: flow.Amount,
  71. BeforeAmount: flow.BeforeAmount,
  72. AfterAmount: flow.AfterAmount,
  73. SysFee: flow.SysFee,
  74. OrdId: flow.OrdId,
  75. Title: flow.Title,
  76. Kind: flow.Kind,
  77. State: flow.State,
  78. Memo: flow.Memo,
  79. CreateTime: flow.CreateAt,
  80. UpdateTime: flow.UpdateAt,
  81. }
  82. list = append(list, temp)
  83. }
  84. resp := md.GetAmountFlowResp{
  85. List: list,
  86. Paginate: md.Paginate{
  87. Limit: limit,
  88. Page: page,
  89. Total: total,
  90. },
  91. }
  92. e.OutSuc(c, resp, nil)
  93. }
  94. // WithdrawGetAmount
  95. // @Summary 蛋蛋星球-钱包-提现余额(获取)
  96. // @Tags 钱包
  97. // @Description 提现余额(获取)
  98. // @Accept json
  99. // @Produce json
  100. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  101. // @Success 200 {object} md.WithdrawGetAmountResp "具体数据"
  102. // @Failure 400 {object} md.Response "具体错误"
  103. // @Router /api/v1/wallet/withdraw/index [GET]
  104. func WithdrawGetAmount(c *gin.Context) {
  105. val, exists := c.Get("user")
  106. if !exists {
  107. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  108. return
  109. }
  110. user, ok := val.(*model.User)
  111. if !ok {
  112. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  113. return
  114. }
  115. walletDb := implement.NewUserWalletDb(db.Db)
  116. wallet, err := walletDb.GetUserVirtualWallet(user.Id)
  117. if err != nil {
  118. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  119. return
  120. }
  121. resp := md.WithdrawGetAmountResp{
  122. Amount: wallet.Amount,
  123. }
  124. e.OutSuc(c, resp, nil)
  125. }
  126. // WithdrawApply
  127. // @Summary 蛋蛋星球-钱包-发起提现
  128. // @Tags 钱包
  129. // @Description 发起提现
  130. // @Accept json
  131. // @Produce json
  132. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  133. // @Param req body md.WithdrawApplyReq true "具体参数"
  134. // @Success 200 {string} "success"
  135. // @Failure 400 {object} md.Response "具体错误"
  136. // @Router /api/v1/wallet/withdraw/apply [POST]
  137. func WithdrawApply(c *gin.Context) {
  138. var req md.WithdrawApplyReq
  139. err := c.ShouldBindJSON(&req)
  140. if err != nil {
  141. err = svc.HandleValidateErr(err)
  142. err1 := err.(e.E)
  143. e.OutErr(c, err1.Code, err1.Error())
  144. return
  145. }
  146. user := svc.GetUser(c)
  147. var userId, openId string
  148. //sysCfgDb := implement.NewSysCfgDb(db.Db, cache.GetPool().Get())
  149. //sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.WxAppId)
  150. if req.Kind == int(enum.FinWithdrawApplyWithdrawKindForAli) {
  151. alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
  152. aliInfo, err := alipayUserInfoDb.GetAlipayUserInfo(user.Id)
  153. if err != nil {
  154. e.OutErr(c, e.ERR, err.Error())
  155. return
  156. }
  157. if aliInfo == nil {
  158. e.OutErr(c, e.ERR, "支付宝用户信息未授权")
  159. return
  160. }
  161. //appId = sysCfgMap[enum.AlipayAppId]
  162. userId = aliInfo.UserId
  163. openId = aliInfo.OpenId
  164. } else if req.Kind == int(enum.FinWithdrawApplyWithdrawKindForWx) {
  165. wxUserInfoDb := implement.NewWxUserInfoDb(db.Db)
  166. wxInfo, err := wxUserInfoDb.GetWxUserInfo(user.Id)
  167. if err != nil {
  168. e.OutErr(c, e.ERR, err.Error())
  169. return
  170. }
  171. if wxInfo == nil {
  172. e.OutErr(c, e.ERR, "微信用户信息未授权")
  173. return
  174. }
  175. //appId = sysCfgMap[enum.WxAppId]
  176. userId = wxInfo.UserId
  177. openId = wxInfo.OpenId
  178. } else {
  179. e.OutErr(c, e.ERR, "未知的提现类型")
  180. return
  181. }
  182. //1、判断是否可以提现
  183. err, realAmount, fee, isAuto, isFirst := svc.CheckWithdraw(c, req.Amount)
  184. if err != nil {
  185. e.OutErr(c, e.ERR, err.Error())
  186. return
  187. }
  188. // 2、加锁 防止并发提取
  189. mutexKey := fmt.Sprintf("egg_app_withdraw_apply:%s", utils.Int64ToStr(user.Id))
  190. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  191. if err != nil {
  192. e.OutErr(c, e.ERR, err)
  193. return
  194. }
  195. if withdrawAvailable != "OK" {
  196. e.OutErr(c, e.ERR, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  197. return
  198. }
  199. // 开启事务
  200. session := db.Db.NewSession()
  201. defer session.Close()
  202. err = session.Begin()
  203. if err != nil {
  204. session.Rollback()
  205. e.OutErr(c, e.ERR_DB_ORM, err)
  206. return
  207. }
  208. //3、处理用户余额
  209. dealUserWalletReq := md2.DealUserWalletReq{
  210. Direction: "sub",
  211. Kind: int(enum.UserWithdrawApply),
  212. Title: enum.UserWithdrawApply.String(),
  213. Uid: user.Id,
  214. Amount: utils.StrToFloat64(req.Amount),
  215. }
  216. err = rule.DealUserWallet(session, dealUserWalletReq)
  217. if err != nil {
  218. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  219. session.Rollback()
  220. return
  221. }
  222. //4、新增提现记录
  223. now := time.Now()
  224. finWithdrawApplyDb := implement.NewFinWithdrawApplyDb(db.Db)
  225. insertAffected, err := finWithdrawApplyDb.FinWithdrawApplyInsertOneBySession(session, &model.FinWithdrawApply{
  226. Uid: user.Id,
  227. AdmId: 0,
  228. Amount: req.Amount,
  229. RealAmount: realAmount,
  230. Fee: fee,
  231. Type: func(isAuto bool) int {
  232. if isAuto {
  233. return 2
  234. }
  235. return 1
  236. }(isAuto),
  237. WithdrawAccount: userId,
  238. WithdrawName: openId,
  239. Reason: 0,
  240. PaymentDate: "",
  241. State: 0,
  242. WithdrawKind: req.Kind,
  243. IsFirst: func(isFirst bool) int {
  244. if isFirst {
  245. return 1
  246. }
  247. return 0
  248. }(isFirst),
  249. Memo: "",
  250. UpdateAt: now.Format("2006-01-02 15:04:05"),
  251. CreateAt: now.Format("2006-01-02 15:04:05"),
  252. })
  253. if err != nil {
  254. session.Rollback()
  255. e.OutErr(c, e.ERR_DB_ORM, err)
  256. return
  257. }
  258. if insertAffected <= 0 {
  259. session.Rollback()
  260. e.OutErr(c, e.ERR_DB_ORM, "生成提现单失败")
  261. return
  262. }
  263. err = session.Begin()
  264. if err != nil {
  265. session.Rollback()
  266. e.OutErr(c, e.ERR_DB_ORM, err)
  267. return
  268. }
  269. //5、推入mq
  270. if isAuto {
  271. }
  272. e.OutSuc(c, "success", nil)
  273. }