Kaynağa Gözat

睿姿得数据 列表 详情 频率列表

gmy 6 ay önce
ebeveyn
işleme
b278cebb89

+ 25 - 92
controllers/data_manage/base_from_rzd_index_controller.go

@@ -12,7 +12,6 @@ import (
 	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"
@@ -107,83 +106,10 @@ func (this *BaseFromRzdIndexController) RzdIndexData() {
 	}
 	frequency := this.GetString("Frequency")
 
-	// 获取指标
-	var condition string
-	var pars []interface{}
-	if classifyId >= 0 {
-		condition += ` AND base_from_rzd_classify_id=? `
-		pars = append(pars, classifyId)
-	}
-
-	if frequency != "" {
-		condition += ` AND frequency=? `
-		pars = append(pars, frequency)
-	}
-
-	indexes, err := data_manage.GetRzdIndex(condition, pars)
+	resultList, err := data.RzdIndexData(classifyId, frequency, currentIndex, startSize, pageSize)
 	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.GetRzdIndexDataCountGroup(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_RZD, indexCodes)
-	if err != nil {
-		return
-	}
-	edbCodeMap := make(map[string]*data_manage.EdbInfo)
-	for _, v := range edbCodeList {
-		edbCodeMap[v.EdbCode] = v
-	}
-
-	resultList := make([]*data_manage.BaseFromRzdIndexList, 0)
-	for _, v := range indexes {
-		product := new(data_manage.BaseFromRzdIndexList)
-		product.BaseFromRzdIndexId = v.BaseFromRzdIndexId
-		product.BaseFromRzdClassifyId = v.BaseFromRzdClassifyId
-		product.Unit = v.Unit
-		product.IndexCode = v.IndexCode
-		product.IndexName = v.IndexName
-		product.Frequency = v.Frequency
-		product.CreateTime = v.CreateTime
-		product.ModifyTime = v.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.GetRzdIndexData(v.IndexCode, startSize, pageSize)
-		if e != nil {
-			br.Msg = "获取数据失败"
-			br.ErrMsg = "获取指标数据失败,Err:" + e.Error()
-			return
-		}
-		if dataList == nil {
-			dataList = make([]*data_manage.BaseFromRzdData, 0)
-		}
-		product.DataList = dataList
-		product.Paging = page
-		resultList = append(resultList, product)
-	}
 
 	br.Ret = 200
 	br.Success = true
@@ -287,8 +213,12 @@ func (this *BaseFromRzdIndexController) GetRzdFrequencyList() {
 	}
 
 	classifyId, _ := this.GetInt("ClassifyId")
-
-	frequencyList, err := data_manage.GetRzdIndexFrequency(classifyId)
+	if classifyId < 0 {
+		br.Msg = "请选择分类"
+		br.ErrMsg = "请选择分类"
+		return
+	}
+	frequencyList, err := data.GetRzdIndexFrequency(classifyId)
 	if err != nil {
 		return
 	}
@@ -548,24 +478,15 @@ func (this *BaseFromRzdIndexController) RzdIndexDataExport() {
 		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.GetRzdIndexFrequency(classifyId)
+
+	var classifyIdList []int
+	classifyIdList = append(classifyIdList, classifyId)
+	frequencies, err := data_manage.GetRzdIndexFrequency(classifyIdList)
 	if err != nil {
 		br.Msg = "查询频度失败"
 		br.ErrMsg = "查询频度失败"
@@ -743,6 +664,18 @@ func (this *BaseFromRzdIndexController) GetRzdIndexInfo() {
 	classifyIds := this.GetString("ClassifyIds")
 	frequencies := this.GetString("Frequencies")
 
+	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)
+
 	var classifyIdList []string
 	var frequencyList []string
 	if classifyIds != "" {
@@ -751,7 +684,7 @@ func (this *BaseFromRzdIndexController) GetRzdIndexInfo() {
 	if frequencies != "" {
 		frequencyList = strings.Split(frequencies, ",")
 	}
-	indexInfoList, err := data.GetRzdIndexInfo(keyWord, classifyIdList, frequencyList)
+	indexInfoPage, err := data.GetRzdIndexInfo(keyWord, classifyIdList, frequencyList, currentIndex, startSize, pageSize)
 	if err != nil {
 		return
 	}
@@ -759,5 +692,5 @@ func (this *BaseFromRzdIndexController) GetRzdIndexInfo() {
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"
-	br.Data = indexInfoList
+	br.Data = indexInfoPage
 }

+ 11 - 1
models/data_manage/base_from_rzd_classify.go

@@ -1,7 +1,9 @@
 // Package data_manage @Author gmy 2024/10/21 9:26:00
 package data_manage
 
-import "github.com/beego/beego/v2/client/orm"
+import (
+	"github.com/beego/beego/v2/client/orm"
+)
 
 type BaseFromRzdClassify struct {
 	BaseFromRzdClassifyId int    `orm:"column(base_from_rzd_classify_id);pk"`
@@ -48,3 +50,11 @@ func GetRzdClassifyItemByClassifyId(classifyId int) (item *BaseFromRzdClassify,
 	}
 	return item, nil
 }
+
+func GetRzdClassifyItemByParentId(parentId int) (items []*BaseFromRzdClassify, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := ` SELECT * FROM base_from_rzd_classify WHERE parent_id = ?`
+
+	_, err = o.Raw(sql, parentId).QueryRows(&items)
+	return
+}

+ 22 - 0
models/data_manage/base_from_rzd_data.go

@@ -4,6 +4,7 @@ package data_manage
 import (
 	"eta/eta_api/utils"
 	"github.com/beego/beego/v2/client/orm"
+	"strings"
 )
 
 type BaseFromRzdData struct {
@@ -68,3 +69,24 @@ func GetRzdDataListByIndexCodes(IndexCodes string) (items []string, err error) {
 	_, err = o.Raw(sql).QueryRows(&items)
 	return
 }
+
+// GetRzdLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
+func GetRzdLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromRzdData, err error) {
+	o := orm.NewOrmUsingDB("data")
+
+	// 将 indexCodes 切片转换为逗号分隔的字符串
+	placeholders := strings.Repeat("?,", len(indexCodes)-1) + "?"
+
+	// 构造 SQL 查询
+	sql := `SELECT index_code, MAX(modify_time) AS modify_time
+            FROM base_from_rzd_data
+            WHERE index_code IN (` + placeholders + `)
+            GROUP BY index_code`
+
+	// 执行 SQL 查询
+	_, err = o.Raw(sql, indexCodes).QueryRows(&items)
+	if err != nil {
+		return nil, err
+	}
+	return items, nil
+}

+ 36 - 8
models/data_manage/base_from_rzd_index.go

@@ -20,11 +20,29 @@ type BaseFromRzdIndex struct {
 	Unit                  string `orm:"column(unit)"`
 }
 
+type BaseFromRzdIndexAndData struct {
+	BaseFromRzdIndexId    int     `orm:"column(base_from_rzd_index_id);pk"`
+	CreateTime            string  `orm:"column(create_time)"`
+	ModifyTime            string  `orm:"column(modify_time)"`
+	BaseFromRzdClassifyId int     `orm:"column(base_from_rzd_classify_id)"`
+	IndexCode             string  `orm:"column(index_code)"`
+	IndexName             string  `orm:"column(index_name)"`
+	Frequency             string  `orm:"column(frequency)"`
+	Unit                  string  `orm:"column(unit)"`
+	ModifyTimeMax         string  `json:"modify_time_max" description:"最后修改时间"`
+	Value                 float64 `orm:"column(value)" description:"数据值"`
+}
+
+type BaseFromRzdIndexPage struct {
+	List   []*BaseFromRzdIndexAndData `description:"指标列表"`
+	Paging *paging.PagingItem         `description:"分页数据"`
+}
+
 type BaseFromRzdIndexList struct {
 	BaseFromRzdIndexId    int    `orm:"column(base_from_rzd_index_id);pk"`
 	CreateTime            string `orm:"column(create_time)"`
 	ModifyTime            string `orm:"column(modify_time)"`
-	BaseFromRzdClassifyId int    `orm:"column(base_from_ly_classify_id)"`
+	BaseFromRzdClassifyId int    `orm:"column(base_from_rzd_classify_id)"`
 	IndexCode             string `orm:"column(index_code)"`
 	IndexName             string `orm:"column(index_name)"`
 	Frequency             string `orm:"column(frequency)"`
@@ -86,21 +104,21 @@ func GetRzdIndex(condition string, pars interface{}) (items []*BaseFromRzdIndexL
 }
 
 // GetRzdIndexFrequency 获取指标频度
-func GetRzdIndexFrequency(classifyId int) (items []*string, err error) {
+func GetRzdIndexFrequency(classifyIdList []int) (items []*string, err error) {
 	sql := `SELECT DISTINCT frequency 
         FROM base_from_rzd_index 
         WHERE frequency is not null`
 
 	// 如果 classifyId > 0,则添加该条件
-	if classifyId > 0 {
-		sql += ` AND base_from_rzd_classify_id = ?`
+	if len(classifyIdList) > 0 {
+		sql += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
 	}
 
 	sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
 
 	o := orm.NewOrmUsingDB("data")
-	if classifyId > 0 {
-		_, err = o.Raw(sql, classifyId).QueryRows(&items)
+	if len(classifyIdList) > 0 {
+		_, err = o.Raw(sql, classifyIdList).QueryRows(&items)
 	} else {
 		_, err = o.Raw(sql).QueryRows(&items)
 	}
@@ -145,14 +163,24 @@ func GetRzdIndexByCodeAndClassify(indexCode string, classifyId int, frequency *s
 	return
 }
 
+// GetRzdIndexInfoCount 分页查询指标信息行数
+func GetRzdIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
+	o := orm.NewOrmUsingDB("data")
+	sql := ` SELECT count(1) FROM base_from_rzd_index WHERE 1=1  `
+	if condition != "" {
+		sql += condition
+	}
+	err = o.Raw(sql, pars).QueryRow(&count)
+	return
+}
+
 // GetRzdIndexInfoPage 分页查询指标信息
-func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndex, err error) {
+func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
 	o := orm.NewOrmUsingDB("data")
 	sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1  `
 	if condition != "" {
 		sql += condition
 	}
-	sql += ` ORDER BY base_from_rzd_index_id asc`
 	_, err = o.Raw(sql, pars).QueryRows(&items)
 	return
 

+ 10 - 0
services/data/base_from_rzd_classify_service.go

@@ -69,3 +69,13 @@ func RzdClassifyList() ([]*data_manage.BaseFromRzdClassifyResponse, error) {
 
 	return rzdClassifyAndIndexInfos, nil
 }
+
+// GetRzdClassifyItemByParentId 根据分类id获取分类信息
+func GetRzdClassifyItemByParentId(classifyId int) ([]*data_manage.BaseFromRzdClassify, error) {
+	rzdClassify, err := data_manage.GetRzdClassifyItemByParentId(classifyId)
+	if err != nil {
+		return nil, err
+	}
+
+	return rzdClassify, nil
+}

+ 178 - 5
services/data/base_from_rzd_index_service.go

@@ -6,8 +6,142 @@ import (
 	"eta/eta_api/models/data_manage"
 	"eta/eta_api/utils"
 	"fmt"
+	"github.com/rdlucklib/rdluck_tools/paging"
 )
 
+// RzdIndexData 获取睿姿得指标数据
+func RzdIndexData(classifyId int, frequency string, currentIndex, startSize, pageSize int) (dataList []*data_manage.BaseFromRzdIndexList, err error) {
+
+	// 获取指标
+	var condition string
+	var pars []interface{}
+	if classifyId >= 0 {
+		// 递归查询子集分类
+		classifyIdList, err := recursiveQuery(classifyId)
+		if err != nil {
+			return nil, err
+		}
+		classifyIdList = append(classifyIdList, classifyId)
+
+		interfaceClassifyIdList := make([]interface{}, len(classifyIdList))
+		for i, id := range classifyIdList {
+			interfaceClassifyIdList[i] = id
+		}
+
+		condition += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
+		pars = append(pars, interfaceClassifyIdList...)
+	}
+
+	if frequency != "" {
+		condition += ` AND frequency=? `
+		pars = append(pars, frequency)
+	}
+
+	indexes, err := data_manage.GetRzdIndex(condition, pars)
+	if err != nil {
+		return nil, err
+	}
+
+	indexCodes := make([]string, 0)
+	for _, v := range indexes {
+		indexCodes = append(indexCodes, v.IndexCode)
+	}
+	indexCounts, e := data_manage.GetRzdIndexDataCountGroup(indexCodes)
+	if e != nil {
+		return nil, err
+	}
+	countMap := make(map[string]int)
+	for _, v := range indexCounts {
+		countMap[v.IndexCode] = v.Count
+	}
+
+	// 判断是否存在于指标库
+	edbCodeList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_RZD, indexCodes)
+	if err != nil {
+		return
+	}
+	edbCodeMap := make(map[string]*data_manage.EdbInfo)
+	for _, v := range edbCodeList {
+		edbCodeMap[v.EdbCode] = v
+	}
+
+	resultList := make([]*data_manage.BaseFromRzdIndexList, 0)
+	for _, v := range indexes {
+		product := new(data_manage.BaseFromRzdIndexList)
+		product.BaseFromRzdIndexId = v.BaseFromRzdIndexId
+		product.BaseFromRzdClassifyId = v.BaseFromRzdClassifyId
+		product.Unit = v.Unit
+		product.IndexCode = v.IndexCode
+		product.IndexName = v.IndexName
+		product.Frequency = v.Frequency
+		product.CreateTime = v.CreateTime
+		product.ModifyTime = v.ModifyTime
+
+		edbInfo := edbCodeMap[v.IndexCode]
+		if edbInfo != nil {
+			product.EdbInfoId = edbInfo.EdbInfoId
+		}
+
+		total := countMap[v.IndexCode]
+		page := paging.GetPaging(currentIndex, pageSize, total)
+		indexDataList, e := data_manage.GetRzdIndexData(v.IndexCode, startSize, pageSize)
+		if e != nil {
+			return nil, err
+		}
+		if indexDataList == nil {
+			indexDataList = make([]*data_manage.BaseFromRzdData, 0)
+		}
+		product.DataList = indexDataList
+		product.Paging = page
+		resultList = append(resultList, product)
+	}
+
+	return resultList, nil
+}
+
+// 递归查询分类
+func recursiveQuery(classifyId int) ([]int, error) {
+	var classifyIdList []int
+
+	// 查询当前分类 ID 的子分类
+	rzdClassifyList, err := GetRzdClassifyItemByParentId(classifyId)
+	if err != nil {
+		return nil, err
+	}
+
+	// 遍历子分类
+	for _, classify := range rzdClassifyList {
+		// 将当前子分类的 ID 添加到列表中
+		classifyIdList = append(classifyIdList, classify.BaseFromRzdClassifyId)
+
+		// 递归查询当前子分类的子分类
+		childIds, err := recursiveQuery(classify.BaseFromRzdClassifyId)
+		if err != nil {
+			return nil, err
+		}
+
+		// 合并子分类的 ID
+		classifyIdList = append(classifyIdList, childIds...)
+	}
+
+	return classifyIdList, nil
+}
+
+func GetRzdIndexFrequency(classify int) ([]*string, error) {
+
+	classifyIdList, err := recursiveQuery(classify)
+	if err != nil {
+		return nil, err
+	}
+	classifyIdList = append(classifyIdList, classify)
+
+	frequencyNameList, err := data_manage.GetRzdIndexFrequency(classifyIdList)
+	if err != nil {
+		return nil, err
+	}
+	return frequencyNameList, nil
+}
+
 // RzdIndexAddValidate 指标添加校验
 func RzdIndexAddValidate(indexCodes []string) (*[]data_manage.RzdIndexCheckData, error) {
 	// 根据指标编码获取指标库 指标信息
@@ -128,16 +262,17 @@ func RzdIndexAdd(req data_manage.RzdIndexAddReq, lang string) (edb *data_manage.
 }
 
 // GetRzdIndexInfo 获取指标信息-分页
-func GetRzdIndexInfo(keyWord string, classifyIdList []string, frequencyList []string) (rzdIndexInfoList []*data_manage.BaseFromRzdIndex, err error) {
+func GetRzdIndexInfo(keyWord string, classifyIdList []string, frequencyList []string, currentIndex, startSize, pageSize int) (rzdIndexInfoList *data_manage.BaseFromRzdIndexPage, err error) {
 
 	// 获取指标
 	var condition string
 	var pars []interface{}
 	if keyWord != "" {
 		condition += ` AND CONCAT(index_name,index_code) LIKE '%` + keyWord + `%'`
+		pars = append(pars, keyWord)
 	}
 	if len(classifyIdList) > 0 {
-		condition += ` AND classify_id IN (`
+		condition += ` AND base_from_rzd_classify_id IN (`
 		for _, v := range classifyIdList {
 			condition += `?,`
 			pars = append(pars, v)
@@ -153,12 +288,50 @@ func GetRzdIndexInfo(keyWord string, classifyIdList []string, frequencyList []st
 		condition = condition[:len(condition)-1] + `)`
 	}
 
-	rzdIndexInfoList, err = data_manage.GetRzdIndexInfoPage(condition, pars)
+	condition += ` ORDER BY base_from_rzd_index_id asc`
+
+	// 分页
+	condition += ` LIMIT ?, ?`
+	pars = append(pars, startSize, pageSize)
+
+	count, err := data_manage.GetRzdIndexInfoCount(condition, pars)
 	if err != nil {
-		return
+		return nil, err
+	}
+	indexPage := data_manage.BaseFromRzdIndexPage{}
+	page := paging.GetPaging(currentIndex, pageSize, count)
+	if count <= 0 {
+		indexPage.Paging = page
+		return &indexPage, nil
 	}
 
-	return
+	indexInfoPage, err := data_manage.GetRzdIndexInfoPage(condition, pars)
+	if err != nil {
+		return nil, err
+	}
+	var indexCodes []string
+	for _, indexInfo := range indexInfoPage {
+		indexCodes = append(indexCodes, indexInfo.IndexCode)
+	}
+	IndexDataList, err := data_manage.GetRzdLastUpdateTimeLastByIndexCode(indexCodes)
+	if err != nil {
+		return nil, err
+	}
+	var indexDataMap = make(map[string]*data_manage.BaseFromRzdData, 0)
+	for _, data := range IndexDataList {
+		indexDataMap[data.IndexCode] = data
+	}
+	for _, indexInfo := range indexInfoPage {
+		if indexDataMap[indexInfo.IndexCode] == nil {
+			continue
+		}
+		indexInfo.ModifyTimeMax = indexDataMap[indexInfo.IndexCode].ModifyTime
+		indexInfo.Value = indexDataMap[indexInfo.IndexCode].Value
+	}
+
+	indexPage.List = indexInfoPage
+	indexPage.Paging = page
+	return &indexPage, nil
 }
 
 // GetRzdIndexDetail 获取指标详情