edb_info.go 59 KB

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