|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- package member_center
-
- import (
- "applet/app/db"
- "applet/app/e"
- md2 "applet/app/md/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")
-
- levelDb := implement.NewUserLevelDb(db.Db)
- levels, total, err := levelDb.UserLevelAllByAscByPage(utils.StrToInt(page), utils.StrToInt(limit))
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- sql := "SElECT count(distinct id) as total, level FROM `user` GROUP BY level"
- results, err := db.QueryNativeString(db.Db, sql)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- var countMap = map[int]string{}
- for _, res := range results {
- countMap[utils.StrToInt(res["level"])] = res["total"]
- }
-
- list := make([]md2.LevelListNode, len(levels))
- for i, level := range levels {
- list[i].LevelWeight = level.LevelWeight
- list[i].LevelID = level.Id
- list[i].LevelName = level.LevelName
- list[i].Count = countMap[level.Id]
- list[i].CreateAt = level.CreateAt
- list[i].IsUse = level.IsUse
- list[i].Memo = level.Memo
- }
-
- 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.DirectPushMembersNum,
- },
- {
- "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)
- }
-
- // AddLevelTask
- // @Summary 制度中心-会员中心-等级明细-等级任务(新增)
- // @Tags 会员中心
- // @Description 等级任务(新增)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.AddLevelTaskReq true "需要新增的任务信息"
- // @Success 200 {int} "新增的任务ID"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/memberCenter/levelManagement/addLevelTask [post]
- func AddLevelTask(c *gin.Context) {
- var req *md2.AddLevelTaskReq
- if err := c.ShouldBindJSON(&req); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
- return
- }
-
- task := model.UserLevelTask{
- Id: 0,
- LevelId: utils.StrToInt(req.LevelID),
- CreateAt: time.Now().Format("2006-01-02 15:04:05"),
- }
- if req.IsMustTask != "" {
- task.IsMustTask = utils.StrToInt(req.IsMustTask)
- }
- if req.TaskType != "" {
- task.TaskType = utils.StrToInt(req.TaskType)
- }
- if req.WithinDays != "" {
- task.WithinDays = utils.StrToInt(req.WithinDays)
- }
- if req.FinishCount != "" {
- task.FinishCount = utils.StrToInt(req.FinishCount)
- }
- if req.TaskTypeLevelId != "" {
- task.TaskTypeLevelId = utils.StrToInt(req.TaskTypeLevelId)
- }
-
- taskDb := implement.NewUserLevelTaskDb(db.Db)
- taskID, err := taskDb.UserLevelTaskInsert(&task)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, taskID, nil)
- }
-
- // UpdateLevelTask
- // @Summary 制度中心-会员中心-等级明细-等级任务(更新)
- // @Tags 会员中心
- // @Description 等级任务(更新)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.UpdateLevelTaskReq true "需要修改的等级任务信息"
- // @Success 200 {int} "成功修改数据条数"
- // @Failure 400 {object} md.Response "具体错误"
- // @Router /api/memberCenter/levelManagement/updateLevelTask [post]
- func UpdateLevelTask(c *gin.Context) {
- var req *md2.UpdateLevelTaskReq
- if err := c.ShouldBindJSON(&req); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err.Error())
- return
- }
-
- task := &model.UserLevelTask{
- Id: utils.StrToInt(req.TaskID),
- }
- if req.IsMustTask != "" {
- task.IsMustTask = utils.StrToInt(req.IsMustTask)
- }
- if req.TaskType != "" {
- task.TaskType = utils.StrToInt(req.TaskType)
- }
- if req.WithinDays != "" {
- task.WithinDays = utils.StrToInt(req.WithinDays)
- }
- if req.FinishCount != "" {
- task.FinishCount = utils.StrToInt(req.FinishCount)
- }
- if req.TaskTypeLevelId != "" {
- task.TaskTypeLevelId = utils.StrToInt(req.TaskTypeLevelId)
- }
-
- taskDb := implement.NewUserLevelTaskDb(db.Db)
- affected, err := taskDb.UserLevelTaskUpdate(task)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, affected, nil)
- }
-
- // UpdateLevel
- // @Summary 制度中心-会员中心-等级管理(更新)
- // @Tags 会员中心
- // @Description 等级管理(更新)
- // @Accept json
- // @Produce json
- // @param Authorization header string true "验证参数Bearer和token空格拼接"
- // @Param req body md.UpdateLevelReq true "需要修改的等级信息"
- // @Success 200 {int} "成功修改数据条数"
- // @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
- }
-
- 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)
- }
- 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)
- }
- forceColumns := []string{"level_weight", "is_use"}
- levelDb := implement.NewUserLevelDb(db.Db)
- affected, err := levelDb.UserLevelUpdate(req.LevelID, level, forceColumns...)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- e.OutSuc(c, affected, 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)
- }
-
- levelDb := implement.NewUserLevelDb(db.Db)
- id, err := levelDb.UserLevelInsert(level)
- if err != nil {
- 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)
- affected, err := taskDb.UserLevelTaskDelete(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
- }
-
- 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
- }
-
- sql := "UPDATE `user` SET level = 0 WHERE level = ?"
- _, err = session.QueryString(sql, req.LevelID)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- session.Commit()
- e.OutSuc(c, affected, nil)
- }
|