// Package data_manage // @Author gmy 2024/8/7 9:38:00 package data_manage import ( "eta/eta_api/utils" "fmt" "github.com/beego/beego/v2/client/orm" "github.com/rdlucklib/rdluck_tools/paging" ) type BaseFromRzdIndex 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)"` } 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)"` IndexCode string `orm:"column(index_code)"` IndexName string `orm:"column(index_name)"` Frequency string `orm:"column(frequency)"` Unit string `orm:"column(unit)"` DataList []*BaseFromRzdData Paging *paging.PagingItem `description:"分页数据"` EdbInfoId int `description:"指标库主键id"` } // RzdNameCheckResult 校验名称是否存在 type RzdNameCheckResult struct { IndexCode string `from:"EdbCode" description:"edb编码"` IndexName string `from:"EdbName" description:"edb名称"` Exist bool } // RzdIndexCheckData 校验编码是否存在 type RzdIndexCheckData struct { IndexCode string `orm:"column(index_code)" description:"指标编码"` IndexName string `orm:"column(index_name)" description:"指标名称"` Frequency string `orm:"column(frequency)" description:"频度"` Unit string `orm:"column(unit)" description:"单位"` EdbInfoId int `json:"edb_info_id" description:"指标库主键id"` UniqueCode string `json:"unique_code" description:"指标库唯一编码"` ClassifyId int `json:"classify_id" description:"分类id"` } // BaseFromRzdIndexBatchAddCheckReq 校验编码是否存在请求参数 type BaseFromRzdIndexBatchAddCheckReq struct { IndexCodes []string `form:"IndexCodes" description:"指标编码列表"` } func init() { orm.RegisterModel(new(BaseFromRzdIndex)) } // GetRzdIndexByClassifyIds 根据分类id获取指标信息 func GetRzdIndexByClassifyIds(classifyIds []int) (items []*BaseFromRzdIndex, err error) { o := orm.NewOrmUsingDB("data") sql := fmt.Sprintf(`SELECT * FROM base_from_rzd_index WHERE base_from_rzd_classify_id IN (`+utils.GetOrmInReplace(len(classifyIds))) + `)` _, err = o.Raw(sql, classifyIds).QueryRows(&items) if err != nil { return nil, err } return items, nil } func GetRzdIndex(condition string, pars interface{}) (items []*BaseFromRzdIndexList, 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 } // GetRzdIndexFrequency 获取指标频度 func GetRzdIndexFrequency(classifyId 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 = ?` } sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')` o := orm.NewOrmUsingDB("data") if classifyId > 0 { _, err = o.Raw(sql, classifyId).QueryRows(&items) } else { _, err = o.Raw(sql).QueryRows(&items) } return items, err } // GetRzdIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传 func GetRzdIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromRzdIndex, 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_rzd_index AS a INNER JOIN base_from_rzd_data AS b ON a.base_from_rzd_index_id = b.base_from_rzd_index_id WHERE 1=1` var params []interface{} if classifyId != 0 { sql += ` AND a.classify_id = ?` params = append(params, classifyId) } // 如果 indexCode 不为空,增加过滤条件 if indexCode != "" { sql += ` AND a.index_code = ?` 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) if err != nil { return nil, err } return } // GetRzdIndexInfoPage 分页查询指标信息 func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndex, 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 }