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

596 lines
17 KiB

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