edb_info.go 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/models/mgo"
  6. "eta/eta_index_lib/utils"
  7. "fmt"
  8. "strconv"
  9. "time"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "github.com/beego/beego/v2/client/orm"
  12. "github.com/shopspring/decimal"
  13. )
  14. type EdbInfo struct {
  15. EdbInfoId int `orm:"column(edb_info_id);pk"`
  16. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  17. SourceName string `description:"来源名称"`
  18. Source int `description:"来源id"`
  19. EdbCode string `description:"指标编码"`
  20. EdbName string `description:"指标名称"`
  21. EdbNameEn string `description:"英文指标名称"`
  22. EdbNameSource string `description:"指标名称来源"`
  23. Frequency string `description:"频率"`
  24. Unit string `description:"单位"`
  25. UnitEn string `description:"英文单位"`
  26. StartDate string `description:"起始日期"`
  27. EndDate string `description:"终止日期"`
  28. ClassifyId int `description:"分类id"`
  29. SysUserId int
  30. SysUserRealName string
  31. UniqueCode string `description:"指标唯一编码"`
  32. CreateTime time.Time
  33. ModifyTime time.Time
  34. BaseModifyTime time.Time
  35. MinValue float64 `description:"指标最小值"`
  36. MaxValue float64 `description:"指标最大值"`
  37. CalculateFormula string `description:"计算公式"`
  38. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  39. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  40. Sort int `description:"排序字段"`
  41. LatestDate string `description:"数据最新日期(实际日期)"`
  42. LatestValue float64 `description:"数据最新值(实际值)"`
  43. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  44. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  45. MoveFrequency string `description:"移动频度"`
  46. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  47. ServerUrl string `description:"服务器地址"`
  48. ChartImage string `description:"图表图片"`
  49. Calendar string `description:"公历/农历" orm:"default(公历);"`
  50. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  51. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  52. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  53. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  54. TerminalCode string `description:"终端编码,用于配置在机器上"`
  55. DataUpdateTime string `description:"最近一次数据发生变化的时间"`
  56. ErDataUpdateDate string `description:"本次更新,数据发生变化的最早日期"`
  57. SourceIndexName string `description:"数据源中的指标名称"`
  58. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  59. SubSourceName string `description:"子数据来源名称"`
  60. IndicatorCode string `description:"指标代码"`
  61. StockCode string `description:"证券代码"`
  62. Extra string `description:"指标的额外配置"`
  63. }
  64. func (e *EdbInfo) Add() (err error) {
  65. o := orm.NewOrm()
  66. _, err = o.Insert(e)
  67. return
  68. }
  69. // AddEdbInfo 添加指标
  70. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  71. o := orm.NewOrm()
  72. lastId, err = o.Insert(item)
  73. return
  74. }
  75. // EdbInfoList 指标数据列表
  76. type EdbInfoList struct {
  77. EdbInfoId int `orm:"column(edb_info_id);pk"`
  78. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  79. SourceName string `description:"来源名称"`
  80. Source int `description:"来源id"`
  81. EdbCode string `description:"指标编码"`
  82. EdbNameEn string `description:"英文指标名称"`
  83. EdbName string `description:"指标名称"`
  84. Frequency string `description:"频率"`
  85. FrequencyEn string `description:"英文频率"`
  86. Unit string `description:"单位"`
  87. UnitEn string `description:"英文单位"`
  88. StartDate string `description:"起始日期"`
  89. EndDate string `description:"终止日期"`
  90. LatestDate string `description:"数据最新日期(实际日期)"`
  91. LatestValue float64 `description:"数据最新值(实际值)"`
  92. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  93. ClassifyId int `description:"分类id"`
  94. UniqueCode string `description:"指标唯一编码"`
  95. SysUserId int `description:"创建人id"`
  96. SysUserRealName string `description:"创建人姓名"`
  97. ModifyTime string `description:"最新修改时间"`
  98. CreateTime string `description:"创建时间"`
  99. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  100. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  101. ChartImage string `description:"图表图片"`
  102. RuleType int `description:"预测规则,1:最新,2:固定值"`
  103. FixedValue float64 `description:"固定值"`
  104. DataList []*EdbData `description:"实际指标数据"`
  105. PredictDataList []*EdbData `description:"预测指标数据"`
  106. Button EdbClassifyItemsButton `description:"操作权限"`
  107. IsEnEdb bool `description:"是否展示英文标识"`
  108. DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
  109. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  110. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  111. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  112. SubSourceName string `description:"子数据来源名称"`
  113. IndicatorCode string `description:"指标代码"`
  114. StockCode string `description:"证券代码"`
  115. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  116. }
  117. // EdbClassifyItemsButton 操作按钮
  118. type EdbClassifyItemsButton struct {
  119. AddButton bool `description:"是否可添加"`
  120. OpButton bool `description:"是否可编辑"`
  121. DeleteButton bool `description:"是否可删除"`
  122. MoveButton bool `description:"是否可移动"`
  123. ShowEdbRelation bool `description:"是否展示关联指标"`
  124. ShowChartRelation bool `description:"是否展示关联图表"`
  125. }
  126. // GetEdbInfoByName 根据指标名称获取所有的指标数据列表
  127. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  128. o := orm.NewOrm()
  129. sql := ` SELECT * FROM edb_info WHERE edb_name=? `
  130. _, err = o.Raw(sql, edbName).QueryRows(&items)
  131. return
  132. }
  133. // ModifyEdbInfoNameSource 根据来源修改指标名称
  134. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  135. o := orm.NewOrm()
  136. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  137. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  138. return
  139. }
  140. // GetEdbInfoById 根据指标id获取指标信息
  141. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  142. o := orm.NewOrm()
  143. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  144. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  145. return
  146. }
  147. // GetEdbInfoByUniqueCode 根据指标唯一编码获取指标信息
  148. func GetEdbInfoByUniqueCode(uniqueCode string) (item *EdbInfo, err error) {
  149. o := orm.NewOrm()
  150. sql := ` SELECT * FROM edb_info WHERE unique_code=? `
  151. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  152. return
  153. }
  154. // GetEdbInfoByIdList 根据指标id列表获取指标信息
  155. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  156. num := len(edbInfoIdList)
  157. if num <= 0 {
  158. return
  159. }
  160. o := orm.NewOrm()
  161. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  162. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  163. return
  164. }
  165. // Update 更新EdbInfo信息
  166. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  167. o := orm.NewOrm()
  168. _, err = o.Update(edbInfo, cols...)
  169. return
  170. }
  171. // EdbInfoSearchData
  172. type EdbInfoSearchData struct {
  173. EdbDataId int `description:"数据ID"`
  174. EdbInfoId int `description:"指标ID"`
  175. DataTime string `description:"数据日期"`
  176. Value float64 `description:"数据"`
  177. EdbCode string `description:"指标编码"`
  178. DataTimestamp int64 `description:"时间戳"`
  179. }
  180. type FindEdbDataListAllCond struct {
  181. EdbInfoId int
  182. StartDataTime string
  183. StartDataTimeCond string
  184. EndDataTime string
  185. EndDataTimeCond string
  186. }
  187. // GetEdbDataListAll
  188. // @Description: 获取指标数据列表 order:1升序,其余值为降序
  189. // @author: Roc
  190. // @datetime 2024-05-08 15:34:01
  191. // @param source int
  192. // @param subSource int
  193. // @param findEdbDataListAllCond FindEdbDataListAllCond
  194. // @param order int order:1升序,其余值为降序
  195. // @return item []*EdbInfoSearchData
  196. // @return err error
  197. func GetEdbDataListAll(source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (item []*EdbInfoSearchData, err error) {
  198. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  199. return GetEdbDataListAllByMongo(source, subSource, findEdbDataListAllCond, order)
  200. }
  201. return GetEdbDataListAllByMysql(source, subSource, findEdbDataListAllCond, order)
  202. }
  203. // GetEdbDataListAllByMysql
  204. // @Description: 从mysql数据库中获取指标数据列表 order:1升序,其余值为降序
  205. // @author: Roc
  206. // @datetime 2024-05-08 15:32:55
  207. // @param source int
  208. // @param subSource int
  209. // @param findEdbDataListAllCond FindEdbDataListAllCond
  210. // @param order int
  211. // @return item []*EdbInfoSearchData
  212. // @return err error
  213. func GetEdbDataListAllByMysql(source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (item []*EdbInfoSearchData, err error) {
  214. if findEdbDataListAllCond.EdbInfoId <= 0 {
  215. return
  216. }
  217. to := orm.NewOrm()
  218. sql := ``
  219. tableName := GetEdbDataTableName(source, subSource)
  220. sql = ` SELECT * FROM %s WHERE 1=1 `
  221. sql = fmt.Sprintf(sql, tableName)
  222. var condition string
  223. var pars []interface{}
  224. condition += " AND edb_info_id=? "
  225. pars = append(pars, findEdbDataListAllCond.EdbInfoId)
  226. // 开始日期
  227. if findEdbDataListAllCond.StartDataTime != "" && findEdbDataListAllCond.StartDataTimeCond != `` {
  228. condition += fmt.Sprintf(" AND data_time %s ? ", findEdbDataListAllCond.StartDataTimeCond)
  229. pars = append(pars, findEdbDataListAllCond.StartDataTime)
  230. }
  231. // 结束日期
  232. if findEdbDataListAllCond.EndDataTime != "" && findEdbDataListAllCond.EndDataTimeCond != `` {
  233. condition += fmt.Sprintf(" AND data_time %s ? ", findEdbDataListAllCond.EndDataTimeCond)
  234. pars = append(pars, findEdbDataListAllCond.EndDataTime)
  235. }
  236. if condition != "" {
  237. sql += condition
  238. }
  239. if order == 1 {
  240. sql += ` ORDER BY data_time ASC `
  241. } else {
  242. sql += ` ORDER BY data_time DESC `
  243. }
  244. _, err = to.Raw(sql, pars).QueryRows(&item)
  245. return
  246. }
  247. // GetEdbDataListAllByTo
  248. // @Description: 根据事务链接获取指标数据列表 order:1升序,其余值为降序
  249. // @author: Roc
  250. // @datetime 2024-05-08 15:34:06
  251. // @param to orm.TxOrmer
  252. // @param source int
  253. // @param subSource int
  254. // @param findEdbDataListAllCond FindEdbDataListAllCond
  255. // @param order int
  256. // @return item []*EdbInfoSearchData
  257. // @return err error
  258. func GetEdbDataListAllByTo(to orm.TxOrmer, source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (item []*EdbInfoSearchData, err error) {
  259. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  260. return GetEdbDataListAllByMongo(source, subSource, findEdbDataListAllCond, order)
  261. }
  262. return GetEdbDataListAllByMysqlTo(to, source, subSource, findEdbDataListAllCond, order)
  263. }
  264. // GetEdbDataListAllByMysqlTo
  265. // @Description: 根据事务链接获取指标数据列表 order:1升序,其余值为降序(Mysql)
  266. // @author: Roc
  267. // @datetime 2024-05-08 15:34:13
  268. // @param to orm.TxOrmer
  269. // @param source int
  270. // @param subSource int
  271. // @param findEdbDataListAllCond FindEdbDataListAllCond
  272. // @param order int
  273. // @return item []*EdbInfoSearchData
  274. // @return err error
  275. func GetEdbDataListAllByMysqlTo(to orm.TxOrmer, source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (item []*EdbInfoSearchData, err error) {
  276. if findEdbDataListAllCond.EdbInfoId <= 0 {
  277. return
  278. }
  279. sql := ``
  280. tableName := GetEdbDataTableName(source, subSource)
  281. sql = ` SELECT * FROM %s WHERE 1=1 `
  282. sql = fmt.Sprintf(sql, tableName)
  283. var condition string
  284. var pars []interface{}
  285. condition += " AND edb_info_id=? "
  286. pars = append(pars, findEdbDataListAllCond.EdbInfoId)
  287. // 开始日期
  288. if findEdbDataListAllCond.StartDataTime != "" && findEdbDataListAllCond.StartDataTimeCond != `` {
  289. condition += fmt.Sprintf(" AND data_time %s ? ", findEdbDataListAllCond.StartDataTimeCond)
  290. pars = append(pars, findEdbDataListAllCond.StartDataTime)
  291. }
  292. // 结束日期
  293. if findEdbDataListAllCond.EndDataTime != "" && findEdbDataListAllCond.EndDataTimeCond != `` {
  294. condition += fmt.Sprintf(" AND data_time %s ? ", findEdbDataListAllCond.EndDataTimeCond)
  295. pars = append(pars, findEdbDataListAllCond.EndDataTime)
  296. }
  297. if condition != "" {
  298. sql += condition
  299. }
  300. if order == 1 {
  301. sql += ` ORDER BY data_time ASC `
  302. } else {
  303. sql += ` ORDER BY data_time DESC `
  304. }
  305. _, err = to.Raw(sql, pars).QueryRows(&item)
  306. return
  307. }
  308. // GetEdbDataListAllByMongo
  309. // @Description: 根据事务链接获取指标数据列表 order:1升序,其余值为降序(Mongo)
  310. // @author: Roc
  311. // @datetime 2024-05-08 15:34:24
  312. // @param source int
  313. // @param subSource int
  314. // @param findEdbDataListAllCond FindEdbDataListAllCond
  315. // @param order int
  316. // @return dataList []*EdbInfoSearchData
  317. // @return err error
  318. func GetEdbDataListAllByMongo(source, subSource int, findEdbDataListAllCond FindEdbDataListAllCond, order int) (dataList []*EdbInfoSearchData, err error) {
  319. dataList = make([]*EdbInfoSearchData, 0)
  320. if findEdbDataListAllCond.EdbInfoId <= 0 {
  321. return
  322. }
  323. mogDataObj := mgo.EdbDataBusiness{}
  324. // 构建查询条件
  325. queryConditions := bson.M{
  326. "edb_info_id": findEdbDataListAllCond.EdbInfoId,
  327. }
  328. dateCondition, err := mgo.BuildDateCondition(findEdbDataListAllCond.StartDataTime, findEdbDataListAllCond.EndDataTime)
  329. if err != nil {
  330. return
  331. }
  332. if len(dateCondition) > 0 {
  333. queryConditions["data_time"] = dateCondition
  334. }
  335. sortList := make([]string, 0)
  336. if order == 1 {
  337. sortList = append(sortList, "data_time")
  338. } else {
  339. sortList = append(sortList, "-data_time")
  340. }
  341. // 获取列表数据
  342. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, sortList)
  343. if tmpErr != nil {
  344. err = tmpErr
  345. return
  346. }
  347. for k, v := range tmpDataList {
  348. dataList = append(dataList, &EdbInfoSearchData{
  349. EdbDataId: k + 1,
  350. EdbInfoId: v.EdbInfoId,
  351. EdbCode: v.EdbCode,
  352. DataTime: v.DataTime.Format(utils.FormatDate),
  353. Value: v.Value,
  354. DataTimestamp: v.DataTimestamp,
  355. })
  356. }
  357. return
  358. }
  359. // EdbInfoMaxAndMinInfo 指标最新数据记录结构体
  360. type EdbInfoMaxAndMinInfo struct {
  361. MinDate string `description:"最小日期" bson:"min_date"`
  362. MaxDate string `description:"最大日期" bson:"max_date"`
  363. MinValue float64 `description:"最小值" bson:"min_value"`
  364. MaxValue float64 `description:"最大值" bson:"max_value"`
  365. LatestValue float64 `description:"最新值" bson:"latest_value"`
  366. LatestDate string `description:"实际数据最新日期" bson:"latest_date"`
  367. EndValue float64 `description:"最新值" bson:"end_value"`
  368. }
  369. // GetEdbInfoMaxAndMinInfo 获取指标的最新数据记录信息
  370. func GetEdbInfoMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  371. o := orm.NewOrm()
  372. sql := ``
  373. tableName := GetEdbDataTableName(source, subSource)
  374. 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=? `
  375. sql = fmt.Sprintf(sql, tableName)
  376. err = o.Raw(sql, edbCode).QueryRow(&item)
  377. var latest_value float64
  378. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  379. sql = fmt.Sprintf(sql, tableName)
  380. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  381. item.LatestValue = latest_value
  382. return
  383. }
  384. type EdbIndoDataUpdateTime struct {
  385. MinDate string `description:"本次更新,数据发生变化的最早日期"`
  386. }
  387. // GetEdbDataUpdateTimeByModify 获取指标的刷新时间获取最早的一条记录
  388. func GetEdbDataUpdateTimeByModify(source, subSource int, edbCode string, dataUpdateTime string) (item *EdbIndoDataUpdateTime, err error) {
  389. o := orm.NewOrm()
  390. sql := ``
  391. tableName := GetEdbDataTableName(source, subSource)
  392. sql = ` SELECT MIN(data_time) AS min_date FROM %s WHERE edb_code=? and modify_time>=?`
  393. sql = fmt.Sprintf(sql, tableName)
  394. err = o.Raw(sql, edbCode, dataUpdateTime).QueryRow(&item)
  395. return
  396. }
  397. // ModifyEdbInfoMaxAndMinInfo 修改指标的最新数据信息
  398. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  399. o := orm.NewOrm()
  400. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?, end_value = ?,modify_time=NOW() WHERE edb_info_id=? `
  401. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, item.LatestValue, edbInfoId).Exec()
  402. return
  403. }
  404. // ModifyEdbTimeAndLastInfo 修改指标的最新数据信息(除上下限)
  405. func ModifyEdbTimeAndLastInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  406. o := orm.NewOrm()
  407. sql := `UPDATE edb_info SET start_date = ?, end_date = ?, is_update = 2,latest_date = ?, latest_value = ?, end_value = ?, modify_time = NOW() WHERE edb_info_id = ?`
  408. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MaxDate, item.LatestValue, item.LatestValue, edbInfoId).Exec()
  409. return
  410. }
  411. // ModifyEdbDataUpdateTime 修改指标刷新,本次数据刷新的最早日期
  412. func ModifyEdbDataUpdateTime(edbInfoId int, dataUpdateTime, erDataUpdateDate string) (err error) {
  413. o := orm.NewOrm()
  414. sql := `UPDATE edb_info SET data_update_time = ?, er_data_update_date = ?, modify_time = NOW() WHERE edb_info_id = ?`
  415. _, err = o.Raw(sql, dataUpdateTime, erDataUpdateDate, edbInfoId).Exec()
  416. return
  417. }
  418. // GetLteZeroEdbDataCount
  419. // @Description: 获取小于等于0的数据数量
  420. // @author: Roc
  421. // @datetime 2024-05-31 10:44:39
  422. // @param source int
  423. // @param subSource int
  424. // @param edbInfoId int
  425. // @return count int
  426. // @return err error
  427. func GetLteZeroEdbDataCount(source, subSource, edbInfoId int) (count int, err error) {
  428. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  429. return GetLteZeroEdbDataCountByMongo(source, subSource, edbInfoId)
  430. }
  431. return GetLteZeroEdbDataCountByMysql(source, subSource, edbInfoId)
  432. }
  433. // GetLteZeroEdbDataCountByMongo
  434. // @Description: 获取小于等于0的数据数量(从mongo)
  435. // @author: Roc
  436. // @datetime 2024-05-31 10:41:04
  437. // @param source int
  438. // @param subSource int
  439. // @param edbInfoId int
  440. // @return count int
  441. // @return err error
  442. func GetLteZeroEdbDataCountByMongo(source, subSource, edbInfoId int) (count int, err error) {
  443. mogDataObj := mgo.EdbDataBusiness{}
  444. // 构建查询条件
  445. whereQuery := bson.M{
  446. "edb_info_id": edbInfoId,
  447. "value": bson.M{"$lte": 0},
  448. }
  449. // 获数量数据
  450. tmpCount, err := mogDataObj.GetCountDataList(whereQuery)
  451. if err != nil {
  452. return
  453. }
  454. count = int(tmpCount)
  455. return
  456. }
  457. // GetLteZeroEdbDataCountByMysql
  458. // @Description: 获取小于等于0的数据数量(从mysql)
  459. // @author: Roc
  460. // @datetime 2024-05-31 10:29:19
  461. // @param source int
  462. // @param subSource int
  463. // @param edbInfoId int
  464. // @return count int
  465. // @return err error
  466. func GetLteZeroEdbDataCountByMysql(source, subSource, edbInfoId int) (count int, err error) {
  467. o := orm.NewOrm()
  468. sql := ``
  469. tableName := GetEdbDataTableName(source, subSource)
  470. sql = ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 AND edb_info_id =? AND value <=0 `
  471. sql = fmt.Sprintf(sql, tableName)
  472. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  473. return
  474. }
  475. // GetLastEdbData 获取最近的一条指标数据
  476. func GetLastEdbData(condition string, pars []interface{}, source, subSource int) (item *EdbInfoSearchData, err error) {
  477. o := orm.NewOrm()
  478. sql := ``
  479. tableName := GetEdbDataTableName(source, subSource)
  480. sql = ` SELECT * FROM %s WHERE 1=1 `
  481. sql = fmt.Sprintf(sql, tableName)
  482. if condition != "" {
  483. sql += condition
  484. }
  485. sql += ` ORDER BY data_time DESC `
  486. err = o.Raw(sql, pars).QueryRow(&item)
  487. return
  488. }
  489. // GetEdbInfoByEdbCode 根据指标code获取指标信息
  490. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  491. o := orm.NewOrm()
  492. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  493. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  494. return
  495. }
  496. // GetEdbInfoOnlyByEdbCode 仅根据指标code获取指标信息
  497. func GetEdbInfoOnlyByEdbCode(edbCode string) (item *EdbInfo, err error) {
  498. o := orm.NewOrm()
  499. sql := ` SELECT * FROM edb_info WHERE edb_code=? `
  500. err = o.Raw(sql, edbCode).QueryRow(&item)
  501. return
  502. }
  503. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  504. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  505. o := orm.NewOrm()
  506. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  507. if condition != "" {
  508. sql += condition
  509. }
  510. _, err = o.Raw(sql, pars).QueryRows(&items)
  511. return
  512. }
  513. // GetEdbInfoCalculateCountByCondition 获取关联指标数量
  514. func GetEdbInfoCalculateCountByCondition(condition string, pars []interface{}) (count int, err error) {
  515. o := orm.NewOrm()
  516. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_mapping WHERE 1=1 `
  517. if condition != "" {
  518. sql += condition
  519. }
  520. err = o.Raw(sql, pars).QueryRow(&count)
  521. return
  522. }
  523. // 优化版本-处理数据精度问题
  524. type EdbInfoSearchDataV1 struct {
  525. EdbDataId int `description:"数据ID"`
  526. DataTime string `description:"数据日期"`
  527. Value string `description:"数据"`
  528. }
  529. // 优化版本-处理数据精度问题
  530. func GetEdbDataListAllV1(condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchDataV1, err error) {
  531. o := orm.NewOrm()
  532. sql := ``
  533. tableName := GetEdbDataTableName(source, subSource)
  534. sql = ` SELECT * FROM %s WHERE 1=1 `
  535. sql = fmt.Sprintf(sql, tableName)
  536. if condition != "" {
  537. sql += condition
  538. }
  539. if order == 1 {
  540. sql += ` ORDER BY data_time ASC `
  541. } else {
  542. sql += ` ORDER BY data_time DESC `
  543. }
  544. _, err = o.Raw(sql, pars).QueryRows(&item)
  545. return
  546. }
  547. // GetEdbDataListAllV1ByTo 通过事务链接获取数据列表
  548. func GetEdbDataListAllV1ByTo(to orm.TxOrmer, condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchDataV1, err error) {
  549. sql := ``
  550. tableName := GetEdbDataTableName(source, subSource)
  551. sql = ` SELECT * FROM %s WHERE 1=1 `
  552. sql = fmt.Sprintf(sql, tableName)
  553. if condition != "" {
  554. sql += condition
  555. }
  556. if order == 1 {
  557. sql += ` ORDER BY data_time ASC `
  558. } else {
  559. sql += ` ORDER BY data_time DESC `
  560. }
  561. _, err = to.Raw(sql, pars).QueryRows(&item)
  562. return
  563. }
  564. // GetEdbInfoByCondition 获取指标列表
  565. func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfo, err error) {
  566. o := orm.NewOrm()
  567. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  568. if condition != "" {
  569. sql += condition
  570. }
  571. if order == 1 {
  572. sql += ` ORDER BY end_date ASC `
  573. } else {
  574. sql += ` ORDER BY edb_info_id ASC `
  575. }
  576. _, err = o.Raw(sql, pars).QueryRows(&item)
  577. return
  578. }
  579. // UnifiedModifyEdbInfoMaxAndMinInfo 统一修改指标的最大最小值
  580. func UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo *EdbInfo) (err error, errMsg string) {
  581. // 修改最大最小值
  582. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode)
  583. if err != nil {
  584. if err.Error() == utils.ErrNoRow() {
  585. err = nil
  586. return
  587. }
  588. errMsg = "刷新指标失败!"
  589. err = errors.New("获取指标最大最小值失败,err:" + err.Error())
  590. return
  591. }
  592. if maxAndMinItem != nil {
  593. // ETA1.0.3改-如果指标有手动保存过, 那么就不更新指标的最大最小值
  594. if edbInfo.ManualSave == 1 {
  595. err = ModifyEdbTimeAndLastInfo(edbInfo.EdbInfoId, maxAndMinItem)
  596. if err != nil {
  597. errMsg = "刷新指标失败!"
  598. err = errors.New("修改指标最新日期和数据失败,err:" + err.Error())
  599. return
  600. }
  601. } else {
  602. err = ModifyEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, maxAndMinItem)
  603. if err != nil {
  604. errMsg = "刷新指标失败!"
  605. err = errors.New("修改指标最大最小值失败,err:" + err.Error())
  606. return
  607. }
  608. }
  609. }
  610. // 刷新关联的预测指标
  611. go RefreshPredictStandardBaseByGeneralEdbInfoId(edbInfo.EdbInfoId)
  612. //go ModifyPredictEdbInfoMaxAndMinInfoBySourceEdbInfoId(edbInfo.EdbInfoId, maxAndMinItem)
  613. return
  614. }
  615. // UnifiedModifyEdbInfoMaxAndMinInfoDataUpdate 统一修改指标的最大最小值以及数据更新时间点
  616. func UnifiedModifyEdbInfoMaxAndMinInfoDataUpdate(edbInfo *EdbInfo, dataUpdateTime string) (erDataUpdateDate string, err error, errMsg string) {
  617. err, errMsg = UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  618. if err != nil {
  619. return
  620. }
  621. // 获取本次刷新,指标数据更新的最早日期
  622. erDataUpdateTime, tErr := GetEdbDataUpdateTimeByModify(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, dataUpdateTime)
  623. if tErr != nil {
  624. if tErr.Error() != utils.ErrNoRow() { //本次刷新指标数据无变化
  625. errMsg = "刷新指标失败!"
  626. err = errors.New("查询指标刷新,数据更新的最早日期失败,err:" + tErr.Error())
  627. return
  628. }
  629. } else if erDataUpdateTime.MinDate != "" {
  630. err = ModifyEdbDataUpdateTime(edbInfo.EdbInfoId, dataUpdateTime, erDataUpdateTime.MinDate)
  631. if err != nil {
  632. errMsg = "刷新指标失败!"
  633. err = errors.New("修改指标数据更新的最早日期失败,err:" + tErr.Error())
  634. return
  635. }
  636. erDataUpdateDate = erDataUpdateTime.MinDate
  637. }
  638. return
  639. }
  640. // UnifiedModifyPredictEdbInfoMaxAndMinInfo 统一修改预测运算指标的最大最小值
  641. func UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo *EdbInfo, latestDateStr string, latestValue float64) (err error, errMsg string) {
  642. // 修改最大最小值
  643. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode)
  644. if err != nil {
  645. if err.Error() == utils.ErrNoRow() {
  646. err = nil
  647. return
  648. }
  649. errMsg = "刷新指标失败!"
  650. err = errors.New("获取指标最大最小值失败,err:" + err.Error())
  651. return
  652. }
  653. // 最晚的日期的值
  654. maxAndMinItem.EndValue = maxAndMinItem.LatestValue
  655. if maxAndMinItem != nil {
  656. maxAndMinItem.LatestDate = latestDateStr
  657. maxAndMinItem.LatestValue = latestValue
  658. err = ModifyPredictEdbInfoMaxAndMinInfo(edbInfo.EdbInfoId, maxAndMinItem)
  659. if err != nil {
  660. errMsg = "刷新指标失败!"
  661. err = errors.New("修改指标最大最小值失败,err:" + err.Error())
  662. return
  663. }
  664. }
  665. return
  666. }
  667. // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
  668. func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []*PredictEdbConfAndData, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*EdbInfoSearchData) (predictEdbInfoData []*EdbInfoSearchData, minValue, maxValue float64, err error) {
  669. endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
  670. if err != nil {
  671. return
  672. }
  673. latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
  674. if err != nil {
  675. return
  676. }
  677. // 开始预测数据的时间
  678. startDate := latestDate
  679. // 如果有筛选时间的话
  680. if filtrateStartDateStr != `` {
  681. filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
  682. if tmpErr != nil {
  683. err = tmpErr
  684. return
  685. }
  686. //如果筛选时间晚于实际数据时间,那么就以筛选时间作为获取预测数据的时间
  687. if filtrateStartDate.After(latestDate) {
  688. startDate = filtrateStartDate.AddDate(0, 0, -1)
  689. }
  690. }
  691. //var dateArr []string
  692. // 对应日期的值
  693. existMap := make(map[string]float64)
  694. for _, v := range realPredictEdbInfoData {
  695. //dateArr = append(dateArr, v.DataTime)
  696. existMap[v.DataTime] = v.Value
  697. }
  698. predictEdbInfoData = make([]*EdbInfoSearchData, 0)
  699. //dataValue := lastDataValue
  700. //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
  701. for _, predictEdbConf := range predictEdbConfList {
  702. //dataEndTime := endDate
  703. //if predictEdbConf.EndDate.Before(dataEndTime) {
  704. // dataEndTime = predictEdbConf.EndDate
  705. //}
  706. endDate = predictEdbConf.EndDate
  707. var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
  708. dayList := getPredictEdbDayList(startDate, endDate, frequency, dataDateType)
  709. if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
  710. continue
  711. }
  712. switch predictEdbConf.RuleType {
  713. case 1: //1:最新
  714. var lastDataValue float64 //最新值
  715. tmpAllData := make([]*EdbInfoSearchData, 0)
  716. tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
  717. tmpAllData = append(tmpAllData, predictEdbInfoData...)
  718. lenTmpAllData := len(tmpAllData)
  719. if lenTmpAllData > 0 {
  720. lastDataValue = tmpAllData[lenTmpAllData-1].Value
  721. }
  722. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
  723. tmpMaxValue = lastDataValue
  724. tmpMinValue = lastDataValue
  725. case 2: //2:固定值
  726. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  727. if tmpErr != nil {
  728. err = tmpErr
  729. return
  730. }
  731. dataValue, _ := tmpValDecimal.Float64()
  732. predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
  733. tmpMaxValue = dataValue
  734. tmpMinValue = dataValue
  735. case 3: //3:同比
  736. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  737. if tmpErr != nil {
  738. err = tmpErr
  739. return
  740. }
  741. tbValue, _ := tmpValDecimal.Float64()
  742. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  743. case 4: //4:同差
  744. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  745. if tmpErr != nil {
  746. err = tmpErr
  747. return
  748. }
  749. tcValue, _ := tmpValDecimal.Float64()
  750. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  751. case 5: //5:环比
  752. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  753. if tmpErr != nil {
  754. err = tmpErr
  755. return
  756. }
  757. hbValue, _ := tmpValDecimal.Float64()
  758. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  759. case 6: //6:环差
  760. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  761. if tmpErr != nil {
  762. err = tmpErr
  763. return
  764. }
  765. hcValue, _ := tmpValDecimal.Float64()
  766. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  767. case 7: //7:N期移动均值
  768. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  769. if tmpErr != nil {
  770. err = tmpErr
  771. return
  772. }
  773. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  774. case 8: //8:N期段线性外推值
  775. nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
  776. if tmpErr != nil {
  777. err = tmpErr
  778. return
  779. }
  780. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  781. if err != nil {
  782. return
  783. }
  784. case 9: //9:动态环差”预测规则;
  785. hcDataMap := make(map[string]float64) //规则计算的环差值map
  786. if predictEdbConf.PredictEdbInfoId > 0 {
  787. tmpPredictEdbRuleDataList, tmpErr := GetPredictEdbRuleDataItemList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  788. if tmpErr != nil {
  789. err = tmpErr
  790. return
  791. }
  792. for _, v := range tmpPredictEdbRuleDataList {
  793. hcDataMap[v.DataTime] = v.Value
  794. }
  795. } else {
  796. if len(predictEdbConf.DataList) <= 0 {
  797. return
  798. }
  799. for _, v := range predictEdbConf.DataList {
  800. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  801. if tmpErr != nil {
  802. continue
  803. }
  804. // 只处理时间段内的数据
  805. if currentDate.Before(startDate) || currentDate.After(endDate) {
  806. continue
  807. }
  808. hcDataMap[v.DataTime] = v.Value
  809. }
  810. }
  811. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, hcDataMap, existMap)
  812. case 10: //10:根据 给定终值后插值 规则获取预测数据
  813. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  814. if tmpErr != nil {
  815. err = tmpErr
  816. return
  817. }
  818. finalValue, _ := tmpValDecimal.Float64()
  819. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  820. case 11: //11:根据 季节性 规则获取预测数据
  821. var seasonConf SeasonConf
  822. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &seasonConf)
  823. if tmpErr != nil {
  824. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  825. return
  826. }
  827. calendar := "公历"
  828. if seasonConf.Calendar == "农历" {
  829. calendar = "农历"
  830. }
  831. yearList := make([]int, 0)
  832. //选择方式,1:连续N年;2:指定年份
  833. if seasonConf.YearType == 1 {
  834. if seasonConf.NValue < 1 {
  835. err = errors.New("连续N年不允许小于1")
  836. return
  837. }
  838. currYear := time.Now().Year()
  839. for i := 0; i < seasonConf.NValue; i++ {
  840. yearList = append(yearList, currYear-i-1)
  841. }
  842. } else {
  843. yearList = seasonConf.YearList
  844. }
  845. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, yearList, calendar, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  846. if err != nil {
  847. return
  848. }
  849. case 12: //12:根据 移动平均同比 规则获取预测数据
  850. var moveAverageConf MoveAverageConf
  851. tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
  852. if tmpErr != nil {
  853. err = errors.New("季节性配置信息异常:" + tmpErr.Error())
  854. return
  855. }
  856. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  857. if err != nil {
  858. return
  859. }
  860. case 13: //13:根据 同比增速差值 规则获取预测数据
  861. tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
  862. if tmpErr != nil {
  863. err = tmpErr
  864. return
  865. }
  866. tbEndValue, _ := tmpValDecimal.Float64()
  867. predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  868. case 14: //14:根据 一元线性拟合 规则获取预测数据
  869. var ruleConf RuleLineNhConf
  870. err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
  871. if err != nil {
  872. err = errors.New("一元线性拟合规则配置信息异常:" + err.Error())
  873. return
  874. }
  875. // 规则计算的拟合残差值map
  876. newNhccDataMap := make(map[string]float64)
  877. if predictEdbConf.PredictEdbInfoId > 0 { //已经生成的动态数据
  878. tmpPredictEdbRuleDataList, tmpErr := GetPredictEdbRuleDataItemList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
  879. if tmpErr != nil {
  880. err = tmpErr
  881. return
  882. }
  883. for _, v := range tmpPredictEdbRuleDataList {
  884. newNhccDataMap[v.DataTime] = v.Value
  885. }
  886. } else { //未生成的动态数据,需要使用外部传入的数据进行计算
  887. newNhccDataMap, err, _ = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
  888. }
  889. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
  890. if err != nil {
  891. return
  892. }
  893. case 15: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
  894. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
  895. if err != nil {
  896. return
  897. }
  898. case 16: //16:年度值倒推
  899. predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
  900. if err != nil {
  901. return
  902. }
  903. }
  904. // 下一个规则的开始日期
  905. {
  906. lenPredictEdbInfoData := len(predictEdbInfoData)
  907. if lenPredictEdbInfoData > 0 {
  908. tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
  909. if startDate.Before(tmpDataEndTime) {
  910. startDate = tmpDataEndTime
  911. }
  912. }
  913. }
  914. if tmpMinValue < minValue {
  915. minValue = tmpMinValue
  916. }
  917. if tmpMaxValue < maxValue {
  918. maxValue = tmpMaxValue
  919. }
  920. }
  921. return
  922. }
  923. // GetPredictEdbDayList 获取预测指标日期列表
  924. // GetPredictEdbDayList 获取预测指标日期列表
  925. func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
  926. if dataDateType == `` {
  927. dataDateType = `交易日`
  928. }
  929. switch frequency {
  930. case "日度":
  931. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
  932. // 如果日期类型是交易日的时候,那么需要将周六、日排除
  933. if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
  934. continue
  935. }
  936. dayList = append(dayList, currDate)
  937. }
  938. case "周度":
  939. //nextDate := startDate.AddDate(0, 0, 7)
  940. for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
  941. dayList = append(dayList, currDate)
  942. }
  943. case "旬度":
  944. for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
  945. nextDate := currDate.AddDate(0, 0, 1)
  946. //每个月的10号、20号、最后一天,那么就写入
  947. if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
  948. dayList = append(dayList, currDate)
  949. }
  950. currDate = nextDate
  951. }
  952. case "月度":
  953. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  954. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  955. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  956. dayList = append(dayList, currDate)
  957. }
  958. currDate = currDate.AddDate(0, 0, 1)
  959. }
  960. case "季度":
  961. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  962. // 每月的最后一天
  963. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  964. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  965. // 季度日期就写入,否则不写入
  966. if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
  967. dayList = append(dayList, currDate)
  968. }
  969. }
  970. currDate = currDate.AddDate(0, 0, 1)
  971. }
  972. case "半年度":
  973. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  974. // 每月的最后一天
  975. currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  976. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  977. // 半年度日期就写入,否则不写入
  978. if currDate.Month() == 6 || currDate.Month() == 12 {
  979. dayList = append(dayList, currDate)
  980. }
  981. }
  982. currDate = currDate.AddDate(0, 0, 1)
  983. }
  984. case "年度":
  985. for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
  986. currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
  987. if !currDate.After(endDate) && !currDate.Equal(startDate) {
  988. dayList = append(dayList, currDate)
  989. }
  990. }
  991. }
  992. return
  993. }
  994. // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据,order:1升序,其余值为降序
  995. func GetPredictDataListByPredictEdbInfo(edbInfo *EdbInfo, order int, startDate string) (dataList []*EdbInfoSearchData, sourceEdbInfoItem *EdbInfo, err error, errMsg string) {
  996. // 查找该预测指标配置
  997. predictEdbConfList, err := GetPredictEdbConfAndDataListById(edbInfo.EdbInfoId)
  998. if err != nil && err.Error() != utils.ErrNoRow() {
  999. errMsg = "获取预测指标配置信息失败"
  1000. return
  1001. }
  1002. if len(predictEdbConfList) == 0 {
  1003. errMsg = "获取预测指标配置信息失败"
  1004. err = errors.New(errMsg)
  1005. return
  1006. }
  1007. predictEdbConf := predictEdbConfList[0]
  1008. // 来源指标
  1009. sourceEdbInfoItem, err = GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
  1010. if err != nil {
  1011. if err.Error() == utils.ErrNoRow() {
  1012. errMsg = "找不到来源指标信息"
  1013. err = errors.New(errMsg)
  1014. }
  1015. return
  1016. }
  1017. dataList, err, errMsg = GetPredictDataListByPredictEdbConfList(edbInfo, sourceEdbInfoItem, predictEdbConfList, order, startDate)
  1018. return
  1019. }
  1020. // GetPredictDataListByPredictEdbConfList 根据预测指标信息获取预测指标的数据,order:1升序,其余值为降序
  1021. func GetPredictDataListByPredictEdbConfList(edbInfo, sourceEdbInfoItem *EdbInfo, predictEdbConfList []*PredictEdbConfAndData, order int, startDate string) (dataList []*EdbInfoSearchData, err error, errMsg string) {
  1022. allDataList := make([]*EdbInfoSearchData, 0)
  1023. //获取指标数据(实际已生成)
  1024. tmpDataList, err := GetEdbDataListAll(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, FindEdbDataListAllCond{
  1025. EdbInfoId: sourceEdbInfoItem.EdbInfoId,
  1026. StartDataTime: startDate,
  1027. StartDataTimeCond: ">=",
  1028. }, 1)
  1029. if err != nil {
  1030. return
  1031. }
  1032. // 默认认为上一个查询是把所有的数据查出来了
  1033. allDataList = tmpDataList
  1034. // 如果条件中有选择了日期,那么上次查询实际并不是把所有的数据查出来了
  1035. // 那么需要把日期给过滤掉,然后筛选所有的数据,用于未来指标的生成
  1036. if startDate != `` {
  1037. allDataList, err = GetEdbDataListAll(sourceEdbInfoItem.Source, sourceEdbInfoItem.SubSource, FindEdbDataListAllCond{
  1038. EdbInfoId: sourceEdbInfoItem.EdbInfoId,
  1039. }, 1)
  1040. if err != nil {
  1041. return
  1042. }
  1043. }
  1044. // 获取预测指标未来的数据
  1045. predictDataList := make([]*EdbInfoSearchData, 0)
  1046. endDateStr := edbInfo.EndDate //预测指标的结束日期
  1047. var predictMinValue, predictMaxValue float64
  1048. // 如果有配置的预测规则,那么就进行预测
  1049. if len(predictEdbConfList) > 0 {
  1050. predictDataList, predictMinValue, predictMaxValue, err = GetChartPredictEdbInfoDataListByConfList(predictEdbConfList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, edbInfo.Frequency, edbInfo.DataDateType, allDataList)
  1051. if err != nil {
  1052. return
  1053. }
  1054. }
  1055. //order:1升序,其余值为降序
  1056. if order == 1 {
  1057. dataList = append(tmpDataList, predictDataList...)
  1058. } else {
  1059. // 先倒序预测数据
  1060. lenPredictDataList := len(predictDataList)
  1061. if lenPredictDataList > 0 {
  1062. for k := range predictDataList {
  1063. dataList = append(dataList, predictDataList[lenPredictDataList-k-1])
  1064. }
  1065. }
  1066. // 接着倒序实际指标
  1067. lenDataList := len(tmpDataList)
  1068. for k := range tmpDataList {
  1069. dataList = append(dataList, tmpDataList[lenDataList-k-1])
  1070. }
  1071. }
  1072. if len(predictDataList) > 0 {
  1073. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  1074. if edbInfo.MinValue > predictMinValue {
  1075. edbInfo.MinValue = predictMinValue
  1076. }
  1077. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  1078. if edbInfo.MaxValue < predictMaxValue {
  1079. edbInfo.MaxValue = predictMaxValue
  1080. }
  1081. }
  1082. return
  1083. }
  1084. // GetPredictEdbDataListAll 获取该预测指标所有的数据 ,order:1升序,其余值为降序
  1085. func GetPredictEdbDataListAll(edbInfo *EdbInfo, order int) (items []*EdbInfoSearchData, err error) {
  1086. if edbInfo.Source == utils.DATA_SOURCE_PREDICT { //普通的预测指标是没有入库数据的,直接往配置里面获取
  1087. items, _, err, _ = GetPredictDataListByPredictEdbInfo(edbInfo, 1, "")
  1088. } else {
  1089. items, err = GetEdbDataListAll(edbInfo.Source, edbInfo.SubSource, FindEdbDataListAllCond{
  1090. EdbInfoId: edbInfo.EdbInfoId,
  1091. }, order)
  1092. }
  1093. return
  1094. }
  1095. // GetPredictEdbDataListAllByStartDate 根据开始日期获取该预测指标所有的数据 ,order:1升序,其余值为降序
  1096. func GetPredictEdbDataListAllByStartDate(edbInfo *EdbInfo, order int, startDate string) (items []*EdbInfoSearchData, err error) {
  1097. if edbInfo.Source == utils.DATA_SOURCE_PREDICT { //普通的预测指标是没有入库数据的,直接往配置里面获取
  1098. items, _, err, _ = GetPredictDataListByPredictEdbInfo(edbInfo, order, startDate)
  1099. } else {
  1100. items, err = GetEdbDataListAll(edbInfo.Source, edbInfo.SubSource, FindEdbDataListAllCond{
  1101. EdbInfoId: edbInfo.EdbInfoId,
  1102. StartDataTime: startDate,
  1103. StartDataTimeCond: ">=",
  1104. }, order)
  1105. }
  1106. return
  1107. }
  1108. // ModifyPredictEdbInfoMaxAndMinInfo 修改预测指标的最新数据信息
  1109. func ModifyPredictEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  1110. o := orm.NewOrm()
  1111. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,end_value=?,modify_time=NOW() WHERE edb_info_id=? `
  1112. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.LatestDate, item.LatestValue, item.EndValue, edbInfoId).Exec()
  1113. return
  1114. }
  1115. // ModifyCalculateEdbInfo 修改计算指标信息
  1116. func ModifyCalculateEdbInfo(edbName, frequency, unit, calculateFormula string, classifyId, edbInfoId int) (err error) {
  1117. o := orm.NewOrm()
  1118. sql := ` UPDATE edb_info
  1119. SET
  1120. edb_name =?,
  1121. edb_name_source =?,
  1122. frequency = ?,
  1123. unit = ?,
  1124. classify_id = ?,
  1125. calculate_formula=?,
  1126. modify_time = NOW()
  1127. WHERE edb_info_id = ?`
  1128. _, err = o.Raw(sql, edbName, edbName, frequency, unit, classifyId, calculateFormula, edbInfoId).Exec()
  1129. return
  1130. }
  1131. func GetEdbInfoItemByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  1132. o := orm.NewOrm()
  1133. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1134. if condition != "" {
  1135. sql += condition
  1136. }
  1137. err = o.Raw(sql, pars).QueryRow(&item)
  1138. return
  1139. }
  1140. func GetEdbInfoItemListByCondition() (items []*EdbInfoList, err error) {
  1141. o := orm.NewOrm()
  1142. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1143. _, err = o.Raw(sql).QueryRows(&items)
  1144. return
  1145. }
  1146. // EdbInfoDetailReq 指标详情请求
  1147. type EdbInfoDetailReq struct {
  1148. UniqueCode string `description:"指标唯一编码"`
  1149. EdbCode string `description:"指标编码"`
  1150. }
  1151. // EdbInfoDetailResp 指标详情返回
  1152. type EdbInfoDetailResp struct {
  1153. SourceName string `description:"来源名称"`
  1154. EdbCode string `description:"指标编码"`
  1155. EdbName string `description:"指标名称"`
  1156. EdbNameSource string `description:"指标名称来源"`
  1157. Frequency string `description:"频率"`
  1158. Unit string `description:"单位"`
  1159. StartDate string `description:"起始日期"`
  1160. EndDate string `description:"终止日期"`
  1161. UniqueCode string `description:"指标唯一编码"`
  1162. CreateTime time.Time
  1163. ModifyTime time.Time
  1164. MinValue float64 `description:"指标最小值"`
  1165. MaxValue float64 `description:"指标最大值"`
  1166. EdbNameEn string `description:"英文指标名称"`
  1167. UnitEn string `description:"英文单位"`
  1168. LatestDate string `description:"数据最新日期"`
  1169. LatestValue float64 `description:"数据最新值"`
  1170. ChartImage string `description:"图表图片"`
  1171. DataList []*EdbInfoSearchData
  1172. }
  1173. type AddEdbInfoParams struct {
  1174. Source int `description:"来源id"`
  1175. EdbCode string `description:"指标编码"`
  1176. EdbName string `description:"指标名称"`
  1177. Frequency string `description:"频率"`
  1178. Unit string `description:"单位"`
  1179. ClassifyId int `description:"分类id"`
  1180. TerminalCode string `description:"终端编码"`
  1181. AdminId int `description:"创建人ID"`
  1182. AdminName string `description:"创建人姓名"`
  1183. }
  1184. // EdbInfoAdd 添加指标到指标库
  1185. func EdbInfoAdd(req *AddEdbInfoParams, serverUrl string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, err error) {
  1186. source := req.Source
  1187. //判断指标名称是否存在
  1188. var condition string
  1189. var pars []interface{}
  1190. //condition += " AND source=? "
  1191. //pars = append(pars, source)
  1192. condition += " AND edb_code=? "
  1193. pars = append(pars, req.EdbCode)
  1194. items, err := GetEdbInfoByCondition(condition, pars, 0)
  1195. if err != nil {
  1196. err = errors.New("判断指标是否存在失败,Err:" + err.Error())
  1197. return
  1198. }
  1199. if len(items) > 0 {
  1200. sql := ` UPDATE edb_info SET classify_id=?,modify_time=NOW() WHERE edb_code=? `
  1201. o := orm.NewOrm()
  1202. _, err = o.Raw(sql, req.ClassifyId, req.EdbCode).Exec()
  1203. if err != nil {
  1204. err = errors.New("更新指标分类信息失败,Err:" + err.Error())
  1205. return
  1206. }
  1207. edbInfo = items[0]
  1208. return
  1209. }
  1210. edbInfo = new(EdbInfo)
  1211. edbInfo.Source = source
  1212. sourceNameMap := map[int]string{
  1213. utils.DATA_SOURCE_THS: "同花顺",
  1214. utils.DATA_SOURCE_WIND: "wind",
  1215. utils.DATA_SOURCE_PB: "彭博",
  1216. utils.DATA_SOURCE_PB_FINANCE: "彭博财务",
  1217. utils.DATA_SOURCE_MANUAL: "手工数据",
  1218. utils.DATA_SOURCE_LZ: "隆众",
  1219. utils.DATA_SOURCE_YS: "SMM",
  1220. utils.DATA_SOURCE_GL: "钢联",
  1221. utils.DATA_SOURCE_ZZ: "郑商所",
  1222. utils.DATA_SOURCE_DL: "大商所",
  1223. utils.DATA_SOURCE_SH: "上期所",
  1224. utils.DATA_SOURCE_CFFEX: "中金所",
  1225. utils.DATA_SOURCE_SHFE: "上期能源",
  1226. utils.DATA_SOURCE_GIE: "欧洲天然气",
  1227. utils.DATA_SOURCE_LT: "路透",
  1228. utils.DATA_SOURCE_COAL: "中国煤炭市场网",
  1229. utils.DATA_SOURCE_GOOGLE_TRAVEL: "our world in data",
  1230. utils.DATA_SOURCE_MYSTEEL_CHEMICAL: "钢联化工",
  1231. utils.DATA_SOURCE_EIA_STEO: "EIA STEO报告",
  1232. utils.DATA_SOURCE_COM_TRADE: "UN",
  1233. utils.DATA_SOURCE_SCI: "SCI",
  1234. utils.DATA_SOURCE_BAIINFO: "BAIINFO",
  1235. utils.DATA_SOURCE_STOCK_PLANT: "存量装置",
  1236. utils.DATA_SOURCE_NATIONAL_STATISTICS: "国家统计局",
  1237. utils.DATA_SOURCE_FUBAO: "富宝数据",
  1238. }
  1239. sourceName, ok := sourceNameMap[source]
  1240. if !ok {
  1241. edbSource := EdbSourceIdMap[source]
  1242. if edbSource != nil {
  1243. sourceName = edbSource.SourceName
  1244. }
  1245. if sourceName == "" {
  1246. err = errors.New("指标来源异常")
  1247. return
  1248. }
  1249. }
  1250. edbInfo.SourceName = sourceName
  1251. edbType := 1 //基础指标
  1252. if source == utils.DATA_SOURCE_STOCK_PLANT {
  1253. edbType = 2 //计算指标
  1254. }
  1255. var sourceIndexName string
  1256. // 钢联化工需要校验下信息是否完整
  1257. if source == utils.DATA_SOURCE_MYSTEEL_CHEMICAL {
  1258. indexObj := new(BaseFromMysteelChemicalIndex)
  1259. tmpItem, tmpErr := indexObj.GetIndexItem(req.EdbCode)
  1260. if tmpErr != nil {
  1261. err = tmpErr
  1262. return
  1263. }
  1264. if tmpItem == nil || tmpItem.IndexName == `` {
  1265. err = errors.New("指标信息不全")
  1266. return
  1267. }
  1268. // 兼容数据
  1269. {
  1270. if req.Frequency == `` {
  1271. req.Frequency = tmpItem.Frequency
  1272. }
  1273. if req.Unit == `` {
  1274. req.Unit = tmpItem.Unit
  1275. }
  1276. }
  1277. }
  1278. edbInfo.EdbCode = req.EdbCode
  1279. edbInfo.EdbName = req.EdbName
  1280. edbInfo.EdbNameSource = req.EdbName
  1281. edbInfo.EdbNameEn = req.EdbName
  1282. edbInfo.Frequency = req.Frequency
  1283. edbInfo.Unit = req.Unit
  1284. edbInfo.UnitEn = req.Unit
  1285. edbInfo.ClassifyId = req.ClassifyId
  1286. edbInfo.SysUserId = sysUserId
  1287. edbInfo.SysUserRealName = sysUserRealName
  1288. edbInfo.StartDate = "1990-01-01"
  1289. edbInfo.CreateTime = time.Now()
  1290. edbInfo.ModifyTime = time.Now()
  1291. edbInfo.ServerUrl = serverUrl
  1292. edbInfo.TerminalCode = req.TerminalCode
  1293. edbInfo.Sort = GetAddEdbMaxSortByClassifyId(req.ClassifyId, 0)
  1294. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1295. edbInfo.UniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + req.EdbCode + timestamp)
  1296. itemVal, err := GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
  1297. if itemVal != nil && err == nil {
  1298. edbInfo.MaxValue = itemVal.MaxValue
  1299. edbInfo.MinValue = itemVal.MinValue
  1300. }
  1301. edbInfo.EdbType = edbType
  1302. edbInfo.SourceIndexName = sourceIndexName
  1303. edbInfoId, err := AddEdbInfo(edbInfo)
  1304. if err != nil {
  1305. err = errors.New("保存失败,Err:" + err.Error())
  1306. return
  1307. }
  1308. edbInfo.EdbInfoId = int(edbInfoId)
  1309. return
  1310. }
  1311. func ModifyEdbInfoBaseTimeById(edbInfoId int, cTime time.Time) (err error) {
  1312. o := orm.NewOrm()
  1313. // 更新修改时间
  1314. sql := ` UPDATE edb_info SET base_modify_time = ? WHERE edb_info_id = ? `
  1315. _, err = o.Raw(sql, cTime, edbInfoId).Exec()
  1316. return
  1317. }
  1318. // EdbInfoRefreshCheckReq 指标数据更新情况查询
  1319. type EdbInfoRefreshCheckReq struct {
  1320. Source int `description:"来源id"`
  1321. LatestDate string `description:"数据最新日期"`
  1322. FrequencyBatch string `description:"更新频度"`
  1323. }
  1324. // EdbInfoRefreshCheckResp 指标数据更新情况查询
  1325. type EdbInfoRefreshCheckResp struct {
  1326. UpdateNum int `description:"已更新指标数"`
  1327. UnUpdateNum int `description:"未更新指标数"`
  1328. }
  1329. // ResetEdbClassifyReq 指标重新分类请求体
  1330. type ResetEdbClassifyReq struct {
  1331. IndexCode string `description:"指标编码"`
  1332. ClassifyFirst string `description:"一级分类名称"`
  1333. ClassifySecond string `description:"二级分类名称"`
  1334. ClassifyThird string `description:"三级分类名称"`
  1335. }
  1336. // GetEdbInfoMaxSortByClassifyId 获取分类下指标的最大的排序数
  1337. func GetEdbInfoMaxSortByClassifyId(classifyId int) (sort int, err error) {
  1338. o := orm.NewOrm()
  1339. sql := `SELECT Max(sort) AS sort FROM edb_info WHERE classify_id=? `
  1340. err = o.Raw(sql, classifyId).QueryRow(&sort)
  1341. return
  1342. }
  1343. func GetEdbAndClassifyMaxSort(parentId int, classifyType uint8) (maxSort int, err error) {
  1344. //获取该层级下最大的排序数
  1345. classifyMaxSort, err := GetEdbClassifyMaxSort(parentId, classifyType)
  1346. if err != nil {
  1347. return
  1348. }
  1349. maxSort = classifyMaxSort
  1350. edbMaxSort, err := GetEdbInfoMaxSortByClassifyId(parentId)
  1351. if err != nil {
  1352. return
  1353. }
  1354. if maxSort < edbMaxSort {
  1355. maxSort = edbMaxSort
  1356. }
  1357. return
  1358. }
  1359. // CalculateEdbExtra 指标运算额外配置
  1360. type CalculateEdbExtra struct {
  1361. DateTag string `description:"时间序列的生成方式,all 表示所选指标的时间序列并集"`
  1362. }
  1363. // CalculateLjzEdbExtra 累计值额外配置
  1364. type CalculateLjzEdbExtra struct {
  1365. LastValType int `description:"最新值处理:0默认、均值填充"`
  1366. }
  1367. type EdbInfoEditRecord struct {
  1368. EdbInfoId int `description:"指标ID"`
  1369. EdbName string `description:"指标名称"`
  1370. Frequency string `description:"频率"`
  1371. Unit string `description:"单位"`
  1372. ClassifyId int `description:"分类id"`
  1373. CalculateFormula string `description:"计算公式"`
  1374. OperateUserId int `description:"操作人id"`
  1375. OperateUserRealName string `description:"操作人姓名"`
  1376. }
  1377. // GetEdbInfoByEdbCodeList
  1378. // @Description: 根据来源和指标编码列表获取指标信息列表
  1379. // @author: Roc
  1380. // @datetime 2024-05-31 16:31:52
  1381. // @param source int
  1382. // @param edbCodeList []string
  1383. // @return items []*EdbInfo
  1384. // @return err error
  1385. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  1386. num := len(edbCodeList)
  1387. if num <= 0 {
  1388. return
  1389. }
  1390. o := orm.NewOrm()
  1391. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code IN (` + utils.GetOrmInReplace(num) + `) `
  1392. _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
  1393. return
  1394. }
  1395. // GetAddEdbMaxSortByClassifyId
  1396. // @Description: 获取添加指标时,该分类下最大的排序(忽略错误信息)
  1397. // @author: Roc
  1398. // @datetime 2024-07-05 09:39:46
  1399. // @param classifyId int
  1400. // @param classifyType uint8
  1401. // @return sort int
  1402. func GetAddEdbMaxSortByClassifyId(classifyId int, classifyType uint8) (sort int) {
  1403. sort, _ = GetEdbAndClassifyMaxSort(classifyId, classifyType)
  1404. sort = sort + 1
  1405. return
  1406. }
  1407. // EdbInfoExtra 指标额外数据-extra字段
  1408. type EdbInfoExtra struct {
  1409. ApiExtraPars string `description:"API-额外参数(如同花顺日期序列)"`
  1410. }
  1411. // GetEdbInfoNoUpdateTotalByIdList 根据指标id列表获取指标信息
  1412. func GetEdbInfoNoUpdateTotalByIdList(edbInfoIdList []int) (total int, err error) {
  1413. num := len(edbInfoIdList)
  1414. if num <= 0 {
  1415. return
  1416. }
  1417. o := orm.NewOrm()
  1418. sql := ` SELECT count(*) FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) and no_update=1`
  1419. err = o.Raw(sql, edbInfoIdList).QueryRow(&total)
  1420. return
  1421. }
  1422. func TransEdbInfoSearchData2DataList(items []*EdbInfoSearchData) (list []*EdbDataList) {
  1423. if len(items) == 0 {
  1424. return
  1425. }
  1426. list = make([]*EdbDataList, 0)
  1427. for _, v := range items {
  1428. list = append(list, &EdbDataList{
  1429. EdbDataId: v.EdbDataId,
  1430. EdbInfoId: v.EdbInfoId,
  1431. DataTime: v.DataTime,
  1432. DataTimestamp: v.DataTimestamp,
  1433. Value: v.Value,
  1434. })
  1435. }
  1436. return
  1437. }
  1438. func TransEdbInfoDataList2SearchData(items []*EdbDataList) (list []*EdbInfoSearchData) {
  1439. if len(items) == 0 {
  1440. return
  1441. }
  1442. list = make([]*EdbInfoSearchData, 0)
  1443. for _, v := range items {
  1444. list = append(list, &EdbInfoSearchData{
  1445. EdbDataId: v.EdbDataId,
  1446. EdbInfoId: v.EdbInfoId,
  1447. DataTime: v.DataTime,
  1448. DataTimestamp: v.DataTimestamp,
  1449. Value: v.Value,
  1450. })
  1451. }
  1452. return
  1453. }
  1454. type SortEdbDataList []*EdbInfoSearchData
  1455. func (m SortEdbDataList) Len() int {
  1456. return len(m)
  1457. }
  1458. func (m SortEdbDataList) Less(i, j int) bool {
  1459. return m[i].DataTime > m[j].DataTime
  1460. }
  1461. func (m SortEdbDataList) Swap(i, j int) {
  1462. m[i], m[j] = m[j], m[i]
  1463. }