蛋蛋星球 后台端
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.
 
 
 
 

540 lines
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: utils.Int64ToStr(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.CustomInviteCode != "" {
  331. list[i].InviteCode = apply.CustomInviteCode
  332. }
  333. if apply.Amount != "" && apply.Fee != "" {
  334. actualReceipt := utils.StrToFloat64(apply.Amount) - utils.StrToFloat64(apply.Fee)
  335. list[i].ActualReceipt = utils.Float64ToStr(actualReceipt)
  336. }
  337. if apply.ParentID != 0 {
  338. v, ok := parentsMap[apply.ParentID]
  339. if ok {
  340. list[i].ParentPhone = v.Phone
  341. }
  342. }
  343. tagList, ok := recordsMap[apply.Uid]
  344. if ok {
  345. list[i].Tag = tagList
  346. }
  347. }
  348. applyDb := implement.NewFinWithdrawApplyDb(db.Db)
  349. underReviewAmount, err := applyDb.FinWithdrawApplyAmountGetByParams(map[string]interface{}{
  350. "key": "state",
  351. "value": 0,
  352. })
  353. if err != nil {
  354. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  355. return
  356. }
  357. pendingAmount, err := applyDb.FinWithdrawApplyAmountGetByParams(map[string]interface{}{
  358. "key": "state",
  359. "value": 4,
  360. })
  361. if err != nil {
  362. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  363. return
  364. }
  365. paySucceedAmount, err := applyDb.FinWithdrawApplyAmountGetByParams(map[string]interface{}{
  366. "key": "state",
  367. "value": 2,
  368. })
  369. if err != nil {
  370. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  371. return
  372. }
  373. payFailedAmount, err := applyDb.FinWithdrawApplyAmountGetByParams(map[string]interface{}{
  374. "key": "state",
  375. "value": 3,
  376. })
  377. if err != nil {
  378. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  379. return
  380. }
  381. resp := md.GetWithdrawApplyListResp{
  382. AuditList: []map[string]interface{}{
  383. {
  384. "value": 1,
  385. "name": "同意",
  386. },
  387. {
  388. "value": 2,
  389. "name": "拒绝",
  390. },
  391. },
  392. ReasonList: []map[string]interface{}{
  393. {
  394. "value": enum.FinWithdrawApplyReasonForNotRule,
  395. "name": enum.FinWithdrawApplyReasonForNotRule.String(),
  396. },
  397. {
  398. "value": enum.FinWithdrawApplyReasonForAccountAbnormal,
  399. "name": enum.FinWithdrawApplyReasonForAccountAbnormal.String(),
  400. },
  401. {
  402. "value": enum.FinWithdrawApplyReasonForFundAbnormal,
  403. "name": enum.FinWithdrawApplyReasonForFundAbnormal.String(),
  404. },
  405. },
  406. LevelsList: levelsList,
  407. TagsList: tagsList,
  408. PendingAmount: utils.Float64ToStr(pendingAmount),
  409. PaySucceedAmount: utils.Float64ToStr(paySucceedAmount),
  410. PayFailedAmount: utils.Float64ToStr(payFailedAmount),
  411. UnderReviewAmount: utils.Float64ToStr(underReviewAmount),
  412. List: list,
  413. Paginate: md.Paginate{
  414. Limit: req.Limit,
  415. Page: req.Page,
  416. Total: total,
  417. },
  418. }
  419. e.OutSuc(c, resp, nil)
  420. }
  421. // WithdrawApplyAudit
  422. // @Summary 财务中心-提现-审核
  423. // @Tags 提现
  424. // @Description 提现审核
  425. // @Accept json
  426. // @Produce json
  427. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  428. // @Param req body md.WithdrawApplyAuditReq false "筛选条件"
  429. // @Success 200 {string} "success"
  430. // @Failure 400 {object} md.Response "具体错误"
  431. // @Router /api/financialCenter/withdraw/audit [POST]
  432. func WithdrawApplyAudit(c *gin.Context) {
  433. var req *md.WithdrawApplyAuditReq
  434. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  435. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  436. return
  437. }
  438. //1、查找对应提现申请单
  439. finWithdrawApplyDb := implement.NewFinWithdrawApplyDb(db.Db)
  440. finWithdrawApply, err := finWithdrawApplyDb.FinWithdrawApplyGet(utils.StrToInt64(req.WithdrawApplyId))
  441. if err != nil {
  442. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  443. return
  444. }
  445. if finWithdrawApply == nil {
  446. e.OutErr(c, e.ERR_NOT_FAN, "提现记录不存在")
  447. return
  448. }
  449. //2、判断审核状态
  450. if req.AuditState == 1 {
  451. //通过(推mq、修改提现单成功)
  452. finWithdrawApply.State = int(enum.FinWithdrawApplyStateForIng)
  453. updateAffected, err1 := finWithdrawApplyDb.UpdateFinWithdrawApply(finWithdrawApply, "state")
  454. if err1 != nil {
  455. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  456. return
  457. }
  458. if updateAffected <= 0 {
  459. e.OutErr(c, e.ERR_DB_ORM, "更新提现单状态失败")
  460. return
  461. }
  462. ch, err1 := rabbit.Cfg.Pool.GetChannel()
  463. if err1 != nil {
  464. e.OutErr(c, e.ERR_INIT_RABBITMQ, err1.Error())
  465. return
  466. }
  467. defer ch.Release()
  468. var data md2.EggFinWithdrawApplyData
  469. err = copier.Copy(&data, &finWithdrawApply)
  470. if err != nil {
  471. e.OutErr(c, e.ERR, err.Error())
  472. return
  473. }
  474. ch.Publish(md2.EggAppExchange, data, md2.EggFinWithdrawApply)
  475. } else {
  476. //拒绝(退余额、修改提现单失败)
  477. session := db.Db.NewSession()
  478. defer session.Close()
  479. session.Begin()
  480. finWithdrawApply.State = int(enum.FinWithdrawApplyStateForBad)
  481. updateAffected, err1 := finWithdrawApplyDb.UpdateFinWithdrawApplyBySession(session, finWithdrawApply, "state")
  482. if err1 != nil {
  483. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  484. return
  485. }
  486. if updateAffected <= 0 {
  487. e.OutErr(c, e.ERR_DB_ORM, "更新提现单状态失败")
  488. return
  489. }
  490. dealUserWalletReq := md3.DealUserWalletReq{
  491. Direction: "sub",
  492. Kind: int(enum.UserWithdrawBad),
  493. Title: enum.UserWithdrawBad.String(),
  494. Uid: finWithdrawApply.Uid,
  495. Amount: utils.StrToFloat64(finWithdrawApply.Amount),
  496. }
  497. err = rule.DealUserWallet(session, dealUserWalletReq)
  498. if err != nil {
  499. session.Rollback()
  500. e.OutErr(c, e.ERR, err.Error())
  501. return
  502. }
  503. err = session.Commit()
  504. if err != nil {
  505. _ = session.Rollback()
  506. e.OutErr(c, e.ERR_DB_ORM, err)
  507. return
  508. }
  509. }
  510. e.OutSuc(c, "success", nil)
  511. }