123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- // Package data_manage
- // @Author gmy 2024/8/12 14:31:00
- package data_manage
- import (
- "encoding/json"
- "eta/eta_api/controllers"
- "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/tealeg/xlsx"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "time"
- )
- type BaseFromLyIndexController struct {
- controllers.BaseAuthController
- }
- var (
- codeMax = 30 // 指标编码最大数量
- )
- // LyIndexList
- // @Title 获取指标列表
- // @Description 获取指标列表
- // @Success 200 {object} []data_manage.BaseFromLyIndexPage
- // @router /ly/index/list [get]
- func (this *BaseFromLyIndexController) LyIndexList() {
- 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")
- searchParam := this.GetString("SearchParam")
- classifyId := this.GetString("ClassifyId")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- // 获取指标列表
- indexList, err := data.GetIndexPage(classifyId, searchParam, currentIndex, pageSize)
- if err != nil {
- br.Msg = "获取指标列表失败"
- br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
- return
- }
- br.Data = indexList
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // LyIndexDataList
- // @Title 获取指标对应的数据列表
- // @Description 获取指标对应的数据列表
- // @Success 200 {object} data_manage.BaseFromLyDataPage
- // @router /ly/index/data/list [get]
- func (this *BaseFromLyIndexController) LyIndexDataList() {
- 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
- }
- // 获取指标数据列表
- indexId, err := this.GetInt("IndexId")
- if err != nil {
- br.Msg = "获取指标ID失败"
- br.ErrMsg = "获取指标ID失败,Err:" + err.Error()
- return
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- indexDataList, err := data.GetIndexDataPage(indexId, currentIndex, pageSize)
- if err != nil {
- br.Msg = "获取指标数据列表失败"
- br.ErrMsg = "获取指标数据列表失败,Err:" + err.Error()
- return
- }
- br.Data = indexDataList
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // LyIndexAddValidate
- // @Title 新增加入到指标库校验
- // @Description 新增加入到指标库校验
- // @Success 200 {object} []data_manage.IndexCheckData
- // @router /ly/index/add/validate [post]
- func (this *BaseFromLyIndexController) LyIndexAddValidate() {
- 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.BaseFromLyIndexBatchAddCheckReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- codeLen := len(req.IndexCodes)
- if codeLen > codeMax {
- br.Msg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
- br.ErrMsg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
- return
- }
- // 校验指标编码是否存在
- addValidate, err := data.LyIndexAddValidate(req.IndexCodes)
- if err != nil {
- return
- }
- br.Data = addValidate
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // LyIndexAdd
- // @Title 指标添加到指标库
- // @Description 指标添加到指标库
- // @Success 200 string "操作成功"
- // @router /ly/index/add [post]
- func (this *BaseFromLyIndexController) LyIndexAdd() {
- 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_LY_" + 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) > codeMax {
- br.Msg = "批量添加指标数量不得超过" + strconv.Itoa(codeMax) + "个"
- return
- }
- indexNames := make([]string, 0)
- resp := make([]*data_manage.NameCheckResult, 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.NameCheckResult{
- IndexCode: index.EdbCode,
- IndexName: index.EdbName,
- Exist: false,
- })
- }
- // 指标名称重复校验
- nameCheck, err := data.LyIndexNameCheck(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 r data.LyIndexAddReq
- 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, e, errMsg, skip := data.LyIndexAdd(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
- }
- // LyIndexDataExport
- // @Title 导出指标数据
- // @Description 导出指标数据
- // @Success 200 string "操作成功"
- // @router /ly/index/data/export [get]
- func (this *BaseFromLyIndexController) LyIndexDataExport() {
- 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") //指标唯一编码
- //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.GetLyIndexFrequency(classifyId)
- if err != nil {
- br.Msg = "查询频度失败"
- br.ErrMsg = "查询频度失败"
- return
- }
- fileName := `粮油商务网`
- if classifyId > 0 && indexCode == "" {
- lyClassify, err := data_manage.GetLyClassifyById(classifyId)
- if err != nil {
- return
- }
- fileName = lyClassify.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 {
- lyIndices, err := data_manage.GetLyIndexByCodeAndClassify(indexCode, classifyId, frequency)
- if err != nil {
- return
- }
- var sheet *xlsx.Sheet
- if len(lyIndices) > 0 {
- sheet, err = xlsxFile.AddSheet(*frequency)
- if err != nil {
- br.Msg = "新增Sheet失败"
- br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
- return
- }
- } else {
- continue
- }
- if indexCode != "" {
- fileName = lyIndices[0].IndexName
- }
- //获取指标数据
- rowSecName := sheet.AddRow()
- celSecName := rowSecName.AddCell()
- celSecName.SetValue("指标名称/Metric Name")
- rowFrequency := sheet.AddRow()
- celFrequency := rowFrequency.AddCell()
- celFrequency.SetValue("频度/Frequency")
- rowUnit := sheet.AddRow()
- celUnit := rowUnit.AddCell()
- celUnit.SetValue("单位/Unit")
- rowModifyDate := sheet.AddRow()
- rowModifyCell := rowModifyDate.AddCell()
- rowModifyCell.SetValue("更新时间/Update Time")
- dataMap := make(map[string]map[string]*data_manage.BaseFromLyData)
- var tradeCodeList []string
- for _, v := range lyIndices {
- cellSenName := rowSecName.AddCell()
- cellSenName.SetValue(v.IndexName)
- celFrequency := rowFrequency.AddCell()
- celFrequency.SetValue(v.Frequency)
- celUnit := rowUnit.AddCell()
- celUnit.SetValue(v.Unit)
- rowModifyCell := rowModifyDate.AddCell()
- rowModifyCell.SetValue(v.ModifyTime)
- tradeCodeList = append(tradeCodeList, v.IndexCode)
- var lyDataList []*data_manage.BaseFromLyData
- lyDataList, err = data_manage.GetBaseFromLyDataByIndexCode(v.IndexCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.ErrMsg = "GetBaseFromLyDataByIndexCode,Err:" + err.Error()
- br.Msg = "获取数据失败"
- return
- }
- for _, item := range lyDataList {
- if dataMap[item.IndexCode] == nil {
- dataMap[item.IndexCode] = make(map[string]*data_manage.BaseFromLyData)
- }
- dataMap[item.IndexCode][item.DataTime] = item
- }
- }
- tradeCodeStr := strings.Join(tradeCodeList, "','")
- tradeCodeStr = "'" + tradeCodeStr + "'"
- dataTimeList, err := data_manage.GetLyDataListByIndexCodes(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 lyIndices {
- 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"
- }
|