瀏覽代碼

Merge remote-tracking branch 'origin/master'

zwxi 8 月之前
父節點
當前提交
39766fb196

+ 1 - 1
controllers/data_manage/data_manage_permission/data_manage_permission.go

@@ -63,7 +63,7 @@ func (c *DataMangePermissionController) SetEdbChartPermission() {
 		return
 	}
 
-	err, errMsg := data_manage_permission.SetEdbChartPermission(req.Source, req.SubSource, req.UserId, req.UserList, req.IsSelectAll, req.DataIdList, req.NoDataIdList, req.Keyword, req.ClassifyId, sysUser.AdminId)
+	err, errMsg := data_manage_permission.SetEdbChartPermission(req.Source, req.SubSource, req.UserId, req.UserList, req.IsSelectAll, req.DataIdList, req.NoDataIdList, req.Keyword, req.Classify, sysUser.AdminId)
 	if err != nil {
 		//br.Success = true
 		br.Msg = "设置失败"

+ 12 - 0
controllers/data_manage/edb_info_calculate.go

@@ -2005,6 +2005,18 @@ func (this *ChartInfoController) CalculateMapping() {
 		br.ErrMsg = "获取失败,Err:" + e.Error()
 		return
 	}
+
+	// 英文来源,显示英文名称
+	if this.Lang == utils.EnLangVersion {
+		fromEdbInfo, e := data_manage.GetEdbInfoById(item.FromEdbInfoId)
+		if e != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取失败,Err:" + e.Error()
+			return
+		}
+		item.FromEdbName = fromEdbInfo.EdbNameEn
+	}
+
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "保存成功"

+ 784 - 25
controllers/data_manage/mysteel_chemical_data.go

@@ -6,17 +6,22 @@ import (
 	"eta/eta_api/models/data_manage"
 	"eta/eta_api/models/data_manage/request"
 	"eta/eta_api/models/data_manage/response"
+	"eta/eta_api/models/system"
 	"eta/eta_api/services/alarm_msg"
 	"eta/eta_api/services/data"
+	"eta/eta_api/services/data_stat"
+	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"
 	"sort"
+	"strconv"
 	"strings"
 	"time"
+
+	"github.com/rdlucklib/rdluck_tools/paging"
+	"github.com/tealeg/xlsx"
 )
 
 // MysteelChemicalClassify
@@ -588,6 +593,17 @@ func (this *EdbInfoController) MysteelChemicalData() {
 		if dataList == nil {
 			dataList = make([]*data_manage.MysteelChemicalData, 0)
 		}
+		edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_MYSTEEL_CHEMICAL, v.IndexCode)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			br.Msg = "获取数据失败"
+			br.ErrMsg = "获取指标库统计数据失败,Err:" + err.Error()
+			return
+		}
+		if edbInfo == nil {
+			list[k].EdbInfoId = 0
+		} else {
+			list[k].EdbInfoId = edbInfo.EdbInfoId
+		}
 		list[k].DataList = dataList
 		list[k].Paging = page
 	}
@@ -844,38 +860,117 @@ func (this *EdbClassifyController) AddMysteelChemical() {
 		br.ErrMsg = "参数解析失败,Err:" + err.Error()
 		return
 	}
+
 	resp := new(response.AddMysteelChemicalIndexInfoResp)
