|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- package member_center
-
- import (
- "applet/app/db"
- "applet/app/e"
- md2 "applet/app/md/member_center"
- svc "applet/app/svc/member_center"
- "applet/app/utils"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
- "errors"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- // GetLevelList
- // @Summary 制度中心-会员中心-等级管理(获取)
- // @Tags 会员中心
- // @Description 标签管理(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param limit query int true "每页大小"
- // @Param page query int true "页数"
- // @Success 200 {object} md.GetLevelListResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/memberCenter/levelManagement/getLevelList [get]
- func GetLevelList(c *gin.Context) {
- page := c.DefaultQuery("page", "1")
- limit := c.DefaultQuery("limit", "10")
-
- list, total, err := svc.LevelManagementGetLevelList(page, limit)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- resp := md2.GetLevelListResp{
- List: list,
- Paginate: md2.Paginate{
- Limit: utils.StrToInt(limit),
- Page: utils.StrToInt(page),
- Total: total,
- },
- }
-
- e.OutSuc(c, resp, nil)
- }
-
- // GetOneLevel
- // @Summary 制度中心-会员中心-等级管理-等级明细(获取)
- // @Tags 会员中心
- // @Description 等级明细(获取)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param level_id query int true "等级 ID"
- // @Success 200 {object} md.GetOneLevelResp "具体数据"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/memberCenter/levelManagement/getOneLevel [get]
- func GetOneLevel(c *gin.Context) {
- id := c.Query("level_id")
- if id == "" {
- e.OutErr(c, e.ERR_INVALID_ARGS, errors.New("必须传入等级ID"))
- return
- }
-
- taskType := []map[string]interface{}{
- {
- "name": enum.UserLevelTaskType.String(enum.DirectPushMembersNum),
- "value": enum.DirectPushMembersNum,
- },
- {
- "name": enum.UserLevelTaskType.String(enum.DirectPushActiveDayNum),
- "value": enum.DirectPushActiveDayNum,
- },
- {
- "name": enum.UserLevelTaskType.String(enum.DirectPushLevel),
- "value": enum.DirectPushLevel,
- },
- }
-
- levelDb := implement.NewUserLevelDb(db.Db)
- level, err := levelDb.UserLevelByID(id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- userLevelTaskDb := implement.NewUserLevelTaskDb(db.Db)
- tasks, err := userLevelTaskDb.UserLevelTaskFindByParams(map[string]interface{}{
- "key": "level_id",
- "value": id,
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- resp := md2.GetOneLevelResp{
- TaskType: taskType,
- LevelID: level.Id,
- IsUse: level.IsUse,
- LevelName: level.LevelName,
- Memo: level.Memo,
- AutoUpdate: level.AutoUpdate,
- ChoosableNum: level.ChoosableNum,
- LevelWeight: level.LevelWeight,
- UserLevelTasks: tasks,
- }
-
- e.OutSuc(c, resp, nil)
- }
-
- // UpdateLevel
- // @Summary 制度中心-会员中心-等级管理(更新)
- // @Tags 会员中心
- // @Description 等级管理(更新)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.UpdateLevelReq true "需要修改的等级信息"
- // @Success 200 {string} "success"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/memberCenter/levelManagement/updateLevel [post]
- func UpdateLevel(c *gin.Context) {
- var req *md2.UpdateLevelReq
- if err := c.ShouldBindJSON(&req); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
- return
- }
- var forceColumns []string
- level := &model.UserLevel{
- Id: utils.StrToInt(req.LevelID),
- LevelName: req.LevelName,
- Memo: req.Memo,
- ChoosableNum: utils.StrToInt(req.ChoosableNum),
- }
- if req.LevelWeight != "" {
- level.LevelWeight = utils.StrToInt(req.LevelWeight)
- forceColumns = append(forceColumns, "level_weight")
- }
- levelDb := implement.NewUserLevelDb(db.Db)
- if req.IsUse == "0" || req.IsUse == "1" {
- // 查询当前等级状态是否被改变
- oldLevel, err := levelDb.UserLevelByID(req.LevelID)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if utils.StrToInt(req.IsUse) != oldLevel.IsUse {
- // 判断当前该等级有没有被使用
- userDb := implement.NewUserDb(db.Db)
- has, err := userDb.UserExistByParams(map[string]interface{}{
- "key": "level",
- "value": req.LevelID,
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if has {
- e.OutErr(c, e.ERR, errors.New("该等级下存在会员,无法关闭该会员等级,请移除对应等级下会员或将会员移动到其他等级").Error())
- return
- }
- }
-
- level.IsUse = utils.StrToInt(req.IsUse)
- forceColumns = append(forceColumns, "is_use")
- }
- if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
- level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
- forceColumns = append(forceColumns, "auto_update")
- }
-
- session := db.Db.NewSession()
- defer session.Close()
-
- // 1. 更新等级信息
- affected, err := levelDb.UserLevelUpdateBySession(session, req.LevelID, level, forceColumns...)
- if err != nil {
- if err.Error() == "no columns found to be updated" {
- e.OutSuc(c, affected, nil)
- return
- }
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- // 2. 更新任务信息
- // 2.1 删除关联任务
- taskDb := implement.NewUserLevelTaskDb(db.Db)
- _, err = taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
- "key": "level_id",
- "value": req.LevelID,
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- // 2.2 添加更新后的任务
- if len(req.UserLevelTasks) > 0 {
- err = svc.BatchAddLevelTask(session, req.UserLevelTasks, utils.StrToInt(req.LevelID))
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- }
-
- err = session.Commit()
- if err != nil {
- session.Rollback()
- e.OutErr(c, e.ERR_DB_ORM, nil)
- return
- }
- e.OutSuc(c, "success", nil)
- }
-
- // AddLevel
- // @Summary 制度中心-会员中心-等级管理(新增)
- // @Tags 会员中心
- // @Description 等级管理(新增)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.AddLevelReq true "需要新增的等级信息"
- // @Success 200 {int} "新增等级ID"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/memberCenter/levelManagement/addLevel [post]
- func AddLevel(c *gin.Context) {
- var req *md2.AddLevelReq
- if err := c.ShouldBindJSON(&req); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
- return
- }
-
- level := &model.UserLevel{
- LevelName: req.LevelName,
- Memo: req.Memo,
- ChoosableNum: utils.StrToInt(req.ChoosableNum),
- CreateAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- if req.LevelWeight != "" {
- level.LevelWeight = utils.StrToInt(req.LevelWeight)
- }
- if req.IsUse == "0" || req.IsUse == "1" {
- level.LevelWeight = utils.StrToInt(req.IsUse)
- }
- if req.AutoUpdate == "0" || req.AutoUpdate == "1" {
- level.AutoUpdate = utils.StrToInt(req.AutoUpdate)
- }
-
- session := db.Db.NewSession()
- defer session.Close()
-
- // 1. 创建等级信息
- levelDb := implement.NewUserLevelDb(db.Db)
- id, err := levelDb.UserLevelInsertBySession(session, level)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- // 2. 创建任务信息
- if len(req.UserLevelTasks) > 0 {
- err = svc.BatchAddLevelTask(session, req.UserLevelTasks, id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- }
-
- err = session.Commit()
- if err != nil {
- session.Rollback()
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, id, nil)
- }
-
- // DeleteLevelTask
- // @Summary 制度中心-会员中心-等级管理-等级任务(删除)
- // @Tags 会员中心
- // @Description 等级任务(删除)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.DeleteTaskReq true "需要删除的等级任务ID"
- // @Success 200 {int} "成功删除数据数量"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/memberCenter/levelManagement/deleteLevelTask [delete]
- func DeleteLevelTask(c *gin.Context) {
- var req *md2.DeleteTaskReq
- if err := c.ShouldBindJSON(&req); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
- return
- }
-
- taskDb := implement.NewUserLevelTaskDb(db.Db)
- session := db.Db.Where("")
- // 不需要使用事务
- affected, err := taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
- "key": "id",
- "value": req.TaskID,
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, affected, nil)
- }
-
- // DeleteLevel
- // @Summary 制度中心-会员中心-等级管理(删除)
- // @Tags 会员中心
- // @Description 等级管理(删除)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.DeleteLevelReq true "需要删除的等级 ID"
- // @Success 200 {int} "成功删除数据数量"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/memberCenter/levelManagement/deleteLevel [delete]
- func DeleteLevel(c *gin.Context) {
- var req *md2.DeleteLevelReq
- if err := c.ShouldBindJSON(&req); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
- return
- }
-
- userDb := implement.NewUserDb(db.Db)
- exist, err := userDb.UserExistByParams(map[string]interface{}{
- "key": "level",
- "value": req.LevelID,
- })
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if exist {
- e.OutErr(c, e.ERR, errors.New("该等级下存在会员,无法删除,请移除对应等级下会员或将会员移动到其他等级").Error())
- return
- }
-
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
- levelDb := implement.NewUserLevelDb(db.Db)
- affected, err := levelDb.UserLevelDeleteBySession(session, req.LevelID)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- taskDb := implement.NewUserLevelTaskDb(db.Db)
- taskAffected, err := taskDb.UserLevelTaskDeleteByParamsBySession(session, map[string]interface{}{
- "key": "level_id",
- "value": req.LevelID,
- })
- if err != nil {
- return
- }
- err = session.Commit()
- if err != nil {
- session.Rollback()
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- affected += taskAffected
- e.OutSuc(c, affected, nil)
- }
|