123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- package controllers
- import (
- "encoding/json"
- "errors"
- "eta/eta_forum_admin/models"
- "eta/eta_forum_admin/services"
- "eta/eta_forum_admin/services/area_graph"
- "eta/eta_forum_admin/utils"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strconv"
- "strings"
- "time"
- )
- type ChartInfoController struct {
- BaseAuthController
- }
- // ChartInfoDetail
- // @Title 获取图表详情
- // @Description 获取图表详情接口
- // @Param ChartInfoId query int true "图表id"
- // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
- // @Param StartDate query string true "自定义开始日期"
- // @Param EndDate query string true "自定义结束日期"
- // @Param Calendar query string true "公历/农历"
- // @Param SeasonStartDate query string true "季节性图开始日期"
- // @Param SeasonEndDate query string true "季节性图结束日期"
- // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
- // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
- // @Success 200 {object} models.ChartInfoDetailResp
- // @router /detail [get]
- func (this *ChartInfoController) ChartInfoDetail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- chartInfoId, _ := this.GetInt("ChartInfoId")
- var err error
- chartInfo := new(models.ChartInfoView)
- chartInfo.HaveOperaAuth = true
- if chartInfoId <= 0 {
- br.Msg = "请选择图表"
- return
- }
- chartInfo, err = models.GetChartInfoViewById(chartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "该图表已删除,自动查看下一图表"
- br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
- br.Ret = 406
- return
- }
- br.Msg = "获取失败"
- br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
- return
- }
- chartType := chartInfo.ChartType
- startDate := chartInfo.StartDate
- endDate := chartInfo.EndDate
- seasonStartDate := chartInfo.SeasonStartDate
- seasonEndDate := chartInfo.SeasonEndDate
- startYear := chartInfo.StartYear
- calendar := this.GetString("Calendar")
- if calendar == "" {
- calendar = chartInfo.Calendar
- }
- if calendar == "" {
- calendar = "公历"
- }
- dateType := chartInfo.DateType
- // 获取主题样式
- chartTheme, err := services.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
- return
- }
- chartInfo.ChartThemeStyle = chartTheme.Config
- chartInfo.ChartThemeId = chartTheme.ChartThemeId
- resp := new(models.ChartInfoDetailResp)
- mappingList := make([]*models.ChartEdbInfoMapping, 0)
- mappingList, err = models.GetChartEdbMappingList(chartInfoId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
- return
- }
- // 图表额外数据参数
- extraConfigStr := chartInfo.ExtraConfig
- // 柱方图的一些配置
- var barConfig models.BarChartInfoReq
- if chartInfo != nil && chartInfo.ChartType == 7 {
- if chartInfo.BarConfig == `` {
- br.Msg = "柱方图未配置"
- br.ErrMsg = "柱方图未配置"
- return
- }
- err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
- if err != nil {
- br.Msg = "柱方图配置异常"
- br.ErrMsg = "柱方图配置异常"
- return
- }
- extraConfigStr = chartInfo.BarConfig
- }
- if chartType == 2 {
- startDate = seasonStartDate
- endDate = seasonEndDate
- if dateType <= 0 {
- if startDate != "" {
- dateType = 5
- } else {
- dateType = utils.DateTypeNYears
- }
- }
- } else {
- if dateType <= 0 {
- dateType = 3
- }
- }
- yearMax := 0
- if dateType == utils.DateTypeNYears {
- for _, v := range mappingList {
- if v.LatestDate != "" {
- lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
- if tErr != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
- return
- }
- if lastDateT.Year() > yearMax {
- yearMax = lastDateT.Year()
- }
- }
- }
- }
- startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
- if chartInfo.ChartType == 2 {
- chartInfo.StartDate = startDate
- chartInfo.EndDate = endDate
- chartInfo.Calendar = calendar
- }
- // 获取图表中的指标数据
- edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := services.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
- if err != nil {
- br.Msg = "获取失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
- return
- }
- // 单位
- if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
- chartInfo.Unit = yDataList[0].Unit
- chartInfo.UnitEn = yDataList[0].UnitEn
- }
- warnEdbList := make([]string, 0)
- for _, v := range edbList {
- if v.IsNullData {
- warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
- }
- }
- if len(warnEdbList) > 0 {
- chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
- }
- // 面积图 面积堆积 数据处理
- if chartInfo.ChartType == utils.CHART_TYPE_AREA {
- err, errMsg = fillAreaGraphData(extraConfigStr, edbList)
- if err != nil {
- br.Msg = "面积图处理失败"
- br.ErrMsg = errMsg
- return
- }
- }
- // 图表的指标来源
- sourceNameList, sourceNameEnList := services.GetEdbSourceByEdbInfoIdList(edbList)
- chartInfo.ChartSource = strings.Join(sourceNameList, ",")
- chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
- //判断是否需要展示英文标识
- chartInfo.IsEnChart = services.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
- resp.EdbInfoList = edbList
- resp.XEdbIdValue = xEdbIdValue
- resp.YDataList = yDataList
- resp.DataResp = dataResp
- chartInfo.Button = models.ChartViewButton{
- IsEdit: chartInfo.IsEdit,
- IsEnChart: chartInfo.IsEnChart,
- IsAdd: chartInfo.IsAdd,
- IsCopy: true,
- IsSetName: chartInfo.IsSetName,
- }
- resp.ChartInfo = chartInfo
- resp.BarChartInfo = barConfig
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- func fillAreaGraphData(extraConfigStr string, edbDataList []*models.ChartEdbInfoMapping) (err error, errMsg string) {
- var tmpConfig models.AreaExtraConf
- if extraConfigStr != `` {
- err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
- if err != nil {
- errMsg = "面积图配置异常"
- err = errors.New(errMsg)
- return
- }
- if tmpConfig.StandardEdbInfoId <= 0 {
- errMsg = "面积图配置异常"
- err = errors.New(errMsg)
- return
- }
- }
- if tmpConfig.IsHeap == 1 {
- standardIndexMap := make(map[string]*models.EdbDataList)
- var startDate, endDate string
- for _, v := range edbDataList {
- // 判断是否为基准指标
- if v.EdbInfoId == tmpConfig.StandardEdbInfoId {
- if dataList, ok := v.DataList.([]*models.EdbDataList); ok {
- startDate = dataList[0].DataTime
- endDate = dataList[len(dataList)-1].DataTime
- for _, dataObject := range dataList {
- standardIndexMap[dataObject.DataTime] = dataObject
- }
- }
- break
- }
- }
- strategy, err := area_graph.CreateStrategy(tmpConfig.NullDealWay)
- if err != nil {
- return err, "创建空值处理器失败"
- }
- err = strategy.Deal(tmpConfig, edbDataList, standardIndexMap, startDate, endDate)
- if err != nil {
- return err, err.Error()
- }
- }
- return nil, ""
- }
- // ChartInfoSearchByEs
- // @Title 图表模糊搜索(从es获取)
- // @Description 图表模糊搜索(从es获取)
- // @Param Keyword query string true "图表名称"
- // @Param IsShowMe query bool true "是否只看我的,true、false"
- // @Success 200 {object} models.ChartInfo
- // @router /search_by_es [get]
- func (this *ChartInfoController) ChartInfoSearchByEs() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- keyword := this.GetString("Keyword")
- //只看我的
- sysUserIds := this.GetString("SysUserIds")
- adminIdsSlice := make([]int, 0)
- if sysUserIds != "" {
- adminIds := strings.Split(sysUserIds, ",")
- if len(adminIds) == 0 {
- br.Msg = "请选择正确的创建人"
- return
- }
- for _, adminId := range adminIds {
- adminIdInt, e := strconv.Atoi(adminId)
- if e != nil {
- br.Msg = "请选择正确的创建人"
- return
- }
- adminIdsSlice = append(adminIdsSlice, adminIdInt)
- }
- }
- var searchList []*models.ChartInfo
- var total int64
- var err error
- if keyword != "" {
- searchList, total, err = services.EsSearchChartInfo(keyword, adminIdsSlice, []int{utils.CHART_SOURCE_DEFAULT}, startSize, pageSize)
- } else {
- total, searchList, err = models.ChartInfoSearchByEmptyKeyWord(adminIdsSlice, []int{utils.CHART_SOURCE_DEFAULT}, startSize, pageSize)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
- return
- }
- }
- finalList := make([]*models.ChartInfoMore, 0)
- if len(searchList) > 0 {
- // 涉及分类id
- classifyIdList := make([]int, 0)
- // 图表id
- chartInfoIdList := make([]int, 0)
- chartInfoIds := ""
- chartEdbMap := make(map[int][]*models.ChartEdbInfoMapping)
- for _, v := range searchList {
- chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
- classifyIdList = append(classifyIdList, v.ChartClassifyId)
- chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
- }
- if chartInfoIds != "" {
- chartInfoIds = strings.Trim(chartInfoIds, ",")
- //判断是否需要展示英文标识
- edbList, e := models.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
- return
- }
- for _, v := range edbList {
- chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
- }
- }
- // 当前列表中的分类map
- chartClassifyMap := make(map[int]*models.ChartClassify)
- // 图表分类
- {
- chartClassifyList, err := models.GetChartClassifyByIdList(classifyIdList)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取图表分类失败,Err:" + err.Error()
- return
- }
- for _, v := range chartClassifyList {
- chartClassifyMap[v.ChartClassifyId] = v
- }
- }
- for _, v := range searchList {
- tmp := new(models.ChartInfoMore)
- tmp.ChartInfo = *v
- //判断是否需要展示英文标识
- if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
- tmp.IsEnChart = services.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
- }
- // 图表权限
- finalList = append(finalList, tmp)
- }
- }
- //新增搜索词记录
- {
- searchKeyword := new(models.SearchKeyword)
- searchKeyword.KeyWord = keyword
- searchKeyword.CreateTime = time.Now()
- go models.AddSearchKeyword(searchKeyword)
- }
- page := paging.GetPaging(currentIndex, pageSize, int(total))
- resp := models.ChartInfoListByEsResp{
- Paging: page,
- List: finalList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // ChartList
- // @Title 根据分类创建人姓名等获取图表列表
- // @Description 根据分类创建人姓名等获取图表列表
- // @Param SysUserIds query string true "根据创建人查询"
- // @Param ChartClassifyIds query string true "图片分类id"
- // @Success 200 {object} models.ChartClassifyListResp
- // @router /list [get]
- func (this *ChartInfoController) ChartList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- resp := new(models.ChartClassifyListResp)
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- chartClassifyIds := this.GetString("ChartClassifyIds")
- sysUserIds := this.GetString("SysUserIds")
- chartName := this.GetString("ChartName")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- condition := ""
- pars := make([]interface{}, 0)
- if chartClassifyIds != "" {
- classifyIds := strings.Split(chartClassifyIds, ",")
- if len(classifyIds) == 0 {
- br.Msg = "请选择正确的分类"
- return
- }
- classifyIdSlice := make([]int, 0)
- for _, id := range classifyIds {
- tmp, e := strconv.Atoi(id)
- if e != nil {
- br.Msg = "请选择正确的分类"
- return
- }
- classifyIdSlice = append(classifyIdSlice, tmp)
- }
- condition += " AND chart_classify_id in (" + utils.GetOrmInReplace(len(classifyIdSlice)) + ") "
- pars = append(pars, classifyIdSlice)
- }
- if sysUserIds != "" {
- adminIds := strings.Split(sysUserIds, ",")
- if len(adminIds) == 0 {
- br.Msg = "请选择正确的创建人"
- return
- }
- adminIdsSlice := make([]int, 0)
- for _, adminId := range adminIds {
- adminIdInt, e := strconv.Atoi(adminId)
- if e != nil {
- br.Msg = "请选择正确的创建人"
- return
- }
- adminIdsSlice = append(adminIdsSlice, adminIdInt)
- }
- condition += " AND sys_user_id in (" + utils.GetOrmInReplace(len(adminIds)) + ") "
- pars = append(pars, adminIdsSlice)
- }
- if chartName != "" {
- condition += " AND chart_name LIKE ? "
- pars = append(pars, utils.GetLikeKeyword(chartName))
- }
- allChartInfo, err := models.GetChartInfoByConditionPage(utils.CHART_SOURCE_DEFAULT, condition, pars, startSize, pageSize)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
- return
- }
- total, err := models.GetChartInfoTotalByCondition(utils.CHART_SOURCE_DEFAULT, condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取图表总数失败,Err:" + err.Error()
- return
- }
- if len(allChartInfo) > 0 {
- /*adminIds := make([]int, 0)
- for _, v := range allChartInfo {
- adminIds = append(adminIds, v.SysUserId)
- }
- adminCompanyMap, er := services.GetCompanyNameByAdmins(adminIds)
- if er != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取机构失败,Err:" + er.Error()
- return
- }*/
- for k, v := range allChartInfo {
- v.Children = make([]*models.ChartClassifyItems, 0)
- //if n, ok := adminCompanyMap[v.SysUserId]; ok {
- allChartInfo[k].Company = utils.HzCompanyName
- //}
- }
- }
- page := paging.GetPaging(currentIndex, pageSize, int(total))
- resp.AllNodes = allChartInfo
- resp.Paging = page
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
|