12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943 |
- package data_manage
- import (
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strconv"
- "strings"
- "time"
- )
- type EdbInfo struct {
- EdbInfoId int `orm:"column(edb_info_id);pk"`
- EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
- SourceName string `description:"来源名称"`
- Source int `description:"来源id"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- EdbNameEn string `description:"英文指标名称"`
- EdbNameSource string `description:"指标名称来源"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- UnitEn 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:"排序字段"`
- LatestDate string `description:"数据最新日期(实际日期)"`
- LatestValue float64 `description:"数据最新值(实际值)"`
- EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
- MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
- MoveFrequency string `description:"移动频度"`
- NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
- ServerUrl string `description:"服务器地址"`
- ChartImage string `description:"图表图片"`
- Calendar string `description:"公历/农历" orm:"default(公历);"`
- DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
- ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
- EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
- MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
- TerminalCode string `description:"终端编码,用于配置在机器上"`
- DataUpdateTime string `description:"最近一次数据发生变化的时间"`
- ErDataUpdateDate string `description:"本次更新,数据发生变化的最早日期"`
- SourceIndexName string `description:"数据源中的指标名称"`
- SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
- SubSourceName string `description:"子数据来源名称"`
- IndicatorCode string `description:"指标代码"`
- StockCode string `description:"证券代码"`
- Extra string `description:"指标额外配置"`
- IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
- }
- type EdbInfoFullClassify struct {
- *EdbInfo
- CorrelationStr string `description:"相关性系数字符串"`
- ClassifyList []*EdbClassifyIdItems
- HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
- }
- func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
- o := orm.NewOrmUsingDB("data")
- lastId, err = o.Insert(item)
- return
- }
- type BaseEdbNameItem struct {
- EdbInfoId int `description:"指标id"`
- EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
- SourceName string `description:"来源名称"`
- Source int `description:"来源id"`
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- EdbNameEn string `description:"指标英文名称"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- UnitEn string `description:"英文单位"`
- HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
- }
- type BaseEdbInfoResp struct {
- List []*BaseEdbNameItem
- }
- // GetEdbInfoAll 用于分类展示
- func GetEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
- _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
- return
- }
- // GetPredictEdbInfoAll 用于分类展示(预测指标)
- func GetPredictEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT edb_info_id,classify_id,edb_name AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
- _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
- return
- }
- // 用于分类展示
- func GetEdbInfoAllList() (items []*EdbInfo, err error) {
- o := orm.NewOrmUsingDB("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
- StockList []*StockInfo `description:"时序数据"`
- }
- type StockInfo struct {
- StockCode string
- EdbCode string
- DataList []*EdbInfoSearchData
- }
- type EdbInfoSearchData struct {
- DataTime string `description:"数据日期"`
- Value float64 `description:"数据"`
- }
- type BaseIndexInfo struct {
- IndexName string `description:"数据日期"`
- Unit string `description:"单位"`
- Frequency string `description:"频率"`
- }
- type EdbInfoSearchResp struct {
- SearchItem *EdbInfoSearch `description:"指标分类"`
- Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
- ClassifyList []*EdbClassifySimplify
- StockSearchList []*EdbInfoSearch
- }
- func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
- o := orm.NewOrmUsingDB("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.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
- err = o.Raw(sql, edbInfoId).QueryRow(&item)
- return
- }
- // GetEdbInfoByUniqueCode
- // @Description: 根据uniqueCode获取指标详情
- // @author: Roc
- // @datetime 2024-01-18 13:40:01
- // @param uniqueCode string
- // @return item *EdbInfo
- // @return err error
- func GetEdbInfoByUniqueCode(uniqueCode string) (item *EdbInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE unique_code=? `
- err = o.Raw(sql, uniqueCode).QueryRow(&item)
- return
- }
- // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
- func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
- num := len(edbInfoIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
- _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
- 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, subSource int) (err error) {
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- // 删除指标信息
- sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
- _, err = to.Raw(sql, edbInfoId).Exec()
- if err != nil {
- return
- }
- var tableName string
- if subSource == 0 {
- // 删除指标数据
- tableName = GetEdbDataTableName(source, subSource)
- } else {
- if source == utils.DATA_SOURCE_WIND {
- tableName = "edb_data_wind_wsd"
- } else if source == utils.DATA_SOURCE_THS {
- tableName = "edb_data_ths_ds"
- }
- }
- if tableName != "" {
- sql = ` DELETE FROM %s WHERE edb_info_id=? `
- sql = fmt.Sprintf(sql, tableName)
- _, err = to.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 = to.Raw(sql, edbInfoId).Exec()
- // return
- //}
- //return
- // 删除计算指标的关系
- sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
- _, err = to.Raw(sql, edbInfoId).Exec()
- // 删除预测指标的配置
- sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id=? `
- _, err = to.Raw(sql, edbInfoId).Exec()
- return
- }
- func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrmUsingDB("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:"计算公式"`
- }
- type EditEdbEnInfoReq struct {
- EdbInfoId int `description:"指标ID"`
- EdbNameEn string `description:"英文指标名称"`
- UnitEn string `description:"英文单位"`
- }
- // EditEdbBaseInfoReq
- // @Description: 修改指标基础信息请求参数
- type EditEdbBaseInfoReq struct {
- EdbInfoId int `description:"指标ID"`
- EdbName string `description:"指标名称"`
- Unit string `description:"指标单位"`
- }
- func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
- o := orm.NewOrmUsingDB("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
- }
- // ModifyEdbEnInfo 修改指标英文信息
- func ModifyEdbEnInfo(item *EditEdbEnInfoReq) (err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` UPDATE edb_info
- SET
- edb_name_en =?,
- unit_en = ?,
- modify_time = NOW()
- WHERE edb_info_id = ?`
- _, err = o.Raw(sql, item.EdbNameEn, item.UnitEn, item.EdbInfoId).Exec()
- return
- }
- type EdbInfoList struct {
- EdbInfoId int `orm:"column(edb_info_id);pk"`
- EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
- SourceName string `description:"来源名称"`
- Source int `description:"来源id"`
- EdbCode string `description:"指标编码"`
- EdbNameEn string `description:"英文指标名称"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频率"`
- FrequencyEn string `description:"英文频率"`
- Unit string `description:"单位"`
- UnitEn string `description:"英文单位"`
- StartDate string `description:"起始日期"`
- EndDate string `description:"终止日期"`
- LatestDate string `description:"数据最新日期(实际日期)"`
- LatestValue float64 `description:"数据最新值(实际值)"`
- EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
- ClassifyId int `description:"分类id"`
- UniqueCode string `description:"指标唯一编码"`
- SysUserId int `description:"创建人id"`
- SysUserRealName string `description:"创建人姓名"`
- ModifyTime string `description:"最新修改时间"`
- CreateTime string `description:"创建时间"`
- EdbNameAlias string `json:"-" description:"指标名称,别名"`
- EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
- ChartImage string `description:"图表图片"`
- RuleType int `description:"预测规则,1:最新,2:固定值"`
- FixedValue float64 `description:"固定值"`
- DataList []*EdbData `description:"实际指标数据"`
- PredictDataList []*EdbData `description:"预测指标数据"`
- Button EdbClassifyItemsButton `description:"操作权限"`
- IsEnEdb bool `description:"是否展示英文标识"`
- DataInsertConfig EdbDataInsertConfigItem `description:"指标数据插入配置"`
- DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
- EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
- MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
- SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
- SubSourceName string `description:"子数据来源名称"`
- IndicatorCode string `description:"指标代码"`
- StockCode string `description:"证券代码"`
- NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
- IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
- HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
- }
- type EdbDataInsertConfigItem struct {
- Date string `description:"插入的日期"`
- RealDate string `description:"实际最晚的日期"`
- Value string `description:"插入的值"`
- }
- 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 []*EdbClassifyIdItems
- }
- func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("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, subSource int) (count int, err error) {
- o := orm.NewOrmUsingDB("data")
- tableName := GetEdbDataTableName(source, subSource)
- 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 GetEdbDataListByCondition(condition string, pars []interface{}, source, subSource, pageSize, startSize int) (item []*EdbData, err error) {
- o := orm.NewOrmUsingDB("data")
- tableName := GetEdbDataTableName(source, subSource)
- sql := ` SELECT * FROM %s WHERE 1=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
- }
- // GetAllEdbDataListByCondition 根据条件获取所有的数据
- func GetAllEdbDataListByCondition(condition string, pars []interface{}, source, subSource int) (item []*EdbData, err error) {
- o := orm.NewOrmUsingDB("data")
- tableName := GetEdbDataTableName(source, subSource)
- 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).QueryRows(&item)
- return
- }
- func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("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.NewOrmUsingDB("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.NewOrmUsingDB("data")
- sql := ` UPDATE edb_info
- SET
- classify_id = ?
- WHERE edb_info_id = ?`
- _, err = o.Raw(sql, classifyId, edbInfoId).Exec()
- return
- }
- // GetEdbInfoByName 根据名称获取普通的指标
- func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 0 `
- _, err = o.Raw(sql, edbName).QueryRows(&items)
- return
- }
- // GetPredictEdbInfoByName 根据指标名称获取预测指标
- func GetPredictEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 1 `
- _, err = o.Raw(sql, edbName).QueryRows(&items)
- return
- }
- func GetEdbInfoByEnName(edbNameEn string) (items []*EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_name_en=? AND edb_info_type = 0 `
- _, err = o.Raw(sql, edbNameEn).QueryRows(&items)
- return
- }
- func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
- o := orm.NewOrmUsingDB("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.NewOrmUsingDB("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) {
- pars := make([]interface{}, 0)
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
- if keyword != "" {
- sql += ` AND (edb_name LIKE ? OR edb_code LIKE ? ) `
- pars = append(pars, utils.GetLikeKeyword(keyword), utils.GetLikeKeyword(keyword))
- }
- sql += ` ORDER BY create_time DESC `
- _, err = o.Raw(sql, pars...).QueryRows(&searchList)
- return
- }
- type EdbInfoMaxAndMinInfo struct {
- MinDate string `description:"最小日期"`
- MaxDate string `description:"最大日期"`
- MinValue float64 `description:"最小值"`
- MaxValue float64 `description:"最大值"`
- LatestValue float64 `description:"最新值"`
- }
- func GetEdbInfoMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ``
- tableName := GetEdbDataTableName(source, subSource)
- 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)
- if err != nil {
- return
- }
- 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
- }
- func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
- 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, subSource, order int) (item []*EdbInfoSearchData, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ``
- tableName := GetEdbDataTableName(source, subSource)
- 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, subSource int) (item *EdbInfoSearchData, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ``
- tableName := GetEdbDataTableName(source, subSource)
- 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, subSource int) (count int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ``
- tableName := GetEdbDataTableName(source, subSource)
- 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.NewOrmUsingDB("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:"指标唯一编码"`
- ClassifyId int `description:"分类id"`
- ExistEdbName []string `description:"重复指标名称"`
- }
- // 根据基础指标获取所有关联指标
- func GetNeedRefreshCalculateEdbInfoFromBase(fromEdbInfo int) (list []*EdbInfo, err error) {
- o := orm.NewOrmUsingDB("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.NewOrmUsingDB("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.NewOrmUsingDB("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.NewOrmUsingDB("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.NewOrmUsingDB("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.NewOrmUsingDB("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.NewOrmUsingDB("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.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 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.NewOrmUsingDB("data")
- sql := ` SELECT b.* 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
- }
- // GetEdbInfoAllCalculateByEdbInfoIdList 根据指标id集合 获取基础指标对应的所有计算指标
- func GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList []int) (list []*EdbInfo, err error) {
- num := len(edbInfoIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT b.* 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 in (` + utils.GetOrmInReplace(num) + `)
- GROUP BY a.edb_info_id
- ORDER BY a.edb_info_id ASC `
- _, err = o.Raw(sql, edbInfoIdList).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.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
- continue
- }
- 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, subSource int, edbCode string) (err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ``
- tableName := GetEdbDataTableName(source, subSource)
- sql = ` UPDATE %s SET edb_info_id=?,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.NewOrmUsingDB("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.NewOrmUsingDB("data")
- sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=?`
- if prevEdbInfoId > 0 {
- sql += ` AND ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
- } else {
- sql += ` AND ( sort > ? )`
- }
- _, err = o.Raw(sql, classifyId, nowSort).Exec()
- return
- }
- // Update 更新指标基础信息
- func (edbInfo *EdbInfo) Update(cols []string) (err error) {
- o := orm.NewOrmUsingDB("data")
- _, err = o.Update(edbInfo, cols...)
- return
- }
- type EdbInfoDataResp struct {
- EdbInfo *EdbInfo
- DataList []*EdbDataList
- }
- type EdbInfoDataFullClassifyResp struct {
- EdbInfo *EdbInfoFullClassify
- DataList []*EdbDataList
- }
- type EdbInfoReplaceReq struct {
- OldEdbInfoId int `description:"原指标ID"`
- NewEdbInfoId int `description:"替换为指标ID"`
- }
- // EdbInfoReplaceV2 替换指标v2版本(更换时间:2022年01月05日15:33:42)
- //func EdbInfoReplaceV2(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (mappingList,replaceChartTotal, replaceCalculateTotal int, err error) {
- // defer func() {
- // if err != nil {
- // fmt.Println("ERR:", err.Error())
- // }
- // }()
- //
- // // 替换关联表中的source_edb_info_id
- // mappingList, replaceChartTotal, replaceCalculateTotal, err := replaceChartEdb(oldEdbInfo, newEdbInfo)
- // if err != nil {
- // return
- // }
- // err = replaceRelationEdbInfo(mappingList, oldEdbInfo, newEdbInfo, sysAdminId, sysAdminRealName)
- // return
- //}
- // ReplaceChartEdb 替换图表中的指标
- func ReplaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (relationEdbInfoIdList []int, replaceChartTotal, replaceCalculateTotal int, err error) {
- var errmsg string
- relationEdbInfoIdList = make([]int, 0) // 关联的指标id
- logMsg := `` // 记录替换的日志
- o := orm.NewOrmUsingDB("data")
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- if logMsg != `` {
- utils.FileLog.Info(fmt.Sprintf("替换指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
- }
- }
- if errmsg != "" {
- fmt.Println("errmsg:" + errmsg)
- }
- if err != nil && errmsg != "" {
- go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
- }
- }()
- //替换图表
- {
- chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
- csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
- _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
- if err != nil {
- errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
- return
- }
- replaceChartTotal = len(chartEdbMappingList)
- if len(chartEdbMappingList) > 0 {
- chartInfoIdList := make([]string, 0)
- for _, mv := range chartEdbMappingList {
- //获取图表所有指标信息
- chartEdbList := make([]*ChartEdbInfoMapping, 0)
- csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
- _, err = to.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 = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
- if err != nil {
- errmsg = "更新图库指标信息失败:Err:" + err.Error()
- return
- }
- chartInfoIdList = append(chartInfoIdList, fmt.Sprint(mv.ChartInfoId))
- }
- logMsg += `涉及到的图表id:` + strings.Join(chartInfoIdList, ",") + ";"
- }
- }
- //替换计算指标
- {
- //获取所有包含的计算指标
- mappingList := make([]*EdbInfoCalculateMapping, 0)
- msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
- _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
- if err != nil {
- errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
- return
- }
- replaceCalculateTotal = len(mappingList)
- //计算指标
- if len(mappingList) > 0 {
- edbInfoIdList := make([]string, 0)
- for _, mv := range mappingList {
- //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
- if mv.EdbInfoId == newEdbInfo.EdbInfoId {
- continue
- }
- //替换原指标
- 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 = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
- if err != nil {
- return
- }
- relationEdbInfoIdList = append(relationEdbInfoIdList, mv.EdbInfoId)
- edbInfoIdList = append(edbInfoIdList, fmt.Sprint(mv.EdbInfoId))
- }
- logMsg += `涉及到的指标id:` + strings.Join(edbInfoIdList, ",") + ";"
- }
- }
- // 替换预测指标中的来源指标
- {
- //获取所有包含的计算指标
- predictEdbConfList := make([]*PredictEdbConf, 0)
- msql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? GROUP BY predict_edb_info_id `
- _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&predictEdbConfList)
- if err != nil {
- errmsg = "获取关联的预测指标信息失败:Err:" + err.Error()
- return
- }
- replaceCalculateTotal += len(predictEdbConfList)
- //替换预测指标的来源指标
- msql = `UPDATE predict_edb_conf SET source_edb_info_id=? WHERE source_edb_info_id=? `
- _, err = to.Raw(msql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- if len(predictEdbConfList) > 0 {
- edbInfoIdList := make([]string, 0)
- for _, v := range predictEdbConfList {
- relationEdbInfoIdList = append(relationEdbInfoIdList, v.PredictEdbInfoId)
- edbInfoIdList = append(edbInfoIdList, fmt.Sprint(v.PredictEdbInfoId))
- }
- logMsg += `涉及到的预测指标id:` + strings.Join(edbInfoIdList, ",") + ";"
- }
- }
- return
- }
- // replaceRelationEdbInfo 替换关联指标
- func replaceRelationEdbInfo(mappingList []*EdbInfoCalculateMapping, oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (err error) {
- errmsg := ``
- defer func() {
- if errmsg != "" {
- fmt.Println("errmsg:" + errmsg)
- }
- if err != nil && errmsg != "" {
- go alarm_msg.SendAlarmMsg("替换关联指标失败提醒,errmsg:"+errmsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
- }
- }()
- //获取计算指标关联指标
- allMappingList := make([]*EdbInfoCalculateMapping, 0)
- allMapping := make(map[int]int)
- tmpMapping := make(map[int]int)
- for _, v := range mappingList {
- if _, ok := allMapping[v.EdbInfoId]; !ok {
- allMappingList = append(allMappingList, v)
- }
- newList, _ := GetAllCalculateV2(v.EdbInfoId, newEdbInfo.EdbInfoId, tmpMapping)
- 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, tmpErr := GetEdbInfoById(mv.EdbInfoId) //计算指标
- err = tmpErr
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errmsg = "计算指标已被删除"
- } else {
- errmsg = "获取计算指标失败:err:" + err.Error()
- }
- return
- }
- fromEdbInfoList, tmpErr := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
- err = tmpErr
- if err != nil {
- errmsg = "获取计算指标失败:err:" + err.Error()
- return
- }
- if len(fromEdbInfoList) <= 0 {
- errmsg = "计算指标所依赖指标不存在"
- err = fmt.Errorf("计算指标所依赖指标不存在")
- return
- }
- fromEdbInfoItem := fromEdbInfoList[0]
- if fromEdbInfoItem == nil {
- return
- }
- fromEdbInfo, tmpErr := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
- err = tmpErr
- if err != nil {
- errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
- return
- }
- edbCode := edbInfo.EdbCode
- uniqueCode := edbInfo.UniqueCode
- sourName := edbInfo.SourceName
- source := edbInfo.Source
- subSource := edbInfo.SubSource
- 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 = "公式错误,请重新填写"
- err = fmt.Errorf("公式错误,请重新填写")
- return
- }
- }
- edbInfoList := make([]*EdbInfo, 0)
- for _, v := range fromEdbInfoList {
- edbInfo, tmpErr := GetEdbInfoById(v.FromEdbInfoId)
- err = tmpErr
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
- } else {
- errmsg = "获取指标失败:Err:" + err.Error()
- }
- return
- }
- edbInfoList = append(edbInfoList, edbInfo)
- }
- //清除历史数据
- err = DeleteCalculateData(edbInfoId)
- if err != nil {
- errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
- return
- }
- err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
- if err != nil {
- errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
- return
- }
- } 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 if source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT {
- req2 := &EdbInfoCalculateBatchEditReq{
- EdbName: edbInfo.EdbName,
- Frequency: edbInfo.Frequency,
- Unit: edbInfo.Unit,
- ClassifyId: edbInfo.ClassifyId,
- Formula: edbInfo.CalculateFormula,
- EdbInfoId: edbInfo.EdbInfoId,
- FromEdbInfoId: fromEdbInfoList[0].FromEdbInfoId,
- Source: edbInfo.Source,
- MoveType: edbInfo.MoveType,
- MoveFrequency: edbInfo.MoveFrequency,
- }
- sourName = "时间移位"
- edbInfoId, err = EditCalculateTimeShift(req2, fromEdbInfo, edbCode, edbInfo)
- } else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ {
- edbInfoIdArr := make([]EdbInfoFromTag, 0)
- //A指标
- aFromEdbInfoId := fromEdbInfoList[0].FromEdbInfoId
- for _, v := range fromEdbInfoList {
- if v.FromTag == "A" {
- if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
- aFromEdbInfoId = newEdbInfo.EdbInfoId
- } else {
- aFromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
- }
- }
- if v.FromTag == "B" {
- if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
- edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
- EdbInfoId: newEdbInfo.EdbInfoId,
- FromTag: v.FromTag,
- MoveValue: v.MoveValue,
- })
- } else {
- edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
- EdbInfoId: v.FromEdbInfoId,
- FromTag: v.FromTag,
- MoveValue: v.MoveValue,
- })
- }
- }
- }
- req2 := &EdbInfoCalculateBatchEditReq{
- EdbName: edbInfo.EdbName,
- Frequency: edbInfo.Frequency,
- Unit: edbInfo.Unit,
- ClassifyId: edbInfo.ClassifyId,
- Formula: edbInfo.CalculateFormula,
- EdbInfoId: edbInfo.EdbInfoId,
- FromEdbInfoId: aFromEdbInfoId,
- Source: edbInfo.Source,
- MoveType: edbInfo.MoveType,
- MoveFrequency: edbInfo.MoveFrequency,
- EdbInfoIdArr: edbInfoIdArr,
- }
- sourName = "直接拼接"
- if len(req2.EdbInfoIdArr) != 1 {
- errmsg = "请传入拼接日期之后的指标"
- err = fmt.Errorf("请传入拼接日期之后的指标")
- return
- }
- secondEdbInfoReq := req2.EdbInfoIdArr[0]
- secondEdbInfo, tmpErr := GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
- err = tmpErr
- if err != nil {
- errmsg = "获取拼接日期之后的指标信息失败:Err:" + err.Error()
- err = fmt.Errorf("获取拼接日期之后的指标信息失败:Err:" + err.Error())
- return
- }
- if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
- errmsg = "两个指标不允许为同一个"
- err = fmt.Errorf("两个指标不允许为同一个")
- return
- }
- tmpEdbInfoId, tmpErr := EditCalculateZjpj(req2, edbInfo, fromEdbInfo, secondEdbInfo)
- err = tmpErr
- edbInfoId = tmpEdbInfoId
- } else if source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ { //累计值同比拼接
- edbInfoIdArr := make([]EdbInfoFromTag, 0)
- //A指标
- aFromEdbInfoId := fromEdbInfoList[0].FromEdbInfoId
- for _, v := range fromEdbInfoList {
- if v.FromTag == "A" {
- if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
- aFromEdbInfoId = newEdbInfo.EdbInfoId
- } else {
- aFromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
- }
- }
- if v.FromTag == "B" {
- if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
- edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
- EdbInfoId: newEdbInfo.EdbInfoId,
- FromTag: v.FromTag,
- MoveValue: v.MoveValue,
- })
- } else {
- edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
- EdbInfoId: v.FromEdbInfoId,
- FromTag: v.FromTag,
- MoveValue: v.MoveValue,
- })
- }
- }
- }
- req2 := &EdbInfoCalculateBatchEditReq{
- EdbName: edbInfo.EdbName,
- Frequency: edbInfo.Frequency,
- Unit: edbInfo.Unit,
- ClassifyId: edbInfo.ClassifyId,
- Formula: edbInfo.CalculateFormula,
- EdbInfoId: edbInfo.EdbInfoId,
- FromEdbInfoId: aFromEdbInfoId,
- Source: edbInfo.Source,
- MoveType: edbInfo.MoveType,
- MoveFrequency: edbInfo.MoveFrequency,
- EdbInfoIdArr: edbInfoIdArr,
- }
- sourName = "累计值同比拼接"
- if fromEdbInfo.Frequency != "月度" {
- errmsg = "累计值同比拼接,待拼接指标只能筛选月度指标"
- err = fmt.Errorf("累计值同比拼接,待拼接指标只能筛选月度指标")
- return
- }
- if len(req2.EdbInfoIdArr) != 1 {
- errmsg = "请传入同比值指标"
- err = fmt.Errorf("请传入同比值指标")
- return
- }
- secondEdbInfoReq := req2.EdbInfoIdArr[0]
- tbzEdbInfo, tmpErr := GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
- err = tmpErr
- if err != nil {
- errmsg = "获取同比值指标信息失败:Err:" + err.Error()
- err = fmt.Errorf("获取同比值指标信息失败:Err:" + err.Error())
- return
- }
- if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
- errmsg = "指标必须是传入同比值指标类型"
- err = fmt.Errorf("指标必须是传入同比值指标类型")
- return
- }
- if tbzEdbInfo.Frequency != "月度" {
- errmsg = "同比值指标只能筛选月度指标"
- err = fmt.Errorf("同比值指标只能筛选月度指标")
- return
- }
- if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
- errmsg = "两个指标不允许为同一个"
- err = fmt.Errorf("两个指标不允许为同一个")
- return
- }
- tmpEdbInfoId, tmpErr := EditCalculateLjztbpj(req2, edbInfo, fromEdbInfo, tbzEdbInfo)
- err = tmpErr
- edbInfoId = tmpEdbInfoId
- } else if source == utils.DATA_SOURCE_PYTHON {
- //python代码运算的过滤
- continue
- } else {
- errmsg = "无效计算方式,source:" + strconv.Itoa(source)
- err = fmt.Errorf("无效计算方式,source:" + strconv.Itoa(source))
- return
- }
- maxAndMinItem, tmpErr := GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
- err = tmpErr
- if err != nil && err.Error() != utils.ErrNoRow() {
- errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
- err = fmt.Errorf("生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error())
- return
- }
- if maxAndMinItem != nil {
- err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
- }
- }
- }
- return
- }
- type EdbInfoView struct {
- EdbInfoId int `orm:"column(edb_info_id);pk"`
- EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
- 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:"数据最新值(实际值)"`
- SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
- SubSourceName string `description:"子数据来源名称"`
- IndicatorCode string `description:"指标代码"`
- StockCode string `description:"证券代码"`
- EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
- }
- // 获取指标的所有计算指标,以及计算指标所依赖计算指标
- func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
- o := orm.NewOrmUsingDB("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, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
- calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- for _, item := range calculateList {
- _, ok := edbInfoMap[item.EdbInfoId]
- if !ok {
- edbInfoMap[item.EdbInfoId] = item.EdbInfoId
- mappingList = append(mappingList, item)
- newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId, edbInfoMap)
- mappingList = append(mappingList, newCalculateInfoArr...)
- }
- }
- return
- }
- // GetAllCalculateByEdbInfoIdV2 获取计算指标的所有所依赖的指标(基础指标+计算指标)
- func GetAllCalculateByEdbInfoIdV2(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
- o := orm.NewOrmUsingDB("data")
- msql := ` SELECT * FROM edb_info_calculate_mapping WHERE edb_info_id=? GROUP BY from_edb_info_id `
- _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
- if err != nil {
- return
- }
- return
- }
- // GetAllCalculateV2 获取所有的指标(如果下层指标包含了待替换指标,那么就不加入)
- func GetAllCalculateV2(oldEdbInfoId, replaceEdbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
- calculateList, err := GetAllCalculateByEdbInfoId(oldEdbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- return
- }
- tmpMappingList := make([]*EdbInfoCalculateMapping, 0)
- for _, item := range calculateList {
- //如果计算指标里面存在即将替换的指标id,那么退出当前循环,进入下一循环
- if item.EdbInfoId == replaceEdbInfoId {
- err = fmt.Errorf("指标重复1")
- return
- }
- //获取该指标的依赖指标列表,如果依赖指标列表中存在即将替换的指标,那么过滤该指标
- childCalculateList, tmpErr := GetAllCalculateByEdbInfoIdV2(item.EdbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- err = tmpErr
- return
- }
- for _, childCalculate := range childCalculateList {
- if childCalculate.FromEdbInfoId == replaceEdbInfoId {
- err = fmt.Errorf("指标重复2")
- return
- }
- }
- _, ok := edbInfoMap[item.EdbInfoId]
- if !ok {
- edbInfoMap[item.EdbInfoId] = item.EdbInfoId
- newCalculateInfoArr, tmpErr := GetAllCalculateV2(item.EdbInfoId, replaceEdbInfoId, edbInfoMap)
- if tmpErr != nil {
- err = tmpErr
- continue
- } else {
- mappingList = append(mappingList, item)
- tmpMappingList = append(tmpMappingList, newCalculateInfoArr...)
- }
- }
- }
- mappingList = append(mappingList, tmpMappingList...)
- return
- }
- // EdbInfoFilterDataResp 搜索指标列表数据返回
- type EdbInfoFilterDataResp struct {
- Paging *paging.PagingItem
- List []*EdbInfoList
- }
- // GetEdbInfoFilterList 获取指指标列表数据接口
- func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
- err = o.Raw(sql, pars).QueryRow(&total)
- if err != nil {
- return
- }
- sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
- return
- }
- // SetEdbInfoImageReq 设置指标图片
- type SetEdbInfoImageReq struct {
- EdbInfoId int `description:"指标ID"`
- ImageUrl string `description:"指标图片地址"`
- }
- // GetNextEdbInfoByCondition 根据条件获取下一个指标
- func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
- err = o.Raw(sql, pars).QueryRow(&item)
- return
- }
- // GetNextEdbInfo 根据分类id获取下一个 指标
- func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT b.* FROM edb_classify AS a
- INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
- WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
- AND a.classify_type=? AND b.edb_info_type=?
- ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
- LIMIT 1 `
- err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).QueryRow(&item)
- return
- }
- // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
- func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
- num := len(edbCodeList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
- _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
- return
- }
- // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
- func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrmUsingDB("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
- }
- // GetEdbInfoListByCondition 根据条件获取指标列表数据
- func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int, orderDesc string) (items []*EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- if orderDesc == `` {
- orderDesc = ` ASC `
- }
- sql += ` ORDER BY edb_info_id `
- sql += orderDesc
- sql += ` LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- return
- }
- // GetEdbInfoListV2ByCondition 根据条件获取指标列表数据(EdbInfo原始数据结构)
- func GetEdbInfoListV2ByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- return
- }
- // GetEdbInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
- func GetEdbInfoListGroupByUserId(edbIdList []string) (items []*EdbInfo, err error) {
- num := len(edbIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
- _, err = o.Raw(sql, edbIdList).QueryRows(&items)
- return
- }
- // GetEdbInfoListByEdbInfoId
- // @Description: 根据指标id列表获取列表信息
- // @param edbIdList
- // @return items
- // @return err
- func GetEdbInfoListByEdbInfoId(edbIdList []string) (items []*EdbInfo, err error) {
- num := len(edbIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
- _, err = o.Raw(sql, edbIdList).QueryRows(&items)
- return
- }
- // GetEdbInfoListByUserId
- // @Description: 根据指标id列表获取列表信息
- // @param userIdList []int
- // @param edbInfoType int
- // @return items
- // @return err
- func GetEdbInfoListByUserId(userIdList []int, edbInfoType int) (items []*EdbInfo, err error) {
- num := len(userIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_info_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
- _, err = o.Raw(sql, edbInfoType, userIdList).QueryRows(&items)
- return
- }
- // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
- func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
- num := len(edbIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
- _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
- return
- }
- // ModifyEdbInfoUserIdByOldUserId
- // @Description: 根据旧用户id修改新用户id
- // @author: Roc
- // @datetime 2024-03-25 19:17:09
- // @param edbIdList []string
- // @param userId int
- // @param userName string
- // @return err error
- func ModifyEdbInfoUserIdByOldUserId(oldUserIdList []int, edbInfoType, userId int, userName string) (err error) {
- num := len(oldUserIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) and edb_info_type = ?`
- _, err = o.Raw(sql, userId, userName, oldUserIdList, edbInfoType).Exec()
- return
- }
- // MoveEdbChartList 图表/指标信息
- type MoveEdbChartList struct {
- DataId string `description:"指标/图表唯一id"`
- Code string `description:"指标code"`
- Name string `description:"指标/图表名称"`
- NameEn string `description:"指标/图表名称"`
- ClassifyName string `description:"分类名称"`
- ClassifyNameEn string `description:"分类名称"`
- CreateUserId int `description:"创建人id"`
- CreateUserName string `description:"创建人名称"`
- }
- // MoveEdbChartListResp 更改创建人返回数据
- type MoveEdbChartListResp struct {
- Paging *paging.PagingItem
- List []MoveEdbChartList
- }
- type EdbChartClassifyResp struct {
- List []*EdbChartClassify
- }
- type EdbChartClassify struct {
- ClassifyId int
- ClassifyName string
- ParentId int
- IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
- Child []*EdbChartClassify
- }
- // EdbInfoDataSeasonalResp 指标季节性数据返回
- type EdbInfoDataSeasonalResp struct {
- EdbInfo *EdbInfo
- DataList interface{}
- }
- // SetEdbDataInsertConfigReq 设置插入规则的请求
- type SetEdbDataInsertConfigReq struct {
- EdbInfoId int `description:"指标ID"`
- Date string `description:"日期"`
- Value string `description:"值"`
- }
- // GetEdbInfoByClassifyId 用于分类展示
- func GetEdbInfoByClassifyId(classifyId, edbInfoType, adminId int) (items []*EdbClassifyItems, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type, sort,is_join_permission FROM edb_info WHERE classify_id = ? AND edb_info_type = ?`
- pars := []interface{}{classifyId, edbInfoType}
- // 如果筛选了用户id
- if adminId > 0 {
- sql += ` AND sys_user_id = ? `
- pars = append(pars, adminId)
- }
- sql += ` order by sort asc,edb_info_id asc `
- _, err = o.Raw(sql, pars).QueryRows(&items)
- return
- }
- type EdbAndClassify struct {
- EdbInfoId int `description:"指标id"`
- EdbCode string `description:"指标code"`
- UniqueCode string `description:"唯一code"`
- EdbName string `description:"指标名称"`
- ClassifyId int `description:"所属分类id"`
- FirstClassifyId int `description:"第1级的分类id"`
- FirstClassifyName string `description:"第1级的分类名称"`
- SecondClassifyId int `description:"第2级的分类id"`
- SecondClassifyName string `description:"第2级的分类名称"`
- }
- // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
- func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
- num := len(edbIdList)
- if num <= 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT a.edb_info_id,a.edb_code,a.unique_code,a.edb_name,a.classify_id FROM edb_info AS a
- WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
- _, err = o.Raw(sql, edbIdList).QueryRows(&items)
- return
- }
- // TraceEdbInfoResp 指标追溯数据返回
- type TraceEdbInfoResp struct {
- EdbInfoId int `description:"指标id"`
- EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
- EdbName string `description:"指标名称"`
- EdbNameEn string `description:"指标英文名称"`
- EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
- RuleTitle string `description:"指标规则"`
- RuleTitleEn string `description:"指标规则(英文)"`
- UniqueCode string `description:"唯一编码"`
- ClassifyId int `description:"分类ID"`
- Child []TraceEdbInfoResp `description:"下级来源"`
- IsStop int8 `description:"是否终止"`
- EdbInfo *EdbInfo `description:"指标信息" json:"-"`
- HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
- }
- // BeforeAndAfterDateDataResp 前后几期数据
- type BeforeAndAfterDateDataResp struct {
- List []*EdbDataList `description:"list"`
- Date string `description:"实际日期"`
- ShowValue string `description:"展示值"`
- }
- // GetEdbInfoAdminList
- func GetEdbInfoAdminList(edbType int) (list []int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT sys_user_id FROM edb_info WHERE edb_info_type = ? GROUP BY sys_user_id `
- _, err = o.Raw(sql, edbType).QueryRows(&list)
- return
- }
- type EdbInfoExistCheckResp struct {
- IndexExist bool `description:"指标是否存在:true,存在,false:不存在"`
- ExistStockCodeArr []string `description:"存在的证券代码"`
- ExistEdbCodeArr []string `description:"存在的指标代码"`
- ExistClassifyId []int `description:"存在的分类id"`
- ExistIndexId []int `description:"存在的指标id"`
- ExistEdbInfo []*EdbInfo `description:"存在的指标"`
- }
- type BatchAddEdbInfoReq struct {
- BatchList []*BatchAddEdbInfo
- }
- type BatchAddEdbInfo struct {
- Source int `description:"来源id"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- ClassifyId int `description:"分类id"`
- StockCode string `description:"证券代码"`
- EdbCode string `description:"指标编码"`
- }
- func GetEdbInfoWsdMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ``
- tableName := GetEdbDataTableNameAndSubSource(source, subSource)
- 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)
- if err != nil {
- return
- }
- 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
- }
- // GetEdbInfoByNames 根据名称获取普通的指标
- func GetEdbInfoByNames(edbNames string) (items []*EdbInfoList, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT * FROM edb_info WHERE edb_name IN (` + edbNames + `) AND edb_info_type = 0 `
- _, err = o.Raw(sql).QueryRows(&items)
- return
- }
- func GetSmmEdbInfoMaxAndMinInfo(edbCodes []string) (item *EdbInfoMaxAndMinInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ``
- 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 edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `)`
- err = o.Raw(sql, edbCodes).QueryRow(&item)
- if err != nil {
- return
- }
- var latest_value float64
- sql = ` SELECT value AS latest_value FROM edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `) ORDER BY data_time DESC LIMIT 1 `
- err = o.Raw(sql, edbCodes).QueryRow(&latest_value)
- item.LatestValue = latest_value
- return
- }
- type EdbInfoSmmExistCheckResp struct {
- EdbInfoExistCheckResp
- ExistAll bool `description:"是否全部重复"`
- }
- // GetEdbBaseInfoList
- // @Description: 获取指标库数据列表
- // @author: Roc
- // @datetime 2024-01-10 14:28:22
- // @param condition string
- // @param pars []interface{}
- // @param orderBy string
- // @param startSize int
- // @param pageSize int
- // @return total int
- // @return items []*BaseRefreshEdbInfo
- // @return err error
- func GetEdbBaseInfoList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRefreshEdbInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- // 数量汇总
- totalSql := ` SELECT count(1) FROM edb_info WHERE 1=1 `
- if condition != "" {
- totalSql += condition
- }
- err = o.Raw(totalSql, pars).QueryRow(&total)
- if err != nil {
- return
- }
- // 列表数据
- sql := ` SELECT edb_info_id as edb_info_id, classify_id as classify_id,edb_code as index_code,edb_name as index_name,end_date,end_value,sys_user_id,sys_user_real_name,frequency,no_update as is_stop,terminal_code FROM edb_info WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- if orderBy != "" {
- sql += ` ORDER BY ` + orderBy
- } else {
- sql += ` ORDER BY edb_info_id ASC `
- }
- sql += ` LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- return
- }
- // ModifyEdbInfoUpdateStatus
- // @Description: 修改指标库停更状态
- // @author: Roc
- // @datetime 2024-01-08 16:23:31
- // @param edbIdList []int
- // @param indexCodeList []string
- // @param isStop int
- // @return err error
- func ModifyEdbInfoUpdateStatus(edbIdList []int, isStop int) (err error) {
- idNum := len(edbIdList)
- if idNum <= 0 {
- return
- }
- o, err := orm.NewOrmUsingDB("data").Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = o.Rollback()
- return
- }
- _ = o.Commit()
- }()
- // 更改指标的更新状态
- sql := ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(idNum) + `) `
- _, err = o.Raw(sql, isStop, edbIdList).Exec()
- if err != nil {
- return
- }
- return
- }
- func GetEdbInfoCount(source int, edbCode string) (count int, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT COUNT(1) AS count FROM edb_info
- WHERE source=? AND edb_code = ?`
- err = o.Raw(sql, source, edbCode).QueryRow(&count)
- return
- }
- // GetEdbInfoByNameArr 根据名称获取指标
- func GetEdbInfoByNameArr(names []string) (items []*EdbInfo, err error) {
- if len(names) == 0 {
- return
- }
- o := orm.NewOrmUsingDB("data")
- sql := fmt.Sprintf(`SELECT edb_info_id, edb_code, edb_name FROM edb_info WHERE edb_name IN (%s)`, utils.GetOrmInReplace(len(names)))
- _, err = o.Raw(sql, names).QueryRows(&items)
- return
- }
- // GetEdbCodesBySource 根据来源获取指标编码
- func GetEdbCodesBySource(source int) (items []*EdbInfo, err error) {
- o := orm.NewOrmUsingDB("data")
- sql := ` SELECT edb_info_id, edb_code, unique_code, classify_id FROM edb_info WHERE source = ? `
- _, err = o.Raw(sql, source).QueryRows(&items)
- return
- }
|