蛋蛋星球 后台端
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

527 linhas
16 KiB

  1. package financial_center
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. md "applet/app/md/financial_center"
  6. svc "applet/app/svc/financial_center"
  7. "applet/app/utils"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  11. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  13. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  14. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  15. "errors"
  16. "github.com/gin-gonic/gin"
  17. "github.com/jinzhu/copier"
  18. "strings"
  19. "time"
  20. )
  21. // GetWithdrawSetting
  22. // @Summary 财务中心-提现-基础设置(获取)
  23. // @Tags 提现
  24. // @Description 基础设置(获取)
  25. // @Accept json
  26. // @Produce json
  27. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  28. // @Success 200 {object} md.GetWithdrawSettingResp "具体数据"
  29. // @Failure 400 {object} md.Response "具体错误"
  30. // @Router /api/financialCenter/withdraw/setting [get]
  31. func GetWithdrawSetting(c *gin.Context) {
  32. // 等级列表
  33. levelDb := implement.NewUserLevelDb(db.Db)
  34. levels, err1 := levelDb.UserLevelAllByAsc()
  35. if err1 != nil {
  36. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  37. return
  38. }
  39. levelsList := make([]map[string]interface{}, 0)
  40. levelsMap := make(map[int]string)
  41. for _, level := range levels {
  42. levelsList = append(levelsList, map[string]interface{}{
  43. "id": level.Id,
  44. "name": level.LevelName,
  45. })
  46. levelsMap[level.Id] = level.LevelName
  47. }
  48. settingDb := implement.NewFinWithdrawSettingDb(db.Db)
  49. setting, err := settingDb.FinWithdrawSettingGetOne()
  50. if err != nil {
  51. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  52. return
  53. }
  54. // 不存在则初始化
  55. var emptyNum []string
  56. if setting == nil {
  57. now := time.Now()
  58. frequency := md2.WithdrawFrequencySettingStruct{
  59. Duration: 1,
  60. Num: emptyNum,
  61. }
  62. withdrawFeeSet := md2.WithdrawFeeSetStruct{
  63. Kind: 1,
  64. Value: 0,
  65. }
  66. firstWithdrawSet := md2.FirstWithdrawSet{
  67. IsNeedRealName: 0,
  68. FirstWithdrawAmountLimit: "",
  69. }
  70. frequencyStr := utils.SerializeStr(frequency)
  71. withdrawFeeSetStr := utils.SerializeStr(withdrawFeeSet)
  72. firstWithdrawSetStr := utils.SerializeStr(firstWithdrawSet)
  73. m := model.FinWithdrawSetting{
  74. FrequencySet: frequencyStr,
  75. WithdrawType: 1,
  76. VipLevelLimit: 0,
  77. IsRealName: 0,
  78. WithdrawNumsLimit: 0,
  79. WithdrawAmountLimit: "",
  80. WithdrawMultipleLimit: "",
  81. IsSupportDecimalPoint: 0,
  82. IsAuto: 0,
  83. IsAutoAmountLimit: "",
  84. WithdrawTimeInterval: "00:00-00:00",
  85. WithdrawFeeSet: withdrawFeeSetStr,
  86. PendingOrdersIsCanApply: 0,
  87. ConditionIsOpen: 0,
  88. FirstWithdrawSet: firstWithdrawSetStr,
  89. Tips: "",
  90. CreateAt: now.Format("2006-01-02 15:04:05"),
  91. }
  92. _, err2 := settingDb.FinWithdrawSettingInsert(&m)
  93. if err2 != nil {
  94. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  95. return
  96. }
  97. setting, err = settingDb.FinWithdrawSettingGetOne()
  98. if err != nil {
  99. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  100. return
  101. }
  102. }
  103. var frequency md2.WithdrawFrequencySettingStruct
  104. var withdrawFeeSet md2.WithdrawFeeSetStruct
  105. var firstWithdrawSet md2.FirstWithdrawSet
  106. utils.Unserialize([]byte(setting.FrequencySet), &frequency)
  107. utils.Unserialize([]byte(setting.WithdrawFeeSet), &withdrawFeeSet)
  108. utils.Unserialize([]byte(setting.FirstWithdrawSet), &firstWithdrawSet)
  109. withdrawTimeIntervals := strings.Split(setting.WithdrawTimeInterval, "-")
  110. withdrawTimeInterval := md2.WithdrawTimeIntervalStruct{
  111. StartAt: withdrawTimeIntervals[0],
  112. EndAt: withdrawTimeIntervals[1],
  113. }
  114. resp := md.GetWithdrawSettingResp{
  115. Id: setting.Id,
  116. FrequencySet: frequency,
  117. WithdrawType: setting.WithdrawType,
  118. VipLevelLimit: setting.VipLevelLimit,
  119. IsRealName: setting.IsRealName,
  120. WithdrawNumsLimit: setting.WithdrawNumsLimit,
  121. WithdrawAmountLimit: setting.WithdrawAmountLimit,
  122. WithdrawMultipleLimit: setting.WithdrawMultipleLimit,
  123. IsSupportDecimalPoint: setting.IsSupportDecimalPoint,
  124. IsAuto: setting.IsAuto,
  125. IsAutoAmountLimit: setting.IsAutoAmountLimit,
  126. WithdrawTimeInterval: withdrawTimeInterval,
  127. WithdrawFeeSet: withdrawFeeSet,
  128. FirstWithdrawSet: firstWithdrawSet,
  129. PendingOrdersIsCanApply: setting.PendingOrdersIsCanApply,
  130. ConditionIsOpen: setting.ConditionIsOpen,
  131. LevelList: levelsList,
  132. Tips: setting.Tips,
  133. }
  134. e.OutSuc(c, resp, nil)
  135. }
  136. // UpdateWithdrawSetting
  137. // @Summary 财务中心-提现-基础设置(更新)
  138. // @Tags 提现
  139. // @Description 基础设置(更新)
  140. // @Accept json
  141. // @Produce json
  142. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  143. // @Param req body md.UpdateWithdrawSettingReq true "id 必填"
  144. // @Success 200 {int} "修改数据条数"
  145. // @Failure 400 {object} md.Response "具体错误"
  146. // @Router /api/financialCenter/withdraw/updateWithdrawSetting [POST]
  147. func UpdateWithdrawSetting(c *gin.Context) {
  148. var req *md.UpdateWithdrawSettingReq
  149. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  150. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  151. return
  152. }
  153. if req.WithdrawTimeInterval.StartAt != "" {
  154. res := utils.IsTimeFormat(req.WithdrawTimeInterval.StartAt)
  155. if req.WithdrawTimeInterval.StartAt > "23:59" {
  156. res = false
  157. }
  158. if !res {
  159. e.OutErr(c, e.ERR_INVALID_ARGS, errors.New("开始时间应为 xx:xx 且不超过 23:59").Error())
  160. return
  161. }
  162. }
  163. if req.WithdrawTimeInterval.EndAt != "" {
  164. res := utils.IsTimeFormat(req.WithdrawTimeInterval.EndAt)
  165. if req.WithdrawTimeInterval.EndAt > "23:59" {
  166. res = false
  167. }
  168. if !res {
  169. e.OutErr(c, e.ERR_INVALID_ARGS, errors.New("结束时间应为 xx:xx 且不超过 23:59").Error())
  170. return
  171. }
  172. }
  173. frequencyStr := utils.SerializeStr(req.FrequencySet)
  174. withdrawFeeSetStr := utils.SerializeStr(req.WithdrawFeeSet)
  175. firstWithdrawSetStr := utils.SerializeStr(req.FirstWithdrawSet)
  176. var withdrawTimeInterval []string
  177. withdrawTimeInterval = append(withdrawTimeInterval, req.WithdrawTimeInterval.StartAt)
  178. withdrawTimeInterval = append(withdrawTimeInterval, req.WithdrawTimeInterval.EndAt)
  179. withdrawTimeIntervalStr := strings.Join(withdrawTimeInterval, "-")
  180. m := model.FinWithdrawSetting{
  181. Id: req.Id,
  182. FrequencySet: frequencyStr,
  183. WithdrawType: req.WithdrawType,
  184. VipLevelLimit: req.VipLevelLimit,
  185. IsRealName: req.IsRealName,
  186. WithdrawNumsLimit: req.WithdrawNumsLimit,
  187. WithdrawAmountLimit: req.WithdrawAmountLimit,
  188. WithdrawMultipleLimit: req.WithdrawMultipleLimit,
  189. IsSupportDecimalPoint: req.IsSupportDecimalPoint,
  190. IsAuto: req.IsAuto,
  191. IsAutoAmountLimit: req.IsAutoAmountLimit,
  192. WithdrawTimeInterval: withdrawTimeIntervalStr,
  193. WithdrawFeeSet: withdrawFeeSetStr,
  194. PendingOrdersIsCanApply: req.PendingOrdersIsCanApply,
  195. ConditionIsOpen: req.ConditionIsOpen,
  196. FirstWithdrawSet: firstWithdrawSetStr,
  197. Tips: req.Tips,
  198. }
  199. forceColumns := []string{"withdraw_type", "is_real_name", "withdraw_nums_limit",
  200. "withdraw_amount_limit", "withdraw_multiple_limit", "is_support_decimal_point",
  201. "is_auto", "first_withdraw_set", "is_auto_amount_limit", "pending_orders_is_can_apply",
  202. "condition_is_open",
  203. }
  204. settingDb := implement.NewFinWithdrawSettingDb(db.Db)
  205. affected, err := settingDb.FinWithdrawSettingUpdate(req.Id, &m, forceColumns...)
  206. if err != nil {
  207. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  208. return
  209. }
  210. e.OutSuc(c, affected, nil)
  211. }
  212. // GetWithdrawApplyList
  213. // @Summary 财务中心-提现-提现申请列表(获取)
  214. // @Tags 提现
  215. // @Description 提现申请列表(获取)
  216. // @Accept json
  217. // @Produce json
  218. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  219. // @Param req body md.GetWithdrawApplyListReq false "筛选条件"
  220. // @Success 200 {object} md.GetWithdrawApplyListResp "具体数据"
  221. // @Failure 400 {object} md.Response "具体错误"
  222. // @Router /api/financialCenter/withdraw/applyList [POST]
  223. func GetWithdrawApplyList(c *gin.Context) {
  224. var req *md.GetWithdrawApplyListReq
  225. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  226. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  227. return
  228. }
  229. levelDb := implement.NewUserLevelDb(db.Db)
  230. levels, err1 := levelDb.UserLevelAllByAsc()
  231. if err1 != nil {
  232. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  233. return
  234. }
  235. levelsList := make([]map[string]interface{}, 0)
  236. levelsMap := make(map[int]string)
  237. for _, level := range levels {
  238. levelsList = append(levelsList, map[string]interface{}{
  239. "id": level.Id,
  240. "name": level.LevelName,
  241. })
  242. levelsMap[level.Id] = level.LevelName
  243. }
  244. tagDb := implement.NewUserTagDb(db.Db)
  245. tags, err2 := tagDb.UserTagAllByAsc()
  246. if err2 != nil {
  247. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  248. return
  249. }
  250. tagsList := make([]map[string]interface{}, 0)
  251. tagsMap := make(map[int]string)
  252. for _, tag := range tags {
  253. tagsList = append(tagsList, map[string]interface{}{
  254. "id": tag.Id,
  255. "name": tag.TagName,
  256. })
  257. tagsMap[tag.Id] = tag.TagName
  258. }
  259. applies, total, err3 := svc.WithDrawManagementGetApply(db.Db, req)
  260. if err3 != nil {
  261. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  262. return
  263. }
  264. list := make([]md.GetWithdrawApplyListNode, len(*applies))
  265. parentIDs := make([]int64, len(*applies))
  266. UserIDs := make([]int64, len(*applies))
  267. for _, apply := range *applies {
  268. parentIDs = append(parentIDs, apply.ParentID)
  269. UserIDs = append(UserIDs, apply.Uid)
  270. }
  271. // 查询上级
  272. userDb := implement.NewUserDb(db.Db)
  273. parents, err := userDb.UserFindByParams(map[string]interface{}{
  274. "key": "id",
  275. "value": parentIDs,
  276. })
  277. if err != nil {
  278. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  279. return
  280. }
  281. parentsMap := make(map[int64]model.User)
  282. for _, parent := range parents {
  283. parentsMap[parent.Id] = parent
  284. }
  285. // 查询标签
  286. recordsDb := implement.NewUserTagRecordsDb(db.Db)
  287. records, err := recordsDb.UserTagRecordsFindByParams(map[string]interface{}{
  288. "key": "uid",
  289. "value": UserIDs,
  290. })
  291. if err != nil {
  292. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  293. return
  294. }
  295. recordsMap := make(map[int64][]md.TagNode)
  296. for _, record := range *records {
  297. temp := md.TagNode{
  298. TagID: record.TagId,
  299. TagName: "",
  300. }
  301. tagName, ok := tagsMap[record.TagId]
  302. temp.TagName = tagName
  303. v, ok := recordsMap[record.Uid]
  304. if ok {
  305. v = append(v, temp)
  306. } else {
  307. recordsMap[record.Uid] = []md.TagNode{temp}
  308. }
  309. }
  310. for i, apply := range *applies {
  311. list[i] = md.GetWithdrawApplyListNode{
  312. WithdrawApplyId: apply.Id,
  313. UserID: apply.Uid,
  314. Nickname: apply.Nickname,
  315. ParentID: apply.ParentID,
  316. AliPayName: apply.AliPayName,
  317. WechatPayName: apply.WxPayName,
  318. AliPayAccount: apply.AliPayAccount,
  319. WechatPayAccount: apply.WxPayAccount,
  320. WithdrawType: apply.WithdrawKind,
  321. InviteCode: apply.InviteCode,
  322. Amount: apply.Amount,
  323. ActualReceipt: apply.RealAmount,
  324. SysFee: apply.Fee,
  325. State: apply.State,
  326. ApplyAt: apply.CreateAt,
  327. PayAt: apply.UpdateAt,
  328. Memo: apply.Memo,
  329. }
  330. if apply.Amount != "" && apply.Fee != "" {
  331. actualReceipt := utils.StrToFloat64(apply.Amount) - utils.StrToFloat64(apply.Fee)
  332. list[i].ActualReceipt = utils.Float64ToStr(actualReceipt)
  333. }
  334. if apply.ParentID != 0 {
  335. v, ok := parentsMap[apply.ParentID]
  336. if ok {
  337. list[i].ParentPhone = v.Phone
  338. }
  339. }
  340. tagList, ok := recordsMap[apply.Uid]
  341. if ok {
  342. list[i].Tag = tagList
  343. }
  344. }
  345. applyDb := implement.NewFinWithdrawApplyDb(db.Db)
  346. underReviewAmount, err := applyDb.FinWithdrawApplyAmountGetByParams(map[string]interface{}{
  347. "key": "state",
  348. "value": 0,
  349. })
  350. if err != nil {
  351. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  352. return
  353. }
  354. pendingAmount, err := applyDb.FinWithdrawApplyAmountGetByParams(map[string]interface{}{
  355. "key": "state",
  356. "value": 4,
  357. })
  358. if err != nil {
  359. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  360. return
  361. }
  362. paySucceedAmount, err := applyDb.FinWithdrawApplyAmountGetByParams(map[string]interface{}{
  363. "key": "state",
  364. "value": 2,
  365. })
  366. if err != nil {
  367. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  368. return
  369. }
  370. payFailedAmount, err := applyDb.FinWithdrawApplyAmountGetByParams(map[string]interface{}{
  371. "key": "state",
  372. "value": 3,
  373. })
  374. if err != nil {
  375. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  376. return
  377. }
  378. resp := md.GetWithdrawApplyListResp{
  379. ReasonList: []map[string]interface{}{
  380. {
  381. "value": enum.FinWithdrawApplyReasonForNotRule,
  382. "name": enum.FinWithdrawApplyReasonForNotRule.String(),
  383. },
  384. {
  385. "value": enum.FinWithdrawApplyReasonForAccountAbnormal,
  386. "name": enum.FinWithdrawApplyReasonForAccountAbnormal.String(),
  387. },
  388. {
  389. "value": enum.FinWithdrawApplyReasonForFundAbnormal,
  390. "name": enum.FinWithdrawApplyReasonForFundAbnormal.String(),
  391. },
  392. },
  393. LevelsList: levelsList,
  394. TagsList: tagsList,
  395. PendingAmount: utils.Float64ToStr(pendingAmount),
  396. PaySucceedAmount: utils.Float64ToStr(paySucceedAmount),
  397. PayFailedAmount: utils.Float64ToStr(payFailedAmount),
  398. UnderReviewAmount: utils.Float64ToStr(underReviewAmount),
  399. List: list,
  400. Paginate: md.Paginate{
  401. Limit: req.Limit,
  402. Page: req.Page,
  403. Total: total,
  404. },
  405. }
  406. e.OutSuc(c, resp, nil)
  407. }
  408. // WithdrawApplyAudit
  409. // @Summary 财务中心-提现-审核
  410. // @Tags 提现
  411. // @Description 提现审核
  412. // @Accept json
  413. // @Produce json
  414. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  415. // @Param req body md.WithdrawApplyAuditReq false "筛选条件"
  416. // @Success 200 {string} "success"
  417. // @Failure 400 {object} md.Response "具体错误"
  418. // @Router /api/financialCenter/withdraw/audit [POST]
  419. func WithdrawApplyAudit(c *gin.Context) {
  420. var req *md.WithdrawApplyAuditReq
  421. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  422. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  423. return
  424. }
  425. //1、查找对应提现申请单
  426. finWithdrawApplyDb := implement.NewFinWithdrawApplyDb(db.Db)
  427. finWithdrawApply, err := finWithdrawApplyDb.FinWithdrawApplyGet(req.WithdrawApplyId)
  428. if err != nil {
  429. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  430. return
  431. }
  432. if finWithdrawApply == nil {
  433. e.OutErr(c, e.ERR_NOT_FAN, "提现记录不存在")
  434. return
  435. }
  436. //2、判断审核状态
  437. if req.AuditState == 1 {
  438. //通过(推mq、修改提现单成功)
  439. finWithdrawApply.State = int(enum.FinWithdrawApplyStateForIng)
  440. updateAffected, err1 := finWithdrawApplyDb.UpdateFinWithdrawApply(finWithdrawApply, "state")
  441. if err1 != nil {
  442. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  443. return
  444. }
  445. if updateAffected <= 0 {
  446. e.OutErr(c, e.ERR_DB_ORM, "更新提现单状态失败")
  447. return
  448. }
  449. ch, err1 := rabbit.Cfg.Pool.GetChannel()
  450. if err1 != nil {
  451. e.OutErr(c, e.ERR_INIT_RABBITMQ, err1.Error())
  452. return
  453. }
  454. defer ch.Release()
  455. var data md2.EggFinWithdrawApplyData
  456. err = copier.Copy(&data, &finWithdrawApply)
  457. if err != nil {
  458. e.OutErr(c, e.ERR, err.Error())
  459. return
  460. }
  461. ch.Publish(md2.EggAppExchange, utils.SerializeStr(data), md2.EggFinWithdrawApply)
  462. } else {
  463. //拒绝(退余额、修改提现单失败)
  464. session := db.Db.NewSession()
  465. defer session.Close()
  466. session.Begin()
  467. finWithdrawApply.State = int(enum.FinWithdrawApplyStateForBad)
  468. updateAffected, err1 := finWithdrawApplyDb.UpdateFinWithdrawApplyBySession(session, finWithdrawApply, "state")
  469. if err1 != nil {
  470. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  471. return
  472. }
  473. if updateAffected <= 0 {
  474. e.OutErr(c, e.ERR_DB_ORM, "更新提现单状态失败")
  475. return
  476. }
  477. dealUserWalletReq := md3.DealUserWalletReq{
  478. Direction: "sub",
  479. Kind: int(enum.UserWithdrawBad),
  480. Title: enum.UserWithdrawBad.String(),
  481. Uid: finWithdrawApply.Uid,
  482. Amount: utils.StrToFloat64(finWithdrawApply.Amount),
  483. }
  484. err = rule.DealUserWallet(session, dealUserWalletReq)
  485. if err != nil {
  486. session.Rollback()
  487. e.OutErr(c, e.ERR, err.Error())
  488. return
  489. }
  490. err = session.Commit()
  491. if err != nil {
  492. _ = session.Rollback()
  493. e.OutErr(c, e.ERR_DB_ORM, err)
  494. return
  495. }
  496. }
  497. e.OutSuc(c, "success", nil)
  498. }