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

601 lines
17 KiB

  1. package member_center
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md/member_center"
  6. svc "applet/app/svc/member_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. "errors"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "strings"
  14. )
  15. // UserManagementGetUserList
  16. // @Summary 制度中心-会员中心-用户管理-用户信息管理(获取)
  17. // @Tags 会员中心
  18. // @Description 用户信息管理(获取)
  19. // @Accept json
  20. // @Produce json
  21. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  22. // @Param req body md.UserManagementGetUserListReq true "分页信息必填"
  23. // @Success 200 {object} md.UserManagementGetUserListResp "具体数据"
  24. // @Failure 400 {object} md.Response "具体错误"
  25. // @Router /api/memberCenter/userManagement/getUserList [post]
  26. func UserManagementGetUserList(c *gin.Context) {
  27. var req *md.UserManagementGetUserListReq
  28. if err := c.ShouldBindJSON(&req); err != nil {
  29. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  30. return
  31. }
  32. levelDb := implement.NewUserLevelDb(db.Db)
  33. levels, err1 := levelDb.UserLevelAllByAsc()
  34. if err1 != nil {
  35. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  36. return
  37. }
  38. levelsList := make([]map[string]interface{}, 0)
  39. levelsMap := make(map[int]string)
  40. for _, level := range levels {
  41. levelsList = append(levelsList, map[string]interface{}{
  42. "id": level.Id,
  43. "name": level.LevelName,
  44. })
  45. levelsMap[level.Id] = level.LevelName
  46. }
  47. tagDb := implement.NewUserTagDb(db.Db)
  48. tags, err2 := tagDb.UserTagAllByAsc()
  49. if err2 != nil {
  50. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  51. return
  52. }
  53. tagsList := make([]map[string]interface{}, 0)
  54. tagsMap := make(map[int]string)
  55. for _, tag := range tags {
  56. tagsList = append(tagsList, map[string]interface{}{
  57. "id": tag.Id,
  58. "name": tag.TagName,
  59. })
  60. tagsMap[tag.Id] = tag.TagName
  61. }
  62. users, total, err3 := svc.UserManagementGetUsers(db.Db, req)
  63. if err3 != nil {
  64. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  65. return
  66. }
  67. var levelCounts []md.LevelCount
  68. err4 := db.Db.Table("user").Select("level, Count(*) AS count").GroupBy("level").Find(&levelCounts)
  69. if err4 != nil {
  70. e.OutErr(c, e.ERR_DB_ORM, err4.Error())
  71. return
  72. }
  73. list := make([]md.UserManagementGetUserListNode, len(*users))
  74. for i, user := range *users {
  75. list[i] = md.UserManagementGetUserListNode{
  76. ID: user.Id,
  77. Sex: user.Sex,
  78. Avatar: user.Avatar,
  79. Nickname: user.Nickname,
  80. Phone: user.Phone,
  81. IsRealName: user.IsRealName,
  82. InviteCode: user.SystemInviteCode,
  83. ParentID: user.ParentUid,
  84. ParentInviteCode: user.ParentSystemInviteCode,
  85. ParentPhone: user.Phone,
  86. RegisterTime: user.CreateAt,
  87. Memo: user.Memo,
  88. Wechat: user.UnionId,
  89. RegisterType: user.RegisterType,
  90. State: user.State,
  91. LastLoginAt: user.UpdateAt,
  92. }
  93. tag, ok := tagsMap[user.TagID]
  94. if ok {
  95. list[i].Tag = tag
  96. }
  97. level, ok := levelsMap[user.Level]
  98. if ok {
  99. list[i].LevelName = level
  100. }
  101. }
  102. resp := md.UserManagementGetUserListResp{
  103. LevelsList: levelsList,
  104. TagsList: tagsList,
  105. List: list,
  106. Paginate: md.Paginate{
  107. Limit: req.Limit,
  108. Page: req.Page,
  109. Total: total,
  110. },
  111. }
  112. e.OutSuc(c, resp, nil)
  113. }
  114. // UserManagementUpdateUserInfo
  115. // @Summary 制度中心-会员中心-用户管理-用户信息管理(更新)
  116. // @Tags 会员中心
  117. // @Description 用户信息管理(更新)
  118. // @Accept json
  119. // @Produce json
  120. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  121. // @Param req body md.UserManagementUpdateUserInfoReq true "用户ID 必传"
  122. // @Success 200 {int} "修改数据行数"
  123. // @Failure 400 {object} md.Response "具体错误"
  124. // @Router /api/memberCenter/userManagement/updateUserInfo [post]
  125. func UserManagementUpdateUserInfo(c *gin.Context) {
  126. var req *md.UserManagementUpdateUserInfoReq
  127. if err := c.ShouldBindJSON(&req); err != nil {
  128. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  129. return
  130. }
  131. session := db.Db.NewSession()
  132. defer session.Close()
  133. session.Begin()
  134. userDb := implement.NewUserDb(db.Db)
  135. var affected int64
  136. var err1 error
  137. forceColumns := make([]string, 0)
  138. user := model.User{
  139. Id: req.UID,
  140. Phone: req.Phone,
  141. UnionId: req.UnionId,
  142. Nickname: req.Nickname,
  143. Avatar: req.Avatar,
  144. LastLoginIp: req.LastLoginIp,
  145. Memo: req.Memo,
  146. }
  147. if req.Level != "" {
  148. user.Level = utils.StrToInt(req.Level)
  149. }
  150. if req.State != "" {
  151. user.State = utils.StrToInt(req.State)
  152. }
  153. if req.Sex != "" {
  154. user.Sex = utils.StrToInt(req.Sex)
  155. }
  156. if req.ParentUid != "" {
  157. user.ParentUid = utils.StrToInt64(req.ParentUid)
  158. } else {
  159. user.ParentUid = 0
  160. }
  161. forceColumns = []string{"sex", "parent_uid"}
  162. affected, err1 = userDb.UserUpdateBySession(session, req.UID, &user, forceColumns...)
  163. if err1 != nil {
  164. session.Rollback()
  165. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  166. return
  167. }
  168. relateDb := implement.NewUserRelateDb(db.Db)
  169. relateForceColumns := []string{"sex", "parent_uid"}
  170. relate := &model.UserRelate{
  171. ParentUid: user.ParentUid,
  172. }
  173. affected, err1 = relateDb.UpdateUserRelateBySession(session, req.UID, relate, relateForceColumns...)
  174. if err1 != nil {
  175. session.Rollback()
  176. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  177. return
  178. }
  179. var err2 error
  180. if req.Tag != "" {
  181. record := model.UserTagRecords{
  182. TagId: utils.StrToInt(req.Tag),
  183. }
  184. recordsDb := implement.NewUserTagRecordsDb(db.Db)
  185. affected, err2 = recordsDb.UserTagRecordsUpdateBySession(session, req.UID, &record, "tag_id")
  186. if err2 != nil {
  187. session.Rollback()
  188. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  189. return
  190. }
  191. }
  192. err3 := session.Commit()
  193. if err3 != nil {
  194. session.Rollback()
  195. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  196. return
  197. }
  198. e.OutSuc(c, affected, nil)
  199. }
  200. // UserManagementGetOneBasic
  201. // @Summary 制度中心-会员中心-用户管理-会员明细概况(获取)
  202. // @Tags 会员中心
  203. // @Description 会员明细概况(获取)
  204. // @Accept json
  205. // @Produce json
  206. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  207. // @Param uid query string true "用户 ID"
  208. // @Success 200 {object} md.UserManagementGetOneBasicResp "会员明细概况具体数据"
  209. // @Failure 400 {object} md.Response "具体错误"
  210. // @Router /api/memberCenter/userManagement/userData [get]
  211. func UserManagementGetOneBasic(c *gin.Context) {
  212. uid := c.Query("uid")
  213. userDb := implement.NewUserDb(db.Db)
  214. user, err := userDb.UserGetOneByParams(map[string]interface{}{
  215. "key": "id",
  216. "value": uid,
  217. })
  218. if err != nil {
  219. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  220. return
  221. }
  222. // 1. 查询等级列表
  223. levelDb := implement.NewUserLevelDb(db.Db)
  224. levels, err1 := levelDb.UserLevelAllByAsc()
  225. if err1 != nil {
  226. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  227. return
  228. }
  229. levelsList := make([]map[string]interface{}, 0)
  230. levelsMap := make(map[int]string)
  231. for _, level := range levels {
  232. levelsList = append(levelsList, map[string]interface{}{
  233. "id": level.Id,
  234. "name": level.LevelName,
  235. })
  236. levelsMap[level.Id] = level.LevelName
  237. }
  238. //2. 查询标签列表
  239. tagDb := implement.NewUserTagDb(db.Db)
  240. tags, err2 := tagDb.UserTagAllByAsc()
  241. if err2 != nil {
  242. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  243. return
  244. }
  245. tagsList := make([]map[string]interface{}, 0)
  246. tagsMap := make(map[int]string)
  247. for _, tag := range tags {
  248. tagsList = append(tagsList, map[string]interface{}{
  249. "id": tag.Id,
  250. "name": tag.TagName,
  251. })
  252. tagsMap[tag.Id] = tag.TagName
  253. }
  254. //3. 查询父用户信息
  255. parent, err := userDb.UserGetOneByParams(map[string]interface{}{
  256. "key": "id",
  257. "value": user.ParentUid,
  258. })
  259. if err != nil {
  260. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  261. return
  262. }
  263. //4. 查询用户标签
  264. recordsDb := implement.NewUserTagRecordsDb(db.Db)
  265. userTagRecord, err := recordsDb.UserTagRecordsGetOneByParams(map[string]interface{}{
  266. "key": "uid",
  267. "value": uid,
  268. })
  269. if err != nil {
  270. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  271. return
  272. }
  273. resp := md.UserManagementGetOneBasicResp{
  274. TagsList: tagsList,
  275. LevelsList: levelsList,
  276. BasicInfo: md.BasicInfoNode{
  277. Avatar: user.Avatar,
  278. Sex: user.Sex,
  279. Nickname: user.Nickname,
  280. Phone: user.Phone,
  281. UnionId: user.UnionId,
  282. Password: user.Password,
  283. State: user.State,
  284. Memo: user.Memo,
  285. },
  286. OtherInfo: md.OtherNode{
  287. LastLoginIp: user.LastLoginIp,
  288. LastLoginAt: user.UpdateAt,
  289. CreateAt: user.CreateAt,
  290. },
  291. }
  292. if userTagRecord != nil {
  293. resp.BasicInfo.TagName = tagsMap[userTagRecord.TagId]
  294. }
  295. if parent != nil {
  296. resp.BasicInfo.ParentUid = parent.Id
  297. resp.BasicInfo.ParentName = parent.Nickname
  298. resp.BasicInfo.ParentPhone = parent.Phone
  299. }
  300. level, ok := levelsMap[user.Level]
  301. if ok {
  302. resp.BasicInfo.LevelName = level
  303. }
  304. e.OutSuc(c, resp, nil)
  305. }
  306. // UserManagementGetFans
  307. // @Summary 制度中心-会员中心-用户管理-会员明细粉丝情况(获取)
  308. // @Tags 会员中心
  309. // @Description 会员明细粉丝情况(获取)
  310. // @Accept json
  311. // @Produce json
  312. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  313. // @Param uid query string true "用户 ID"
  314. // @Param type query string true "粉丝类型(1.全部 2.直推 3.二代 4.二代以后)"
  315. // @Param limit query string true "每页大小"
  316. // @Param page query string true "页数"
  317. // @Success 200 {object} md.UserManagementGetFansResp "具体数据"
  318. // @Failure 400 {object} md.Response "具体错误"
  319. // @Router /api/memberCenter/userManagement/getFans [get]
  320. func UserManagementGetFans(c *gin.Context) {
  321. uid := c.Query("uid")
  322. fansType := c.Query("type")
  323. limit := c.Query("limit")
  324. page := c.Query("page")
  325. //1. 查询符合情况用户 ID
  326. relateDb := implement.NewUserRelateDb(db.Db)
  327. var level int
  328. switch fansType {
  329. case "0":
  330. level = 0
  331. case "1":
  332. level = 1
  333. case "2":
  334. level = 2
  335. default:
  336. level = 3
  337. }
  338. relates, total, err := relateDb.PageFindUserRelateByParentUidAndLevel(utils.StrToInt64(uid), utils.StrToInt(page), utils.StrToInt(limit), level)
  339. if err != nil {
  340. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  341. return
  342. }
  343. userIDs := make([]int64, len(*relates))
  344. for i, relate := range *relates {
  345. userIDs[i] = relate.Uid
  346. }
  347. //2. 查询所属代数
  348. platoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(db.Db)
  349. levelTotals, err := platoonUserRelationDb.PublicPlatoonUserRelationFindByParamsByPage(map[string]interface{}{
  350. "key": "uid",
  351. "value": userIDs,
  352. }, utils.StrToInt(page), utils.StrToInt(limit))
  353. if err != nil {
  354. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  355. return
  356. }
  357. levelTotalMap := make(map[int64]int)
  358. for _, levelTotal := range *levelTotals {
  359. levelTotalMap[levelTotal.Uid] = levelTotal.LevelTotal
  360. }
  361. //3. 查询可提现余额
  362. walletDb := implement.NewUserWalletDb(db.Db)
  363. wallets, err := walletDb.FindUserWalletByParams(map[string]interface{}{
  364. "key": uid,
  365. "value": userIDs,
  366. })
  367. if err != nil {
  368. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  369. return
  370. }
  371. walletMap := make(map[int64]string)
  372. for _, wallet := range *wallets {
  373. walletMap[wallet.Uid] = wallet.Amount
  374. }
  375. //4. 查询累计收益
  376. var incomeMap = map[int64]string{}
  377. if len(*relates) > 0 {
  378. userIDStrs := make([]string, len(*relates))
  379. for i, id := range userIDs {
  380. userIDStrs[i] = utils.Int64ToStr(id)
  381. }
  382. sql := "SELECT SUM(amount) as total, uid FROM `user_wallet_flow` WHERE uid in (%s) AND direction = 1 Group BY uid"
  383. results, err := db.QueryNativeString(db.Db, fmt.Sprintf(sql, strings.Join(userIDStrs, ",")))
  384. if err != nil {
  385. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  386. return
  387. }
  388. for _, res := range results {
  389. incomeMap[utils.StrToInt64(res["uid"])] = res["total"]
  390. }
  391. }
  392. //5. 查询用户信息
  393. userDb := implement.NewUserDb(db.Db)
  394. users, err := userDb.UserFindByParams(map[string]interface{}{
  395. "key": "uid",
  396. "value": userIDs,
  397. })
  398. if err != nil {
  399. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  400. return
  401. }
  402. list := make([]md.FansNode, len(users))
  403. for i, user := range users {
  404. list[i] = md.FansNode{
  405. ID: user.Id,
  406. Nickname: user.Nickname,
  407. Phone: user.Phone,
  408. RegisterAt: user.CreateAt,
  409. }
  410. val, ok := incomeMap[user.Id]
  411. if ok {
  412. list[i].TotalIncome = val
  413. }
  414. levelTotal, ok := levelTotalMap[user.Id]
  415. if ok {
  416. list[i].LevelTotal = levelTotal
  417. }
  418. amount, ok := walletMap[user.Id]
  419. if ok {
  420. list[i].Amount = amount
  421. }
  422. }
  423. resp := md.UserManagementGetFansResp{
  424. List: list,
  425. Paginate: md.Paginate{
  426. Limit: utils.StrToInt(limit),
  427. Page: utils.StrToInt(page),
  428. Total: total,
  429. },
  430. }
  431. e.OutSuc(c, resp, nil)
  432. }
  433. // UserManagementGetBalanceDetail
  434. // @Summary 制度中心-会员中心-用户管理-会员明细(余额获取)
  435. // @Tags 会员中心
  436. // @Description 会员明细(余额获取)
  437. // @Accept json
  438. // @Produce json
  439. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  440. // @Param uid query string true "用户 ID"
  441. // @Param limit query string true "每页大小"
  442. // @Param page query string true "页数"
  443. // @Success 200 {object} md.UserManagementGetBalanceDetailResp "具体数据"
  444. // @Failure 400 {object} md.Response "具体错误"
  445. // @Router /api/memberCenter/userManagement/balanceDetail [get]
  446. func UserManagementGetBalanceDetail(c *gin.Context) {
  447. uid := c.Query("uid")
  448. page := c.DefaultQuery("page", "1")
  449. limit := c.DefaultQuery("limit", "10")
  450. walletFlowDb := implement.NewUserWalletFlowDb(db.Db)
  451. walletFlows, total, err := walletFlowDb.UserWalletFlowFindByParams(map[string]interface{}{
  452. "key": "uid",
  453. "value": uid,
  454. }, utils.StrToInt(page), utils.StrToInt(limit))
  455. if err != nil {
  456. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  457. return
  458. }
  459. kindMap := map[int]string{
  460. 1: "管理员操作增加余额",
  461. 2: "管理员操作扣除余额",
  462. 3: "蛋蛋能量兑换余额",
  463. 4: "余额兑换蛋蛋能量",
  464. }
  465. list := make([]md.BalanceDetailNode, len(*walletFlows))
  466. for i, flow := range *walletFlows {
  467. list[i] = md.BalanceDetailNode{
  468. ID: flow.Id,
  469. Amount: flow.Amount,
  470. AfterAmount: flow.AfterAmount,
  471. Kind: kindMap[flow.Kind],
  472. CreateAt: flow.CreateAt,
  473. }
  474. }
  475. resp := md.UserManagementGetBalanceDetailResp{
  476. List: list,
  477. Paginate: md.Paginate{
  478. Limit: utils.StrToInt(limit),
  479. Page: utils.StrToInt(page),
  480. Total: total,
  481. },
  482. }
  483. e.OutSuc(c, resp, nil)
  484. }
  485. // UserManagementGetVirtualCoinDetail
  486. // @Summary 制度中心-会员中心-用户管理-会员明细(积分明细获取)
  487. // @Tags 会员中心
  488. // @Description 会员明细(积分明细获取)
  489. // @Accept json
  490. // @Produce json
  491. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  492. // @Param uid query string true "用户 ID"
  493. // @Param coinId query string true "货币 ID"
  494. // @Param limit query string true "每页大小"
  495. // @Param page query string true "页数"
  496. // @Success 200 {object} md.UserManagementGetVirtualCoinDetailResp "具体数据"
  497. // @Failure 400 {object} md.Response "具体错误"
  498. // @Router /api/memberCenter/userManagement/getVirtualCoinDetail [get]
  499. func UserManagementGetVirtualCoinDetail(c *gin.Context) {
  500. page := c.DefaultQuery("page", "1")
  501. limit := c.DefaultQuery("limit", "10")
  502. uid := c.Query("uid")
  503. coinID := c.Query("coinId")
  504. coinDb := implement.NewVirtualCoinDb(db.Db)
  505. coins, err := coinDb.VirtualCoinFindAll()
  506. if err != nil {
  507. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  508. return
  509. }
  510. if coins == nil {
  511. e.OutErr(c, e.ERR_NO_DATA, errors.New("未初始化货币"))
  512. return
  513. }
  514. coinsList := make([]map[string]interface{}, len(coins))
  515. coinsMap := map[int]string{}
  516. for i, coin := range coins {
  517. coinsList[i] = map[string]interface{}{
  518. "coinID": coin.Id,
  519. "name": coin.Name,
  520. }
  521. coinsMap[coin.Id] = coin.Name
  522. }
  523. // 传入不存在的货币类型时 默认为第一种货币类型
  524. var reqCoinID int
  525. _, ok := coinsMap[utils.StrToInt(coinID)]
  526. if !ok {
  527. reqCoinID = coinsList[0]["coinID"].(int)
  528. } else {
  529. reqCoinID = utils.StrToInt(coinID)
  530. }
  531. flowDb := implement.NewUserVirtualCoinFlowDb(db.Db)
  532. flows, total, err := flowDb.UserVirtualCoinFlowFindByCoinAndUser(utils.StrToInt(page), utils.StrToInt(limit), reqCoinID, utils.StrToInt64(uid), "", "", 0, false, 0)
  533. if err != nil {
  534. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  535. return
  536. }
  537. list := make([]md.VirtualCoinDetailNode, len(flows))
  538. for i, flow := range flows {
  539. list[i] = md.VirtualCoinDetailNode{
  540. Uid: flow.Uid,
  541. Amount: flow.Amount,
  542. AfterAmount: flow.AfterAmount,
  543. Direction: flow.Direction,
  544. CreateAt: flow.CreateAt,
  545. }
  546. }
  547. resp := md.UserManagementGetVirtualCoinDetailResp{
  548. CoinList: coinsList,
  549. List: list,
  550. Paginate: md.Paginate{
  551. Limit: utils.StrToInt(limit),
  552. Page: utils.StrToInt(page),
  553. Total: total,
  554. },
  555. }
  556. e.OutSuc(c, resp, nil)
  557. }