123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package controllers
- import (
- "eta/eta_mobile/models"
- "eta/eta_mobile/utils"
- )
- // 分类
- type ClassifyController struct {
- BaseAuthController
- }
- // @Title 获取分类列表
- // @Description 获取分类列表
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param KeyWord query string true "检索关键词"
- // @Param CompanyType query string false "产品类型,枚举值:'ficc','权益';不传默认返回全部"
- // @Param HideDayWeek query int false "是否隐藏晨周报"
- // @Success 200 {object} models.Classify
- // @router /list [get]
- func (this *ClassifyController) ListClassify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- keyWord := this.GetString("KeyWord")
- companyType := this.GetString("CompanyType")
- hideDayWeek, _ := this.GetInt("HideDayWeek")
- reqEnabled, _ := this.GetInt("Enabled", -1)
- // 商家不隐藏晨周报
- if utils.BusinessCode != utils.BusinessCodeRelease {
- hideDayWeek = 0
- }
- enabled := -1
- if reqEnabled == 1 {
- enabled = reqEnabled
- }
- list, err := models.GetClassifyList(keyWord, companyType, hideDayWeek, enabled)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- parentIds := make([]int, 0)
- for i := range list {
- parentIds = append(parentIds, list[i].Id)
- }
- parentIdLen := len(parentIds)
- if parentIdLen == 0 {
- resp := &models.ClassifyListResp{
- List: list,
- }
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- // 获取一级分类-子目录列表
- menuListMap := make(map[int][]*models.ClassifyMenu, 0)
- var menuCond string
- var menuPars []interface{}
- menuCond += ` AND classify_id IN (` + utils.GetOrmInReplace(parentIdLen) + `)`
- menuPars = append(menuPars, parentIds)
- parentMenus, e := models.GetClassifyMenuList(menuCond, menuPars)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取一级分类子目录列表失败"
- return
- }
- for i := range parentMenus {
- if menuListMap[parentMenus[i].ClassifyId] == nil {
- menuListMap[parentMenus[i].ClassifyId] = make([]*models.ClassifyMenu, 0)
- }
- menuListMap[parentMenus[i].ClassifyId] = append(menuListMap[parentMenus[i].ClassifyId], parentMenus[i])
- }
- // 获取子分类
- children, e := models.GetClassifyChildByParentIds(parentIds, keyWord, enabled)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取子分类失败"
- return
- }
- childrenIds := make([]int, 0)
- for i := range children {
- childrenIds = append(childrenIds, children[i].Id)
- }
- childrenIdsLen := len(childrenIds)
- // 获取二级分类-子目录关联
- relateMap := make(map[int]int, 0)
- if childrenIdsLen > 0 {
- var relateCond string
- var relatePars []interface{}
- relateCond += ` AND classify_id IN (` + utils.GetOrmInReplace(childrenIdsLen) + `)`
- relatePars = append(relatePars, childrenIds)
- relates, e := models.GetClassifyMenuRelationList(relateCond, relatePars)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取二级分类子目录关联失败, Err: " + e.Error()
- return
- }
- for i := range relates {
- relateMap[relates[i].ClassifyId] = relates[i].MenuId
- }
- }
- // 二级分类
- childrenMap := make(map[int][]*models.ClassifyItem, 0)
- for i := range children {
- if childrenMap[children[i].ParentId] == nil {
- childrenMap[children[i].ParentId] = make([]*models.ClassifyItem, 0)
- }
- tmp := &models.ClassifyItem{
- Classify: *children[i],
- ClassifyMenuId: relateMap[children[i].Id],
- }
- childrenMap[children[i].ParentId] = append(childrenMap[children[i].ParentId], tmp)
- }
- // 一级分类
- for i := range list {
- list[i].ClassifyMenuList = menuListMap[list[i].Id]
- list[i].Child = childrenMap[list[i].Id]
- }
- resp := new(models.ClassifyListResp)
- resp.List = list
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
|