|
@@ -3,16 +3,33 @@
|
|
|
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/beego/beego/v2/adapter/logs"
|
|
|
+ "github.com/tealeg/xlsx"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type BaseFromLyIndexController struct {
|
|
|
controllers.BaseAuthController
|
|
|
}
|
|
|
|
|
|
+const (
|
|
|
+ codeMax = 30 // 指标编码最大数量
|
|
|
+ dataSource = "粮油商务网"
|
|
|
+)
|
|
|
+
|
|
|
// LyIndexList
|
|
|
// @Title 获取指标列表
|
|
|
// @Description 获取指标列表
|
|
@@ -22,22 +39,9 @@ type BaseFromLyIndexController struct {
|
|
|
// @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
|
|
|
+ br := this.InitResponse()
|
|
|
+ _, err := this.CheckSysUser(br)
|
|
|
+ if err != nil {
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -75,24 +79,12 @@ func (this *BaseFromLyIndexController) LyIndexList() {
|
|
|
// @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
|
|
|
+ br := this.InitResponse()
|
|
|
+ _, err := this.CheckSysUser(br)
|
|
|
+ if err != nil {
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
// 获取指标数据列表
|
|
|
indexId, err := this.GetInt("indexId")
|
|
|
if err != nil {
|
|
@@ -122,3 +114,417 @@ func (this *BaseFromLyIndexController) LyIndexDataList() {
|
|
|
br.Success = true
|
|
|
br.Msg = "获取成功"
|
|
|
}
|
|
|
+
|
|
|
+// LyIndexAddValidate
|
|
|
+// @Title 新增加入到指标库校验
|
|
|
+// @Description 新增加入到指标库校验
|
|
|
+// @Param req body data_manage.BaseFromLyIndexBatchAddCheckReq true "请求参数"
|
|
|
+// @Success 200 string "操作成功"
|
|
|
+// @router /ly/index/add/validate [post]
|
|
|
+func (this *BaseFromLyIndexController) LyIndexAddValidate() {
|
|
|
+ br := this.InitResponse()
|
|
|
+ _, err := this.CheckSysUser(br)
|
|
|
+ if err != nil {
|
|
|
+ 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 = "指标编码数量超过限制"
|
|
|
+ br.ErrMsg = "指标编码数量超过限制,最大数量为" + string(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 指标添加到指标库
|
|
|
+// @Param req body []data_manage.AddEdbInfoReq true "请求参数"
|
|
|
+// @Success 200 string "操作成功"
|
|
|
+// @router /ly/index/add [post]
|
|
|
+func (this *BaseFromLyIndexController) LyIndexAdd() {
|
|
|
+ br := this.InitResponse()
|
|
|
+ sysUser, err := this.CheckSysUser(br)
|
|
|
+ if err != nil {
|
|
|
+ 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 = "批量添加指标数量不得超过30个"
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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
|
|
|
+}
|
|
|
+
|
|
|
+// LyIndexDataExport
|
|
|
+// @Title 导出指标数据
|
|
|
+// @Description 导出指标数据
|
|
|
+// @Param IndexCodes query string true "指标编码"
|
|
|
+// @Success 200 string "操作成功"
|
|
|
+// @router /ly/index/data/export [get]
|
|
|
+func (this *BaseFromLyIndexController) LyIndexDataExport() {
|
|
|
+ br := this.InitResponse()
|
|
|
+ _, err := this.CheckSysUser(br)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取指标
|
|
|
+ var condition string
|
|
|
+ var pars []interface{}
|
|
|
+
|
|
|
+ classifyId, _ := this.GetInt("ClassifyId")
|
|
|
+ if classifyId < 0 {
|
|
|
+ br.Msg = "请选择分类"
|
|
|
+ br.ErrMsg = "请选择分类"
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if classifyId >= 0 {
|
|
|
+ condition += ` AND classify_id=? `
|
|
|
+ pars = append(pars, classifyId)
|
|
|
+ }
|
|
|
+
|
|
|
+ isCheckAll, err := this.GetBool("IsCheckAll")
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var indexCodeString string
|
|
|
+ if !isCheckAll {
|
|
|
+ indexCodeString = strings.TrimSpace(this.GetString("IndexCodes"))
|
|
|
+ if indexCodeString == "" {
|
|
|
+ br.Msg = "请选择指标"
|
|
|
+ br.ErrMsg = "请选择指标"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ indexCodes := strings.Split(indexCodeString, ",")
|
|
|
+ if len(indexCodes) > 0 && !isCheckAll {
|
|
|
+ condition += ` AND index_code IN (`
|
|
|
+ for _, v := range indexCodes {
|
|
|
+ condition += "?,"
|
|
|
+ pars = append(pars, v)
|
|
|
+ }
|
|
|
+ condition = condition[:len(condition)-1]
|
|
|
+ condition += `) `
|
|
|
+ pars = append(pars, indexCodes)
|
|
|
+ }
|
|
|
+
|
|
|
+ indexList, err := data_manage.GetLyIndexList(condition, pars)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "获取数据失败"
|
|
|
+ br.ErrMsg = "获取数据失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ dir, _ := os.Executable()
|
|
|
+ exPath := filepath.Dir(dir)
|
|
|
+
|
|
|
+ downLoadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
|
|
|
+ xlsxFile := xlsx.NewFile()
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("新增Sheet失败", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //获取指标数据
|
|
|
+ sheetNew, _ := xlsxFile.AddSheet(dataSource)
|
|
|
+ indexNameRow := sheetNew.AddRow()
|
|
|
+ frequencyRow := sheetNew.AddRow()
|
|
|
+ unitRow := sheetNew.AddRow()
|
|
|
+ lastModifyDateRow := sheetNew.AddRow()
|
|
|
+ //获取分类下指标最大数据量
|
|
|
+ dataMax, err := data_manage.GetLyDataMaxCount(classifyId)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("获取指标最大数据量失败", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ logs.Info("dataMax:", dataMax)
|
|
|
+
|
|
|
+ setRowIndex := 6
|
|
|
+ for k, sv := range indexList {
|
|
|
+ //获取数据
|
|
|
+ dataList, err := data_manage.GetBaseFromLyDataByIndexCode(sv.IndexCode)
|
|
|
+ if err != nil {
|
|
|
+ br.Msg = "获取数据失败"
|
|
|
+ br.ErrMsg = "获取数据失败,Err:" + err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(dataList) > 0 {
|
|
|
+ indexNameRow.AddCell().SetValue("指标名称")
|
|
|
+ frequencyRow.AddCell().SetValue("频率")
|
|
|
+ unitRow.AddCell().SetValue("单位")
|
|
|
+ lastModifyDateRow.AddCell().SetValue("更新时间")
|
|
|
+
|
|
|
+ indexNameRow.AddCell().SetValue(sv.IndexName)
|
|
|
+ frequencyRow.AddCell().SetValue(sv.Frequency)
|
|
|
+ unitRow.AddCell().SetValue(sv.Unit)
|
|
|
+ lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)
|
|
|
+
|
|
|
+ indexNameRow.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()
|
|
|
+ row.AddCell()
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ endRowIndex := 0
|
|
|
+ for rk, dv := range dataList {
|
|
|
+ rowIndex := setRowIndex + rk
|
|
|
+ row := sheetNew.Row(rowIndex)
|
|
|
+
|
|
|
+ // 在第一列显示时间
|
|
|
+ if k == 0 {
|
|
|
+ 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)
|
|
|
+ } else {
|
|
|
+ row.AddCell() // 其他列不显示时间
|
|
|
+ }
|
|
|
+
|
|
|
+ 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()
|
|
|
+ row.AddCell()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 := dataSource
|
|
|
+ fileName += time.Now().Format(utils.FormatDateUnSpace) + `.xlsx` //文件名称
|
|
|
+ this.Ctx.Output.Download(downLoadFilePath, fileName)
|
|
|
+ defer func() {
|
|
|
+ os.Remove(downLoadFilePath)
|
|
|
+ }()
|
|
|
+ br.Ret = 200
|
|
|
+ br.Success = true
|
|
|
+ br.Msg = "success"
|
|
|
+}
|
|
|
+
|
|
|
+func (this *BaseFromLyIndexController) InitResponse() *models.BaseResponse {
|
|
|
+ br := new(models.BaseResponse).Init()
|
|
|
+ defer func() {
|
|
|
+ if br.ErrMsg == "" {
|
|
|
+ br.IsSendEmail = false
|
|
|
+ }
|
|
|
+ this.Data["json"] = br
|
|
|
+ this.ServeJSON()
|
|
|
+ }()
|
|
|
+ return br
|
|
|
+}
|
|
|
+
|
|
|
+func (this *BaseFromLyIndexController) CheckSysUser(br *models.BaseResponse) (*system.Admin, error) {
|
|
|
+ sysUser := this.SysUser
|
|
|
+ if sysUser == nil {
|
|
|
+ br.Msg = "请登录"
|
|
|
+ br.ErrMsg = "请登录,SysUser Is Empty"
|
|
|
+ br.Ret = 408
|
|
|
+ return nil, fmt.Errorf("请登录")
|
|
|
+ }
|
|
|
+ return sysUser, nil
|
|
|
+}
|