1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/services/data"
- etaTrialService "eta/eta_api/services/eta_trial"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/tealeg/xlsx"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "time"
- )
- // HisugarClassify
- // @title 获取泛糖科技分类列表
- // @Description 获取泛糖科技分类列表
- // @Success 200 {object} models.
- // @router /data/hisugar/classify [get]
- func (this *TradeCommonController) HisugarClassify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- classifyList, err := data_manage.GetHisugarClassifyAll()
- if err != nil {
- br.Msg = "查询失败"
- br.ErrMsg = "查询失败, Err:" + err.Error()
- return
- }
- finalList := make([]*data_manage.BaseFromHisugarClassifyItem, 0)
- classifyTree := getHisugarClassifyTree(classifyList, 0)
- finalList = append(finalList, classifyTree...)
- br.Msg = "查询成功"
- br.Data = finalList
- br.Success = true
- br.Ret = 200
- }
- // HisugarIndexList
- // @title 获取泛糖科技指标列表
- // @Description 获取泛糖科技指标列表
- // @Success 200 {object} models.
- // @router /data/hisugar/indexList [get]
- func (this *TradeCommonController) HisugarIndexList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- //pageSize, _ := this.GetInt("PageSize")
- //currentIndex, _ := this.GetInt("CurrentIndex")
- //var startSize int
- //
- //if pageSize <= 0 {
- // pageSize = utils.PageSize20
- //}
- //if currentIndex <= 0 {
- // currentIndex = 1
- //}
- //startSize = utils.StartIndex(currentIndex, pageSize)
- classifyId, _ := this.GetInt("ClassifyId")
- var condition string
- var pars []interface{}
- if classifyId > 0 {
- condition += ` AND b.classify_id=? `
- pars = append(pars, classifyId)
- }
- //keyword := this.GetString("KeyWord")
- //if keyword != "" {
- // condition += ` AND (index_code =? OR index_name LIKE ?) `
- // pars = append(pars, keyword)
- // pars = append(pars, "%"+keyword+"%")
- //}
- indexList, e := data_manage.GetHisugarIndexViewList(condition, pars)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取泛糖科技分类数据分类失败, Err: " + e.Error()
- return
- }
- indexIdList := make([]int, 0)
- for _, v := range indexList {
- indexIdList = append(indexIdList, v.BaseFromHisugarIndexId)
- }
- dataList, err := data_manage.GetHisugarDataViewList(indexIdList)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取泛糖科技分类数据分类失败, Err: " + err.Error()
- return
- }
- for i, v := range indexList {
- for _, v1 := range dataList {
- if v.BaseFromHisugarIndexId == v1.BaseFromHisugarIndexId {
- indexList[i].DataTime = v1.DataTime
- indexList[i].Value = v1.Value
- }
- }
- }
- //total, err := data_manage.GetHisugarIndexViewListCount(condition, pars)
- //if err != nil {
- // br.Msg = "获取失败"
- // br.ErrMsg = "获取泛糖科技分类数据分类失败, Err: " + err.Error()
- // return
- //}
- //page := paging.GetPaging(currentIndex, pageSize, total)
- resp := data_manage.BaseFromHisugarIndexListResp{}
- resp.List = indexList
- //resp.Paging = page
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // HisugarMineData
- // @title 获取泛糖科技详细数据列表
- // @Description 获取泛糖科技详细数据接口
- // @Param ClassifyId query int true "数据id"
- // @Param GroupName query string true "分组名"
- // @Param Frequency query string true "频度"
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Success 200 {object} []data_manage.CoalmineDataResp
- // @router /data/hisugar/data [get]
- func (this *TradeCommonController) HisugarData() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- indexCode := this.GetString("IndexCode")
- if indexCode == "" {
- br.Msg = "请选择指标"
- br.ErrMsg = "请选择指标"
- return
- }
- //获取指标
- var condition string
- var pars []interface{}
- condition += ` AND index_code =? `
- pars = append(pars, indexCode)
- index, err := data_manage.GetHisugarIndexByCode(indexCode)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- dataList, err := data_manage.GetHisugarIndexData(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- resp := data_manage.BaseFromHisugarIndexList{}
- resp.BaseFromHisugarIndexId = index.BaseFromHisugarIndexId
- resp.ClassifyId = index.ClassifyId
- resp.IndexCode = index.IndexCode
- resp.IndexName = index.IndexName
- resp.Frequency = index.Frequency
- resp.Unit = index.Unit
- resp.EdbExist = index.EdbExist
- resp.EdbInfoId = index.EdbInfoId
- total, err := data_manage.GetHisugarDataListCount(condition, pars)
- page := paging.GetPaging(currentIndex, pageSize, total)
- if len(dataList) > 0 {
- resp.ModifyTime = dataList[0].ModifyTime
- }
- resp.Paging = page
- resp.DataList = dataList
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // CoalSearchList
- // @Title 泛糖科技模糊搜索
- // @Description 泛糖科技模糊搜索
- // @Param Keyword query string ture "关键字搜索"
- // @Success 200 {object} models.BaseResponse
- // @router /data/hisugar/search [get]
- func (this *TradeCommonController) HisugarSearchList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- //关键字
- keyword := this.GetString("Keyword")
- list, err := data_manage.GetHisugarItemList(keyword)
- if err != nil {
- br.ErrMsg = "获取失败,Err:" + err.Error()
- br.Msg = "获取失败"
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = list
- }
- // BatchIndexList
- // @Title 泛糖科技批量操作指标列表
- // @Description 泛糖科技批量操作指标列表
- // @Param request body request.SciHqDataBatchListReq true "type json string"
- // @Success 200 {object} data_manage.BaseFromMysteelChemicalIndexResp
- // @router /data/hisugar/batch_list [post]
- func (this *TradeCommonController) HisugarBatchIndexList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req data_manage.HisugarDataBatchListReq
- if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- var condition string
- var pars []interface{}
- if req.KeyWord != "" {
- condition += `AND (index_code=? OR index_name=?)`
- pars = utils.GetLikeKeywordPars(pars, req.KeyWord, 2)
- }
- if req.IsSelectAll {
- if len(req.SelectedId) > 0 {
- condition += ` AND base_from_hisugar_index_id NOT IN (` + utils.GetOrmInReplace(len(req.SelectedId)) + `)`
- pars = append(pars, req.SelectedId)
- }
- } else {
- if len(req.SelectedId) > 0 {
- condition += ` AND base_from_hisugar_index_id IN (` + utils.GetOrmInReplace(len(req.SelectedId)) + `)`
- pars = append(pars, req.SelectedId)
- }
- }
- if req.ClassifyId >= 0 {
- condition += ` AND classify_id=?`
- pars = append(pars, req.ClassifyId)
- }
- count, err := data_manage.GetHisugarIndexListCount(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- MaxLen := 30
- if count > MaxLen {
- br.Msg = "批量操作数量不能超过" + strconv.Itoa(MaxLen)
- return
- }
- indexList, err := data_manage.GetHisugarIndexByCondition(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = indexList
- }
- // AddCheck
- // @Title 新增校验
- // @Description 新增校验
- // @Param request body request.BusinessDataBatchAddCheckReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /data/hisugar/add_check [post]
- func (this *TradeCommonController) HisugarAddCheck() {
- 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
- }
- var req *data_manage.HisugarDataBatchAddCheckReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- codeMax := 30
- codeLen := len(req.IndexCodes)
- if len(req.IndexCodes) == 0 {
- br.Msg = "请选择指标"
- return
- }
- if codeLen > codeMax {
- br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
- return
- }
- // 获取指标库已有指标
- existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_HISUGAR)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取自有数据已添加的指标失败, Err: " + e.Error()
- return
- }
- existMap := make(map[string]*data_manage.EdbInfo)
- for _, v := range existsEdb {
- existMap[v.EdbCode] = v
- }
- // 查询选中的指标
- cond := fmt.Sprintf(` AND index_code IN (%s)`, utils.GetOrmInReplace(codeLen))
- pars := make([]interface{}, 0)
- pars = append(pars, req.IndexCodes)
- list, err := data_manage.GetHisugarIndexAndEdbInfoByCondition(cond, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取卓创红期原始指标列表失败, Err: " + err.Error()
- return
- }
- resp := make([]*data_manage.BaseFromHisugarIndexView, 0)
- for _, v := range list {
- if v.EdbInfoId > 0 {
- v.EdbExist = 1
- }
- resp = append(resp, v)
- }
- br.Data = resp
- br.Msg = "校验成功"
- br.Ret = 200
- br.Success = true
- }
- // NameCheck
- // @Title 重名校验
- // @Description 批量新增
- // @Param request body data_manage.AddEdbInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /data/hisugar/name_check [post]
- func (c *TradeCommonController) HisugarNameCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req []*data_manage.NameCheckEdbInfoReq
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if len(req) == 0 {
- br.Msg = "请选择指标"
- return
- }
- codeMax := 30
- codeLen := len(req)
- if codeLen > codeMax {
- br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
- return
- }
- type NameCheckResult struct {
- EdbCode string
- EdbName string
- Exist bool
- }
- indexNames := make([]string, 0)
- resp := make([]*NameCheckResult, 0)
- for _, v := range req {
- v.EdbCode = strings.TrimSpace(v.EdbCode)
- if v.EdbCode == "" {
- br.Msg = "指标ID不可为空"
- return
- }
- v.EdbName = strings.TrimSpace(v.EdbName)
- if v.EdbName == "" {
- br.Msg = "请输入指标名称"
- return
- }
- indexNames = append(indexNames, v.EdbName)
- resp = append(resp, &NameCheckResult{
- EdbCode: v.EdbCode,
- EdbName: v.EdbName,
- })
- dataItems, err := data_manage.GetEdbDataAllByEdbCode(v.EdbCode, utils.DATA_SOURCE_HISUGAR, 0, utils.EDB_DATA_LIMIT)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取泛糖科技已存在信息失败,Err:" + err.Error()
- return
- }
- if len(dataItems) <= 0 {
- respItem, err := data.AddEdbData(utils.DATA_SOURCE_HISUGAR, v.EdbCode, v.Frequency)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if respItem.Ret != 200 {
- br.Msg = "未搜索到该指标"
- br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + v.EdbCode
- return
- }
- }
- }
- // 重名校验
- edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
- return
- }
- nameExists := make(map[string]bool)
- for _, v := range edbList {
- nameExists[v.EdbName] = true
- }
- if len(nameExists) > 0 {
- for _, v := range resp {
- v.Exist = nameExists[v.EdbName]
- }
- }
- br.Data = resp
- br.Msg = "校验成功"
- br.Ret = 200
- br.Success = true
- }
- // BatchAdd
- // @Title 批量新增
- // @Description 批量新增
- // @Param request body data_manage.AddEdbInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /data/hisugar/batch_add [post]
- func (this *TradeCommonController) HisugarBatchAdd() {
- 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
- }
- deleteCache := true
- cacheKey := "CACHE_EDB_INFO_BATCH_ADD_OILCHEM_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- var req []*data_manage.AddEdbInfoReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if len(req) == 0 {
- br.Msg = "请选择指标"
- return
- }
- if len(req) > 30 {
- br.Msg = "批量添加指标数量不得超过30个"
- return
- }
- for _, v := range req {
- v.EdbCode = strings.TrimSpace(v.EdbCode)
- if v.EdbCode == "" {
- br.Msg = "指标ID不可为空"
- return
- }
- v.EdbName = strings.TrimSpace(v.EdbName)
- if v.EdbName == "" {
- br.Msg = "请输入指标名称"
- return
- }
- v.Frequency = strings.TrimSpace(v.Frequency)
- if v.Frequency == "" {
- br.Msg = "请选择频度"
- return
- }
- v.Unit = strings.TrimSpace(v.Unit)
- if v.Unit == "" {
- br.Msg = "请输入单位"
- return
- }
- if v.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- }
- // 限定同一时间最多批量新增30个指标
- for _, v := range req {
- var r data.HisugarIndexSource2EdbReq
- r.EdbCode = v.EdbCode
- r.EdbName = v.EdbName
- r.Frequency = v.Frequency
- r.Unit = v.Unit
- r.ClassifyId = v.ClassifyId
- r.AdminId = sysUser.AdminId
- r.AdminRealName = sysUser.RealName
- edbInfo, errMsg, skip, e := data.HisugarIndexSource2Edb(r, this.Lang)
- if e != nil {
- br.Msg = "操作失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = e.Error()
- return
- }
- if skip {
- continue
- }
- // 试用平台更新用户累计新增指标数
- if utils.BusinessCode == utils.BusinessCodeSandbox {
- go func() {
- adminItem, e := system.GetSysAdminById(sysUser.AdminId)
- if e != nil {
- tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
- utils.FileLog.Info(tips)
- return
- }
- if adminItem.DepartmentName != "ETA试用客户" {
- return
- }
- var ur etaTrialService.EtaTrialUserReq
- ur.Mobile = adminItem.Mobile
- _, _ = etaTrialService.UpdateUserIndexNum(ur)
- }()
- }
- // 新增操作日志
- {
- edbLog := new(data_manage.EdbInfoLog)
- edbLog.EdbInfoId = edbInfo.EdbInfoId
- edbLog.SourceName = edbInfo.SourceName
- edbLog.Source = edbInfo.Source
- edbLog.EdbCode = edbInfo.EdbCode
- edbLog.EdbName = edbInfo.EdbName
- edbLog.ClassifyId = edbInfo.ClassifyId
- edbLog.SysUserId = sysUser.AdminId
- edbLog.SysUserRealName = sysUser.RealName
- edbLog.CreateTime = time.Now()
- edbLog.Content = string(this.Ctx.Input.RequestBody)
- edbLog.Status = "新增指标"
- edbLog.Method = this.Ctx.Input.URI()
- go data_manage.AddEdbInfoLog(edbLog)
- }
- }
- br.Msg = "操作成功"
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- }
- // ExporthisugarList
- // @Title 导出泛糖科技数据
- // @Description 导出泛糖科技数据
- // @Param request body request.ExportHisugarExcelReq true "type json string"
- // @Success 200 导出成功
- // @router /data/export/hisugarList [post]
- func (this *TradeCommonController) HisugarExporthisugarList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- var req data_manage.ExportHisugarExcelReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- secNameList := make([]*data_manage.BaseFromHisugarIndex, 0)
- dir, _ := os.Executable()
- exPath := filepath.Dir(dir)
- downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- var condition string
- var pars []interface{}
- if req.KeyWord != "" {
- condition += `AND (index_code=? OR index_name=?)`
- pars = utils.GetLikeKeywordPars(pars, req.KeyWord, 2)
- }
- if req.IsSelectedAll {
- if len(req.IndexCode) > 0 {
- condition += ` AND index_code NOT IN (` + utils.GetOrmInReplace(len(req.IndexCode)) + `)`
- pars = append(pars, req.IndexCode)
- }
- } else {
- if len(req.IndexCode) > 0 {
- condition += ` AND index_code IN (` + utils.GetOrmInReplace(len(req.IndexCode)) + `)`
- pars = append(pars, req.IndexCode)
- }
- }
- if req.ClassifyId > 0 {
- classifyIds, err := data_manage.GetHisugarClassifyById(req.ClassifyId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- if classifyIds != ""{
- condition += ` AND classify_id IN (`+ classifyIds +`)`
- }
- }
- frequencies, err := data_manage.GetHisugarFrequencyByCondition(condition, pars)
- if err != nil {
- fmt.Println("GetSciFrequency err:", err.Error())
- utils.FileLog.Info("GetSciFrequency err:" + err.Error())
- return
- }
- frequencyMap := map[string]string{
- "日度": "Daily",
- "周度": "Weekly",
- "旬度": "ten-day",
- "月度": "Monthly",
- "季度": "Quarterly",
- "半年度": "Semi-annual",
- "年度": "Annual",
- }
- for _, frequency := range frequencies {
- //获取指标
- secNameList, err = data_manage.GetHisugarIndexByConditionAndFrequency(condition, *frequency, pars)
- if err != nil {
- fmt.Println("获取数据失败,Err:" + err.Error())
- return
- }
- if len(secNameList) <= 0 {
- fmt.Println("secNameList长度为0")
- return
- }
- sheetNew, err := xlsxFile.AddSheet(*frequency+frequencyMap[*frequency])
- if err != nil {
- fmt.Println("新增Sheet失败", err.Error())
- return
- }
- secNameRow := sheetNew.AddRow()
- frequencyRow := sheetNew.AddRow()
- unitRow := sheetNew.AddRow()
- lastModifyDateRow := sheetNew.AddRow()
- var indexIdList []int
- for _, sv := range secNameList {
- indexIdList = append(indexIdList, sv.BaseFromHisugarIndexId)
- }
- dataTimeList, err := data_manage.GetHisugarDataDataTimeByIndexId(indexIdList)
- if err != nil {
- fmt.Println("获取数据时间失败", err.Error())
- return
- }
- // 添加excel左侧指标日期
- setRowIndex := 4
- for rk, dv := range dataTimeList {
- rowIndex := setRowIndex + rk
- row := sheetNew.Row(rowIndex)
- displayDate, _ := time.Parse(utils.FormatDate, dv)
- displayDateCell := row.AddCell()
- style := new(xlsx.Style)
- style.ApplyAlignment = true
- style.Alignment.WrapText = true
- displayDateCell.SetStyle(style)
- displayDateCell.SetDate(displayDate)
- }
- for k, sv := range secNameList {
- //获取数据
- dataList, err := data_manage.GetHisugarIndexDataByCode(sv.IndexCode)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- if k == 0 {
- secNameRow.AddCell().SetValue("指标名称/Metric Name")
- frequencyRow.AddCell().SetValue("频度/Frequency")
- unitRow.AddCell().SetValue("单位/Unit")
- lastModifyDateRow.AddCell().SetValue("更新时间/Update Time")
- min := k * 3
- sheetNew.SetColWidth(min, min, 15)
- }
- if len(dataList) == 0 {
- continue
- }
- secNameRow.AddCell().SetValue(sv.IndexName)
- frequencyRow.AddCell().SetValue(sv.Frequency)
- unitRow.AddCell().SetValue(sv.Unit)
- lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
- dataInfoMap := make(map[string]*data_manage.BaseFromHisugarData)
- for _, v := range dataList {
- dataInfoMap[v.DataTime] = v
- }
- for rk, dtv := range dataTimeList {
- rowIndex := setRowIndex + rk
- row := sheetNew.Row(rowIndex)
- displayDateCell := row.AddCell()
- tmpData, ok := dataInfoMap[dtv]
- if ok {
- displayDateCell.SetValue(tmpData.Value)
- }
- }
- }
- }
- err = xlsxFile.Save(downLoadnFilePath)
- if err != nil {
- //有指标无数据时先导出一遍空表
- sheet, err := xlsxFile.AddSheet("无数据")
- if err != nil {
- br.Msg = "新增Sheet失败"
- br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
- return
- }
- rowSecName := sheet.AddRow()
- celSecName := rowSecName.AddCell()
- celSecName.SetValue("")
- err = xlsxFile.Save(downLoadnFilePath)
- if err != nil {
- br.Msg = "保存文件失败"
- br.ErrMsg = "保存文件失败"
- return
- }
- }
- fileName := `泛糖科技`
- if len(secNameList) > 0 {
- fileName = secNameList[0].IndexName
- }
- fileName += time.Now().Format("2006.01.02") + `.xlsx` //文件名称
- this.Ctx.Output.Download(downLoadnFilePath, fileName)
- defer func() {
- os.Remove(downLoadnFilePath)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "success"
- }
- // getHisugarClassifyTree 返回泛糖科技的树形结构
- func getHisugarClassifyTree(items []*data_manage.BaseFromHisugarClassifyItem, parentId int) []*data_manage.BaseFromHisugarClassifyItem {
- res := make([]*data_manage.BaseFromHisugarClassifyItem, 0)
- for _, item := range items {
- if item.ParentId == parentId {
- t := new(data_manage.BaseFromHisugarClassifyItem)
- t.BaseFromHisugarClassifyId = item.BaseFromHisugarClassifyId
- t.ClassifyName = item.ClassifyName
- t.ParentId = item.ParentId
- t.Level = item.Level
- t.Sort = item.Sort
- t.ModifyTime = item.ModifyTime
- t.CreateTime = item.CreateTime
- t.ClassifyNameEn = item.ClassifyNameEn
- if item.UniqueCode == "" {
- t.UniqueCode = strconv.Itoa(item.BaseFromHisugarClassifyId)
- }
- t.Children = getHisugarClassifyTree(items, item.BaseFromHisugarClassifyId)
- res = append(res, t)
- }
- }
- return res
- }
- // FenweiIndexData
- // @Title 获取数据
- // @Description 获取泛糖科技数据接口
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param ClassifyId query string true "分类id"
- // @Param Frequency query string true "频率"
- // @Success 200 {object} data_manage.LzFrequency
- // @router /data/hisugar/index/data [get]
- func (this *TradeCommonController) HisugarIndexData() {
- 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
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- classifyId, _ := this.GetInt("ClassifyId")
- if classifyId < 0 {
- br.Msg = "请选择分类"
- br.ErrMsg = "请选择分类"
- return
- }
- frequency := this.GetString("Frequency")
- indexCode := this.GetString("IndexCode")
- // 获取指标
- var condition string
- var pars []interface{}
- if classifyId >= 0 {
- classifyIds, err := data_manage.GetHisugarClassifyById(classifyId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- if classifyIds != ""{
- condition += ` AND classify_id IN (`+ classifyIds +`)`
- }
- }
- if frequency != "" {
- condition += ` AND frequency=? `
- pars = append(pars, frequency)
- }
- if indexCode != "" {
- condition += ` AND index_code=? `
- pars = append(pars, indexCode)
- }
- indexes, err := data_manage.GetHisugarIndex(condition, pars)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- indexCodes := make([]string, 0)
- for _, v := range indexes {
- indexCodes = append(indexCodes, v.IndexCode)
- }
- indexCounts, e := data_manage.GetHisugarIndexDataCountGroup(indexCodes)
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取指标数据总量失败, Err:" + err.Error()
- return
- }
- countMap := make(map[string]int)
- for _, v := range indexCounts {
- countMap[v.IndexCode] = v.Count
- }
- // 判断是否存在于指标库
- edbCodeList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_HISUGAR, indexCodes)
- if err != nil {
- return
- }
- edbCodeMap := make(map[string]*data_manage.EdbInfo)
- for _, v := range edbCodeList {
- edbCodeMap[v.EdbCode] = v
- }
- resultList := make([]*data_manage.BaseFromHisugarIndexList, 0)
- for _, v := range indexes {
- product := new(data_manage.BaseFromHisugarIndexList)
- product.BaseFromHisugarIndexId = v.BaseFromHisugarIndexId
- product.ClassifyId = v.ClassifyId
- product.Unit = v.Unit
- product.IndexCode = v.IndexCode
- product.IndexName = v.IndexName
- product.Frequency = v.Frequency
- product.CreateTime = v.CreateTime
- product.ModifyTime = v.ModifyTime
- edbInfo := edbCodeMap[v.IndexCode]
- if edbInfo != nil {
- product.EdbInfoId = edbInfo.EdbInfoId
- product.EdbExist = 1
- }
- total := countMap[v.IndexCode]
- page := paging.GetPaging(currentIndex, pageSize, total)
- dataList, e := data_manage.GetHisugarIndexDataV2(v.IndexCode, startSize, pageSize)
- if e != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
- return
- }
- if dataList == nil {
- dataList = make([]*data_manage.BaseFromHisugarData, 0)
- }
- product.DataList = dataList
- product.Paging = page
- resultList = append(resultList, product)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resultList
- }
- // GetFenWeiIndexInfo
- // @Title 添加指标-根据条件获取指标信息
- // @Description 添加指标-根据条件获取指标信息
- // @Param KeyWord query string false "关键字"
- // @Param ClassifyIds query string false "分类id"
- // @Param Frequencies query string false "频率"
- // @Param PageSize query int false "每页数据条数"
- // @Param CurrentIndex query int false "当前页页码,从1开始"
- // @Success 200 {object} data_manage.BaseFromFenWeiIndexPage
- // @router /data/hisugar/index/info [get]
- func (this *TradeCommonController) GetHisugarIndexInfo() {
- 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
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- keyWord := this.GetString("KeyWord")
- classifyIds := this.GetString("ClassifyIds")
- frequencies := this.GetString("Frequencies")
- var classifyIdList []string
- var frequencyList []string
- if classifyIds != "" {
- classifyIdList = strings.Split(classifyIds, ",")
- }
- if frequencies != "" {
- frequencyList = strings.Split(frequencies, ",")
- }
- indexInfoPage, err := data.GethisugarIndexInfo(keyWord, classifyIdList, frequencyList, currentIndex, pageSize)
- if err != nil {
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = indexInfoPage
- }
- // FrequencyList
- // @Title 指标频度列表
- // @Description 指标频度列表
- // @Success 200 string "获取成功"
- // @router /data/hisugar/frequency_list [get]
- func (this *TradeCommonController) FrequencyList() {
- 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"
- return
- }
- classifyId, _ := this.GetInt("ClassifyId")
- var condition string
- var pars []interface{}
- if classifyId > 0 {
- classifyIds, err := data_manage.GetHisugarClassifyById(classifyId)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- if classifyIds != ""{
- condition += ` AND classify_id IN (`+ classifyIds +`)`
- }
- }
- frequencies, err := data_manage.GetHisugarFrequencyByCondition(condition, pars)
- if err != nil {
- fmt.Println("GetSciFrequency err:", err.Error())
- utils.FileLog.Info("GetSciFrequency err:" + err.Error())
- return
- }
- br.Data = frequencies
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
|