123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package controllers
- import (
- "eta/eta_mini_crm/models"
- "eta/eta_mini_crm/models/request"
- "eta/eta_mini_crm/models/response"
- "eta/eta_mini_crm/services"
- "eta/eta_mini_crm/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strconv"
- "strings"
- )
- // ReportController 报告接口
- type ReportController struct {
- BaseAuthController
- }
- // ClassifyTree
- // @Title 分类树
- // @Description 分类树
- // @Success 200 {object} models.ClassifyItem
- // @router /classify_tree [get]
- func (this *ReportController) ClassifyTree() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- classifyOb := new(models.Classify)
- list, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
- return
- }
- classifies := make([]*models.ClassifyItem, 0)
- for _, v := range list {
- classifies = append(classifies, v.Format2Item())
- }
- classifies = services.GetClassifyTreeRecursive(classifies, 0)
- br.Data = classifies
- br.Ret = 200
- br.Msg = "获取成功"
- br.Success = true
- }
- // ReadRecord
- // @Title 阅读统计
- // @Description 阅读统计
- // @Param request body request.ReadRecordListForm true "type json string"
- // @Success 200 {object} response.ReadRecordListResp
- // @router /read_record [get]
- func (this *ReportController) ReadRecord() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- params := new(request.ReadRecordListForm)
- if e := this.ParseForm(params); e != nil {
- br.Msg = "参数解析异常"
- br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
- return
- }
- if params.UserId <= 0 {
- br.Msg = "参数有误"
- br.ErrMsg = fmt.Sprintf("参数有误, UserId: %d", params.UserId)
- return
- }
- resp := new(response.ReadRecordListResp)
- respList := make([]*models.UserReadRecordItem, 0)
- recordOb := new(models.UserReadRecord)
- // 分页
- var startSize int
- if params.PageSize <= 0 {
- params.PageSize = utils.PageSize20
- }
- if params.CurrentIndex <= 0 {
- params.CurrentIndex = 1
- }
- startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
- // 分类筛选
- cond := fmt.Sprintf(` AND %s = ?`, recordOb.Cols().UserId)
- pars := make([]interface{}, 0)
- pars = append(pars, params.UserId)
- if params.ClassifyIds != "" {
- idArr := strings.Split(params.ClassifyIds, ",")
- var ids []int
- for _, v := range idArr {
- id, _ := strconv.Atoi(v)
- ids = append(ids, id)
- }
- if len(ids) == 0 {
- page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
- resp.Paging = page
- resp.List = respList
- br.Data = resp
- br.Ret = 200
- br.Msg = "获取成功"
- br.Success = true
- return
- }
- classifyOb := new(models.Classify)
- classifyCond := fmt.Sprintf(` AND %s IN (%s)`, classifyOb.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
- classifyPars := make([]interface{}, 0)
- classifyPars = append(classifyPars, ids)
- classifies, e := classifyOb.GetItemsByCondition(classifyCond, classifyPars, []string{}, "")
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取分类列表失败, %v", e)
- return
- }
- // 根据层级
- var firstClassifyIds, secondClassifyIds, thirdClassifyIds []int
- for _, v := range classifies {
- switch v.Level {
- case 1:
- firstClassifyIds = append(firstClassifyIds, v.Id)
- case 2:
- secondClassifyIds = append(secondClassifyIds, v.Id)
- case 3:
- thirdClassifyIds = append(thirdClassifyIds, v.Id)
- }
- }
- var classifyIdsCond string
- if len(firstClassifyIds) > 0 {
- classifyIdsCond += fmt.Sprintf(` %s IN (%s)`, recordOb.Cols().ClassifyIdFirst, utils.GetOrmInReplace(len(firstClassifyIds)))
- pars = append(pars, firstClassifyIds)
- }
- if len(secondClassifyIds) > 0 {
- if classifyIdsCond != "" {
- classifyIdsCond += fmt.Sprintf(` OR %s IN (%s)`, recordOb.Cols().ClassifyIdSecond, utils.GetOrmInReplace(len(secondClassifyIds)))
- } else {
- classifyIdsCond += fmt.Sprintf(` %s IN (%s)`, recordOb.Cols().ClassifyIdSecond, utils.GetOrmInReplace(len(secondClassifyIds)))
- }
- pars = append(pars, secondClassifyIds)
- }
- if len(thirdClassifyIds) > 0 {
- if classifyIdsCond != "" {
- classifyIdsCond += fmt.Sprintf(` OR %s IN (%s)`, recordOb.Cols().ClassifyIdThird, utils.GetOrmInReplace(len(thirdClassifyIds)))
- } else {
- classifyIdsCond += fmt.Sprintf(` %s IN (%s)`, recordOb.Cols().ClassifyIdThird, utils.GetOrmInReplace(len(thirdClassifyIds)))
- }
- pars = append(pars, thirdClassifyIds)
- }
- if classifyIdsCond != "" {
- cond += fmt.Sprintf(` AND (%s)`, classifyIdsCond)
- }
- }
- total, e := recordOb.GetCountByCondition(cond, pars)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取阅读记录计数失败, %v", e)
- return
- }
- list, e := recordOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, params.PageSize)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprintf("获取阅读记录列表失败, %v", e)
- return
- }
- for _, v := range list {
- respList = append(respList, v.Format2Item())
- }
- page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
- resp.Paging = page
- resp.List = respList
- br.Data = resp
- br.Ret = 200
- br.Msg = "获取成功"
- br.Success = true
- }
|