123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047 |
- package data_manage
- 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"
- )
- // FenweiClassify
- // @Title 汾渭数据分类
- // @Description 汾渭数据分类接口
- // @Success 200 {object} data_manage.BaseFromFenweiClassifyItem
- // @router /fenwei/classify [get]
- func (this *EdbInfoController) FenweiClassify() {
- 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
- }
- classifies, e := data_manage.GetBaseFromFenweiClassify()
- if e != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取汾渭数据分类失败, Err: " + e.Error()
- return
- }
- classifyOb := new(data_manage.BaseFromFenweiClassify)
- resp := make([]*data_manage.BaseFromFenweiClassifyItem, 0)
- parentMap := make(map[int][]*data_manage.BaseFromFenweiClassifyItem)
- for _, v := range classifies {
- t := classifyOb.Format2Item(v)
- if v.ParentId == 0 {
- resp = append(resp, t)
- }
- if v.ParentId > 0 {
- if parentMap[v.ParentId] == nil {
- parentMap[v.ParentId] = make([]*data_manage.BaseFromFenweiClassifyItem, 0)
- }
- parentMap[v.ParentId] = append(parentMap[v.ParentId], t)
- }
- }
- for _, v := range resp {
- addChildren(v, parentMap)
- }
- br.Data = resp
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- func addChildren(parent *data_manage.BaseFromFenweiClassifyItem, parentMap map[int][]*data_manage.BaseFromFenweiClassifyItem) {
- if children, ok := parentMap[parent.ClassifyId]; ok {
- parent.Child = children
- for _, child := range children {
- addChildren(child, parentMap)
- }
- }
- }
- // 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 /fenwei/index/data [get]
- func (this *EdbInfoController) FenweiIndexData() {
- 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")
- // 获取指标
- var condition string
- var pars []interface{}
- if classifyId >= 0 {
- condition += ` AND classify_id=? `
- pars = append(pars, classifyId)
- }
- if frequency != "" {
- condition += ` AND frequency=? `
- pars = append(pars, frequency)
- }
- indexes, err := data_manage.GetFenweiIndex(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.GetFenweiIndexDataCountGroup(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_FENWEI, indexCodes)
- if err != nil {
- return
- }
- edbCodeMap := make(map[string]*data_manage.EdbInfo)
- for _, v := range edbCodeList {
- edbCodeMap[v.EdbCode] = v
- }
- resultList := make([]*data_manage.BaseFromFenweiIndexList, 0)
- for _, v := range indexes {
- product := new(data_manage.BaseFromFenweiIndexList)
- product.FenweiIndexId = v.FenweiIndexId
- 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
- }
- total := countMap[v.IndexCode]
- page := paging.GetPaging(currentIndex, pageSize, total)
- dataList, e := data_manage.GetFenweiIndexData(v.IndexCode, startSize, pageSize)
- if e != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
- return
- }
- if dataList == nil {
- dataList = make([]*data_manage.BaseFromFenweiData, 0)
- }
- product.DataList = dataList
- product.Paging = page
- resultList = append(resultList, product)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resultList
- }
- // FenweiSearchList
- // @Title Fenwei模糊搜索
- // @Description Fenwei模糊搜索
- // @Param Keyword query string ture "关键字搜索"
- // @Success 200 {object} data_manage.BaseFromFenweiIndexSearchItem
- // @router /fenwei/search_list [get]
- func (this *EdbInfoController) FenweiSearchList() {
- 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
- }
- list := make([]*data_manage.BaseFromFenweiIndexSearchItem, 0)
- var err error
- // 关键字
- keyword := this.GetString("Keyword")
- if keyword != "" {
- keyWordArr := strings.Split(keyword, " ")
- if len(keyWordArr) > 0 {
- condition := ""
- for _, v := range keyWordArr {
- condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
- }
- list, err = data_manage.GetFenweiItemList(condition)
- if err != nil {
- br.ErrMsg = "获取失败,Err:" + err.Error()
- br.Msg = "获取失败"
- return
- }
- }
- } else {
- list, err = data_manage.GetFenweiItemList("")
- if err != nil {
- br.ErrMsg = "获取失败,Err:" + err.Error()
- br.Msg = "获取失败"
- return
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = list
- }
- // FenweiSingleData
- // @Title 获取Fenwei数据
- // @Description 获取Fenwei单条数据接口
- // @Param IndexCode query string true "指标唯一编码"
- // @Success 200 {object} models.BaseResponse
- // @router /fenwei/single_data [get]
- func (this *EdbInfoController) FenweiSingleData() {
- 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
- }
- indexCode := this.GetString("IndexCode")
- indexInfo, err := data_manage.GetBaseFromFenweiIndexByIndexCode(indexCode)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- dataTmpList, err := data_manage.GetFenweiIndexDataByCode(indexCode)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- indexCodeList := []string{indexCode}
- edbCodeList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_FENWEI, indexCodeList)
- if err != nil {
- return
- }
- var ret data_manage.FenweiSingleDataResp
- var dataList []*data_manage.FenweiSingleData
- ret.ClassifyId = indexInfo.ClassifyId
- ret.FenweiIndexId = indexInfo.FenweiIndexId
- ret.IndexCode = indexInfo.IndexCode
- ret.IndexName = indexInfo.IndexName
- ret.Frequency = indexInfo.Frequency
- ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
- ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
- ret.Unit = indexInfo.Unit
- for _, v := range dataTmpList {
- tmp := &data_manage.FenweiSingleData{
- Value: v.Value,
- DataTime: v.DataTime,
- }
- dataList = append(dataList, tmp)
- }
- ret.Data = dataList
- if len(edbCodeList) > 0 {
- edbInfo := edbCodeList[0]
- ret.EdbInfoId = edbInfo.EdbInfoId
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = ret
- }
- // ExportFenweiList
- // @Title 导出Fenwei数据
- // @Description 导出Fenwei数据
- // @Param IndexName query string false "名称关键词"
- // @Param IndexCode query string false "指标唯一编码"
- // @Param ClassifyId query string true "分类"
- // @Success 200 导出成功
- // @router /fenwei/export [get]
- func (this *EdbInfoController) ExportFenweiList() {
- 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 = "请重新登录"
- return
- }
- indexCode := this.GetString("IndexCode") //指标唯一编码
- classifyId, _ := this.GetInt("ClassifyId")
- //secNameList := make([]*models.EdbdataExportList, 0)
- dir, _ := os.Executable()
- exPath := filepath.Dir(dir)
- downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- var pars []interface{}
- condition := ""
- if classifyId > 0 {
- //获取指标
- condition += " AND classify_id=?"
- pars = append(pars, classifyId)
- }
- if indexCode != "" {
- //获取指标
- condition += " AND index_code=?"
- pars = append(pars, indexCode)
- }
- indexList, err := data_manage.GetFenweiIndex(condition, pars)
- if err != nil {
- fmt.Println("获取数据失败,Err:" + err.Error())
- return
- }
- if len(indexList) <= 0 {
- fmt.Println("indexList 为空")
- br.Ret = 200
- br.Success = true
- br.Msg = "success"
- return
- }
- sheetNew := new(xlsx.Sheet)
- // todo 分类名称
- sheetNew, err = xlsxFile.AddSheet("汾渭数据")
- //sheetNew.SetColWidth()
- //获取指标数据
- windRow := sheetNew.AddRow()
- secNameRow := sheetNew.AddRow()
- indexCodeRow := sheetNew.AddRow()
- frequencyRow := sheetNew.AddRow()
- unitRow := sheetNew.AddRow()
- lastModifyDateRow := sheetNew.AddRow()
- //获取分类下指标最大数据量
- var dataMax int
- setRowIndex := 6
- indexCodeList := make([]string, 0)
- for _, v := range indexList {
- indexCodeList = append(indexCodeList, v.IndexCode)
- }
- dataListMap := make(map[string][]*data_manage.BaseFromFenweiData)
- if len(indexList) > 0 {
- allDataList, e := data_manage.GetFenweiIndexDataByCodes(indexCodeList)
- if e != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + e.Error()
- return
- }
- for _, v := range allDataList {
- dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
- }
- for _, v := range dataListMap {
- if len(v) > dataMax {
- dataMax = len(v)
- }
- }
- }
- for k, sv := range indexList {
- //获取数据
- dataList, ok := dataListMap[sv.IndexCode]
- if !ok {
- continue
- }
- if len(dataList) > 0 {
- windRow.AddCell().SetValue("汾渭数据")
- secNameRow.AddCell().SetValue("指标名称")
- indexCodeRow.AddCell().SetValue("指标ID")
- frequencyRow.AddCell().SetValue("频率")
- unitRow.AddCell().SetValue("单位")
- lastModifyDateRow.AddCell().SetValue("更新时间")
- secNameRow.AddCell().SetValue(sv.IndexName)
- indexCodeRow.AddCell().SetValue(sv.IndexCode)
- frequencyRow.AddCell().SetValue(sv.Frequency)
- unitRow.AddCell().SetValue(sv.Unit)
- lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
- windRow.AddCell()
- windRow.AddCell()
- secNameRow.AddCell()
- indexCodeRow.AddCell()
- frequencyRow.AddCell()
- unitRow.AddCell()
- lastModifyDateRow.AddCell()
- min := k * 3
- sheetNew.SetColWidth(min, min, 15)
- if len(dataList) <= 0 {
- for n := 0; n < dataMax; n++ {
- rowIndex := setRowIndex + n
- row := sheetNew.Row(rowIndex)
- row.AddCell()
- row.AddCell()
- row.AddCell()
- }
- } else {
- endRowIndex := 0
- for rk, dv := range dataList {
- rowIndex := setRowIndex + rk
- row := sheetNew.Row(rowIndex)
- displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
- displayDateCell := row.AddCell()
- style := new(xlsx.Style)
- style.ApplyAlignment = true
- style.Alignment.WrapText = true
- displayDateCell.SetStyle(style)
- displayDateCell.SetDate(displayDate)
- row.AddCell().SetValue(dv.Value)
- row.AddCell()
- endRowIndex = rowIndex
- }
- if len(dataList) < dataMax {
- dataLen := dataMax - len(dataList)
- for n := 0; n < dataLen; n++ {
- rowIndex := (endRowIndex + 1) + n
- row := sheetNew.Row(rowIndex)
- row.AddCell()
- row.AddCell()
- row.AddCell()
- }
- }
- }
- }
- }
- 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(indexList) > 0 {
- fileName = indexList[0].IndexName
- }
- fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
- this.Ctx.Output.Download(downLoadnFilePath, fileName)
- defer func() {
- os.Remove(downLoadnFilePath)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "success"
- }
- // GetFenWeiFrequencyList
- // @Title 查询频率列表
- // @Description 查询频率列表
- // @Param classifyId query int false "指标唯一编码"
- // @Success 200 {object} []string
- // @router /fenwei/frequency/list [get]
- func (this *EdbInfoController) GetFenWeiFrequencyList() {
- 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
- }
- classifyId, _ := this.GetInt("ClassifyId")
- frequencyList, err := data_manage.GetFenWeiIndexFrequency(classifyId)
- if err != nil {
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = frequencyList
- }
- // FenWeiIndexAddValidate
- // @Title 新增加入到指标库校验
- // @Description 新增加入到指标库校验
- // @Param req body data_manage.BaseFromFenWeiIndexBatchAddCheckReq true "请求参数"
- // @Success 200 {object} []data_manage.IndexCheckData
- // @router /fenwei/index/add/validate [post]
- func (this *EdbInfoController) FenWeiIndexAddValidate() {
- 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.BaseFromFenWeiIndexBatchAddCheckReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- codeLen := len(req.IndexCodes)
- var codeMax = 30
- if codeLen > codeMax {
- br.Msg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
- br.ErrMsg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
- return
- }
- // 校验指标编码是否存在
- addValidate, err := data.FenWeiIndexAddValidate(req.IndexCodes)
- if err != nil {
- return
- }
- br.Data = addValidate
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // FenWeiIndexAdd
- // @Title 指标添加到指标库
- // @Description 指标添加到指标库
- // @Param req body []data_manage.AddEdbInfoReq true "请求参数"
- // @Success 200 string "操作成功"
- // @router /fenwei/index/add [post]
- func (this *EdbInfoController) FenWeiIndexAdd() {
- 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_FENWEI_" + 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
- }
- codeMax := 30
- if len(req) > codeMax {
- br.Msg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
- return
- }
- indexNames := make([]string, 0)
- resp := make([]*data_manage.FenWeiNameCheckResult, 0)
- for _, index := range req {
- index.EdbCode = strings.TrimSpace(index.EdbCode)
- if index.EdbCode == "" {
- br.Msg = "指标ID不可为空"
- return
- }
- index.EdbName = strings.TrimSpace(index.EdbName)
- if index.EdbName == "" {
- br.Msg = "请输入指标名称"
- return
- }
- index.Frequency = strings.TrimSpace(index.Frequency)
- if index.Frequency == "" {
- br.Msg = "请选择频度"
- return
- }
- index.Unit = strings.TrimSpace(index.Unit)
- if index.Unit == "" {
- br.Msg = "请输入单位"
- return
- }
- if index.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- indexNames = append(indexNames, index.EdbName)
- resp = append(resp, &data_manage.FenWeiNameCheckResult{
- IndexCode: index.EdbCode,
- IndexName: index.EdbName,
- Exist: false,
- })
- }
- // 指标名称重复校验
- nameCheck, err := data.FenWeiIndexNameCheck(indexNames, resp)
- if err != nil {
- br.Msg = err.Error()
- br.ErrMsg = err.Error()
- return
- }
- for _, v := range nameCheck {
- if v.Exist {
- br.Msg = "指标名称重复"
- br.Data = nameCheck
- br.Ret = 200
- br.Success = true
- return
- }
- }
- for _, v := range req {
- var fenWeiIndexAddReq data_manage.FenWeiIndexAddReq
- fenWeiIndexAddReq.EdbCode = v.EdbCode
- fenWeiIndexAddReq.EdbName = v.EdbName
- fenWeiIndexAddReq.Frequency = v.Frequency
- fenWeiIndexAddReq.Unit = v.Unit
- fenWeiIndexAddReq.ClassifyId = v.ClassifyId
- fenWeiIndexAddReq.AdminId = sysUser.AdminId
- fenWeiIndexAddReq.AdminRealName = sysUser.RealName
- // 新增指标到指标库
- edbInfo, e, errMsg, skip := data.FenWeiIndexAdd(fenWeiIndexAddReq, this.Lang)
- if e != nil {
- br.Msg = "操作失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = e.Error()
- return
- }
- if skip {
- continue
- }
- // todo 下面两段代码能否抽离出来???
- // 试用平台更新用户累计新增指标数
- 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
- }
- // FenWeiIndexDataExport
- // @Title 导出指标数据
- // @Description 导出指标数据
- // @Param IndexCode query string false "指标编码"
- // @Param ClassifyId query int false "分类ID"
- // @Success 200 string "操作成功"
- // @router /fenwei/index/data/export [get]
- func (this *EdbInfoController) FenWeiIndexDataExport() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- classifyId, _ := this.GetInt("ClassifyId") //分类
- indexCode := this.GetString("IndexCode") //指标唯一编码
- if classifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- //userId := sysUser.AdminId
- //超管账号可以查看分类下的所有频度数据
- /*if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
- userId = 0
- }*/
- //获取账户所拥有权限的分类id集合
- /*classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
- if err != nil {
- br.Msg = "获取分类数据失败"
- return
- }*/
- dir, _ := os.Executable()
- exPath := filepath.Dir(dir)
- downLoadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- frequencies, err := data_manage.GetFenWeiIndexFrequency(classifyId)
- if err != nil {
- br.Msg = "查询频度失败"
- br.ErrMsg = "查询频度失败"
- return
- }
- fileName := `汾渭网页数据`
- if classifyId > 0 && indexCode == "" {
- fenWeiClassify, err := data_manage.GetFenweiClassifyItemByClassifyId(classifyId)
- if err != nil {
- return
- }
- fileName = fenWeiClassify.ClassifyName
- }
- if frequencies == 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("")
- }
- for _, frequency := range frequencies {
- fenWeiIndices, err := data_manage.GetFenWeiIndexByCodeAndClassify(indexCode, classifyId, frequency)
- if err != nil {
- return
- }
- var sheet *xlsx.Sheet
- if len(fenWeiIndices) > 0 {
- sheetName := *frequency
- if sheetName == "" {
- sheetName = "无频度"
- }
- sheet, err = xlsxFile.AddSheet(sheetName)
- if err != nil {
- br.Msg = "新增Sheet失败"
- br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
- return
- }
- } else {
- continue
- }
- if indexCode != "" {
- fileName = fenWeiIndices[0].IndexName
- }
- //获取指标数据
- rowSecName := sheet.AddRow()
- celSecName := rowSecName.AddCell()
- celSecName.SetValue("指标名称")
- rowFrequency := sheet.AddRow()
- celFrequency := rowFrequency.AddCell()
- celFrequency.SetValue("频率")
- rowUnit := sheet.AddRow()
- celUnit := rowUnit.AddCell()
- celUnit.SetValue("单位")
- rowModifyDate := sheet.AddRow()
- rowModifyCell := rowModifyDate.AddCell()
- rowModifyCell.SetValue("更新时间")
- dataMap := make(map[string]map[string]*data_manage.BaseFromFenweiData)
- var tradeCodeList []string
- for _, v := range fenWeiIndices {
- cellSenName := rowSecName.AddCell()
- cellSenName.SetValue(v.IndexName)
- celFrequency := rowFrequency.AddCell()
- celFrequency.SetValue(v.Frequency)
- celUnit := rowUnit.AddCell()
- celUnit.SetValue(v.Unit)
- rowModifyCell := rowModifyDate.AddCell()
- updateTimeStr := utils.TimeToStr(v.ModifyTime, utils.FormatDate)
- rowModifyCell.SetValue(updateTimeStr)
- tradeCodeList = append(tradeCodeList, v.IndexCode)
- var dataList []*data_manage.BaseFromFenweiData
- dataList, err = data_manage.GetBaseFromFenWeiDataByIndexCode(v.IndexCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.ErrMsg = "GetBaseFromFenWeiDataByIndexCode,Err:" + err.Error()
- br.Msg = "获取数据失败"
- return
- }
- for _, item := range dataList {
- if dataMap[item.IndexCode] == nil {
- dataMap[item.IndexCode] = make(map[string]*data_manage.BaseFromFenweiData)
- }
- dataMap[item.IndexCode][item.DataTime] = item
- }
- }
- tradeCodeStr := strings.Join(tradeCodeList, "','")
- tradeCodeStr = "'" + tradeCodeStr + "'"
- dataTimeList, err := data_manage.GetFenWeiDataListByIndexCodes(tradeCodeStr)
- if err != nil {
- br.Msg = "获取数据失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
- for _, dataTime := range dataTimeList {
- rowData := sheet.AddRow()
- celDate := rowData.AddCell()
- celDate.SetValue(dataTime)
- for _, m := range fenWeiIndices {
- celData := rowData.AddCell()
- if dataMap[m.IndexCode][dataTime] != nil {
- celData.SetValue(dataMap[m.IndexCode][dataTime].Value)
- }
- }
- }
- }
- err = xlsxFile.Save(downLoadFilePath)
- 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(downLoadFilePath)
- if err != nil {
- br.Msg = "保存文件失败"
- br.ErrMsg = "保存文件失败"
- return
- }
- }
- fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
- this.Ctx.Output.Download(downLoadFilePath, fileName)
- defer func() {
- os.Remove(downLoadFilePath)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "success"
- }
- // 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 /fenwei/get/index/info [get]
- func (this *EdbInfoController) GetFenWeiIndexInfo() {
- 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, ",")
- }
- indexInfoList, err := data.GetFenWeiIndexInfo(keyWord, classifyIdList, frequencyList)
- if err != nil {
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = indexInfoList
- }
|