123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932 |
- package data_manage
- import (
- "fmt"
- "hongze/hongze_chart_lib/utils"
- "github.com/rdlucklib/rdluck_tools/orm"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strconv"
- "strings"
- "time"
- )
- type EdbInfo struct {
- EdbInfoId int `orm:"column(edb_info_id);pk"`
- SourceName string `description:"来源名称"`
- Source int `description:"来源id"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- EdbNameSource string `description:"指标名称来源"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- StartDate string `description:"起始日期"`
- EndDate string `description:"终止日期"`
- ClassifyId int `description:"分类id"`
- SysUserId int
- SysUserRealName string
- UniqueCode string `description:"指标唯一编码"`
- CreateTime time.Time
- ModifyTime time.Time
- MinValue float64 `description:"指标最小值"`
- MaxValue float64 `description:"指标最大值"`
- CalculateFormula string `description:"计算公式"`
- EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
- Sort int `description:"排序字段"`
- MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
- MoveFrequency string `description:"移动频度"`
- }
- func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
- o := orm.NewOrm()
- o.Using("data")
- lastId, err = o.Insert(item)
- return
- }
- type EdbInfoItem struct {
- EdbInfoId int `description:"指标id"`
- EdbName string `description:"指标名称"`
- ClassifyId int `description:"指标分类"`
- }
- //用于分类展示
- func GetEdbInfoAll() (items []*EdbClassifyItems, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info order by sort asc,edb_info_id asc`
- _, err = o.Raw(sql).QueryRows(&items)
- return
- }
- //用于分类展示
- func GetEdbInfoAllList() (items []*EdbInfo, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT * FROM edb_info `
- _, err = o.Raw(sql).QueryRows(&items)
- return
- }
- //指标检索数据
- type EdbInfoSearch struct {
- EdbCode string `description:"指标编码"`
- StartDate string `description:"起始日期"`
- EndDate string `description:"终止日期"`
- EdbName string `description:"指标名称"`
- Unit string `description:"单位"`
- Frequency string `description:"频率"`
- DataList []*EdbInfoSearchData
- }
- type EdbInfoSearchData struct {
- DataTime string `description:"数据日期"`
- Value float64 `description:"数据"`
- }
- type EdbInfoSearchResp struct {
- SearchItem *EdbInfoSearch `description:"指标分类"`
- Status int `description:"1:数据已存在于弘则数据库,2:新数据"`
- ClassifyList []*EdbClassifySimplify
- }
- func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
- err = o.Raw(sql, source, edbCode).QueryRow(&item)
- return
- }
- func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&item)
- return
- }
- type AddEdbInfoReq struct {
- Source int `description:"来源id"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- ClassifyId int `description:"分类id"`
- StartDate string `description:"起始日期"`
- EndDate string `description:"终止日期"`
- }
- func DeleteEdbInfoAndData(edbInfoId, source int) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- o.Begin()
- defer func() {
- if err != nil {
- o.Rollback()
- } else {
- o.Commit()
- }
- }()
- sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
- _, err = o.Raw(sql, edbInfoId).Exec()
- if err != nil {
- return
- }
- tableName := GetEdbDataTableName(source)
- if tableName != "" {
- sql = ` DELETE FROM %s WHERE edb_info_id=? `
- sql = fmt.Sprintf(sql, tableName)
- _, err = o.Raw(sql, edbInfoId).Exec()
- if err != nil {
- return
- }
- }
- //calculateTableName := GetEdbInfoCalculateTableName(source)
- //if calculateTableName != "" {
- // sql = ` DELETE FROM %s WHERE edb_info_id=? `
- // sql = fmt.Sprintf(sql, calculateTableName)
- // _, err = o.Raw(sql, edbInfoId).Exec()
- // return
- //}
- //return
- sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
- _, err = o.Raw(sql, edbInfoId).Exec()
- return
- }
- func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- type EditEdbInfoReq struct {
- EdbInfoId int `description:"指标ID"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- ClassifyId int `description:"分类id"`
- CalculateFormula string `description:"计算公式"`
- }
- func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` UPDATE edb_info
- SET
- edb_name =?,
- edb_name_source =?,
- frequency = ?,
- unit = ?,
- classify_id = ?,
- modify_time = NOW()
- WHERE edb_info_id = ?`
- _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.EdbInfoId).Exec()
- return
- }
- type EdbInfoList struct {
- EdbInfoId int `orm:"column(edb_info_id);pk"`
- SourceName string `description:"来源名称"`
- Source int `description:"来源id"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- StartDate string `description:"起始日期"`
- EndDate string `description:"终止日期"`
- ClassifyId int `description:"分类id"`
- UniqueCode string `description:"指标唯一编码"`
- SysUserId int `description:"创建人id"`
- SysUserRealName string `description:"创建人姓名"`
- ModifyTime string `description:"最新修改时间"`
- EdbNameAlias string `json:"-" description:"指标名称,别名"`
- EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
- DataList []*EdbData
- }
- type EdbData struct {
- EdbDataId int `orm:"column(edb_data_id);pk"`
- EdbInfoId int
- DataTime string
- Value float64
- }
- type EdbInfoListResp struct {
- Paging *paging.PagingItem
- Item *EdbInfoList
- ClassifyList []*EdbClassifySimplify
- }
- func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT * FROM edb_info WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- err = o.Raw(sql, pars).QueryRow(&item)
- return
- }
- func GetEdbDataCountByCondition(condition string, pars []interface{}, source int) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- tableName := GetEdbDataTableName(source)
- sql := ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 AND status=1 `
- sql = fmt.Sprintf(sql, tableName)
- if condition != "" {
- sql += condition
- }
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- func GetEdbDataListByCondition(condition string, pars []interface{}, source, pageSize, startSize int) (item []*EdbData, err error) {
- o := orm.NewOrm()
- o.Using("data")
- tableName := GetEdbDataTableName(source)
- sql := ` SELECT * FROM %s WHERE 1=1 AND status=1 `
- sql = fmt.Sprintf(sql, tableName)
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY data_time DESC `
- sql += ` LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
- return
- }
- func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT * FROM edb_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
- err = o.Raw(sql).QueryRow(&item)
- return
- }
- func ModifyEdbInfoModifyTime(edbInfoId int64) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` UPDATE edb_info SET modify_time = NOW() WHERE edb_info_id = ? `
- _, err = o.Raw(sql, edbInfoId).Exec()
- return
- }
- type MoveEdbInfoReq struct {
- EdbInfoId int `description:"指标ID"`
- PrevEdbInfoId int `description:"上一个指标ID"`
- NextEdbInfoId int `description:"下一个指标ID"`
- ClassifyId int `description:"分类id"`
- }
- func MoveEdbInfo(edbInfoId, classifyId int) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` UPDATE edb_info
- SET
- classify_id = ?
- WHERE edb_info_id = ?`
- _, err = o.Raw(sql, classifyId, edbInfoId).Exec()
- return
- }
- func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT * FROM edb_info WHERE edb_name=? `
- _, err = o.Raw(sql, edbName).QueryRows(&items)
- return
- }
- func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
- _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
- return
- }
- func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&count)
- return
- }
- type ChartEdbInfo struct {
- EdbInfoId int `description:"指标id"`
- EdbName string `description:"指标名称"`
- SourceName string `json:"-"`
- EdbNameAlias string `json:"-" description:"指标名称,别名"`
- }
- func EdbInfoSearchByKeyWord(KeyWord string) (searchList []*ChartEdbInfo, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info `
- if KeyWord != "" {
- sql += `WHERE (edb_name LIKE '%` + KeyWord + `%' OR edb_code LIKE '%` + KeyWord + `%' ) `
- }
- sql += ` ORDER BY create_time DESC `
- _, err = o.Raw(sql).QueryRows(&searchList)
- return
- }
- type EdbInfoMaxAndMinInfo struct {
- MinDate string `description:"最小日期"`
- MaxDate string `description:"最大日期"`
- MinValue float64 `description:"最小值"`
- MaxValue float64 `description:"最大值"`
- LatestValue float64 `description:"最新值"`
- }
- func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ``
- tableName := GetEdbDataTableName(source)
- sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM %s WHERE edb_code=? `
- sql = fmt.Sprintf(sql, tableName)
- err = o.Raw(sql, edbCode).QueryRow(&item)
- var latest_value float64
- sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
- sql = fmt.Sprintf(sql, tableName)
- err = o.Raw(sql, edbCode).QueryRow(&latest_value)
- item.LatestValue = latest_value
- return
- }
- func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? `
- _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
- return
- }
- func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT * FROM edb_info WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY create_time DESC `
- _, err = o.Raw(sql, pars).QueryRows(&list)
- return
- }
- //order:1升序,其余值为降序
- func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ``
- tableName := GetEdbDataTableName(source)
- sql = ` SELECT * FROM %s WHERE 1=1 `
- sql = fmt.Sprintf(sql, tableName)
- if condition != "" {
- sql += condition
- }
- if order == 1 {
- sql += ` ORDER BY data_time ASC `
- } else {
- sql += ` ORDER BY data_time DESC `
- }
- _, err = o.Raw(sql, pars).QueryRows(&item)
- return
- }
- // GetLastEdbData 获取最近的一条指标数据
- func GetLastEdbData(condition string, pars []interface{}, source int) (item *EdbInfoSearchData, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ``
- tableName := GetEdbDataTableName(source)
- sql = ` SELECT * FROM %s WHERE 1=1 `
- sql = fmt.Sprintf(sql, tableName)
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY data_time DESC `
- err = o.Raw(sql, pars).QueryRow(&item)
- return
- }
- //order:1升序,其余值为降序
- func GetEdbDataCount(condition string, pars []interface{}, source int) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ``
- tableName := GetEdbDataTableName(source)
- sql = ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
- sql = fmt.Sprintf(sql, tableName)
- if condition != "" {
- sql += condition
- }
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- func ModifyCalculateEdbInfo(item *EditEdbInfoReq) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` UPDATE edb_info
- SET
- edb_name =?,
- edb_name_source =?,
- frequency = ?,
- unit = ?,
- classify_id = ?,
- calculate_formula=?,
- modify_time = NOW()
- WHERE edb_info_id = ?`
- _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.CalculateFormula, item.EdbInfoId).Exec()
- return
- }
- type AddEdbInfoResp struct {
- EdbInfoId int `description:"指标ID"`
- UniqueCode string `description:"指标唯一编码"`
- }
- //根据基础指标获取所有关联指标
- func GetNeedRefreshCalculateEdbInfoFromBase(fromEdbInfo int) (list []*EdbInfo, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT b.*
- FROM edb_info_calculate AS a
- INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
- WHERE from_edb_info_id=? `
- _, err = o.Raw(sql, fromEdbInfo).QueryRows(&list)
- return
- }
- func GetEdbInfoCalculateCount(edbInfoId int) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate WHERE from_edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&count)
- return
- }
- func GetEdbInfoCalculateLjzzyCount(edbInfoId int) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_ljzzy WHERE from_edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&count)
- return
- }
- func GetEdbInfoCalculateTbzCount(edbInfoId int) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tbz WHERE from_edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&count)
- return
- }
- func GetEdbInfoCalculateTczCount(edbInfoId int) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tcz WHERE from_edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&count)
- return
- }
- func GetEdbInfoCalculateNszydpjjsCount(edbInfoId int) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_nszydpjjs WHERE from_edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&count)
- return
- }
- func GetEdbInfoCalculateCountByCondition(source int, condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- o.Using("data")
- //calculateTableName := GetEdbInfoCalculateTableName(source)
- //if calculateTableName == "" {
- // err = errors.New("无效的表名")
- // return
- //}
- sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_mapping WHERE 1=1 `
- //sql = fmt.Sprintf(sql, calculateTableName)
- if condition != "" {
- sql += condition
- }
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- // GetEdbInfoCalculateListByCondition 获取指标关系列表
- func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
- o := orm.NewOrm()
- o.Using("data")
- //calculateTableName := GetEdbInfoCalculateTableName(source)
- //if calculateTableName == "" {
- // err = errors.New("无效的表名")
- // return
- //}
- sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
- //sql = fmt.Sprintf(sql, calculateTableName)
- if condition != "" {
- sql += condition
- }
- _, err = o.Raw(sql, pars).QueryRows(&items)
- return
- }
- func GetRefreshEdbInfoFromCalculate(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
- calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- for _, item := range calculateList {
- if item.EdbType == 1 {
- baseEdbInfoArr = append(baseEdbInfoArr, item)
- } else {
- calculateInfoArr = append(calculateInfoArr, item)
- newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromCalculate(item.EdbInfoId, item.Source)
- baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
- calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
- }
- }
- return
- }
- //获取基础指标对应的所有计算指标
- func GetEdbInfoAllCalculate(edbInfoId int) (list []*EdbInfo, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT a.*,b.start_date,b.end_date,b.frequency FROM edb_info_calculate_mapping AS a
- INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
- WHERE a.from_edb_info_id=?
- GROUP BY a.edb_info_id
- ORDER BY a.edb_info_id ASC `
- _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
- return
- }
- func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
- calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- for _, item := range calculateList {
- if item.EdbType == 1 {
- baseEdbInfoArr = append(baseEdbInfoArr, item)
- } else {
- calculateInfoArr = append(calculateInfoArr, item)
- newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
- baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
- calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
- }
- }
- return
- }
- func ModifyEdbInfoDataStatus(edbInfoId int64, source int, edbCode string) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ``
- tableName := GetEdbDataTableName(source)
- sql = ` UPDATE %s SET edb_info_id=?, status=1,modify_time=NOW() WHERE edb_code=? `
- sql = fmt.Sprintf(sql, tableName)
- _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
- return
- }
- // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
- func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
- err = o.Raw(sql, classifyId).QueryRow(&item)
- return
- }
- // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
- func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=? and sort > ?`
- if prevEdbInfoId > 0 {
- sql += ` or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` ) `
- }
- _, err = o.Raw(sql, classifyId, nowSort).Exec()
- return
- }
- // Update 更新指标基础信息
- func (edbInfo *EdbInfo) Update(cols []string) (err error) {
- o := orm.NewOrm()
- o.Using("data")
- _, err = o.Update(edbInfo, cols...)
- return
- }
- type EdbInfoDataResp struct {
- EdbInfo *EdbInfo
- DataList []*EdbDataList
- }
- type EdbInfoReplaceReq struct {
- OldEdbInfoId int `description:"原指标ID"`
- NewEdbInfoId int `description:"替换为指标ID"`
- }
- /*
- 1、替换图库中的指标
- 2、替换计算指标中的指标,完了计算指标中的指标,全部重新计算
- */
- func EdbInfoReplace(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (replaceChartTotal, replaceCalculateTotal int, err error) {
- var errmsg string
- o := orm.NewOrm()
- o.Using("data")
- o.Begin()
- defer func() {
- if errmsg != "" {
- fmt.Println("errmsg:" + errmsg)
- }
- }()
- //替换图表
- chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
- csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
- _, err = o.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
- if err != nil {
- errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
- return
- }
- replaceChartTotal = len(chartEdbMappingList)
- for _, mv := range chartEdbMappingList {
- //获取图表所有指标信息
- chartEdbList := make([]*ChartEdbInfoMapping, 0)
- csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
- _, err = o.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
- if err != nil {
- errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
- return
- }
- var minData, maxData float64
- minData = newEdbInfo.MinValue
- maxData = newEdbInfo.MaxValue
- for _, cv := range chartEdbList {
- if mv.IsAxis == cv.IsAxis {
- if minData > cv.MinData {
- minData = cv.MinData
- }
- if maxData < cv.MaxData {
- maxData = cv.MaxData
- }
- }
- }
- //修改图表关联指标
- rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
- _, err = o.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
- if err != nil {
- errmsg = "更新图库指标信息失败:Err:" + err.Error()
- return
- }
- }
- //替换计算指标
- //获取所有包含的计算指标
- mappingList := make([]*EdbInfoCalculateMapping, 0)
- msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
- _, err = o.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
- if err != nil {
- errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
- return
- }
- replaceCalculateTotal = len(mappingList)
- //计算指标
- for _, mv := range mappingList {
- //替换原指标
- msql := `UPDATE edb_info_calculate_mapping SET from_edb_info_id=?,from_edb_code=?,from_edb_name=?,from_source=?,from_source_name=? WHERE edb_info_calculate_mapping_id=? `
- _, err = o.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
- if err != nil {
- return
- }
- }
- if err != nil && errmsg != "" {
- go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
- o.Rollback()
- } else {
- o.Commit()
- }
- //获取计算指标关联指标
- allMappingList := make([]*EdbInfoCalculateMapping, 0)
- allMapping := make(map[int]int)
- for _, v := range mappingList {
- if _, ok := allMapping[v.EdbInfoId]; !ok {
- allMappingList = append(allMappingList, v)
- }
- newList, _ := GetAllCalculate(v.EdbInfoId)
- for _, nv := range newList {
- if _, ok := allMapping[v.EdbInfoId]; !ok {
- allMappingList = append(allMappingList, nv)
- }
- allMapping[v.EdbInfoId] = v.EdbInfoId
- }
- allMapping[v.EdbInfoId] = v.EdbInfoId
- }
- for mk, mv := range allMappingList { //原指标关联的所有计算指标
- fmt.Println("mk/mv", mk, mv)
- //重新计算计算指标
- {
- edbInfo, err := GetEdbInfoById(mv.EdbInfoId) //计算指标
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errmsg = "计算指标已被删除"
- return replaceChartTotal, replaceCalculateTotal, err
- }
- errmsg = "获取计算指标失败:err:" + err.Error()
- return replaceChartTotal, replaceCalculateTotal, err
- }
- fromEdbInfoList, err := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
- if err != nil {
- errmsg = "获取计算指标失败:err:" + err.Error()
- return replaceChartTotal, replaceCalculateTotal, err
- }
- if len(fromEdbInfoList) <= 0 {
- errmsg = "计算指标所依赖指标不存在"
- return replaceChartTotal, replaceCalculateTotal, err
- }
- fromEdbInfoItem := fromEdbInfoList[0]
- if fromEdbInfoItem == nil {
- return replaceChartTotal, replaceCalculateTotal, err
- }
- fromEdbInfo, err := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
- if err != nil {
- errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
- return replaceChartTotal, replaceCalculateTotal, err
- }
- edbCode := edbInfo.EdbCode
- uniqueCode := edbInfo.UniqueCode
- sourName := edbInfo.SourceName
- source := edbInfo.Source
- edbInfoId := edbInfo.EdbInfoId
- req := new(EdbInfoCalculateBatchSaveReq)
- req.EdbInfoId = edbInfoId
- req.EdbName = edbInfo.EdbName
- req.Frequency = edbInfo.Frequency
- req.Unit = edbInfo.Unit
- req.ClassifyId = edbInfo.ClassifyId
- req.FromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
- if source == utils.DATA_SOURCE_CALCULATE {
- //检验公式
- var formulaStr string
- var edbInfoIdBytes []string
- for _, v := range fromEdbInfoList {
- formulaStr += v.FromTag + ","
- edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
- }
- formulaStr = strings.Trim(formulaStr, ",")
- formulaMap := CheckFormula(edbInfo.CalculateFormula)
- for _, v := range formulaMap {
- if !strings.Contains(formulaStr, v) {
- errmsg = "公式错误,请重新填写"
- return replaceChartTotal, replaceCalculateTotal, err
- }
- }
- edbInfoList := make([]*EdbInfo, 0)
- for _, v := range fromEdbInfoList {
- edbInfo, err := GetEdbInfoById(v.FromEdbInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
- return replaceChartTotal, replaceCalculateTotal, err
- }
- errmsg = "获取指标失败:Err:" + err.Error()
- return replaceChartTotal, replaceCalculateTotal, err
- }
- edbInfoList = append(edbInfoList, edbInfo)
- }
- //清除历史数据
- err = DeleteCalculateData(edbInfoId)
- if err != nil {
- errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
- return replaceChartTotal, replaceCalculateTotal, err
- }
- err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
- if err != nil {
- errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
- return replaceChartTotal, replaceCalculateTotal, err
- }
- } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
- _, err = AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
- } else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
- sourName = "同比值"
- edbInfoId, err = AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
- } else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
- sourName = "同差值"
- edbInfoId, err = AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
- } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
- sourName = "N数值移动平均计算"
- formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
- edbInfoId, err = AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
- } else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
- sourName = "环比值"
- formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
- edbInfoId, err = AddCalculateHbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
- } else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
- sourName = "环差值"
- formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
- edbInfoId, err = AddCalculateHcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
- } else if source == utils.DATA_SOURCE_CALCULATE_BP {
- sourName = "变频"
- fmt.Println("变频", req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
- edbInfoId, err = AddCalculateBp(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
- } else {
- errmsg = "无效计算方式,source:" + strconv.Itoa(source)
- return replaceChartTotal, replaceCalculateTotal, err
- }
- maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(source, edbCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
- return replaceChartTotal, replaceCalculateTotal, err
- }
- if maxAndMinItem != nil {
- err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
- }
- }
- }
- return
- }
- type EdbInfoView struct {
- EdbInfoId int `orm:"column(edb_info_id);pk"`
- SourceName string `description:"来源名称"`
- Source int `description:"来源id"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- EdbNameSource string `description:"指标名称来源"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- StartDate string `description:"起始日期"`
- EndDate string `description:"终止日期"`
- ClassifyId int `description:"分类id"`
- SysUserId int
- SysUserRealName string
- UniqueCode string `description:"指标唯一编码"`
- CreateTime string
- ModifyTime string
- MinValue float64 `description:"指标最小值"`
- MaxValue float64 `description:"指标最大值"`
- CalculateFormula string `description:"计算公式"`
- EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
- Sort int `description:"排序字段"`
- IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
- LatestDate string `description:"数据最新日期"`
- LatestValue float64 `description:"数据最新值"`
- }
- //获取指标的所有计算指标,以及计算指标所依赖计算指标
- func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
- o := orm.NewOrm()
- o.Using("data")
- msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
- _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
- if err != nil {
- return
- }
- return
- }
- func GetAllCalculate(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
- calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- for _, item := range calculateList {
- mappingList = append(mappingList, item)
- newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId)
- mappingList = append(mappingList, newCalculateInfoArr...)
- }
- return
- }
|