-	for i, v := range req.List {
-		if v.IndexCode == "" {
-			br.Msg = "请输入指标ID"
-			br.IsSendEmail = false
+	conf, err := models.GetBusinessConf()
+	if err != nil {
+		br.Msg = "添加指标失败"
+		br.ErrMsg = "获取业务配置失败,Err:" + err.Error()
+		return
+	}
+	// 判断钢联化工的数据刷新方式
+	if conf["MySteelDataMethod"] == "api" {
+		if len(req.List) > 150 {
+			br.Msg = "添加指标失败,指标数量不能超过150条"
 			return
 		}
-		if v.BaseFromMysteelChemicalClassifyId <= 0 {
-			br.Msg = "请选择分类"
-			br.IsSendEmail = false
+		addMySteelList := make([]*data_manage.BaseFromMysteelChemicalIndex, 0)
+		for _, v := range req.List {
+			if v.IndexCode == "" {
+				br.Msg = "请输入指标ID"
+				br.IsSendEmail = false
+				return
+			}
+			if v.BaseFromMysteelChemicalClassifyId <= 0 {
+				br.Msg = "请选择分类"
+				br.IsSendEmail = false
+				return
+			}
+
+			var updateTime1, updateTime2 string
+			if v.UpdateTime != `` {
+				updateTimeList := strings.Split(v.UpdateTime, ",")
+				updateTime1 = updateTimeList[0]
+				if len(updateTimeList) > 1 {
+					updateTime2 = updateTimeList[1]
+				}
+			}
+			v.IndexCode = utils.TrimStr(v.IndexCode)
+			v.IndexCode = strings.Replace(v.IndexCode, "\t", "", -1)
+
+			addMySteelList = append(addMySteelList, &data_manage.BaseFromMysteelChemicalIndex{
+				BaseFromMysteelChemicalClassifyId: v.BaseFromMysteelChemicalClassifyId,
+				IndexCode:                         strings.ToUpper(v.IndexCode),
+				IndexName:                         "",
+				Unit:                              "",
+				Frequency:                         "",
+				StartDate:                         "",
+				EndDate:                           "",
+				Describe:                          "",
+				Source:                            "api",
+				TerminalCode:                      "",
+				UpdateWeek:                        v.UpdateWeek,
+				UpdateTime:                        updateTime1,
+				UpdateTime2:                       updateTime2,
+				SysUserId:                         this.SysUser.AdminId,
+				SysUserRealName:                   this.SysUser.RealName,
+				ModifyTime:                        time.Now(),
+				CreateTime:                        time.Now(),
+			})
+		}
+		mySteelChemicalIndexInfos, err, errMsg := data.BatchAddMysteelChemicalIndex(addMySteelList, this.Lang)
+		if err != nil {
+			br.Msg = errMsg
+			br.ErrMsg = errMsg + ";Err:" + err.Error()
 			return
 		}
 
-		v.IndexCode = utils.TrimStr(v.IndexCode)
-		v.IndexCode = strings.Replace(v.IndexCode, "\t", "", -1)
-
-		//添加指标
-		mysteelChemicalIndexInfo, err, errMsg := data.AddMysteelChemicalIndex(v.BaseFromMysteelChemicalClassifyId, strings.ToUpper(v.IndexCode), v.UpdateWeek, v.UpdateTime, this.SysUser.AdminId, this.SysUser.RealName, this.Lang)
-		if errMsg != `` {
-			br.Msg = errMsg
-			br.ErrMsg = errMsg
+		if len(mySteelChemicalIndexInfos) > 0 {
+			resp.BaseFromMysteelChemicalIndexId = mySteelChemicalIndexInfos[0].BaseFromMysteelChemicalIndexId
+			resp.IndexCode = mySteelChemicalIndexInfos[0].IndexCode
+			resp.UniqueCode = fmt.Sprint(mySteelChemicalIndexInfos[0].BaseFromMysteelChemicalClassifyId, "_", mySteelChemicalIndexInfos[0].BaseFromMysteelChemicalIndexId)
+		}
+		for _, v := range mySteelChemicalIndexInfos {
+			err = utils.Rc.LPush(utils.CACHE_MYSTEEL_CHEMICAL_ADD_DATA, v.IndexCode)
 			if err != nil {
-				br.ErrMsg = errMsg + ";Err:" + err.Error()
-			} else {
-				br.IsSendEmail = false
+				br.Msg = "添加指标失败" + v.IndexCode
+				br.ErrMsg = "添加指标缓存失败" + v.IndexCode + ";Err:" + err.Error()
+				return
 			}
-			return
 		}
-		if i == 0 {
-			resp.BaseFromMysteelChemicalIndexId = mysteelChemicalIndexInfo.BaseFromMysteelChemicalIndexId
-			resp.IndexCode = mysteelChemicalIndexInfo.IndexCode
-			resp.UniqueCode = fmt.Sprint(mysteelChemicalIndexInfo.BaseFromMysteelChemicalClassifyId, "_", mysteelChemicalIndexInfo.BaseFromMysteelChemicalIndexId)
+	} else {
+		for i, v := range req.List {
+			if v.IndexCode == "" {
+				br.Msg = "请输入指标ID"
+				br.IsSendEmail = false
+				return
+			}
+			if v.BaseFromMysteelChemicalClassifyId <= 0 {
+				br.Msg = "请选择分类"
+				br.IsSendEmail = false
+				return
+			}
+
+			v.IndexCode = utils.TrimStr(v.IndexCode)
+			v.IndexCode = strings.Replace(v.IndexCode, "\t", "", -1)
+
+			//添加指标
+			mysteelChemicalIndexInfo, err, errMsg := data.AddMysteelChemicalIndex(v.BaseFromMysteelChemicalClassifyId, strings.ToUpper(v.IndexCode), v.UpdateWeek, v.UpdateTime, this.SysUser.AdminId, this.SysUser.RealName, this.Lang)
+			if errMsg != `` {
+				br.Msg = errMsg
+				br.ErrMsg = errMsg
+				if err != nil {
+					br.ErrMsg = errMsg + ";Err:" + err.Error()
+				} else {
+					br.IsSendEmail = false
+				}
+				return
+			}
+			if i == 0 {
+				resp.BaseFromMysteelChemicalIndexId = mysteelChemicalIndexInfo.BaseFromMysteelChemicalIndexId
+				resp.IndexCode = mysteelChemicalIndexInfo.IndexCode
+				resp.UniqueCode = fmt.Sprint(mysteelChemicalIndexInfo.BaseFromMysteelChemicalClassifyId, "_", mysteelChemicalIndexInfo.BaseFromMysteelChemicalIndexId)
+			}
 		}
 	}
 
@@ -1173,3 +1268,667 @@ func (this *EdbInfoController) MysteelChemicalRefresh() {
 	br.Msg = "刷新成功"
 	br.Data = item
 }
+
+// NameCheck
+// @Title 重名校验
+// @Description 批量新增
+// @Param	request	body data_manage.AddEdbInfoReq true "type json string"
+// @Success 200 string "操作成功"
+// @router /mysteel_chemical/name_check [post]
+func (c *EdbInfoController) NameCheck() {
+	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 e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + e.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_MYSTEEL_CHEMICAL, 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_MYSTEEL_CHEMICAL, 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 /mysteel_chemical/batch_add [post]
+func (this *EdbInfoController) BatchAdd() {
+	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_MYSTEEL_CHEMICAL_" + 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.MysteelChemicalIndexSource2EdbReq
+		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.MysteelChemicalIndexSource2Edb(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
+}
+
+// EdbInfoAdd
+// @Title 新增指标接口
+// @Description 新增指标接口
+// @Param	request	body data_manage.AddEdbInfoReq true "type json string"
+// @Success Ret=200 保存成功
+// @router /mysteel_chemical/edb_info/add [post]
+func (this *EdbInfoController) Add() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		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_ADD_" + 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
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+
+	req.EdbName = strings.Trim(req.EdbName, " ")
+	req.EdbCode = strings.Trim(req.EdbCode, " ")
+
+	if req.EdbCode == "" {
+		br.Msg = "指标ID不能为空"
+		return
+	}
+
+	if req.EdbName == "" {
+		br.Msg = "指标名称不能为空"
+		return
+	}
+
+	if req.Frequency == "" {
+		br.Msg = "频率不能为空"
+		return
+	}
+
+	if req.Unit == "" {
+		br.Msg = "单位不能为空"
+		return
+	}
+
+	if req.ClassifyId <= 0 {
+		br.Msg = "请选择分类"
+		return
+	}
+
+	tmpInfo, err := data_manage.GetBaseFromMysteelChemicalIndexByCode(req.EdbCode)
+	if err != nil {
+		if err.Error() != utils.ErrNoRow() {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取失败,Err:" + err.Error()
+			return
+		}
+	}
+	var source int
+	if tmpInfo != nil {
+		source = utils.DATA_SOURCE_MYSTEEL_CHEMICAL
+	} else {
+		br.Msg = "指标不存在"
+		return
+	}
+
+	// 指标入库
+	edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, req.StartDate, req.EndDate, sysUser.AdminId, sysUser.RealName, this.Lang)
+	if err != nil {
+		br.Msg = "保存失败"
+		if errMsg != `` {
+			br.Msg = errMsg
+		}
+		br.ErrMsg = err.Error()
+		br.IsSendEmail = isSendEmail
+		return
+	}
+
+	// 试用平台更新用户累计新增指标数
+	adminItem, e := system.GetSysAdminById(sysUser.AdminId)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
+		return
+	}
+	if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
+		go func() {
+			var r etaTrialService.EtaTrialUserReq
+			r.Mobile = adminItem.Mobile
+			_, _ = etaTrialService.UpdateUserIndexNum(r)
+		}()
+	}
+
+	//新增操作日志
+	{
+		// 添加钢联指标更新日志
+		if edbInfo.Source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
+			go data_stat.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", sysUser, 2)
+		}
+
+		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)
+	}
+
+	// 更新es
+	go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
+
+	resp := new(data_manage.AddEdbInfoResp)
+	resp.EdbInfoId = edbInfo.EdbInfoId
+	resp.UniqueCode = edbInfo.UniqueCode
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "保存成功"
+	br.Data = resp
+	br.IsAddLog = true
+}
+
+// AddCheck
+// @Title 新增指标检测接口
+// @Description 新增指标检测接口
+// @Param   EdbCode   query   string  true      "指标编码/指标代码"
+// @Success Ret=200 保存成功
+// @router /mysteel_chemical/edb_info/add_check [get]
+// func (this *EdbInfoController) AddCheck() {
+// 	br := new(models.BaseResponse).Init()
+// 	defer func() {
+// 		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.AddCheckEdbInfoReq
+// 	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
+// 		br.Msg = "参数解析异常!"
+// 		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+// 		return
+// 	}
+
+// 	source := utils.DATA_SOURCE_MYSTEEL_CHEMICAL
+// 	indexNameMap := make(map[string]string)
+// 	indexNames := make([]string, 0)
+// 	for _, v := range req {
+// 		v.EdbCode = strings.Trim(v.EdbCode, "\t")
+// 		v.EdbCode = strings.Trim(v.EdbCode, " ")
+// 		v.EdbCode = strings.Replace(v.EdbCode, "\t", "", -1)
+// 		if v.EdbCode == "" {
+// 			br.Msg = "请输入指标ID"
+// 			return
+// 		}
+// 		v.EdbName = strings.Trim(v.EdbName, "\t")
+// 		v.EdbName = strings.Trim(v.EdbName, " ")
+// 		v.EdbName = strings.Replace(v.EdbName, "\t", "", -1)
+// 		if v.EdbName == "" {
+// 			br.Msg = "请输入指标名称"
+// 			return
+// 		}
+// 		indexNames = append(indexNames, v.EdbName)
+
+// 		item, err := data_manage.GetEdbInfoByEdbCode(source, edbCode)
+// 		if err != nil && err.Error() != utils.ErrNoRow() {
+// 			br.Msg = "获取失败"
+// 			br.ErrMsg = "获取失败,Err:" + err.Error()
+// 			return
+// 		}
+
+// 		resp := new(data_manage.EdbInfoMySteelChemicalCheckResp)
+// 		if item != nil && item.EdbInfoId > 0 {
+// 			resp.Status = 1
+// 			// 查询该指标是否有权限
+// 			obj := data_manage.EdbInfoNoPermissionAdmin{}
+// 			conf, err := obj.GetByEdbInfoIdAndAdminId(this.SysUser.AdminId, item.EdbInfoId)
+// 			if err != nil && err.Error() != utils.ErrNoRow() {
+// 				br.Msg = "获取失败"
+// 				br.ErrMsg = "获取当前账号的不可见指标配置失败,err:" + err.Error()
+// 				return
+// 			}
+// 			if conf != nil {
+// 				resp.Status = 3
+// 			}
+// 		} else {
+// 			resp.Status = 2
+// 			dataItems, err := data_manage.GetEdbDataAllByEdbCode(edbCode, source, 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(source, edbCode, frequency)
+// 				if err != nil {
+// 					br.Msg = "获取失败"
+// 					br.ErrMsg = "获取失败,Err:" + err.Error()
+// 					return
+// 				}
+// 				if respItem.Ret != 200 {
+// 					br.Msg = "未搜索到该指标"
+// 					br.ErrMsg = respItem.ErrMsg + ";EdbCode:" + edbCode
+// 					return
+// 				}
+// 			}
+// 		}
+// 	}
+
+// 	br.Ret = 200
+// 	br.Success = true
+// 	br.Msg = "保存成功"
+// 	br.Data = resp
+// 	br.IsAddLog = true
+// }
+
+// AddCheck
+// @Title 新增校验
+// @Description 新增校验
+// @Param	request	body request.BusinessDataBatchAddCheckReq true "type json string"
+// @Success 200 string "操作成功"
+// @router /mysteel_chemical/edb_info/add_check [post]
+func (c *EdbInfoController) AddCheck() {
+	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.MysteelChemicalDataBatchAddCheckReq
+	if e := json.Unmarshal(c.Ctx.Input.RequestBody, &req); e != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + e.Error()
+		return
+	}
+	codeMax := 30
+	codeLen := len(req.IndexCodes)
+
+	// 获取指标库已有指标
+	existsEdb, e := data_manage.GetEdbCodesBySource(utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
+	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
+	}
+
+	obj := data_manage.BaseFromMysteelChemicalIndex{}
+	if codeLen == 0 {
+		br.Msg = "请选择指标"
+		return
+	}
+	if codeLen > codeMax {
+		br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
+		return
+	}
+
+	// 查询选中的指标
+	cond := fmt.Sprintf(` AND %s IN (%s)`, data_manage.BaseFromMysteelChemicalIndexCols.IndexCode, utils.GetOrmInReplace(codeLen))
+	pars := make([]interface{}, 0)
+	pars = append(pars, req.IndexCodes)
+	list, e := obj.GeItemsByCondition(cond, pars, 0)
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取自有数据原始指标列表失败, Err: " + e.Error()
+		return
+	}
+
+	if len(list) > codeMax {
+		br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
+		return
+	}
+
+	resp := make([]*data_manage.BaseFromMysteelChemicalIndexItem, 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
+}
+
+// MysteelChemicalSearch
+// @Title 钢联化工指标查询
+// @Description 钢联化工指标查询
+// @Param   BaseFromMysteelChemicalClassifyIds   query   string  true       "分类id"
+// @Param   Keyword   query   string  true       "名称关键词"
+// @Success 200 {object} data_manage.LzFrequency
+// @router /mysteel_chemical/batch_search [get]
+func (this *EdbInfoController) MysteelChemicalBatchSearch() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	sysUser := this.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+	classifyIdStr := this.GetString("BaseFromMysteelChemicalClassifyIds")
+
+	var condition string
+	var pars []interface{}
+	classifyIds := strings.Split(classifyIdStr, ",")
+	if len(classifyIds) > 0 && classifyIds[0] != `` {
+		condition += " AND base_from_mysteel_chemical_classify_id IN (" + utils.GetOrmInReplace(len(classifyIds)) + " ) "
+		pars = append(pars, classifyIds)
+	}
+	keyword := this.GetString("Keyword")
+	if keyword != `` {
+		condition += " AND (index_name like ? OR index_code like ?) "
+		pars = utils.GetLikeKeywordPars(pars, keyword, 2)
+	}
+
+	if classifyIdStr == `` && keyword == `` {
+		var list = make([]*data_manage.MysteelChemicalList, 0)
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "获取成功"
+		br.Data = list
+		return
+	}
+
+	list, err := data_manage.GetMysteelChemicalIndex(condition, pars)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取失败,Err:" + err.Error()
+		return
+	}
+	classifyIdInts := make([]int, 0)
+	for _, v := range list {
+		v.UniqueCode = fmt.Sprint(v.BaseFromMysteelChemicalClassifyId, "_", v.Id)
+		if v.BaseFromMysteelChemicalClassifyId > 0 {
+			classifyIdInts = append(classifyIdInts, v.BaseFromMysteelChemicalClassifyId)
+		}
+	}
+	if len(classifyIdInts) > 0 {
+		// 查询父级分类信息
+		classifyList, e := data_manage.GetBaseFromMysteelChemicalClassifyByIds(classifyIdInts)
+		if e != nil {
+			br.Msg = "获取目录信息失败"
+			br.ErrMsg = "获取失获取目录信息失败,Err:" + e.Error()
+			return
+		}
+		classifyListMap := make(map[int]int, 0)
+		for _, v := range classifyList {
+			classifyListMap[v.BaseFromMysteelChemicalClassifyId] = v.ParentId
+		}
+		for _, v := range list {
+			v.UniqueCode = fmt.Sprint(v.BaseFromMysteelChemicalClassifyId, "_", v.Id)
+			if p, ok := classifyListMap[v.BaseFromMysteelChemicalClassifyId]; ok {
+				v.ParentClassifyId = p
+			}
+		}
+	}
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = list
+}

