edb_info.go 59 KB

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