edb_info.go 60 KB

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