+ 1 - 1
models/data_manage/data_manage_permission/req_and_resp.go

@@ -40,7 +40,7 @@ type SetEdbChartPermissionReq struct {
 	NoDataIdList []string `description:"指标/图表/表格唯一id列表"`
 	UserList     []int    `description:"赋权用户id列表,如果为空,说明要给这些指标移除权限管控"`
 	IsSelectAll  bool     `description:"是否选择所有指标"`
-	ClassifyId   string   `description:"分类id,支持多选,用英文,隔开"`
+	Classify     string   `description:"分类id,支持多选,用英文,隔开"`
 	Keyword      string   `description:"关键字"`
 }
 

+ 39 - 3
models/data_manage/edb_info.go

@@ -7,12 +7,13 @@ import (
 	"eta/eta_api/services/alarm_msg"
 	"eta/eta_api/utils"
 	"fmt"
-	"github.com/beego/beego/v2/client/orm"
-	"github.com/rdlucklib/rdluck_tools/paging"
-	"go.mongodb.org/mongo-driver/bson"
 	"strconv"
 	"strings"
 	"time"
+
+	"github.com/beego/beego/v2/client/orm"
+	"github.com/rdlucklib/rdluck_tools/paging"
+	"go.mongodb.org/mongo-driver/bson"
 )
 
 type EdbInfo struct {
@@ -156,6 +157,10 @@ type EdbInfoSearchResp struct {
 	StockSearchList []*EdbInfoSearch
 }
 
+type EdbInfoMySteelChemicalCheckResp struct {
+	Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
+}
+
 func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
 	o := orm.NewOrmUsingDB("data")
 	sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
@@ -196,6 +201,20 @@ func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
 	return
 }
 
