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

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