// Package data_manage // @Author gmy 2024/8/7 9:38:00 package data_manage import ( "github.com/beego/beego/v2/client/orm" "github.com/rdlucklib/rdluck_tools/paging" ) type BaseFromLyIndex struct { BaseFromLyIndexId int `orm:"column(base_from_ly_index_id);pk" description:"指标ID"` CreateTime string `orm:"column(create_time)" description:"创建时间"` ModifyTime string `orm:"column(modify_time)" description:"修改时间"` BaseFromLyClassifyId int `orm:"column(base_from_ly_classify_id)" description:"原始数据指标分类id"` 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:"单位"` EdbExist int `orm:"column(edb_exist)" description:"指标库是否已添加:0-否;1-是"` } // 在 init 函数中注册模型 func init() { orm.RegisterModel(new(BaseFromLyIndex)) } type BaseFromLyIndexPage struct { List []*BaseFromLyIndex `description:"指标列表"` Paging *paging.PagingItem `description:"分页数据"` } type BaseFromLyIndexBatchAddCheckReq struct { IndexCodes []string `form:"IndexCodes" description:"指标编码列表"` } type BaseFromLyIndexNameCheck struct { IndexCode string `from:"IndexCode" description:"指标编码"` IndexName string `from:"IndexName" description:"指标名称"` } type NameCheckResult struct { IndexCode string `from:"EdbCode" description:"edb编码"` IndexName string `from:"EdbName" description:"edb名称"` Exist bool } // GetLyIndexByClassifyIds 通过分类ids查询指标列表 func GetLyIndexByClassifyIds(classifyIds []int) (items []*BaseFromLyIndex, err error) { o := orm.NewOrmUsingDB("data") // 创建查询条件 qs := o.QueryTable("base_from_ly_index") // 使用 Filter 进行查询 _, err = qs.Filter("base_from_ly_classify_id__in", classifyIds).All(&items) return } // GetLyIndexCount 获取指标总数 func GetLyIndexCount(classifyId string, searchParam string) (count int, err error) { o := orm.NewOrmUsingDB("data") qs := o.QueryTable("base_from_ly_index") // 精确匹配 classifyId if classifyId != "" { qs = qs.Filter("base_from_ly_classify_id", classifyId) } if searchParam != "" { qs = qs.Filter("index_name__icontains", searchParam). Filter("index_code", searchParam) } number, err := qs.Count() if err != nil { return 0, err } return int(number), nil } // GetLyIndexPage 获取指标列表 func GetLyIndexPage(classifyId string, searchParam string, currentIndex, pageSize int) (items []*BaseFromLyIndex, err error) { o := orm.NewOrmUsingDB("data") qs := o.QueryTable("base_from_ly_index") // 精确匹配 classifyId if classifyId != "" { qs = qs.Filter("base_from_ly_classify_id", classifyId) } // 添加搜索条件 if searchParam != "" { cond := orm.NewCondition() cond = cond.Or("index_name__icontains", searchParam).Or("index_code", searchParam) qs = qs.SetCond(cond) } // id降序排序与分页 qs = qs.OrderBy("-base_from_ly_index_id") qs = qs.Limit(pageSize, (currentIndex-1)*pageSize) // 执行查询 _, err = qs.All(&items) return } // UpdateLyIndexEdbExist 指标库标记已添加 func UpdateLyIndexEdbExist(indexCode string) (err error) { o := orm.NewOrmUsingDB("data") lyIndex := BaseFromLyIndex{IndexCode: indexCode} lyIndex.EdbExist = 1 _, err = o.Update(&lyIndex, "edb_exist") if err != nil { return err } return } // GetLyIndexList 根据传入条件查询指标列表 func GetLyIndexList(condition string, pars interface{}) (items []*BaseFromLyIndex, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM base_from_ly_index WHERE 1=1 ` if condition != "" { sql += condition } sql += `ORDER BY base_from_ly_index_id ASC ` _, err = o.Raw(sql, pars).QueryRows(&items) return } // GetLyDataMaxCount 获取分类下指标最大数据量 func GetLyDataMaxCount(classifyId int) (count int, err error) { o := orm.NewOrmUsingDB("data") sql := `SELECT MAX(t.num) AS count FROM ( SELECT COUNT(1) AS num 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=? GROUP BY a.base_from_ly_index_id )AS t ` err = o.Raw(sql, classifyId).QueryRow(&count) return }