edb_info.go 56 KB

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