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

374 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. 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. levelDb := implement.NewUserLevelDb(db.Db)
  133. if req.IsUse == "0" || req.IsUse == "1" {
  134. // 查询当前等级状态是否被改变
  135. oldLevel, err := levelDb.UserLevelByID(req.LevelID)
  136. if err != nil {
  137. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  138. return
  139. }
  140. if utils.StrToInt(req.IsUse) != oldLevel.IsUse {
  141. // 判断当前该等级有没有被使用
  142. userDb := implement.NewUserDb(db.Db)
  143. has, err := userDb.UserExistByParams(map[string]interface{}{
  144. "key": "level",
  145. "value": req.LevelID,
  146. })
  147. if err != nil {
  148. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  149. return
  150. }
  151. if has {
  152. e.OutErr(c, e.ERR, errors.New("该等级下存在会员,无法关闭该会员等级,请移除对应等级下会员或将会员移动到其他等级").Error())
  153. return
  154. }
  155. }
  156. level.IsUse = utils.StrToInt(req.IsUse)
  157. forceColumns = append(forceColumns, "is_use")
  158. }
  159. if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
  160. level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
  161. forceColumns = append(forceColumns, "auto_update")
  162. }
  163. session := db.Db.NewSession()
  164. defer session.Close()
  165. // 1. 更新等级信息
  166. affected, err := levelDb.UserLevelUpdateBySession(session, req.LevelID, level, forceColumns...)
  167. if err != nil {
  168. if err.Error() == "no columns found to be updated" {
  169. e.OutSuc(c, affected, nil)
  170. return
  171. }
  172. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  173. return
  174. }
  175. // 2. 更新任务信息
  176. // 2.1 删除关联任务
  177. taskDb := implement.NewUserLevelTaskDb(db.Db)
  178. _, err = taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
  179. "key": "level_id",
  180. "value": req.LevelID,
  181. })
  182. if err != nil {
  183. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  184. return
  185. }
  186. // 2.2 添加更新后的任务
  187. if len(req.UserLevelTasks) > 0 {
  188. err = svc.BatchAddLevelTask(session, req.UserLevelTasks, utils.StrToInt(req.LevelID))
  189. if err != nil {
  190. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  191. return
  192. }
  193. }
  194. err = session.Commit()
  195. if err != nil {
  196. session.Rollback()
  197. e.OutErr(c, e.ERR_DB_ORM, nil)
  198. return
  199. }
  200. e.OutSuc(c, "success", nil)
  201. }
  202. // AddLevel
  203. // @Summary 制度中心-会员中心-等级管理(新增)
  204. // @Tags 会员中心
  205. // @Description 等级管理(新增)
  206. // @Accept json
  207. // @Produce json
  208. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  209. // @Param req body md.AddLevelReq true "需要新增的等级信息"
  210. // @Success 200 {int} "新增等级ID"
  211. // @Failure 400 {object} md.Response "具体错误"
  212. // @Router /api/memberCenter/levelManagement/addLevel [post]
  213. func AddLevel(c *gin.Context) {
  214. var req *md2.AddLevelReq
  215. if err := c.ShouldBindJSON(&req); err != nil {
  216. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  217. return
  218. }
  219. level := &model.UserLevel{
  220. LevelName: req.LevelName,
  221. Memo: req.Memo,
  222. ChoosableNum: utils.StrToInt(req.ChoosableNum),
  223. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  224. }
  225. if req.LevelWeight != "" {
  226. level.LevelWeight = utils.StrToInt(req.LevelWeight)
  227. }
  228. if req.IsUse == "0" || req.IsUse == "1" {
  229. level.LevelWeight = utils.StrToInt(req.IsUse)
  230. }
  231. if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
  232. level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
  233. }
  234. session := db.Db.NewSession()
  235. defer session.Close()
  236. // 1. 创建等级信息
  237. levelDb := implement.NewUserLevelDb(db.Db)
  238. id, err := levelDb.UserLevelInsertBySession(session, level)
  239. if err != nil {
  240. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  241. return
  242. }
  243. // 2. 创建任务信息
  244. if len(req.UserLevelTasks) > 0 {
  245. err = svc.BatchAddLevelTask(session, req.UserLevelTasks, id)
  246. if err != nil {
  247. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  248. return
  249. }
  250. }
  251. err = session.Commit()
  252. if err != nil {
  253. session.Rollback()
  254. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  255. return
  256. }
  257. e.OutSuc(c, id, nil)
  258. }
  259. // DeleteLevelTask
  260. // @Summary 制度中心-会员中心-等级管理-等级任务(删除)
  261. // @Tags 会员中心
  262. // @Description 等级任务(删除)
  263. // @Accept json
  264. // @Produce json
  265. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  266. // @Param req body md.DeleteTaskReq true "需要删除的等级任务ID"
  267. // @Success 200 {int} "成功删除数据数量"
  268. // @Failure 400 {object} md.Response "具体错误"
  269. // @Router /api/memberCenter/levelManagement/deleteLevelTask [delete]
  270. func DeleteLevelTask(c *gin.Context) {
  271. var req *md2.DeleteTaskReq
  272. if err := c.ShouldBindJSON(&req); err != nil {
  273. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  274. return
  275. }
  276. taskDb := implement.NewUserLevelTaskDb(db.Db)
  277. session := db.Db.Where("")
  278. // 不需要使用事务
  279. affected, err := taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
  280. "key": "id",
  281. "value": req.TaskID,
  282. })
  283. if err != nil {
  284. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  285. return
  286. }
  287. e.OutSuc(c, affected, nil)
  288. }
  289. // DeleteLevel
  290. // @Summary 制度中心-会员中心-等级管理(删除)
  291. // @Tags 会员中心
  292. // @Description 等级管理(删除)
  293. // @Accept json
  294. // @Produce json
  295. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  296. // @Param req body md.DeleteLevelReq true "需要删除的等级 ID"
  297. // @Success 200 {int} "成功删除数据数量"
  298. // @Failure 400 {object} md.Response "具体错误"
  299. // @Router /api/memberCenter/levelManagement/deleteLevel [delete]
  300. func DeleteLevel(c *gin.Context) {
  301. var req *md2.DeleteLevelReq
  302. if err := c.ShouldBindJSON(&req); err != nil {
  303. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  304. return
  305. }
  306. userDb := implement.NewUserDb(db.Db)
  307. exist, err := userDb.UserExistByParams(map[string]interface{}{
  308. "key": "level",
  309. "value": req.LevelID,
  310. })
  311. if err != nil {
  312. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  313. return
  314. }
  315. if exist {
  316. e.OutErr(c, e.ERR, errors.New("该等级下存在会员,无法删除,请移除对应等级下会员或将会员移动到其他等级").Error())
  317. return
  318. }
  319. session := db.Db.NewSession()
  320. defer session.Close()
  321. session.Begin()
  322. levelDb := implement.NewUserLevelDb(db.Db)
  323. affected, err := levelDb.UserLevelDeleteBySession(session, req.LevelID)
  324. if err != nil {
  325. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  326. return
  327. }
  328. taskDb := implement.NewUserLevelTaskDb(db.Db)
  329. taskAffected, err := taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
  330. "key": "level_id",
  331. "value": req.LevelID,
  332. })
  333. if err != nil {
  334. return
  335. }
  336. err = session.Commit()
  337. if err != nil {
  338. session.Rollback()
  339. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  340. return
  341. }
  342. affected += taskAffected
  343. e.OutSuc(c, affected, nil)
  344. }