123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- // Package data_manage @Author gmy 2024/8/7 9:50:00
- package data_manage
- import (
- "eta/eta_api/utils"
- "github.com/beego/beego/v2/client/orm"
- )
- type BaseFromRzdData struct {
- BaseFromRzdDataId int `orm:"column(base_from_rzd_data_id);pk"`
- BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id)"`
- CreateTime string `orm:"column(create_time)"`
- DataTime string `orm:"column(data_time)"`
- IndexCode string `orm:"column(index_code)"`
- ModifyTime string `orm:"column(modify_time)"`
- Value float64 `orm:"column(value)"`
- }
- // RzdIndexAddReq 指标添加vo
- type RzdIndexAddReq struct {
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频度"`
- Unit string `description:"单位"`
- ClassifyId int `description:"分类ID"`
- AdminId int `description:"管理员ID"`
- AdminRealName string `description:"管理员名称"`
- }
- type RzdIndexDataCountGroup struct {
- IndexCode string
- Count int
- }
- func init() {
- orm.RegisterModel(new(BaseFromRzdData))
- }
- func GetRzdIndexDataCountGroup(indexCodes []string) (items []*RzdIndexDataCountGroup, err error) {
- if len(indexCodes) <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT COUNT(1) AS count, index_code FROM base_from_rzd_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code`
- _, err = o.Raw(sql, indexCodes).QueryRows(&items)
- return
- }
- func GetRzdIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromRzdData, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM base_from_rzd_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
- _, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
- return
- }
- // GetBaseFormRzdDataByIndexCode 根据指标编码查询
- func GetBaseFormRzdDataByIndexCode(indexCode string) (items []*BaseFromRzdData, err error) {
- sql := `SELECT * FROM base_from_rzd_data WHERE index_code=? ORDER BY data_time desc`
- o := orm.NewOrmUsingDB("data")
- _, err = o.Raw(sql, indexCode).QueryRows(&items)
- return
- }
- func GetBaseFormRzdDataByConditionCount(condition string, pars interface{}) (count int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT count(1) FROM base_from_rzd_data WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- err = o.Raw(sql, pars).QueryRow(&count)
- if err != nil {
- return 0, err
- }
- return
- }
- func GetBaseFormRzdDataByCondition(condition string, pars interface{}) (items []*BaseFromRzdData, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM base_from_rzd_data WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- _, err = o.Raw(sql, pars).QueryRows(&items)
- return
- }
- // GetRzdDataListByIndexCodes 根据指标编码查询
- func GetRzdDataListByIndexCodes(IndexCodes string) (items []string, err error) {
- sql := ` SELECT data_time FROM base_from_rzd_data WHERE index_code IN(` + IndexCodes + `) GROUP BY data_time DESC `
- o := orm.NewOrmUsingDB("data")
- _, err = o.Raw(sql).QueryRows(&items)
- return
- }
- // GetRzdLastUpdateTimeLastByIndexCode 根据指标编码查询 返回ModifyTime最后一条数据
- func GetRzdLastUpdateTimeLastByIndexCode(indexCodes []string) (items []*BaseFromRzdData, err error) {
- o := orm.NewOrmUsingDB("data")
- // 构造 SQL 查询
- sql := `SELECT t1.index_code, t1.data_time, t2.value
- FROM (
- SELECT index_code, MAX(data_time) AS data_time
- FROM base_from_rzd_data
- WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `)
- GROUP BY index_code
- ) AS t1
- JOIN base_from_rzd_data AS t2 ON t1.index_code = t2.index_code AND t1.data_time = t2.data_time`
- // 执行 SQL 查询
- _, err = o.Raw(sql, indexCodes).QueryRows(&items)
- if err != nil {
- return nil, err
- }
- return items, nil
- }
|