+// type MysteelChemicalDataListReq struct {
+// 	Frequency    string `form:"Frequency" description:"频度"`
+// 	Source       int    `form:"Source" description:"来源id"`
+// 	Keywords     string `form:"Keywords" description:"指标ID/指标名称"`
+// 	SortField    int    `form:"SortField" description:"排序字段: 0-默认; 1-开始时间; 2-最新时间; 3-更新时间"`
+// 	SortRule     int    `form:"SortRule" description:"排序方式: 0-默认; 1-正序; 2-倒序"`
+// }
+
+// MysteelChemicalDataBatchAddCheckReq 钢联化工指标批量添加校验
+type MysteelChemicalDataBatchAddCheckReq struct {
+	// MysteelChemicalDataListReq
+	IndexCodes []string `form:"IndexCodes" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
+}
+
 type AddEdbInfoReq struct {
 	Source     int    `description:"来源id"`
 	EdbCode    string `description:"指标编码"`
@@ -207,6 +226,12 @@ type AddEdbInfoReq struct {
 	EndDate    string `description:"终止日期"`
 }
 
+type NameCheckEdbInfoReq struct {
+	EdbCode   string `description:"指标编码"`
+	EdbName   string `description:"指标名称"`
+	Frequency string `description:"频率"`
+}
+
 func DeleteEdbInfoAndData(edbInfoId, source, subSource int) (err error) {
 	o := orm.NewOrmUsingDB("data")
 	to, err := o.Begin()
@@ -1707,6 +1732,17 @@ func GetEdbInfoByNameArr(names []string, edbInfoType int) (items []*EdbInfo, err
 	return
 }
 
+// GetEdbInfoByNameArr 根据指标编码获取指标
+func GetEdbInfoByCodeArr(codes []string, edbInfoType int) (items []*EdbInfo, err error) {
+	if len(codes) == 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("data")
+	sql := fmt.Sprintf(`SELECT edb_info_id, edb_code, edb_name FROM edb_info WHERE edb_code IN (%s) AND edb_info_type = ? `, utils.GetOrmInReplace(len(codes)))
+	_, err = o.Raw(sql, codes, edbInfoType).QueryRows(&items)
+	return
+}
+
 // GetEdbCodesBySource 根据来源获取指标编码
 func GetEdbCodesBySource(source int) (items []*EdbInfo, err error) {
 	o := orm.NewOrmUsingDB("data")

+ 90 - 1
models/data_manage/mysteel_chemical_index.go

@@ -3,9 +3,11 @@ package data_manage
 import (
 	"eta/eta_api/utils"
 	"fmt"
+	"strings"
+	"time"
+
 	"github.com/beego/beego/v2/client/orm"
 	"github.com/rdlucklib/rdluck_tools/paging"
-	"time"
 )
 
 // BaseFromMysteelChemicalIndex 钢联化工指标表
@@ -34,12 +36,75 @@ type BaseFromMysteelChemicalIndex struct {
 	EndValue                          float64   `description:"指标的最新值"`
 }
 
+type BaseFromMysteelChemicalIndexItem struct {
+	BaseFromMysteelChemicalIndexId int64  `orm:"column(base_from_mysteel_chemical_index_id);pk"`
+	IndexCode                      string `description:"指标编码"`
+	IndexName                      string `description:"指标名称"`
+	Unit                           string `description:"单位"`
+	Frequency                      string `description:"频度"`
+	Source                         int    `description:"数据来源"`
+	StartDate                      string `description:"开始日期"`
+	EndDate                        string `description:"结束日期"`
+	CreateTime                     string `description:"创建时间"`
+	ModifyTime                     string `description:"修改时间"`
+	EdbInfoId                      int    `description:"eta指标库的id"`
+	EdbUniqueCode                  string `description:"指标库唯一编码"`
+	EdbClassifyId                  int    `description:"指标库分类ID"`
+	IsStop                         int    `description:"是否停更:1:停更,0:未停更"`
+	EdbExist                       int    `description:"指标库是否已添加:0-否;1-是"`
+}
+
+var BaseFromMysteelChemicalIndexCols = struct {
+	BaseFromMysteelChemicalIndexId string
+	IndexCode                      string
+	IndexName                      string
+	Unit                           string
+	Frequency                      string
+	Source                         string
+	StartDate                      string
+	EndDate                        string
+	CreateTime                     string
+	ModifyTime                     string
+}{
+	BaseFromMysteelChemicalIndexId: "base_from_mysteel_chemical_index_id",
+	IndexCode:                      "index_code",
+	IndexName:                      "index_name",
+	Unit:                           "unit",
+	Frequency:                      "frequency",
+	Source:                         "source",
+	StartDate:                      "start_date",
+	EndDate:                        "end_date",
+	CreateTime:                     "create_time",
+	ModifyTime:                     "modify_time",
+}
+
 // Update 更新钢联化工指标基础信息
 func (item *BaseFromMysteelChemicalIndex) Update(cols []string) (err error) {
 	o := orm.NewOrmUsingDB("data")
 	_, err = o.Update(item, cols...)
 	return
 }
+func (m *BaseFromMysteelChemicalIndex) GeItemsByCondition(condition string, pars []interface{}, limitSize int) (items []*BaseFromMysteelChemicalIndexItem, err error) {
+	if pars == nil {
+		pars = make([]interface{}, 0)
+	}
+
+	sql := `select a.*,b.edb_info_id,b.unique_code as edb_unique_code,b.classify_id as edb_classify_id FROM base_from_mysteel_chemical_index AS a LEFT JOIN edb_info b on a.index_code=b.edb_code AND b.source=? WHERE 1=1 `
+	if condition != "" {
+		sql += condition
+	}
+	sql += ` order by a.base_from_mysteel_chemical_index_id desc `
+
+	if limitSize > 0 {
+		sql += " limit ? "
+		pars = append(pars, limitSize)
+	}
+
+	o := orm.NewOrmUsingDB("data")
+	_, err = o.Raw(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, pars).QueryRows(&items)
+
+	return
+}
 
 // AddBaseFromMysteelChemicalIndex 添加钢联化工指标
 func AddBaseFromMysteelChemicalIndex(item *BaseFromMysteelChemicalIndex) (lastId int64, err error) {
@@ -53,6 +118,13 @@ func AddBaseFromMysteelChemicalIndex(item *BaseFromMysteelChemicalIndex) (lastId
 	return
 }
 
+// AddBaseFromMysteelChemicalIndex 添加钢联化工指标
+func BatchAddBaseFromMysteelChemicalIndex(items []*BaseFromMysteelChemicalIndex) (lastId int64, err error) {
+	o := orm.NewOrmUsingDB("data")
+	_, err = o.InsertMulti(len(items), items)
+	return
+}
+
 // BaseFromMysteelChemicalData 钢联化工指标数据表
 type BaseFromMysteelChemicalData struct {
 	BaseFromMysteelChemicalDataId  int       `orm:"column(base_from_mysteel_chemical_data_id);pk"`
@@ -115,6 +187,7 @@ type MysteelChemicalList struct {
 	UnitName                          string             `orm:"column(unit)"`
 	UniqueCode                        string             `description:"唯一编码"`
 	FrequencyName                     string             `orm:"column(frequency)"`
+	EdbInfoId                         int                `description:"指标库的id"`
 	UpdateTime                        string             `orm:"column(modify_time)"`
 	IsStop                            int                `description:"是否停更:1:停更,0:未停更"`
 	Paging                            *paging.PagingItem `description:"分页数据"`
@@ -184,6 +257,22 @@ func GetBaseFromMysteelChemicalIndexByCode(indexCode string) (item *BaseFromMyst
 	return
 }
 
+// GetBaseFromMysteelChemicalIndexByCodeList 根据指标code获取指标信息
+func GetBaseFromMysteelChemicalIndexByCodeList(indexCodeList []string) (items []*BaseFromMysteelChemicalIndex, err error) {
+	if len(indexCodeList) <= 0 {
+		return
+	}
+	o := orm.NewOrmUsingDB("data")
+	sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE index_code IN (%s) `
+	holder := make([]string, 0)
+	for range indexCodeList {
+		holder = append(holder, "?")
+	}
+	sql = fmt.Sprintf(sql, strings.Join(holder, ","))
+	_, err = o.Raw(sql, indexCodeList).QueryRows(&items)
+	return
+}
+
 // GetBaseFromMysteelChemicalIndexByClassifyIdAndName 根据分类id和指标名名获取指标信息
 func GetBaseFromMysteelChemicalIndexByClassifyIdAndName(classifyId int, chartName string) (item *BaseFromMysteelChemicalIndex, err error) {
 	o := orm.NewOrmUsingDB("data")

+ 45 - 0
routers/commentsRouter.go

@@ -4201,6 +4201,24 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"],
+        beego.ControllerComments{
+            Method: "BatchAdd",
+            Router: `/mysteel_chemical/batch_add`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"],
+        beego.ControllerComments{
+            Method: "MysteelChemicalBatchSearch",
+            Router: `/mysteel_chemical/batch_search`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"],
         beego.ControllerComments{
             Method: "MysteelChemicalClassify",
@@ -4228,6 +4246,24 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"],
+        beego.ControllerComments{
+            Method: "Add",
+            Router: `/mysteel_chemical/edb_info/add`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"],
+        beego.ControllerComments{
+            Method: "AddCheck",
+            Router: `/mysteel_chemical/edb_info/add_check`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"],
         beego.ControllerComments{
             Method: "MysteelChemicalFrequency",
@@ -4246,6 +4282,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"],
+        beego.ControllerComments{
+            Method: "NameCheck",
+            Router: `/mysteel_chemical/name_check`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInfoController"],
         beego.ControllerComments{
             Method: "MysteelChemicalRefresh",

+ 161 - 2
services/data/mysteel_chemical.go

@@ -3,13 +3,16 @@ package data
 import (
 	"encoding/json"
 	"errors"
+	"eta/eta_api/models"
 	"eta/eta_api/models/data_manage"
 	"eta/eta_api/models/system"
+	"eta/eta_api/services/alarm_msg"
 	"eta/eta_api/utils"
 	"fmt"
-	"github.com/rdlucklib/rdluck_tools/http"
 	"strings"
 	"time"
+
+	"github.com/rdlucklib/rdluck_tools/http"
 )
 
 // AddMysteelChemicalClassify 添加钢联化工分类
@@ -301,8 +304,137 @@ func DelMysteelChemicalClassify(classifyId int, sysUser *system.Admin) (err erro
 	return
 }
 
+// BatchAddMysteelChemicalIndex 批量添加钢联化工指标
+func BatchAddMysteelChemicalIndex(items []*data_manage.BaseFromMysteelChemicalIndex, lang string) (baseFromMysteelChemicalIndexs []*data_manage.BaseFromMysteelChemicalIndex, err error, errMsg string) {
+	indexCodeList := make([]string, 0)
+	for _, v := range items {
+		indexCodeList = append(indexCodeList, v.IndexCode)
+	}
+
+	baseFromMysteelChemicalIndexs, err = data_manage.GetBaseFromMysteelChemicalIndexByCodeList(indexCodeList)
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		errMsg = `获取数据失败`
+		return
+	}
+	err = nil
+	// 过滤已存在的指标
+	filterMap := make(map[string]struct{})
+	if len(baseFromMysteelChemicalIndexs) > 0 {
+		existIndexCodeList := make([]string, 0)
+		for _, v := range baseFromMysteelChemicalIndexs {
+			existIndexCodeList = append(existIndexCodeList, v.IndexCode)
+			filterMap[v.IndexCode] = struct{}{}
+		}
+		if lang == utils.EnLangVersion {
+			errMsg = fmt.Sprintf(`Metric ID: %s already exists, please re-enter.`, strings.Join(existIndexCodeList, ","))
+		} else {
+			errMsg = fmt.Sprintf(`指标ID: %s 已存在,请重新输入`, strings.Join(existIndexCodeList, ","))
+		}
+		return
+	}
+
+	terminal := getEdbTerminal(utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
+	addItems := make([]*data_manage.BaseFromMysteelChemicalIndex, 0)
+	preAddIndexCodeItems := make([]string, 0)
+	for _, v := range items {
+		if _, ok := filterMap[v.IndexCode]; ok {
+			continue
+		}
+		v.TerminalCode = terminal.TerminalCode
+		addItems = append(addItems, v)
+		preAddIndexCodeItems = append(preAddIndexCodeItems, v.IndexCode)
+	}
+
+	_, err = data_manage.BatchAddBaseFromMysteelChemicalIndex(addItems)
+	if err != nil {
+		errMsg = "保存分类失败"
+		return
+	}
+	baseFromMysteelChemicalIndexs, err = data_manage.GetBaseFromMysteelChemicalIndexByCodeList(preAddIndexCodeItems)
+	if err != nil {
+		errMsg = "获取数据失败"
+		return
+	}
+	return
+}
+
+type HandleMysteelChemicalIndexReq struct {
+	List []*data_manage.BaseFromMysteelChemicalIndex
+}
+
+func BatchAddMysteelChemicalData(items []*data_manage.BaseFromMysteelChemicalIndex, lang string) (resp *models.BaseResponse, err error) {
+	url := utils.EDB_LIB_URL + "mysteel_chemical/handle/api/mysteel/index"
+
+	req := new(HandleMysteelChemicalIndexReq)
+	req.List = items
+	postData, err := json.Marshal(req)
+	if err != nil {
+		return
+	}
+	body, err := HttpPost(url, string(postData), lang)
+	if err != nil {
+		return
+	}
+	if err = json.Unmarshal(body, &resp); err != nil {
+		return
+	}
+	return
+}
+
+type MysteelChemicalIndexSource2EdbReq struct {
+	EdbCode       string
+	EdbName       string
+	Frequency     string
+	Unit          string
+	ClassifyId    int
+	AdminId       int
+	AdminRealName string
+}
+
+// MysteelChemicalIndexSource2Edb 新增钢联化工数据源到指标库
+func MysteelChemicalIndexSource2Edb(req MysteelChemicalIndexSource2EdbReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
+	if req.EdbCode == "" {
+		err = fmt.Errorf("指标ID为空")
+		return
+	}
+	defer func() {
+		if err != nil {
+			tips := fmt.Sprintf("MysteelChemicalIndexSource2Edb新增失败, Err: %s", err.Error())
+			fmt.Println(tips)
+			utils.FileLog.Info(tips)
+		}
+	}()
+	source := utils.DATA_SOURCE_MYSTEEL_CHEMICAL
+
+	// 是否新增过指标
+	exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
+	if e != nil && e.Error() != utils.ErrNoRow() {
+		err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
+		return
+	}
+	if exist != nil {
+		skip = true
+		return
+	}
+
+	// 开始结束时间
+	var startDate, endDate string
+
+	// 新增指标库
+	edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
+	if e != nil {
+		errMsg = msg
+		err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
+		return
+	}
+
+	edb = edbInfo
+
+	return
+}
+
 // AddMysteelChemicalIndex 添加钢联化工指标
-func AddMysteelChemicalIndex(classifyId int, indexCode, updateWeek, updateTimeStr string, sysUserId int, sysUserName,lang string) (baseFromMysteelChemicalIndex *data_manage.BaseFromMysteelChemicalIndex, err error, errMsg string) {
+func AddMysteelChemicalIndex(classifyId int, indexCode, updateWeek, updateTimeStr string, sysUserId int, sysUserName, lang string) (baseFromMysteelChemicalIndex *data_manage.BaseFromMysteelChemicalIndex, err error, errMsg string) {
 	baseFromMysteelChemicalIndex, err = data_manage.GetBaseFromMysteelChemicalIndexByCode(indexCode)
 	if err != nil && err.Error() != utils.ErrNoRow() {
 		errMsg = `获取数据失败`
@@ -688,3 +820,30 @@ func getEdbTerminal(source int) (edbTerminal *data_manage.EdbTerminal) {
 
 	return
 }
+
+// RefreshMysteelChemicalData 根据手工指标code刷新手工指标数据
+func RefreshMysteelChemicalData(edbCode string) {
+	var err error
+	var errMsg string
+	defer func() {
+		if err != nil {
+			go alarm_msg.SendAlarmMsg("根据钢联化工的code刷新指标数据失败提醒,Err"+err.Error(), 3)
+			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"导入手工数据后,根据手工指标code刷新手工指标数据失败提醒", "errmsg:"+err.Error(), utils.EmailSendToUsers)
+		}
+		if errMsg != "" {
+			go alarm_msg.SendAlarmMsg("根据钢联化工的code刷新指标数据失败提醒,errMsg"+errMsg, 3)
+		}
+	}()
+
+	baseFromMysteelChemicalIndexs, err := data_manage.GetBaseFromMysteelChemicalIndexByCodeList([]string{edbCode})
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		return
+	}
+	resp, err := BatchAddMysteelChemicalData(baseFromMysteelChemicalIndexs, "zh")
+	if err != nil {
+		return
+	}
+	if !resp.Success {
+		errMsg = resp.ErrMsg
+	}
+}

+ 21 - 0
services/task.go

@@ -34,6 +34,9 @@ func Task() {
 	//手工数据表格导入后的指标库刷新
 	go ImportManualDataRefresh()
 
+	// 加入钢联化工指标数据之后的刷新
+	go MysteelChemicalDataAdd()
+
 	//修复用户关注标识
 	//GetWxUsersSubscribe()
 
@@ -90,6 +93,24 @@ func ImportManualDataRefresh() {
 	}
 }
 
+// MysteelChemicalDataAdd 加入钢联化工指标数据之后的刷新
+func MysteelChemicalDataAdd() {
+	defer func() {
+		if err := recover(); err != nil {
+			fmt.Println("[ImportManualDataRefresh]", err)
+		}
+	}()
+	for {
+		utils.Rc.Brpop(utils.CACHE_MYSTEEL_CHEMICAL_ADD_DATA, func(b []byte) {
+			edbCode := string(b)
+			edbCode = strings.TrimPrefix(edbCode, `"`)
+			edbCode = strings.TrimSuffix(edbCode, `"`)
+
+			data.RefreshMysteelChemicalData(edbCode)
+		})
+	}
+}
+
 //func init()  {
 //	fmt.Println("start task init")
 //	UpdateEnglishEmailLogErrMsg()

+ 1 - 0
utils/constants.go

@@ -209,6 +209,7 @@ const (
 	CACHE_CHART_INFO_DATA             = "chart:info:data:"                    //图表数据
 	CACHE_CHART_CLASSIFY              = "chart:classify"                      //图表分类数据
 	CACHE_IMPORT_MANUAL_DATA          = "import:manual:data"                  //手工数据导入后刷新
+	CACHE_MYSTEEL_CHEMICAL_ADD_DATA   = "import:mysteelchemical:data"         //钢联化工的数据源导入后刷新
 	CACHE_ACCESS_TOKEN_LOGIN          = "pc_eta_admin:login:"                 //管理后台登录
 	CACHE_ACCESS_TOKEN_LOGIN_NO_TRUST = "pc_eta_admin:login:no_trust:"        //管理后台登录(不可信登录态)
 	CACHE_ABNORMAL_LOGIN              = "pc_eta_admin:login:abnormal:"        //管理后台登录-异常登录