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

366 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. 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. if len(req.UserLevelTasks) > 0 {
  180. err = svc.BatchAddLevelTask(session, req.UserLevelTasks, utils.StrToInt(req.LevelID))
  181. if err != nil {
  182. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  183. return
  184. }
  185. }
  186. err = session.Commit()
  187. if err != nil {
  188. session.Rollback()
  189. e.OutErr(c, e.ERR_DB_ORM, nil)
  190. return
  191. }
  192. e.OutSuc(c, "success", nil)
  193. }
  194. // AddLevel
  195. // @Summary 制度中心-会员中心-等级管理(新增)
  196. // @Tags 会员中心
  197. // @Description 等级管理(新增)
  198. // @Accept json
  199. // @Produce json
  200. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  201. // @Param req body md.AddLevelReq true "需要新增的等级信息"
  202. // @Success 200 {int} "新增等级ID"
  203. // @Failure 400 {object} md.Response "具体错误"
  204. // @Router /api/memberCenter/levelManagement/addLevel [post]
  205. func AddLevel(c *gin.Context) {
  206. var req *md2.AddLevelReq
  207. if err := c.ShouldBindJSON(&req); err != nil {
  208. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  209. return
  210. }
  211. level := &model.UserLevel{
  212. LevelName: req.LevelName,
  213. Memo: req.Memo,
  214. ChoosableNum: utils.StrToInt(req.ChoosableNum),
  215. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  216. }
  217. if req.LevelWeight != "" {
  218. level.LevelWeight = utils.StrToInt(req.LevelWeight)
  219. }
  220. if req.IsUse == "0" || req.IsUse == "1" {
  221. level.LevelWeight = utils.StrToInt(req.IsUse)
  222. }
  223. if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
  224. level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
  225. }
  226. session := db.Db.NewSession()
  227. defer session.Close()
  228. // 1. 创建等级信息
  229. levelDb := implement.NewUserLevelDb(db.Db)
  230. id, err := levelDb.UserLevelInsertBySession(session, level)
  231. if err != nil {
  232. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  233. return
  234. }
  235. // 2. 创建任务信息
  236. if len(req.UserLevelTasks) > 0 {
  237. err = svc.BatchAddLevelTask(session, req.UserLevelTasks, id)
  238. if err != nil {
  239. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  240. return
  241. }
  242. }
  243. err = session.Commit()
  244. if err != nil {
  245. session.Rollback()
  246. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  247. return
  248. }
  249. e.OutSuc(c, id, nil)
  250. }
  251. // DeleteLevelTask
  252. // @Summary 制度中心-会员中心-等级管理-等级任务(删除)
  253. // @Tags 会员中心
  254. // @Description 等级任务(删除)
  255. // @Accept json
  256. // @Produce json
  257. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  258. // @Param req body md.DeleteTaskReq true "需要删除的等级任务ID"
  259. // @Success 200 {int} "成功删除数据数量"
  260. // @Failure 400 {object} md.Response "具体错误"
  261. // @Router /api/memberCenter/levelManagement/deleteLevelTask [delete]
  262. func DeleteLevelTask(c *gin.Context) {
  263. var req *md2.DeleteTaskReq
  264. if err := c.ShouldBindJSON(&req); err != nil {
  265. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  266. return
  267. }
  268. taskDb := implement.NewUserLevelTaskDb(db.Db)
  269. session := db.Db.Where("")
  270. // 不需要使用事务
  271. affected, err := taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
  272. "key": "id",
  273. "value": req.TaskID,
  274. })
  275. if err != nil {
  276. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  277. return
  278. }
  279. e.OutSuc(c, affected, nil)
  280. }
  281. // DeleteLevel
  282. // @Summary 制度中心-会员中心-等级管理(删除)
  283. // @Tags 会员中心
  284. // @Description 等级管理(删除)
  285. // @Accept json
  286. // @Produce json
  287. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  288. // @Param req body md.DeleteLevelReq true "需要删除的等级 ID"
  289. // @Success 200 {int} "成功删除数据数量"
  290. // @Failure 400 {object} md.Response "具体错误"
  291. // @Router /api/memberCenter/levelManagement/deleteLevel [delete]
  292. func DeleteLevel(c *gin.Context) {
  293. var req *md2.DeleteLevelReq
  294. if err := c.ShouldBindJSON(&req); err != nil {
  295. e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
  296. return
  297. }
  298. userDb := implement.NewUserDb(db.Db)
  299. exist, err := userDb.UserExistByParams(map[string]interface{}{
  300. "key": "level",
  301. "value": req.LevelID,
  302. })
  303. if err != nil {
  304. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  305. return
  306. }
  307. if exist {
  308. e.OutErr(c, e.ERR, errors.New("该等级下存在会员,无法删除,请移除对应等级下会员或将会员移动到其他等级").Error())
  309. return
  310. }
  311. session := db.Db.NewSession()
  312. defer session.Close()
  313. session.Begin()
  314. levelDb := implement.NewUserLevelDb(db.Db)
  315. affected, err := levelDb.UserLevelDeleteBySession(session, req.LevelID)
  316. if err != nil {
  317. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  318. return
  319. }
  320. taskDb := implement.NewUserLevelTaskDb(db.Db)
  321. taskAffected, err := taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
  322. "key": "level_id",
  323. "value": req.LevelID,
  324. })
  325. if err != nil {
  326. return
  327. }
  328. err = session.Commit()
  329. if err != nil {
  330. session.Rollback()
  331. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  332. return
  333. }
  334. affected += taskAffected
  335. e.OutSuc(c, affected, nil)
  336. }