12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070 |
- 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)
- if indexes != nil {
- // 获取指标数据最新更新时间
- lastModifyTimeList, err := data_manage.GetFenWeiDataLastModifyTimeList(indexCodes)
- if err != nil {
- return
- }
- lastModifyTimeMap := make(map[string]string)
- for _, v := range lastModifyTimeList {
- lastModifyTimeMap[v.IndexCode] = v.ModifyTime
- }
- 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
- if lastModifyTimeMap[v.IndexCode] != "" {
- product.ModifyTime = lastModifyTimeMap[v.IndexCode]
- } else {
- product.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.Unit = indexInfo.Unit
- // 获取数据最新更新时间
- lastModifyTimeList, err := data_manage.GetFenWeiDataLastModifyTimeList([]string{indexInfo.IndexCode})
- if err != nil {
- return
- }
- ret.ModifyTime = lastModifyTimeList[0].ModifyTime
- 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
- }
|