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

370 lines
11 KiB

  1. package member_center
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. md2 "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. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  11. "errors"
  12. "github.com/gin-gonic/gin"
  13. "time"
  14. )
  15. // GetLevelList
  16. // @Summary 制度中心-会员中心-等级管理(获取)
  17. // @Tags 会员中心
  18. // @Description 标签管理(获取)
  19. // @Accept json
  20. // @Produce json
  21. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  22. // @Param limit query int true "每页大小"
  23. // @Param page query int true "页数"
  24. // @Success 200 {object} md.GetLevelListResp "具体数据"
  25. // @Failure 400 {object} md.Response "具体错误"
  26. // @Router /api/memberCenter/levelManagement/getLevelList [get]
  27. func GetLevelList(c *gin.Context) {
  28. page := c.DefaultQuery("page", "1")
  29. limit := c.DefaultQuery("limit", "10")
  30. levelDb := implement.NewUserLevelDb(db.Db)
  31. levels, total, err := levelDb.UserLevelAllByAscByPage(utils.StrToInt(page), utils.StrToInt(limit))
  32. if err != nil {
  33. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  34. return
  35. }
  36. sql := "SElECT count(distinct id) as total, level FROM `user` GROUP BY level"
  37. results, err := db.QueryNativeString(db.Db, sql)
  38. if err != nil {
  39. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  40. return
  41. }
  42. var countMap = map[int]string{}
  43. for _, res := range results {
  44. countMap[utils.StrToInt(res["level"])] = res["total"]
  45. }
  46. list := make([]md2.LevelListNode, len(levels))
  47. for i, level := range levels {
  48. list[i].LevelWeight = level.LevelWeight
  49. list[i].LevelID = level.Id
  50. list[i].LevelName = level.LevelName
  51. list[i].Count = countMap[level.Id]
  52. list[i].CreateAt = level.CreateAt
  53. list[i].IsUse = level.IsUse
  54. list[i].Memo = level.Memo
  55. }
  56. resp := md2.GetLevelListResp{
  57. List: list,
  58. Paginate: md2.Paginate{
  59. Limit: utils.StrToInt(limit),
  60. Page: utils.StrToInt(page),
  61. Total: total,
  62. },
  63. }
  64. e.OutSuc(c, resp, nil)
  65. }
  66. // GetOneLevel
  67. // @Summary 制度中心-会员中心-等级管理-等级明细(获取)
  68. // @Tags 会员中心
  69. // @Description 等级明细(获取)
  70. // @Accept json
  71. // @Produce json
  72. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  73. // @Param level_id query int true "等级 ID"
  74. // @Success 200 {object} md.GetOneLevelResp "具体数据"
  75. // @Failure 400 {object} md.Response "具体错误"
  76. // @Router /api/memberCenter/levelManagement/getOneLevel [get]
  77. func GetOneLevel(c *gin.Context) {
  78. id := c.Query("level_id")
  79. if id == "" {
  80. e.OutErr(c, e.ERR_INVALID_ARGS, errors.New("必须传入等级ID"))
  81. return
  82. }
  83. taskType := []map[string]interface{}{
  84. {
  85. "name": enum.UserLevelTaskType.String(enum.DirectPushMembersNum),
  86. "value": enum.DirectPushMembersNum,
  87. },
  88. {
  89. "name": enum.UserLevelTaskType.String(enum.DirectPushActiveDayNum),
  90. "value": enum.DirectPushActiveDayNum,
  91. },
  92. {
  93. "name": enum.UserLevelTaskType.String(enum.DirectPushLevel),
  94. "value": enum.DirectPushLevel,
  95. },
  96. }
  97. levelDb := implement.NewUserLevelDb(db.Db)
  98. level, err := levelDb.UserLevelByID(id)
  99. if err != nil {
  100. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  101. return
  102. }
  103. userLevelTaskDb := implement.NewUserLevelTaskDb(db.Db)
  104. tasks, err := userLevelTaskDb.UserLevelTaskFindByParams(map[string]interface{}{
  105. "key": "level_id",
  106. "value": id,
  107. })
  108. if err != nil {
  109. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  110. return
  111. }
  112. resp := md2.GetOneLevelResp{
  113. TaskType: taskType,
  114. LevelID: level.Id,
  115. IsUse: level.IsUse,
  116. LevelName: level.LevelName,
  117. Memo: level.Memo,
  118. AutoUpdate: level.AutoUpdate,
  119. ChoosableNum: level.ChoosableNum,
  120. LevelWeight: level.LevelWeight,
  121. UserLevelTasks: tasks,
  122. }
  123. e.OutSuc(c, resp, nil)
  124. }
  125. // UpdateLevel
  126. // @Summary 制度中心-会员中心-等级管理(更新)
  127. // @Tags 会员中心
  128. // @Description 等级管理(更新)
  129. // @Accept json
  130. // @Produce json
  131. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  132. // @Param req body md.UpdateLevelReq true "需要修改的等级信息"
  133. // @Success 200 {string} "success"
  134. // @Failure 400 {object} md.Response "具体错误"
  135. // @Router /api/memberCenter/levelManagement/updateLevel [post]
  136. func UpdateLevel(c *gin.Context) {
  137. var req *md2.UpdateLevelReq
  138. if err := c.ShouldBindJSON(&req); err != nil {
  139. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  140. return
  141. }
  142. var forceColumns []string
  143. level := &model.UserLevel{
  144. Id: utils.StrToInt(req.LevelID),
  145. LevelName: req.LevelName,
  146. Memo: req.Memo,
  147. ChoosableNum: utils.StrToInt(req.ChoosableNum),
  148. }
  149. if req.LevelWeight != "" {
  150. level.LevelWeight = utils.StrToInt(req.LevelWeight)
  151. forceColumns = append(forceColumns, "level_weight")
  152. }
  153. if req.IsUse == "0" || req.IsUse == "1" {
  154. level.LevelWeight = utils.StrToInt(req.IsUse)
  155. forceColumns = append(forceColumns, "is_use")
  156. }
  157. if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
  158. level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
  159. forceColumns = append(forceColumns, "auto_update")
  160. }
  161. session := db.Db.NewSession()
  162. defer session.Close()
  163. // 1. 更新等级信息
  164. levelDb := implement.NewUserLevelDb(db.Db)
  165. affected, err := levelDb.UserLevelUpdateBySession(session, req.LevelID, level, forceColumns...)
  166. if err != nil {
  167. if err.Error() == "no columns found to be updated" {
  168. e.OutSuc(c, affected, nil)
  169. return
  170. }
  171. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  172. return
  173. }
  174. // 2. 更新任务信息
  175. // 2.1 删除关联任务
  176. taskDb := implement.NewUserLevelTaskDb(db.Db)
  177. _, err = taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
  178. "key": "level_id",
  179. "value": req.LevelID,
  180. })
  181. if err != nil {
  182. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  183. return
  184. }
  185. // 2.2 添加更新后的任务
  186. err = svc.BatchAddLevelTask(session, req.UserLevelTasks, utils.StrToInt(req.LevelID))
  187. if err != nil {
  188. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  189. return
  190. }
  191. err = session.Commit()
  192. if err != nil {
  193. session.Rollback()
  194. e.OutErr(c, e.ERR_DB_ORM, nil)
  195. return
  196. }
  197. e.OutSuc(c, "success", nil)
  198. }
  199. // AddLevel
  200. // @Summary 制度中心-会员中心-等级管理(新增)
  201. // @Tags 会员中心
  202. // @Description 等级管理(新增)
  203. // @Accept json
  204. // @Produce json
  205. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  206. // @Param req body md.AddLevelReq true "需要新增的等级信息"
  207. // @Success 200 {int} "新增等级ID"
  208. // @Failure 400 {object} md.Response "具体错误"
  209. // @Router /api/memberCenter/levelManagement/addLevel [post]
  210. func AddLevel(c *gin.Context) {
  211. var req *md2.AddLevelReq
  212. if err := c.ShouldBindJSON(&req); err != nil {
  213. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  214. return
  215. }
  216. level := &model.UserLevel{
  217. LevelName: req.LevelName,
  218. Memo: req.Memo,
  219. ChoosableNum: utils.StrToInt(req.ChoosableNum),
  220. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  221. }
  222. if req.LevelWeight != "" {
  223. level.LevelWeight = utils.StrToInt(req.LevelWeight)
  224. }
  225. if req.IsUse == "0" || req.IsUse == "1" {
  226. level.LevelWeight = utils.StrToInt(req.IsUse)
  227. }
  228. if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
  229. level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
  230. }
  231. session := db.Db.NewSession()
  232. defer session.Close()
  233. // 1. 创建等级信息
  234. levelDb := implement.NewUserLevelDb(db.Db)
  235. id, err := levelDb.UserLevelInsertBySession(session, level)
  236. if err != nil {
  237. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  238. return
  239. }
  240. // 2. 创建任务信息
  241. err = svc.BatchAddLevelTask(session, req.UserLevelTasks, id)
  242. if err != nil {
  243. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  244. return
  245. }
  246. err = session.Commit()
  247. if err != nil {
  248. session.Rollback()
  249. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  250. return
  251. }
  252. e.OutSuc(c, id, nil)
  253. }
  254. // DeleteLevelTask
  255. // @Summary 制度中心-会员中心-等级管理-等级任务(删除)
  256. // @Tags 会员中心
  257. // @Description 等级任务(删除)
  258. // @Accept json
  259. // @Produce json
  260. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  261. // @Param req body md.DeleteTaskReq true "需要删除的等级任务ID"
  262. // @Success 200 {int} "成功删除数据数量"
  263. // @Failure 400 {object} md.Response "具体错误"
  264. // @Router /api/memberCenter/levelManagement/deleteLevelTask [delete]
  265. func DeleteLevelTask(c *gin.Context) {
  266. var req *md2.DeleteTaskReq
  267. if err := c.ShouldBindJSON(&req); err != nil {
  268. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  269. return
  270. }
  271. taskDb := implement.NewUserLevelTaskDb(db.Db)
  272. session := db.Db.Where("")
  273. // 不需要使用事务
  274. affected, err := taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
  275. "key": "id",
  276. "value": req.TaskID,
  277. })
  278. if err != nil {
  279. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  280. return
  281. }
  282. e.OutSuc(c, affected, nil)
  283. }
  284. // DeleteLevel
  285. // @Summary 制度中心-会员中心-等级管理(删除)
  286. // @Tags 会员中心
  287. // @Description 等级管理(删除)
  288. // @Accept json
  289. // @Produce json
  290. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  291. // @Param req body md.DeleteLevelReq true "需要删除的等级 ID"
  292. // @Success 200 {int} "成功删除数据数量"
  293. // @Failure 400 {object} md.Response "具体错误"
  294. // @Router /api/memberCenter/levelManagement/deleteLevel [delete]
  295. func DeleteLevel(c *gin.Context) {
  296. var req *md2.DeleteLevelReq
  297. if err := c.ShouldBindJSON(&req); err != nil {
  298. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  299. return
  300. }
  301. userDb := implement.NewUserDb(db.Db)
  302. exist, err := userDb.UserExistByParams(map[string]interface{}{
  303. "key": "level",
  304. "value": req.LevelID,
  305. })
  306. if err != nil {
  307. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  308. return
  309. }
  310. if exist {
  311. e.OutErr(c, e.ERR, errors.New("该等级下存在会员,无法删除,请移除对应等级下会员或将会员移动到其他等级").Error())
  312. return
  313. }
  314. session := db.Db.NewSession()
  315. defer session.Close()
  316. session.Begin()
  317. levelDb := implement.NewUserLevelDb(db.Db)
  318. affected, err := levelDb.UserLevelDeleteBySession(session, req.LevelID)
  319. if err != nil {
  320. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  321. return
  322. }
  323. taskDb := implement.NewUserLevelTaskDb(db.Db)
  324. taskAffected, err := taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
  325. "key": "level_id",
  326. "value": req.LevelID,
  327. })
  328. if err != nil {
  329. return
  330. }
  331. err = session.Commit()
  332. if err != nil {
  333. session.Rollback()
  334. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  335. return
  336. }
  337. affected += taskAffected
  338. e.OutSuc(c, affected, nil)
  339. }