浏览代码

Merge branch 'refs/heads/eta_2.0.8_ly_0804@guomengyuan' into debug

gmy 7 月之前
父节点
当前提交
b75b0c783d

+ 21 - 35
controllers/data_manage/base_from_ly_index_controller.go

@@ -373,13 +373,8 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 		return
 	}
 
-	//keyWord := this.GetString("KeyWord")
-	/*startDate := this.GetString("StartDate")
-	endDate := this.GetString("EndDate")*/
-	//frequency := this.GetString("Frequency")
 	classifyId, _ := this.GetInt("ClassifyId") //分类
-	//mobile := this.GetString("Mobile")         //分类
-	indexCode := this.GetString("IndexCode") //指标唯一编码
+	indexCode := this.GetString("IndexCode")   //指标唯一编码
 
 	if classifyId <= 0 {
 		br.Msg = "请选择分类"
@@ -401,13 +396,8 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 	dir, _ := os.Executable()
 	exPath := filepath.Dir(dir)
 
-	downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	downLoadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
 	xlsxFile := xlsx.NewFile()
-	/*if err != nil {
-		br.Msg = "生成文件失败"
-		br.ErrMsg = "生成文件失败"
-		return
-	}*/
 	frequencies, err := data_manage.GetLyIndexFrequency(classifyId)
 	if err != nil {
 		br.Msg = "查询频度失败"
@@ -416,6 +406,13 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 	}
 
 	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 {
@@ -429,13 +426,12 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 	}
 
 	for _, frequency := range frequencies {
-		//secNameList := make([]*data_manage.BaseFromLyIndex, 0)
-		lyIndices, err := data_manage.GetLyIndexByCodeAndClassify(indexCode, classifyId)
+		lyIndices, err := data_manage.GetLyIndexByCodeAndClassify(indexCode, classifyId, frequency)
 		if err != nil {
 			return
 		}
 
-		if indexCode == "" {
+		if indexCode != "" {
 			fileName = lyIndices[0].IndexName
 		}
 
@@ -460,7 +456,6 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 		rowUnit := sheet.AddRow()
 		celUnit := rowUnit.AddCell()
 		celUnit.SetValue("单位")
-
 		rowModifyDate := sheet.AddRow()
 		rowModifyCell := rowModifyDate.AddCell()
 		rowModifyCell.SetValue("更新时间")
@@ -480,14 +475,8 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 
 			var lyDataList []*data_manage.BaseFromLyData
 			lyDataList, err = data_manage.GetBaseFromLyDataByIndexCode(v.IndexCode)
-			if err != nil {
-				br.Msg = "获取数据失败"
-				br.ErrMsg = "获取数据失败,Err:" + err.Error()
-				return
-			}
-			//items, err := models.GetEdbdataAllByTradeCode(v.IndexCode)
 			if err != nil && err.Error() != utils.ErrNoRow() {
-				br.ErrMsg = "GetEdbdataAllByTradeCode,Err:" + err.Error()
+				br.ErrMsg = "GetBaseFromLyDataByIndexCode,Err:" + err.Error()
 				br.Msg = "获取数据失败"
 				return
 			}
@@ -501,30 +490,27 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 
 		tradeCodeStr := strings.Join(tradeCodeList, "','")
 		tradeCodeStr = "'" + tradeCodeStr + "'"
-		dateList, err := models.GetEdbdataList(tradeCodeStr)
+		dataTimeList, err := data_manage.GetLyDataListByIndexCodes(tradeCodeStr)
 		if err != nil {
 			br.Msg = "获取数据失败"
 			br.ErrMsg = "获取数据失败,Err:" + err.Error()
 			return
 		}
-		for _, n := range dateList {
+		for _, dataTime := range dataTimeList {
 			rowData := sheet.AddRow()
 			celDate := rowData.AddCell()
-			celDate.SetValue(n.Dt)
+			celDate.SetValue(dataTime)
 
 			for _, m := range lyIndices {
 				celData := rowData.AddCell()
-				fmt.Println(m.IndexCode)
-				fmt.Println(n.Dt)
-				if dataMap[m.IndexCode][n.Dt] != nil {
-					celData.SetValue(dataMap[m.IndexCode][n.Dt].Value)
+				if dataMap[m.IndexCode][dataTime] != nil {
+					celData.SetValue(dataMap[m.IndexCode][dataTime].Value)
 				}
 			}
 		}
-
 	}
 
-	err = xlsxFile.Save(downLoadnFilePath)
+	err = xlsxFile.Save(downLoadFilePath)
 	if err != nil {
 		//有指标无数据时先导出一遍空表
 		sheet, err := xlsxFile.AddSheet("无数据")
@@ -536,7 +522,7 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 		rowSecName := sheet.AddRow()
 		celSecName := rowSecName.AddCell()
 		celSecName.SetValue("")
-		err = xlsxFile.Save(downLoadnFilePath)
+		err = xlsxFile.Save(downLoadFilePath)
 		if err != nil {
 			br.Msg = "保存文件失败"
 			br.ErrMsg = "保存文件失败"
@@ -545,9 +531,9 @@ func (this *BaseFromLyIndexController) LyIndexDataExport() {
 	}
 
 	fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
-	this.Ctx.Output.Download(downLoadnFilePath, fileName)
+	this.Ctx.Output.Download(downLoadFilePath, fileName)
 	defer func() {
-		os.Remove(downLoadnFilePath)
+		os.Remove(downLoadFilePath)
 	}()
 	br.Ret = 200
 	br.Success = true

+ 8 - 0
models/data_manage/base_from_ly_classify.go

@@ -38,3 +38,11 @@ func GetAllLyClassify() (items []*BaseFromLyClassify, err error) {
 	_, err = o.Raw(sql).QueryRows(&items)
 	return
 }
+
+// GetLyClassifyById 根据分类id查询
+func GetLyClassifyById(classifyId int) (item *BaseFromLyClassify, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := `SELECT * FROM base_from_ly_classify WHERE base_from_ly_classify_id=?`
+	err = o.Raw(sql, classifyId).QueryRow(&item)
+	return
+}

+ 8 - 1
models/data_manage/base_from_ly_data.go

@@ -45,12 +45,19 @@ func GetLyDataPageByIndexId(indexId int, startSize, pageSize int) (items []*Base
 
 // GetBaseFromLyDataByIndexCode 根据指标编码查询
 func GetBaseFromLyDataByIndexCode(indexCode string) (items []*BaseFromLyData, err error) {
-	sql := `SELECT * FROM base_from_ly_data WHERE index_code=?`
+	sql := `SELECT * FROM base_from_ly_data WHERE index_code=? ORDER BY data_time desc`
 	o := orm.NewOrmUsingDB("data")
 	_, err = o.Raw(sql, indexCode).QueryRows(&items)
 	return
 }
 
+func GetLyDataListByIndexCodes(IndexCodes string) (items []string, err error) {
+	sql := ` SELECT data_time FROM base_from_ly_data WHERE index_code IN(` + IndexCodes + `)  GROUP BY data_time DESC `
+	o := orm.NewOrmUsingDB("data")
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}
+
 // GetLyDataLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
 func GetLyDataLastByIndexCode(indexCodes []string) (items []*BaseFromLyData, err error) {
 	o := orm.NewOrmUsingDB("data")

+ 17 - 8
models/data_manage/base_from_ly_index.go

@@ -144,14 +144,14 @@ func GetLyIndexPage(classifyId string, searchParam string, currentIndex, pageSiz
 }
 
 // UpdateLyIndexEdbExist 指标库标记已添加
-func UpdateLyIndexEdbExist(indexCode string) (err error) {
+func UpdateLyIndexEdbExist(indexCode string, isExist int) (err error) {
 	o := orm.NewOrmUsingDB("data")
 
-	// 构建 SQL 语句
-	sql := `UPDATE base_from_ly_index SET edb_exist = 1 WHERE index_code = ?`
+	// 构建 SQL 更新语句
+	sql := `UPDATE base_from_ly_index SET edb_exist = ? WHERE index_code = ?`
 
 	// 执行 SQL 语句
-	_, err = o.Raw(sql, indexCode).Exec()
+	_, err = o.Raw(sql, isExist, indexCode).Exec()
 	if err != nil {
 		return err
 	}
@@ -184,17 +184,21 @@ func GetLyDataMaxCount(classifyId int) (count int, err error) {
 }
 
 // GetLyIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
-func GetLyIndexByCodeAndClassify(indexCode string, classifyId int) (items []*BaseFromLyIndex, err error) {
+func GetLyIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromLyIndex, err error) {
 	o := orm.NewOrmUsingDB("data")
 
 	// SQL 查询语句
 	sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
 			FROM base_from_ly_index AS a
 			INNER JOIN base_from_ly_data AS b ON a.base_from_ly_index_id = b.base_from_ly_index_id
-			WHERE a.base_from_ly_classify_id = ?`
+			WHERE 1=1`
 
 	var params []interface{}
-	params = append(params, classifyId)
+
+	if classifyId != 0 {
+		sql += ` AND a.base_from_ly_classify_id = ?`
+		params = append(params, classifyId)
+	}
 
 	// 如果 indexCode 不为空,增加过滤条件
 	if indexCode != "" {
@@ -202,6 +206,11 @@ func GetLyIndexByCodeAndClassify(indexCode string, classifyId int) (items []*Bas
 		params = append(params, indexCode)
 	}
 
+	if frequency != nil {
+		sql += ` AND a.frequency = ?`
+		params = append(params, *frequency)
+	}
+
 	sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
 
 	_, err = o.Raw(sql, params...).QueryRows(&items)
@@ -214,7 +223,7 @@ func GetLyIndexByCodeAndClassify(indexCode string, classifyId int) (items []*Bas
 // GetLyIndexFrequency 获取指标频度
 func GetLyIndexFrequency(classifyId int) (items []*string, err error) {
 	sql := `SELECT DISTINCT frequency FROM base_from_ly_index where base_from_ly_classify_id=? AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
-	o := orm.NewOrmUsingDB("edb")
+	o := orm.NewOrmUsingDB("data")
 	_, err = o.Raw(sql, classifyId).QueryRows(&items)
 	return
 }

+ 1 - 1
services/data/base_from_ly_index_service.go

@@ -209,7 +209,7 @@ func LyIndexAdd(req LyIndexAddReq, lang string) (edb *data_manage.EdbInfo, err e
 	edb = edbInfo
 
 	// 标记原始指标为已添加
-	err = data_manage.UpdateLyIndexEdbExist(req.EdbCode)
+	err = data_manage.UpdateLyIndexEdbExist(req.EdbCode, utils.IS_YES)
 	if err != nil {
 		err = fmt.Errorf("BaseIndex: 标记已添加指标库失败, err: %s", err.Error())
 		return

+ 10 - 0
services/data/edb_classify.go

@@ -903,6 +903,16 @@ func Delete(classifyId, edbInfoId int, sysUser *system.Admin, requestBody, reque
 			return
 		}
 
+		// 维护粮油数据源指标领取状态
+		if edbInfo.Source == utils.DATA_SOURCE_LY {
+			err := data_manage.UpdateLyIndexEdbExist(edbInfo.EdbCode, utils.IS_NO)
+			if err != nil {
+				errMsg = "删除失败"
+				err = errors.New("删除失败,Err:" + tmpErr.Error())
+				return
+			}
+		}
+
 		// 如果是同花顺高频数据或类似数据, 还需要删除base_from_edb_mapping对应关系
 		baseMappingOb := new(data_manage.BaseFromEdbMapping)
 		{

+ 6 - 0
utils/constants.go

@@ -331,6 +331,12 @@ const (
 	PREDICT_EDB_INFO_TYPE = 1 //指标类型 1:预测指标
 )
 
+// 是否关系 无实义
+const (
+	IS_YES = 1
+	IS_NO  = 0
+)
+
 // FrequencyDaysMap 频度日期的map关系
 var FrequencyDaysMap = map[string]int{
 	"天": 1, "周": 7, "月": 30, "季": 90, "年": 365,