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

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