package data_manage import ( "errors" "fmt" "github.com/beego/beego/v2/client/orm" "strconv" "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:"计算公式"` NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"` } 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 time.Time `description:"起始日期"` EndDate time.Time `description:"终止日期"` ClassifyId int `description:"分类id"` UniqueCode string `description:"指标唯一编码"` CalculateFormula string `description:"计算公式"` ModifyTime string `description:"更新时间"` NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"` } type EdbInfoSearchData struct { EdbDataId int `description:"指标数据Id"` DataTime string `description:"数据日期"` Value float64 `description:"数据"` } type EdbInfoSearchDataV1 struct { DataTime string `description:"数据日期"` Value string `description:"数据"` } func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfoList, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM edb_info WHERE 1=1 ` if condition != "" { sql += condition } if order == 1 { sql += ` ORDER BY end_date ASC ` } else { sql += ` ORDER BY edb_info_id ASC ` } _, err = o.Raw(sql, pars).QueryRows(&item) return } func ModifyEdbDataInfoDate(edbInfoId int, maxDate string) (err error) { o := orm.NewOrmUsingDB("data") sql := ` UPDATE edb_info SET end_date=?,modify_time=NOW() WHERE edb_info_id=? ` _, err = o.Raw(sql, maxDate, edbInfoId).Exec() 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.NewOrmUsingDB("data") sql := `` tableName := GetEdbDataTableName(source) if tableName == "" { err = errors.New("无效的表名称:source:" + strconv.Itoa(source)) return nil, err } 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.NewOrmUsingDB("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 } //order:1升序,其余值为降序 func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) { o := orm.NewOrmUsingDB("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 } func GetEdbDataListAllV1(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchDataV1, err error) { o := orm.NewOrmUsingDB("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 } func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT * FROM edb_info WHERE edb_info_id=? ` err = o.Raw(sql, edbInfoId).QueryRow(&item) return } func GetQuarterEdbInfo() (item []*EdbInfo, err error) { o := orm.NewOrmUsingDB("data") sql := ` SELECT c.* FROM chart_info AS a INNER JOIN chart_edb_mapping AS b ON a.chart_info_id=b.chart_info_id INNER JOIN edb_info AS c ON b.edb_info_id=c.edb_info_id WHERE a.chart_type=2 GROUP BY b.edb_info_id ORDER BY b.edb_info_id ASC ` _, err = o.Raw(sql).QueryRows(&item) return } func ResetEdbInfoIsUpdate() (err error) { o := orm.NewOrmUsingDB("data") sql := ` UPDATE edb_info SET is_update=1 ` _, err = o.Raw(sql).Exec() return } // GetEdbInfoCalculateListByCondition 获取指标关系列表 func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) { o := orm.NewOrmUsingDB("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 DeleteEdbDataByIdAndSource(edbDataId, source int) (err error) { sql := ` DELETE FROM %s WHERE edb_data_id=? ` tableName := GetEdbDataTableName(source) sql = fmt.Sprintf(sql, tableName) o := orm.NewOrmUsingDB("data") _, err = o.Raw(sql, edbDataId).Exec() return }