edb_info.go 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models/data_manage/line_equation/request"
  5. "eta/eta_api/models/mgo"
  6. "eta/eta_api/services/alarm_msg"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/beego/beego/v2/client/orm"
  13. "github.com/rdlucklib/rdluck_tools/paging"
  14. "go.mongodb.org/mongo-driver/bson"
  15. )
  16. type EdbInfo struct {
  17. EdbInfoId int `orm:"column(edb_info_id);pk"`
  18. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  19. SourceName string `description:"来源名称"`
  20. Source int `description:"来源id"`
  21. EdbCode string `description:"指标编码"`
  22. EdbName string `description:"指标名称"`
  23. EdbNameEn string `description:"英文指标名称"`
  24. EdbNameSource string `description:"指标名称来源"`
  25. Frequency string `description:"频率"`
  26. Unit string `description:"单位"`
  27. UnitEn string `description:"英文单位"`
  28. StartDate string `description:"起始日期"`
  29. EndDate string `description:"终止日期"`
  30. ClassifyId int `description:"分类id"`
  31. SysUserId int
  32. SysUserRealName string
  33. UniqueCode string `description:"指标唯一编码"`
  34. CreateTime time.Time
  35. ModifyTime time.Time
  36. MinValue float64 `description:"指标最小值"`
  37. MaxValue float64 `description:"指标最大值"`
  38. CalculateFormula string `description:"计算公式"`
  39. EdbType 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. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  51. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  52. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  53. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  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. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  64. }
  65. type EdbInfoFullClassify struct {
  66. *EdbInfo
  67. CorrelationStr string `description:"相关性系数字符串"`
  68. ClassifyList []*EdbClassifyIdItems
  69. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  70. }
  71. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. lastId, err = o.Insert(item)
  74. return
  75. }
  76. type BaseEdbNameItem struct {
  77. EdbInfoId int `description:"指标id"`
  78. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  79. SourceName string `description:"来源名称"`
  80. Source int `description:"来源id"`
  81. EdbCode string `description:"指标编码"`
  82. EdbName string `description:"指标名称"`
  83. EdbNameEn string `description:"指标英文名称"`
  84. Frequency string `description:"频率"`
  85. Unit string `description:"单位"`
  86. UnitEn string `description:"英文单位"`
  87. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  88. }
  89. type BaseEdbInfoResp struct {
  90. List []*BaseEdbNameItem
  91. }
  92. // GetEdbInfoAll 用于分类展示
  93. func GetEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  94. o := orm.NewOrmUsingDB("data")
  95. sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
  96. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  97. return
  98. }
  99. // GetPredictEdbInfoAll 用于分类展示(预测指标)
  100. func GetPredictEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT edb_info_id,classify_id,edb_name AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
  103. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  104. return
  105. }
  106. // 用于分类展示
  107. func GetEdbInfoAllList() (items []*EdbInfo, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := ` SELECT * FROM edb_info `
  110. _, err = o.Raw(sql).QueryRows(&items)
  111. return
  112. }
  113. // 指标检索数据
  114. type EdbInfoSearch struct {
  115. EdbCode string `description:"指标编码"`
  116. StartDate string `description:"起始日期"`
  117. EndDate string `description:"终止日期"`
  118. EdbName string `description:"指标名称"`
  119. Unit string `description:"单位"`
  120. Frequency string `description:"频率"`
  121. DataList []*EdbInfoSearchData
  122. StockList []*StockInfo `description:"时序数据"`
  123. }
  124. type StockInfo struct {
  125. StockCode string
  126. EdbCode string
  127. DataList []*EdbInfoSearchData
  128. }
  129. type EdbInfoSearchData struct {
  130. DataTime string `description:"数据日期"`
  131. Value float64 `description:"数据"`
  132. }
  133. type BaseIndexInfo struct {
  134. IndexName string `description:"数据日期"`
  135. Unit string `description:"单位"`
  136. Frequency string `description:"频率"`
  137. }
  138. type EdbInfoSearchResp struct {
  139. SearchItem *EdbInfoSearch `description:"指标分类"`
  140. Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
  141. ClassifyList []*EdbClassifySimplify
  142. StockSearchList []*EdbInfoSearch
  143. }
  144. type EdbInfoMySteelChemicalCheckResp struct {
  145. Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
  146. }
  147. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  148. o := orm.NewOrmUsingDB("data")
  149. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  150. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  151. return
  152. }
  153. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  154. o := orm.NewOrmUsingDB("data")
  155. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  156. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  157. return
  158. }
  159. // GetEdbInfoByUniqueCode
  160. // @Description: 根据uniqueCode获取指标详情
  161. // @author: Roc
  162. // @datetime 2024-01-18 13:40:01
  163. // @param uniqueCode string
  164. // @return item *EdbInfo
  165. // @return err error
  166. func GetEdbInfoByUniqueCode(uniqueCode string) (item *EdbInfo, err error) {
  167. o := orm.NewOrmUsingDB("data")
  168. sql := ` SELECT * FROM edb_info WHERE unique_code=? `
  169. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  170. return
  171. }
  172. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  173. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  174. num := len(edbInfoIdList)
  175. if num <= 0 {
  176. return
  177. }
  178. o := orm.NewOrmUsingDB("data")
  179. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  180. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  181. return
  182. }
  183. // type MysteelChemicalDataListReq struct {
  184. // Frequency string `form:"Frequency" description:"频度"`
  185. // Source int `form:"Source" description:"来源id"`
  186. // Keywords string `form:"Keywords" description:"指标ID/指标名称"`
  187. // SortField int `form:"SortField" description:"排序字段: 0-默认; 1-开始时间; 2-最新时间; 3-更新时间"`
  188. // SortRule int `form:"SortRule" description:"排序方式: 0-默认; 1-正序; 2-倒序"`
  189. // }
  190. // MysteelChemicalDataBatchAddCheckReq 钢联化工指标批量添加校验
  191. type MysteelChemicalDataBatchAddCheckReq struct {
  192. // MysteelChemicalDataListReq
  193. IndexCodes []string `form:"IndexCodes" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  194. }
  195. type AddEdbInfoReq struct {
  196. Source int `description:"来源id"`
  197. EdbCode string `description:"指标编码"`
  198. EdbName string `description:"指标名称"`
  199. Frequency string `description:"频率"`
  200. Unit string `description:"单位"`
  201. ClassifyId int `description:"分类id"`
  202. StartDate string `description:"起始日期"`
  203. EndDate string `description:"终止日期"`
  204. }
  205. type NameCheckEdbInfoReq struct {
  206. EdbCode string `description:"指标编码"`
  207. EdbName string `description:"指标名称"`
  208. Frequency string `description:"频率"`
  209. }
  210. func DeleteEdbInfoAndData(edbInfoId, source, subSource int) (err error) {
  211. o := orm.NewOrmUsingDB("data")
  212. to, err := o.Begin()
  213. if err != nil {
  214. return
  215. }
  216. defer func() {
  217. if err != nil {
  218. _ = to.Rollback()
  219. } else {
  220. _ = to.Commit()
  221. }
  222. }()
  223. // 删除指标信息
  224. sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
  225. _, err = to.Raw(sql, edbInfoId).Exec()
  226. if err != nil {
  227. return
  228. }
  229. // 删除指标的明细数据
  230. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  231. err = deleteAllEdbDataByMongo(to, edbInfoId, source, subSource)
  232. } else if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  233. mogDataObj := mgo.EdbDataThsHf{}
  234. err = mogDataObj.RemoveMany(bson.M{"edb_info_id": edbInfoId})
  235. } else {
  236. err = deleteAllEdbDataByMysql(to, edbInfoId, source, subSource)
  237. }
  238. if err != nil {
  239. return
  240. }
  241. //calculateTableName := GetEdbInfoCalculateTableName(source)
  242. //if calculateTableName != "" {
  243. // sql = ` DELETE FROM %s WHERE edb_info_id=? `
  244. // sql = fmt.Sprintf(sql, calculateTableName)
  245. // _, err = to.Raw(sql, edbInfoId).Exec()
  246. // return
  247. //}
  248. //return
  249. // 删除计算指标的关系
  250. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  251. _, err = to.Raw(sql, edbInfoId).Exec()
  252. if err != nil {
  253. return
  254. }
  255. // 删除预测指标的配置
  256. sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id=? `
  257. _, err = to.Raw(sql, edbInfoId).Exec()
  258. if err != nil {
  259. return
  260. }
  261. // 删除跨品种分析标签绑定的指标
  262. sql = ` DELETE FROM chart_tag_variety WHERE edb_info_id=?`
  263. _, err = to.Raw(sql, edbInfoId).Exec()
  264. if err != nil {
  265. return
  266. }
  267. return
  268. }
  269. // deleteAllEdbDataByMysql
  270. // @Description: 删除指标的明细数据(mysql)
  271. // @author: Roc
  272. // @datetime 2024-05-09 13:31:54
  273. // @param to orm.TxOrmer
  274. // @param edbInfoId int
  275. // @param source int
  276. // @param subSource int
  277. // @return err error
  278. func deleteAllEdbDataByMysql(to orm.TxOrmer, edbInfoId, source, subSource int) (err error) {
  279. tableName := GetEdbDataTableName(source, subSource)
  280. if tableName == `` {
  281. return
  282. }
  283. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id=? `, tableName)
  284. _, err = to.Raw(sql, edbInfoId).Exec()
  285. return
  286. }
  287. // deleteAllEdbDataByMongo
  288. // @Description: 删除指标的明细数据(mongo)
  289. // @author: Roc
  290. // @datetime 2024-05-09 13:31:37
  291. // @param to orm.TxOrmer
  292. // @param edbInfoId int
  293. // @param source int
  294. // @param subSource int
  295. // @return err error
  296. func deleteAllEdbDataByMongo(to orm.TxOrmer, edbInfoId, source, subSource int) (err error) {
  297. mogDataObj := mgo.EdbDataBusiness{}
  298. err = mogDataObj.RemoveMany(bson.M{"edb_info_id": edbInfoId})
  299. return
  300. }
  301. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  302. o := orm.NewOrmUsingDB("data")
  303. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  304. if condition != "" {
  305. sql += condition
  306. }
  307. err = o.Raw(sql, pars).QueryRow(&count)
  308. return
  309. }
  310. type EditEdbInfoReq struct {
  311. EdbInfoId int `description:"指标ID"`
  312. EdbName string `description:"指标名称"`
  313. Frequency string `description:"频率"`
  314. Unit string `description:"单位"`
  315. ClassifyId int `description:"分类id"`
  316. CalculateFormula string `description:"计算公式"`
  317. }
  318. type EditEdbEnInfoReq struct {
  319. EdbInfoId int `description:"指标ID"`
  320. EdbNameEn string `description:"英文指标名称"`
  321. UnitEn string `description:"英文单位"`
  322. }
  323. // EditEdbBaseInfoReq
  324. // @Description: 修改指标基础信息请求参数
  325. type EditEdbBaseInfoReq struct {
  326. EdbInfoId int `description:"指标ID"`
  327. EdbName string `description:"指标名称"`
  328. Unit string `description:"指标单位"`
  329. }
  330. func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
  331. o := orm.NewOrmUsingDB("data")
  332. sql := ` UPDATE edb_info
  333. SET
  334. edb_name =?,
  335. edb_name_source =?,
  336. frequency = ?,
  337. unit = ?,
  338. classify_id = ?,
  339. modify_time = NOW()
  340. WHERE edb_info_id = ?`
  341. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.EdbInfoId).Exec()
  342. return
  343. }
  344. // ModifyEdbEnInfo 修改指标英文信息
  345. func ModifyEdbEnInfo(item *EditEdbEnInfoReq) (err error) {
  346. o := orm.NewOrmUsingDB("data")
  347. sql := ` UPDATE edb_info
  348. SET
  349. edb_name_en =?,
  350. unit_en = ?,
  351. modify_time = NOW()
  352. WHERE edb_info_id = ?`
  353. _, err = o.Raw(sql, item.EdbNameEn, item.UnitEn, item.EdbInfoId).Exec()
  354. return
  355. }
  356. type EdbInfoList struct {
  357. EdbInfoId int `orm:"column(edb_info_id);pk"`
  358. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  359. SourceName string `description:"来源名称"`
  360. Source int `description:"来源id"`
  361. EdbCode string `description:"指标编码"`
  362. EdbNameEn string `description:"英文指标名称"`
  363. EdbName string `description:"指标名称"`
  364. Frequency string `description:"频率"`
  365. FrequencyEn string `description:"英文频率"`
  366. Unit string `description:"单位"`
  367. UnitEn string `description:"英文单位"`
  368. StartDate string `description:"起始日期"`
  369. EndDate string `description:"终止日期"`
  370. LatestDate string `description:"数据最新日期(实际日期)"`
  371. LatestValue float64 `description:"数据最新值(实际值)"`
  372. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  373. ClassifyId int `description:"分类id"`
  374. UniqueCode string `description:"指标唯一编码"`
  375. SysUserId int `description:"创建人id"`
  376. SysUserRealName string `description:"创建人姓名"`
  377. ModifyTime string `description:"最新修改时间"`
  378. CreateTime string `description:"创建时间"`
  379. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  380. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  381. ChartImage string `description:"图表图片"`
  382. RuleType int `description:"预测规则,1:最新,2:固定值"`
  383. FixedValue float64 `description:"固定值"`
  384. DataList []*EdbData `description:"实际指标数据"`
  385. PredictDataList []*EdbData `description:"预测指标数据"`
  386. Button EdbClassifyItemsButton `description:"操作权限"`
  387. IsEnEdb bool `description:"是否展示英文标识"`
  388. DataInsertConfig EdbDataInsertConfigItem `description:"指标数据插入配置"`
  389. DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
  390. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  391. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  392. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  393. SubSourceName string `description:"子数据来源名称"`
  394. IndicatorCode string `description:"指标代码"`
  395. StockCode string `description:"证券代码"`
  396. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  397. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  398. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  399. }
  400. type EdbDataInsertConfigItem struct {
  401. Date string `description:"插入的日期"`
  402. RealDate string `description:"实际最晚的日期"`
  403. Value string `description:"插入的值"`
  404. }
  405. type EdbData struct {
  406. EdbDataId int `orm:"column(edb_data_id);pk"`
  407. EdbInfoId int
  408. DataTime string
  409. Value float64
  410. }
  411. type EdbInfoListResp struct {
  412. Paging *paging.PagingItem
  413. Item *EdbInfoList
  414. ClassifyList []*EdbClassifyIdItems
  415. }
  416. type WindEdbInfoList struct {
  417. *EdbInfoList
  418. ClassifyList []*EdbClassifyIdItems
  419. }
  420. func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  421. o := orm.NewOrmUsingDB("data")
  422. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  423. if condition != "" {
  424. sql += condition
  425. }
  426. err = o.Raw(sql, pars).QueryRow(&item)
  427. return
  428. }
  429. func GetEdbDataCountByCondition(condition string, pars []interface{}, source, subSource int) (count int, err error) {
  430. o := orm.NewOrmUsingDB("data")
  431. tableName := GetEdbDataTableName(source, subSource)
  432. sql := ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  433. sql = fmt.Sprintf(sql, tableName)
  434. if condition != "" {
  435. sql += condition
  436. }
  437. err = o.Raw(sql, pars).QueryRow(&count)
  438. return
  439. }
  440. func GetEdbDataListByCondition(condition string, pars []interface{}, source, subSource, pageSize, startSize int) (item []*EdbData, err error) {
  441. o := orm.NewOrmUsingDB("data")
  442. tableName := GetEdbDataTableName(source, subSource)
  443. sql := ` SELECT * FROM %s WHERE 1=1 `
  444. sql = fmt.Sprintf(sql, tableName)
  445. if condition != "" {
  446. sql += condition
  447. }
  448. sql += ` ORDER BY data_time DESC `
  449. sql += ` LIMIT ?,? `
  450. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  451. return
  452. }
  453. // GetAllEdbDataListByCondition 根据条件获取所有的数据
  454. func GetAllEdbDataListByCondition(condition string, pars []interface{}, source, subSource int) (item []*EdbData, err error) {
  455. o := orm.NewOrmUsingDB("data")
  456. tableName := GetEdbDataTableName(source, subSource)
  457. sql := ` SELECT * FROM %s WHERE 1=1 `
  458. sql = fmt.Sprintf(sql, tableName)
  459. if condition != "" {
  460. sql += condition
  461. }
  462. sql += ` ORDER BY data_time DESC `
  463. _, err = o.Raw(sql, pars).QueryRows(&item)
  464. return
  465. }
  466. func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
  467. o := orm.NewOrmUsingDB("data")
  468. sql := ` SELECT * FROM edb_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
  469. err = o.Raw(sql).QueryRow(&item)
  470. return
  471. }
  472. func ModifyEdbInfoModifyTime(edbInfoId int64) (err error) {
  473. o := orm.NewOrmUsingDB("data")
  474. sql := ` UPDATE edb_info SET modify_time = NOW() WHERE edb_info_id = ? `
  475. _, err = o.Raw(sql, edbInfoId).Exec()
  476. return
  477. }
  478. type MoveEdbInfoReq struct {
  479. EdbInfoId int `description:"指标ID"`
  480. PrevEdbInfoId int `description:"上一个指标ID"`
  481. NextEdbInfoId int `description:"下一个指标ID"`
  482. ClassifyId int `description:"分类id"`
  483. }
  484. func MoveEdbInfo(edbInfoId, classifyId int) (err error) {
  485. o := orm.NewOrmUsingDB("data")
  486. sql := ` UPDATE edb_info
  487. SET
  488. classify_id = ?
  489. WHERE edb_info_id = ?`
  490. _, err = o.Raw(sql, classifyId, edbInfoId).Exec()
  491. return
  492. }
  493. // GetEdbInfoByName 根据名称获取普通的指标
  494. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  495. o := orm.NewOrmUsingDB("data")
  496. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 0 `
  497. _, err = o.Raw(sql, edbName).QueryRows(&items)
  498. return
  499. }
  500. // GetPredictEdbInfoByName 根据指标名称获取预测指标
  501. func GetPredictEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  502. o := orm.NewOrmUsingDB("data")
  503. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 1 `
  504. _, err = o.Raw(sql, edbName).QueryRows(&items)
  505. return
  506. }
  507. func GetEdbInfoByEnName(edbNameEn string) (items []*EdbInfoList, err error) {
  508. o := orm.NewOrmUsingDB("data")
  509. sql := ` SELECT * FROM edb_info WHERE edb_name_en=? AND edb_info_type = 0 `
  510. _, err = o.Raw(sql, edbNameEn).QueryRows(&items)
  511. return
  512. }
  513. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  514. o := orm.NewOrmUsingDB("data")
  515. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  516. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  517. return
  518. }
  519. func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
  520. o := orm.NewOrmUsingDB("data")
  521. sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
  522. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  523. return
  524. }
  525. type ChartEdbInfo struct {
  526. EdbInfoId int `description:"指标id"`
  527. EdbName string `description:"指标名称"`
  528. SourceName string `json:"-"`
  529. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  530. }
  531. func EdbInfoSearchByKeyWord(keyword string) (searchList []*ChartEdbInfo, err error) {
  532. pars := make([]interface{}, 0)
  533. o := orm.NewOrmUsingDB("data")
  534. sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  535. if keyword != "" {
  536. sql += ` AND (edb_name LIKE ? OR edb_code LIKE ? ) `
  537. pars = append(pars, utils.GetLikeKeyword(keyword), utils.GetLikeKeyword(keyword))
  538. }
  539. sql += ` ORDER BY create_time DESC `
  540. _, err = o.Raw(sql, pars...).QueryRows(&searchList)
  541. return
  542. }
  543. type EdbInfoMaxAndMinInfo struct {
  544. MinDate string `description:"最小日期"`
  545. MaxDate string `description:"最大日期"`
  546. MinValue float64 `description:"最小值"`
  547. MaxValue float64 `description:"最大值"`
  548. LatestValue float64 `description:"最新值"`
  549. }
  550. // GetEdbInfoMaxAndMinInfo
  551. // @Description: 获取指标最大最小值
  552. // @author: Roc
  553. // @datetime 2024-05-07 15:34:00
  554. // @param source int
  555. // @param subSource int
  556. // @param edbCode string
  557. // @return item *EdbInfoMaxAndMinInfo
  558. // @return err error
  559. func GetEdbInfoMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  560. // 自有数据需要额外处理(从mongo获取)
  561. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  562. return GetEdbInfoMaxAndMinInfoByMongo(source, subSource, edbCode)
  563. }
  564. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  565. return GetThsHfEdbInfoMaxAndMinInfoByMongo(source, subSource, edbCode)
  566. }
  567. // 默认走mysql
  568. return GetEdbInfoMaxAndMinInfoByMysql(source, subSource, edbCode)
  569. }
  570. // GetEdbInfoMaxAndMinInfoByMysql
  571. // @Description: 从mysql中获取指标最大最小值
  572. // @author: Roc
  573. // @datetime 2024-05-07 15:33:43
  574. // @param source int
  575. // @param subSource int
  576. // @param edbCode string
  577. // @return item *EdbInfoMaxAndMinInfo
  578. // @return err error
  579. func GetEdbInfoMaxAndMinInfoByMysql(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  580. o := orm.NewOrmUsingDB("data")
  581. sql := ``
  582. tableName := GetEdbDataTableName(source, subSource)
  583. 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=? `
  584. sql = fmt.Sprintf(sql, tableName)
  585. err = o.Raw(sql, edbCode).QueryRow(&item)
  586. if err != nil {
  587. return
  588. }
  589. var latest_value float64
  590. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  591. sql = fmt.Sprintf(sql, tableName)
  592. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  593. item.LatestValue = latest_value
  594. return
  595. }
  596. // GetEdbInfoMaxAndMinInfoByMongo
  597. // @Description: 从mongo中获取指标最大最小值
  598. // @author: Roc
  599. // @datetime 2024-05-07 15:33:53
  600. // @param source int
  601. // @param subSource int
  602. // @param edbCode string
  603. // @return item *EdbInfoMaxAndMinInfo
  604. // @return err error
  605. func GetEdbInfoMaxAndMinInfoByMongo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  606. mogDataObj := new(mgo.EdbDataBusiness)
  607. pipeline := []bson.M{
  608. {"$match": bson.M{"edb_code": edbCode}},
  609. {"$group": bson.M{
  610. "_id": nil,
  611. "min_date": bson.M{"$min": "$data_time"},
  612. "max_date": bson.M{"$max": "$data_time"},
  613. "min_value": bson.M{"$min": "$value"},
  614. "max_value": bson.M{"$max": "$value"},
  615. }},
  616. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  617. }
  618. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  619. if err != nil {
  620. fmt.Println("EdbDataBusiness getEdbDataBusinessList Err:" + err.Error())
  621. return
  622. }
  623. if !result.MaxDate.IsZero() {
  624. whereQuery := bson.M{"edb_code": edbCode, "data_time": result.MaxDate}
  625. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  626. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  627. if tmpErr != nil {
  628. err = tmpErr
  629. return
  630. }
  631. result.LatestValue = latestValue.Value
  632. result.EndValue = latestValue.Value
  633. }
  634. item = &EdbInfoMaxAndMinInfo{
  635. MinDate: result.MinDate.Format(utils.FormatDate),
  636. MaxDate: result.MaxDate.Format(utils.FormatDate),
  637. MinValue: result.MinValue,
  638. MaxValue: result.MaxValue,
  639. LatestValue: result.LatestValue,
  640. }
  641. return
  642. }
  643. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  644. o := orm.NewOrmUsingDB("data")
  645. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? `
  646. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  647. return
  648. }
  649. func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  650. o := orm.NewOrmUsingDB("data")
  651. sql := ` SELECT * FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  652. if condition != "" {
  653. sql += condition
  654. }
  655. sql += ` ORDER BY create_time DESC `
  656. _, err = o.Raw(sql, pars).QueryRows(&list)
  657. return
  658. }
  659. type AddEdbInfoResp struct {
  660. EdbInfoId int `description:"指标ID"`
  661. UniqueCode string `description:"指标唯一编码"`
  662. ClassifyId int `description:"分类id"`
  663. ExistEdbName []string `description:"重复指标名称"`
  664. }
  665. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  666. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  667. o := orm.NewOrmUsingDB("data")
  668. //calculateTableName := GetEdbInfoCalculateTableName(source)
  669. //if calculateTableName == "" {
  670. // err = errors.New("无效的表名")
  671. // return
  672. //}
  673. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  674. //sql = fmt.Sprintf(sql, calculateTableName)
  675. if condition != "" {
  676. sql += condition
  677. }
  678. _, err = o.Raw(sql, pars).QueryRows(&items)
  679. return
  680. }
  681. // GetEdbInfoAllCalculateByEdbInfoIdList 根据指标id集合 获取基础指标对应的所有计算指标
  682. func GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList []int) (list []*EdbInfo, err error) {
  683. num := len(edbInfoIdList)
  684. if num <= 0 {
  685. return
  686. }
  687. o := orm.NewOrmUsingDB("data")
  688. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  689. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  690. WHERE a.from_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  691. GROUP BY a.edb_info_id
  692. ORDER BY a.edb_info_id ASC `
  693. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  694. return
  695. }
  696. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  697. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  698. if err != nil && err.Error() != utils.ErrNoRow() {
  699. return
  700. }
  701. for _, item := range calculateList {
  702. if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
  703. continue
  704. }
  705. if item.EdbType == 1 {
  706. baseEdbInfoArr = append(baseEdbInfoArr, item)
  707. } else {
  708. calculateInfoArr = append(calculateInfoArr, item)
  709. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  710. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  711. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  712. }
  713. }
  714. return
  715. }
  716. func ModifyEdbInfoDataStatus(edbInfoId int64, source, subSource int, edbCode string) (err error) {
  717. o := orm.NewOrmUsingDB("data")
  718. sql := ``
  719. tableName := GetEdbDataTableName(source, subSource)
  720. sql = ` UPDATE %s SET edb_info_id=?,modify_time=NOW() WHERE edb_code=? `
  721. sql = fmt.Sprintf(sql, tableName)
  722. _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
  723. return
  724. }
  725. // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  726. func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
  727. o := orm.NewOrmUsingDB("data")
  728. sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
  729. err = o.Raw(sql, classifyId).QueryRow(&item)
  730. return
  731. }
  732. // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
  733. func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  734. o := orm.NewOrmUsingDB("data")
  735. sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=?`
  736. if prevEdbInfoId > 0 {
  737. sql += ` AND ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  738. } else {
  739. sql += ` AND ( sort > ? )`
  740. }
  741. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  742. return
  743. }
  744. // Update 更新指标基础信息
  745. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  746. o := orm.NewOrmUsingDB("data")
  747. _, err = o.Update(edbInfo, cols...)
  748. return
  749. }
  750. type EdbInfoDataResp struct {
  751. EdbInfo *EdbInfo
  752. DataList []*EdbDataList
  753. }
  754. type EdbInfoDataFullClassifyResp struct {
  755. EdbInfo *EdbInfoFullClassify
  756. DataList []*EdbDataList
  757. }
  758. type EdbInfoReplaceReq struct {
  759. OldEdbInfoId int `description:"原指标ID"`
  760. NewEdbInfoId int `description:"替换为指标ID"`
  761. }
  762. // EdbInfoReplaceV2 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  763. //func EdbInfoReplaceV2(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (mappingList,replaceChartTotal, replaceCalculateTotal int, err error) {
  764. // defer func() {
  765. // if err != nil {
  766. // fmt.Println("ERR:", err.Error())
  767. // }
  768. // }()
  769. //
  770. // // 替换关联表中的source_edb_info_id
  771. // mappingList, replaceChartTotal, replaceCalculateTotal, err := replaceChartEdb(oldEdbInfo, newEdbInfo)
  772. // if err != nil {
  773. // return
  774. // }
  775. // err = replaceRelationEdbInfo(mappingList, oldEdbInfo, newEdbInfo, sysAdminId, sysAdminRealName)
  776. // return
  777. //}
  778. // ReplaceChartEdb 替换图表中的指标
  779. func ReplaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (relationEdbInfoIdList []int, replaceChartTotal, replaceCalculateTotal int, err error) {
  780. var errmsg string
  781. relationEdbInfoIdList = make([]int, 0) // 关联的指标id
  782. logMsg := `` // 记录替换的日志
  783. o := orm.NewOrmUsingDB("data")
  784. to, err := o.Begin()
  785. if err != nil {
  786. return
  787. }
  788. defer func() {
  789. if err != nil {
  790. _ = to.Rollback()
  791. } else {
  792. _ = to.Commit()
  793. if logMsg != `` {
  794. utils.FileLog.Info(fmt.Sprintf("替换指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  795. }
  796. }
  797. if errmsg != "" {
  798. fmt.Println("errmsg:" + errmsg)
  799. }
  800. if err != nil && errmsg != "" {
  801. go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
  802. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  803. }
  804. }()
  805. //替换图表
  806. {
  807. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  808. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  809. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  810. if err != nil {
  811. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  812. return
  813. }
  814. replaceChartTotal = len(chartEdbMappingList)
  815. // 查询所有的相关的图表信息
  816. chartInfoIds := make([]string, 0)
  817. if len(chartEdbMappingList) > 0 {
  818. for _, chartEdbMapping := range chartEdbMappingList {
  819. chartInfoIds = append(chartInfoIds, strconv.Itoa(chartEdbMapping.ChartInfoId))
  820. }
  821. }
  822. if len(chartInfoIds) > 0 {
  823. chartInfoList, e := GetChartInfoListByChartIdList(chartInfoIds)
  824. if e != nil {
  825. err = e
  826. errmsg = "获取图表信息失败:Err:" + e.Error()
  827. return
  828. }
  829. for _, chartInfo := range chartInfoList {
  830. updateStr := make([]string, 0)
  831. if chartInfo.EdbInfoIds != "" && strings.Contains(chartInfo.EdbInfoIds, strconv.Itoa(oldEdbInfo.EdbInfoId)) {
  832. //需要更换
  833. //解析字符串
  834. edbInfoIds := strings.Split(chartInfo.EdbInfoIds, ",")
  835. for i, edbInfoId := range edbInfoIds {
  836. if edbInfoId == strconv.Itoa(oldEdbInfo.EdbInfoId) {
  837. edbInfoIds[i] = strconv.Itoa(newEdbInfo.EdbInfoId)
  838. }
  839. }
  840. chartInfo.EdbInfoIds = strings.Join(edbInfoIds, ",")
  841. updateStr = append(updateStr, "EdbInfoIds")
  842. }
  843. if chartInfo.ExtraConfig != "" || chartInfo.BarConfig != "" {
  844. //判断是否是拟合方程或者散点图截面图
  845. if chartInfo.Source == utils.CHART_SOURCE_FUTURE_GOOD {
  846. // 商品价格曲线图的一些配置
  847. var barConfig FutureGoodBarChartInfoReq
  848. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  849. if err != nil {
  850. errmsg = "商品价格曲线图配置异常 json.Unmarshal:Err:" + err.Error()
  851. return
  852. }
  853. if barConfig.BaseEdbInfoId == oldEdbInfo.EdbInfoId {
  854. barConfig.BaseEdbInfoId = newEdbInfo.EdbInfoId
  855. }
  856. for k, item := range barConfig.EdbInfoIdList {
  857. if item.EdbInfoId == oldEdbInfo.EdbInfoId && item.Source == 1 {
  858. barConfig.EdbInfoIdList[k].EdbInfoId = newEdbInfo.EdbInfoId
  859. }
  860. }
  861. // 更新图表配置信息
  862. // 重新序列化
  863. configJson, e := json.Marshal(barConfig)
  864. if e != nil {
  865. err = e
  866. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  867. return
  868. }
  869. configStr := string(configJson)
  870. chartInfo.BarConfig = configStr
  871. updateStr = append(updateStr, "BarConfig")
  872. } else if chartInfo.Source == utils.CHART_SOURCE_FUTURE_GOOD_PROFIT {
  873. // 商品利润曲线图的一些配置
  874. var extraConf FutureGoodProfitChartInfoReq
  875. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
  876. if err != nil {
  877. errmsg = "商品利润曲线图配置异常 json.Unmarshal:Err:" + err.Error()
  878. return
  879. }
  880. if extraConf.BaseEdbInfoId == oldEdbInfo.EdbInfoId {
  881. extraConf.BaseEdbInfoId = newEdbInfo.EdbInfoId
  882. }
  883. for k, v := range extraConf.EdbInfoIdList {
  884. if v == oldEdbInfo.EdbInfoId {
  885. extraConf.EdbInfoIdList[k] = newEdbInfo.EdbInfoId
  886. }
  887. }
  888. // 更新图表配置信息
  889. // 重新序列化
  890. configJson, e := json.Marshal(extraConf)
  891. if e != nil {
  892. err = e
  893. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  894. return
  895. }
  896. configStr := string(configJson)
  897. chartInfo.ExtraConfig = configStr
  898. updateStr = append(updateStr, "ExtraConfig")
  899. } else if chartInfo.Source == utils.CHART_SOURCE_LINE_EQUATION {
  900. //解析配置内容
  901. var lineChartInfoConfig request.LineChartInfoReq
  902. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &lineChartInfoConfig)
  903. if err != nil {
  904. errmsg = "获取图表配置信息失败 json.Unmarshal:Err:" + err.Error()
  905. return
  906. }
  907. xEdbInfoIdList := lineChartInfoConfig.XEdbInfoIdList
  908. yEdbInfoIdList := lineChartInfoConfig.YEdbInfoIdList
  909. if len(xEdbInfoIdList) > 0 {
  910. // 循环判断是否存在
  911. for k, xEdbInfoId := range xEdbInfoIdList {
  912. if xEdbInfoId == oldEdbInfo.EdbInfoId {
  913. xEdbInfoIdList[k] = newEdbInfo.EdbInfoId
  914. }
  915. }
  916. }
  917. if len(yEdbInfoIdList) > 0 {
  918. // 循环判断是否存在
  919. for k, yEdbInfoId := range yEdbInfoIdList {
  920. if yEdbInfoId == oldEdbInfo.EdbInfoId {
  921. yEdbInfoIdList[k] = newEdbInfo.EdbInfoId
  922. }
  923. }
  924. }
  925. // 替换新的指标信息
  926. lineChartInfoConfig.XEdbInfoIdList = xEdbInfoIdList
  927. lineChartInfoConfig.YEdbInfoIdList = yEdbInfoIdList
  928. // 重新序列化
  929. lineChartInfoConfigJson, e := json.Marshal(lineChartInfoConfig)
  930. if e != nil {
  931. err = e
  932. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  933. return
  934. }
  935. lineChartInfoConfigStr := string(lineChartInfoConfigJson)
  936. // 更新图表配置信息
  937. chartInfo.ExtraConfig = lineChartInfoConfigStr
  938. updateStr = append(updateStr, "ExtraConfig")
  939. } else if chartInfo.ChartType == utils.CHART_TYPE_SECTION_SCATTER {
  940. //解析配置内容
  941. var tmpExtraConfig SectionScatterReq
  942. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &tmpExtraConfig)
  943. if err != nil {
  944. errmsg = "获取截面散点图配置信息失败 json.Unmarshal:Err:" + err.Error()
  945. return
  946. }
  947. //找到指标信息,并替换
  948. if len(tmpExtraConfig.SeriesList) > 0 {
  949. for k1, series := range tmpExtraConfig.SeriesList {
  950. for k2, item := range series.EdbInfoList {
  951. if item.XEdbInfoId == oldEdbInfo.EdbInfoId {
  952. tmpExtraConfig.SeriesList[k1].EdbInfoList[k2].XEdbInfoId = newEdbInfo.EdbInfoId
  953. }
  954. if item.YEdbInfoId == oldEdbInfo.EdbInfoId {
  955. tmpExtraConfig.SeriesList[k1].EdbInfoList[k2].YEdbInfoId = newEdbInfo.EdbInfoId
  956. }
  957. }
  958. }
  959. }
  960. // 重新序列化
  961. sectionScatterConfigJson, e := json.Marshal(tmpExtraConfig)
  962. if e != nil {
  963. err = e
  964. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  965. return
  966. }
  967. sectionScatterConfigStr := string(sectionScatterConfigJson)
  968. // 更新图表配置信息
  969. chartInfo.ExtraConfig = sectionScatterConfigStr
  970. updateStr = append(updateStr, "ExtraConfig")
  971. }
  972. }
  973. if len(updateStr) > 0 {
  974. updateStr = append(updateStr, "ModifyTime")
  975. chartInfo.ModifyTime = time.Now()
  976. err = chartInfo.Update(updateStr)
  977. if err != nil {
  978. errmsg = "更新图表信息失败:chartInfo.Update Err: " + err.Error()
  979. return
  980. }
  981. }
  982. }
  983. }
  984. // 查询所有的图表信息
  985. if len(chartEdbMappingList) > 0 {
  986. chartInfoIdList := make([]string, 0)
  987. for _, mv := range chartEdbMappingList {
  988. //获取图表所有指标信息
  989. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  990. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  991. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  992. if err != nil {
  993. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  994. return
  995. }
  996. // 查询图表信息
  997. var minData, maxData float64
  998. minData = newEdbInfo.MinValue
  999. maxData = newEdbInfo.MaxValue
  1000. for _, cv := range chartEdbList {
  1001. if mv.IsAxis == cv.IsAxis {
  1002. if minData > cv.MinData {
  1003. minData = cv.MinData
  1004. }
  1005. if maxData < cv.MaxData {
  1006. maxData = cv.MaxData
  1007. }
  1008. }
  1009. }
  1010. //修改图表关联指标
  1011. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  1012. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  1013. if err != nil {
  1014. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  1015. return
  1016. }
  1017. chartInfoIdList = append(chartInfoIdList, fmt.Sprint(mv.ChartInfoId))
  1018. }
  1019. logMsg += `涉及到的图表id:` + strings.Join(chartInfoIdList, ",") + ";"
  1020. }
  1021. }
  1022. //替换计算指标
  1023. {
  1024. //获取所有包含的计算指标
  1025. mappingList := make([]*EdbInfoCalculateMapping, 0)
  1026. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  1027. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  1028. if err != nil {
  1029. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  1030. return
  1031. }
  1032. replaceCalculateTotal = len(mappingList)
  1033. //计算指标
  1034. if len(mappingList) > 0 {
  1035. edbInfoIdList := make([]string, 0)
  1036. for _, mv := range mappingList {
  1037. //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
  1038. if mv.EdbInfoId == newEdbInfo.EdbInfoId {
  1039. continue
  1040. }
  1041. //替换原指标
  1042. msql := `UPDATE edb_info_calculate_mapping SET from_edb_info_id=?,from_edb_code=?,from_edb_name=?,from_source=?,from_source_name=? WHERE edb_info_calculate_mapping_id=? `
  1043. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  1044. if err != nil {
  1045. return
  1046. }
  1047. relationEdbInfoIdList = append(relationEdbInfoIdList, mv.EdbInfoId)
  1048. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(mv.EdbInfoId))
  1049. }
  1050. logMsg += `涉及到的指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1051. }
  1052. }
  1053. // 替换预测指标中的来源指标
  1054. {
  1055. //获取所有包含的计算指标
  1056. predictEdbConfList := make([]*PredictEdbConf, 0)
  1057. msql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? GROUP BY predict_edb_info_id `
  1058. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&predictEdbConfList)
  1059. if err != nil {
  1060. errmsg = "获取关联的预测指标信息失败:Err:" + err.Error()
  1061. return
  1062. }
  1063. replaceCalculateTotal += len(predictEdbConfList)
  1064. //替换预测指标的来源指标
  1065. msql = `UPDATE predict_edb_conf SET source_edb_info_id=? WHERE source_edb_info_id=? `
  1066. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).Exec()
  1067. if err != nil {
  1068. return
  1069. }
  1070. if len(predictEdbConfList) > 0 {
  1071. edbInfoIdList := make([]string, 0)
  1072. for _, v := range predictEdbConfList {
  1073. relationEdbInfoIdList = append(relationEdbInfoIdList, v.PredictEdbInfoId)
  1074. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(v.PredictEdbInfoId))
  1075. }
  1076. logMsg += `涉及到的预测指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1077. }
  1078. }
  1079. return
  1080. }
  1081. type EdbInfoView struct {
  1082. EdbInfoId int `orm:"column(edb_info_id);pk"`
  1083. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  1084. SourceName string `description:"来源名称"`
  1085. Source int `description:"来源id"`
  1086. EdbCode string `description:"指标编码"`
  1087. EdbName string `description:"指标名称"`
  1088. EdbNameSource string `description:"指标名称来源"`
  1089. Frequency string `description:"频率"`
  1090. Unit string `description:"单位"`
  1091. StartDate string `description:"起始日期"`
  1092. EndDate string `description:"终止日期"`
  1093. ClassifyId int `description:"分类id"`
  1094. SysUserId int
  1095. SysUserRealName string
  1096. UniqueCode string `description:"指标唯一编码"`
  1097. CreateTime string
  1098. ModifyTime string
  1099. MinValue float64 `description:"指标最小值"`
  1100. MaxValue float64 `description:"指标最大值"`
  1101. CalculateFormula string `description:"计算公式"`
  1102. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  1103. Sort int `description:"排序字段"`
  1104. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  1105. LatestDate string `description:"数据最新日期(实际日期)"`
  1106. LatestValue float64 `description:"数据最新值(实际值)"`
  1107. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  1108. SubSourceName string `description:"子数据来源名称"`
  1109. IndicatorCode string `description:"指标代码"`
  1110. StockCode string `description:"证券代码"`
  1111. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  1112. }
  1113. // 获取指标的所有计算指标,以及计算指标所依赖计算指标
  1114. func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1115. o := orm.NewOrmUsingDB("data")
  1116. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  1117. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  1118. if err != nil {
  1119. return
  1120. }
  1121. return
  1122. }
  1123. // 获取指标的所有计算指标,以及计算指标所依赖计算指标
  1124. func GetAllCalculateByEdbInfoIds(edbInfoIds []int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1125. o := orm.NewOrmUsingDB("data")
  1126. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id in (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) GROUP BY edb_info_id `
  1127. _, err = o.Raw(msql, edbInfoIds).QueryRows(&mappingList)
  1128. if err != nil {
  1129. return
  1130. }
  1131. return
  1132. }
  1133. func GetAllCalculate(edbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1134. calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
  1135. if err != nil && err.Error() != utils.ErrNoRow() {
  1136. return
  1137. }
  1138. for _, item := range calculateList {
  1139. _, ok := edbInfoMap[item.EdbInfoId]
  1140. if !ok {
  1141. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  1142. mappingList = append(mappingList, item)
  1143. newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId, edbInfoMap)
  1144. mappingList = append(mappingList, newCalculateInfoArr...)
  1145. }
  1146. }
  1147. return
  1148. }
  1149. // GetAllCalculateByEdbInfoIdV2 获取计算指标的所有所依赖的指标(基础指标+计算指标)
  1150. func GetAllCalculateByEdbInfoIdV2(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1151. o := orm.NewOrmUsingDB("data")
  1152. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE edb_info_id=? GROUP BY from_edb_info_id `
  1153. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  1154. if err != nil {
  1155. return
  1156. }
  1157. return
  1158. }
  1159. // GetAllCalculateV2 获取所有的指标(如果下层指标包含了待替换指标,那么就不加入)
  1160. func GetAllCalculateV2(oldEdbInfoId, replaceEdbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1161. calculateList, err := GetAllCalculateByEdbInfoId(oldEdbInfoId)
  1162. if err != nil && err.Error() != utils.ErrNoRow() {
  1163. return
  1164. }
  1165. tmpMappingList := make([]*EdbInfoCalculateMapping, 0)
  1166. for _, item := range calculateList {
  1167. //如果计算指标里面存在即将替换的指标id,那么退出当前循环,进入下一循环
  1168. if item.EdbInfoId == replaceEdbInfoId {
  1169. err = fmt.Errorf("指标重复1")
  1170. return
  1171. }
  1172. //获取该指标的依赖指标列表,如果依赖指标列表中存在即将替换的指标,那么过滤该指标
  1173. childCalculateList, tmpErr := GetAllCalculateByEdbInfoIdV2(item.EdbInfoId)
  1174. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1175. err = tmpErr
  1176. return
  1177. }
  1178. for _, childCalculate := range childCalculateList {
  1179. if childCalculate.FromEdbInfoId == replaceEdbInfoId {
  1180. err = fmt.Errorf("指标重复2")
  1181. return
  1182. }
  1183. }
  1184. _, ok := edbInfoMap[item.EdbInfoId]
  1185. if !ok {
  1186. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  1187. newCalculateInfoArr, tmpErr := GetAllCalculateV2(item.EdbInfoId, replaceEdbInfoId, edbInfoMap)
  1188. if tmpErr != nil {
  1189. err = tmpErr
  1190. continue
  1191. } else {
  1192. mappingList = append(mappingList, item)
  1193. tmpMappingList = append(tmpMappingList, newCalculateInfoArr...)
  1194. }
  1195. }
  1196. }
  1197. mappingList = append(mappingList, tmpMappingList...)
  1198. return
  1199. }
  1200. // EdbInfoFilterDataResp 搜索指标列表数据返回
  1201. type EdbInfoFilterDataResp struct {
  1202. Paging *paging.PagingItem
  1203. List []*EdbInfoList
  1204. }
  1205. // GetEdbInfoFilterList 获取指指标列表数据接口
  1206. func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
  1207. o := orm.NewOrmUsingDB("data")
  1208. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  1209. err = o.Raw(sql, pars).QueryRow(&total)
  1210. if err != nil {
  1211. return
  1212. }
  1213. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  1214. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  1215. return
  1216. }
  1217. // SetEdbInfoImageReq 设置指标图片
  1218. type SetEdbInfoImageReq struct {
  1219. EdbInfoId int `description:"指标ID"`
  1220. ImageUrl string `description:"指标图片地址"`
  1221. }
  1222. // GetNextEdbInfoByCondition 根据条件获取下一个指标
  1223. func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
  1224. o := orm.NewOrmUsingDB("data")
  1225. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1226. if condition != "" {
  1227. sql += condition
  1228. }
  1229. sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
  1230. err = o.Raw(sql, pars).QueryRow(&item)
  1231. return
  1232. }
  1233. // GetNextEdbInfo 根据分类id获取下一个 指标
  1234. func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
  1235. o := orm.NewOrmUsingDB("data")
  1236. sql := ` SELECT b.* FROM edb_classify AS a
  1237. INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
  1238. WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
  1239. AND a.classify_type=? AND b.edb_info_type=?
  1240. ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
  1241. LIMIT 1 `
  1242. err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).QueryRow(&item)
  1243. return
  1244. }
  1245. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  1246. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  1247. num := len(edbCodeList)
  1248. if num <= 0 {
  1249. return
  1250. }
  1251. o := orm.NewOrmUsingDB("data")
  1252. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  1253. _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
  1254. return
  1255. }
  1256. // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
  1257. func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
  1258. o := orm.NewOrmUsingDB("data")
  1259. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  1260. if condition != "" {
  1261. sql += condition
  1262. }
  1263. err = o.Raw(sql, pars).QueryRow(&count)
  1264. return
  1265. }
  1266. // GetEdbInfoListByCondition 根据条件获取指标列表数据
  1267. func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int, orderDesc string) (items []*EdbInfoList, err error) {
  1268. o := orm.NewOrmUsingDB("data")
  1269. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1270. if condition != "" {
  1271. sql += condition
  1272. }
  1273. if orderDesc == `` {
  1274. orderDesc = ` ASC `
  1275. }
  1276. sql += ` ORDER BY edb_info_id `
  1277. sql += orderDesc
  1278. sql += ` LIMIT ?,? `
  1279. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1280. return
  1281. }
  1282. // GetEdbInfoListV2ByCondition 根据条件获取指标列表数据(EdbInfo原始数据结构)
  1283. func GetEdbInfoListV2ByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfo, err error) {
  1284. o := orm.NewOrmUsingDB("data")
  1285. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1286. if condition != "" {
  1287. sql += condition
  1288. }
  1289. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  1290. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1291. return
  1292. }
  1293. // GetEdbInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
  1294. func GetEdbInfoListGroupByUserId(edbIdList []string) (items []*EdbInfo, err error) {
  1295. num := len(edbIdList)
  1296. if num <= 0 {
  1297. return
  1298. }
  1299. o := orm.NewOrmUsingDB("data")
  1300. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
  1301. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1302. return
  1303. }
  1304. // GetEdbInfoListByEdbInfoId
  1305. // @Description: 根据指标id列表获取列表信息
  1306. // @param edbIdList
  1307. // @return items
  1308. // @return err
  1309. func GetEdbInfoListByEdbInfoId(edbIdList []string) (items []*EdbInfo, err error) {
  1310. num := len(edbIdList)
  1311. if num <= 0 {
  1312. return
  1313. }
  1314. o := orm.NewOrmUsingDB("data")
  1315. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1316. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1317. return
  1318. }
  1319. // GetEdbInfoListByUserId
  1320. // @Description: 根据指标id列表获取列表信息
  1321. // @param userIdList []int
  1322. // @param edbInfoType int
  1323. // @return items
  1324. // @return err
  1325. func GetEdbInfoListByUserId(userIdList []int, edbInfoType int) (items []*EdbInfo, err error) {
  1326. num := len(userIdList)
  1327. if num <= 0 {
  1328. return
  1329. }
  1330. o := orm.NewOrmUsingDB("data")
  1331. sql := ` SELECT * FROM edb_info WHERE edb_info_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  1332. _, err = o.Raw(sql, edbInfoType, userIdList).QueryRows(&items)
  1333. return
  1334. }
  1335. // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
  1336. func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1337. num := len(edbIdList)
  1338. if num <= 0 {
  1339. return
  1340. }
  1341. o := orm.NewOrmUsingDB("data")
  1342. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1343. _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
  1344. return
  1345. }
  1346. // ModifyEdbInfoUserIdByOldUserId
  1347. // @Description: 根据旧用户id修改新用户id
  1348. // @author: Roc
  1349. // @datetime 2024-03-25 19:17:09
  1350. // @param edbIdList []string
  1351. // @param userId int
  1352. // @param userName string
  1353. // @return err error
  1354. func ModifyEdbInfoUserIdByOldUserId(oldUserIdList []int, edbInfoType, userId int, userName string) (err error) {
  1355. num := len(oldUserIdList)
  1356. if num <= 0 {
  1357. return
  1358. }
  1359. o := orm.NewOrmUsingDB("data")
  1360. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) and edb_info_type = ?`
  1361. _, err = o.Raw(sql, userId, userName, oldUserIdList, edbInfoType).Exec()
  1362. return
  1363. }
  1364. // MoveEdbChartList 图表/指标信息
  1365. type MoveEdbChartList struct {
  1366. DataId string `description:"指标/图表唯一id"`
  1367. Code string `description:"指标code"`
  1368. Name string `description:"指标/图表名称"`
  1369. NameEn string `description:"指标/图表名称"`
  1370. ClassifyName string `description:"分类名称"`
  1371. ClassifyNameEn string `description:"分类名称"`
  1372. CreateUserId int `description:"创建人id"`
  1373. CreateUserName string `description:"创建人名称"`
  1374. }
  1375. // MoveEdbChartListResp 更改创建人返回数据
  1376. type MoveEdbChartListResp struct {
  1377. Paging *paging.PagingItem
  1378. List []MoveEdbChartList
  1379. }
  1380. type EdbChartClassifyResp struct {
  1381. List []*EdbChartClassify
  1382. }
  1383. type EdbChartClassify struct {
  1384. ClassifyId int
  1385. ClassifyName string
  1386. ParentId int
  1387. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  1388. Child []*EdbChartClassify
  1389. }
  1390. // EdbInfoDataSeasonalResp 指标季节性数据返回
  1391. type EdbInfoDataSeasonalResp struct {
  1392. EdbInfo *EdbInfo
  1393. DataList interface{}
  1394. }
  1395. // SetEdbDataInsertConfigReq 设置插入规则的请求
  1396. type SetEdbDataInsertConfigReq struct {
  1397. EdbInfoId int `description:"指标ID"`
  1398. Date string `description:"日期"`
  1399. Value string `description:"值"`
  1400. }
  1401. // GetEdbInfoByClassifyId 用于分类展示
  1402. func GetEdbInfoByClassifyId(classifyId, edbInfoType, adminId int) (items []*EdbClassifyItems, err error) {
  1403. o := orm.NewOrmUsingDB("data")
  1404. sql := ` SELECT edb_info_id,classify_id,edb_name AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type, sort,is_join_permission FROM edb_info WHERE classify_id = ? AND edb_info_type = ?`
  1405. pars := []interface{}{classifyId, edbInfoType}
  1406. // 如果筛选了用户id
  1407. if adminId > 0 {
  1408. sql += ` AND sys_user_id = ? `
  1409. pars = append(pars, adminId)
  1410. }
  1411. sql += ` order by sort asc,edb_info_id asc `
  1412. _, err = o.Raw(sql, pars).QueryRows(&items)
  1413. return
  1414. }
  1415. type EdbAndClassify struct {
  1416. EdbInfoId int `description:"指标id"`
  1417. EdbCode string `description:"指标code"`
  1418. UniqueCode string `description:"唯一code"`
  1419. EdbName string `description:"指标名称"`
  1420. ClassifyId int `description:"所属分类id"`
  1421. FirstClassifyId int `description:"第1级的分类id"`
  1422. FirstClassifyName string `description:"第1级的分类名称"`
  1423. SecondClassifyId int `description:"第2级的分类id"`
  1424. SecondClassifyName string `description:"第2级的分类名称"`
  1425. }
  1426. // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
  1427. func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
  1428. num := len(edbIdList)
  1429. if num <= 0 {
  1430. return
  1431. }
  1432. o := orm.NewOrmUsingDB("data")
  1433. sql := ` SELECT a.edb_info_id,a.edb_code,a.unique_code,a.edb_name,a.classify_id FROM edb_info AS a
  1434. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1435. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1436. return
  1437. }
  1438. // TraceEdbInfoResp 指标追溯数据返回
  1439. type TraceEdbInfoResp struct {
  1440. EdbInfoId int `description:"指标id"`
  1441. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  1442. EdbName string `description:"指标名称"`
  1443. EdbNameEn string `description:"指标英文名称"`
  1444. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  1445. RuleTitle string `description:"指标规则"`
  1446. RuleTitleEn string `description:"指标规则(英文)"`
  1447. UniqueCode string `description:"唯一编码"`
  1448. ClassifyId int `description:"分类ID"`
  1449. Child []TraceEdbInfoResp `description:"下级来源"`
  1450. IsStop int8 `description:"是否终止"`
  1451. EdbInfo *EdbInfo `description:"指标信息" json:"-"`
  1452. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  1453. }
  1454. // BeforeAndAfterDateDataResp 前后几期数据
  1455. type BeforeAndAfterDateDataResp struct {
  1456. List []*EdbDataList `description:"list"`
  1457. Date string `description:"实际日期"`
  1458. ShowValue string `description:"展示值"`
  1459. }
  1460. // GetEdbInfoAdminList
  1461. func GetEdbInfoAdminList(edbType int) (list []int, err error) {
  1462. o := orm.NewOrmUsingDB("data")
  1463. sql := ` SELECT sys_user_id FROM edb_info WHERE edb_info_type = ? GROUP BY sys_user_id `
  1464. _, err = o.Raw(sql, edbType).QueryRows(&list)
  1465. return
  1466. }
  1467. type EdbInfoExistCheckResp struct {
  1468. IndexExist bool `description:"指标是否存在:true,存在,false:不存在"`
  1469. ExistStockCodeArr []string `description:"存在的证券代码"`
  1470. ExistEdbCodeArr []string `description:"存在的指标代码"`
  1471. ExistClassifyId []int `description:"存在的分类id"`
  1472. ExistIndexId []int `description:"存在的指标id"`
  1473. ExistEdbInfo []*EdbInfo `description:"存在的指标"`
  1474. }
  1475. type BatchAddEdbInfoReq struct {
  1476. BatchList []*BatchAddEdbInfo
  1477. }
  1478. type BatchAddEdbInfo struct {
  1479. Source int `description:"来源id"`
  1480. EdbName string `description:"指标名称"`
  1481. Frequency string `description:"频率"`
  1482. Unit string `description:"单位"`
  1483. ClassifyId int `description:"分类id"`
  1484. StockCode string `description:"证券代码"`
  1485. EdbCode string `description:"指标编码"`
  1486. ApiExtraPars string `description:"API额外参数"`
  1487. }
  1488. func GetEdbInfoWsdMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  1489. o := orm.NewOrmUsingDB("data")
  1490. sql := ``
  1491. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  1492. 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=? `
  1493. sql = fmt.Sprintf(sql, tableName)
  1494. err = o.Raw(sql, edbCode).QueryRow(&item)
  1495. if err != nil {
  1496. return
  1497. }
  1498. var latest_value float64
  1499. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  1500. sql = fmt.Sprintf(sql, tableName)
  1501. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  1502. item.LatestValue = latest_value
  1503. return
  1504. }
  1505. // GetEdbInfoByNames 根据名称获取普通的指标
  1506. func GetEdbInfoByNames(edbNames string) (items []*EdbInfoList, err error) {
  1507. o := orm.NewOrmUsingDB("data")
  1508. sql := ` SELECT * FROM edb_info WHERE edb_name IN (` + edbNames + `) AND edb_info_type = 0 `
  1509. _, err = o.Raw(sql).QueryRows(&items)
  1510. return
  1511. }
  1512. func GetSmmEdbInfoMaxAndMinInfo(edbCodes []string) (item *EdbInfoMaxAndMinInfo, err error) {
  1513. o := orm.NewOrmUsingDB("data")
  1514. sql := ``
  1515. 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 edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `)`
  1516. err = o.Raw(sql, edbCodes).QueryRow(&item)
  1517. if err != nil {
  1518. return
  1519. }
  1520. var latest_value float64
  1521. sql = ` SELECT value AS latest_value FROM edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `) ORDER BY data_time DESC LIMIT 1 `
  1522. err = o.Raw(sql, edbCodes).QueryRow(&latest_value)
  1523. item.LatestValue = latest_value
  1524. return
  1525. }
  1526. type EdbInfoSmmExistCheckResp struct {
  1527. EdbInfoExistCheckResp
  1528. ExistAll bool `description:"是否全部重复"`
  1529. }
  1530. // GetEdbBaseInfoList
  1531. // @Description: 获取指标库数据列表
  1532. // @author: Roc
  1533. // @datetime 2024-01-10 14:28:22
  1534. // @param condition string
  1535. // @param pars []interface{}
  1536. // @param orderBy string
  1537. // @param startSize int
  1538. // @param pageSize int
  1539. // @return total int
  1540. // @return items []*BaseRefreshEdbInfo
  1541. // @return err error
  1542. func GetEdbBaseInfoList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRefreshEdbInfo, err error) {
  1543. o := orm.NewOrmUsingDB("data")
  1544. // 数量汇总
  1545. totalSql := ` SELECT count(1) FROM edb_info WHERE 1=1 `
  1546. if condition != "" {
  1547. totalSql += condition
  1548. }
  1549. err = o.Raw(totalSql, pars).QueryRow(&total)
  1550. if err != nil {
  1551. return
  1552. }
  1553. // 列表数据
  1554. sql := ` SELECT edb_info_id as edb_info_id, classify_id as classify_id,edb_code as index_code,edb_name as index_name,end_date,end_value,sys_user_id,sys_user_real_name,frequency,no_update as is_stop,terminal_code FROM edb_info WHERE 1=1 `
  1555. if condition != "" {
  1556. sql += condition
  1557. }
  1558. if orderBy != "" {
  1559. sql += ` ORDER BY ` + orderBy
  1560. } else {
  1561. sql += ` ORDER BY edb_info_id ASC `
  1562. }
  1563. sql += ` LIMIT ?,? `
  1564. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1565. return
  1566. }
  1567. // ModifyEdbInfoUpdateStatus
  1568. // @Description: 修改指标库停更状态
  1569. // @author: Roc
  1570. // @datetime 2024-01-08 16:23:31
  1571. // @param edbIdList []int
  1572. // @param indexCodeList []string
  1573. // @param isStop int
  1574. // @return err error
  1575. func ModifyEdbInfoUpdateStatus(edbIdList []int, isStop int, calculateEdbInfoIds []int) (err error) {
  1576. idNum := len(edbIdList)
  1577. if idNum <= 0 {
  1578. return
  1579. }
  1580. o, err := orm.NewOrmUsingDB("data").Begin()
  1581. if err != nil {
  1582. return
  1583. }
  1584. defer func() {
  1585. if err != nil {
  1586. _ = o.Rollback()
  1587. return
  1588. }
  1589. _ = o.Commit()
  1590. }()
  1591. // 更改指标的更新状态
  1592. sql := ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  1593. _, err = o.Raw(sql, isStop, edbIdList).Exec()
  1594. if err != nil {
  1595. return
  1596. }
  1597. if len(calculateEdbInfoIds) > 0 {
  1598. // 批量更新相关联的指标ID
  1599. sql = ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) `
  1600. _, err = o.Raw(sql, isStop, calculateEdbInfoIds).Exec()
  1601. if err != nil {
  1602. return
  1603. }
  1604. }
  1605. return
  1606. }
  1607. func GetEdbInfoCount(source int, edbCode string) (count int, err error) {
  1608. o := orm.NewOrmUsingDB("data")
  1609. sql := ` SELECT COUNT(1) AS count FROM edb_info
  1610. WHERE source=? AND edb_code = ?`
  1611. err = o.Raw(sql, source, edbCode).QueryRow(&count)
  1612. return
  1613. }
  1614. // GetEdbInfoByNameArr 根据名称获取指标
  1615. func GetEdbInfoByNameArr(names []string, edbInfoType int) (items []*EdbInfo, err error) {
  1616. if len(names) == 0 {
  1617. return
  1618. }
  1619. o := orm.NewOrmUsingDB("data")
  1620. sql := fmt.Sprintf(`SELECT edb_info_id, edb_code, edb_name FROM edb_info WHERE edb_name IN (%s) AND edb_info_type = ? `, utils.GetOrmInReplace(len(names)))
  1621. _, err = o.Raw(sql, names, edbInfoType).QueryRows(&items)
  1622. return
  1623. }
  1624. // GetEdbInfoByNameArr 根据指标编码获取指标
  1625. func GetEdbInfoByCodeArr(codes []string, edbInfoType int) (items []*EdbInfo, err error) {
  1626. if len(codes) == 0 {
  1627. return
  1628. }
  1629. o := orm.NewOrmUsingDB("data")
  1630. sql := fmt.Sprintf(`SELECT edb_info_id, edb_code, edb_name FROM edb_info WHERE edb_code IN (%s) AND edb_info_type = ? `, utils.GetOrmInReplace(len(codes)))
  1631. _, err = o.Raw(sql, codes, edbInfoType).QueryRows(&items)
  1632. return
  1633. }
  1634. // GetEdbCodesBySource 根据来源获取指标编码
  1635. func GetEdbCodesBySource(source int) (items []*EdbInfo, err error) {
  1636. o := orm.NewOrmUsingDB("data")
  1637. sql := ` SELECT edb_info_id, edb_code, unique_code, classify_id FROM edb_info WHERE source = ? `
  1638. _, err = o.Raw(sql, source).QueryRows(&items)
  1639. return
  1640. }
  1641. // GetAllEdbDataListData
  1642. // @Description: 获取指标的所有数据
  1643. // @author: Roc
  1644. // @datetime 2024-05-07 15:18:27
  1645. // @param edbInfoId int
  1646. // @param source int
  1647. // @param subSource int
  1648. // @param startDataTime string
  1649. // @return dataList []*EdbData
  1650. // @return err error
  1651. func GetAllEdbDataListData(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1652. // 自有数据需要额外处理(从mongo获取)
  1653. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  1654. return getAllDataByMongo(edbInfoId, source, subSource, startDataTime)
  1655. }
  1656. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  1657. return getThsHfAllDataByMongo(edbInfoId, source, subSource, startDataTime)
  1658. }
  1659. // 默认走mysql
  1660. return getAllDataByMysql(edbInfoId, source, subSource, startDataTime)
  1661. }
  1662. // getAllDataByMysql
  1663. // @Description: 从mysql获取指标的所有数据
  1664. // @author: Roc
  1665. // @datetime 2024-05-07 15:18:41
  1666. // @param edbInfoId int
  1667. // @param source int
  1668. // @param subSource int
  1669. // @param startDataTime string
  1670. // @return dataList []*data_manage.EdbData
  1671. // @return err error
  1672. func getAllDataByMysql(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1673. dataList = make([]*EdbData, 0)
  1674. var dataCondition string
  1675. var dataPars []interface{}
  1676. dataCondition += ` AND edb_info_id=? `
  1677. dataPars = append(dataPars, edbInfoId)
  1678. // 结束日期
  1679. if startDataTime != "" {
  1680. dataCondition += ` AND data_time > ? `
  1681. dataPars = append(dataPars, startDataTime)
  1682. }
  1683. // 获取列表数据
  1684. dataList, err = GetAllEdbDataListByCondition(dataCondition, dataPars, source, subSource)
  1685. if err != nil {
  1686. return
  1687. }
  1688. return
  1689. }
  1690. // getAllDataByMongo
  1691. // @Description: 从mongo获取指标的所有数据
  1692. // @author: Roc
  1693. // @datetime 2024-05-07 15:18:53
  1694. // @param edbInfoId int
  1695. // @param source int
  1696. // @param subSource int
  1697. // @param startDataTime string
  1698. // @return dataList []*data_manage.EdbData
  1699. // @return err error
  1700. func getAllDataByMongo(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1701. dataList = make([]*EdbData, 0)
  1702. mogDataObj := mgo.EdbDataBusiness{}
  1703. // 构建查询条件
  1704. queryConditions := bson.M{
  1705. "edb_info_id": edbInfoId,
  1706. }
  1707. // 开始日期
  1708. dateCondition, err := mgo.BuildDateCondition(startDataTime, "")
  1709. if err != nil {
  1710. return
  1711. }
  1712. if len(dateCondition) > 0 {
  1713. queryConditions["data_time"] = dateCondition
  1714. }
  1715. // 获取列表数据
  1716. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"-data_time"})
  1717. if tmpErr != nil {
  1718. err = tmpErr
  1719. return
  1720. }
  1721. for k, v := range tmpDataList {
  1722. dataList = append(dataList, &EdbData{
  1723. EdbDataId: k + 1,
  1724. EdbInfoId: v.EdbInfoId,
  1725. DataTime: v.DataTime.Format(utils.FormatDate),
  1726. Value: v.Value,
  1727. })
  1728. }
  1729. return
  1730. }
  1731. // GetEdbClassifyIdListBySource 获取普通指标的分类列表
  1732. func GetEdbClassifyIdListBySource(source int) (items []int, err error) {
  1733. o := orm.NewOrmUsingDB("data")
  1734. sql := `SELECT distinct classify_id FROM edb_info WHERE source = ? and edb_info_type=0`
  1735. _, err = o.Raw(sql, source).QueryRows(&items)
  1736. return
  1737. }
  1738. // GetEdbInfoByClassifyIdAndSource 用于分类展示
  1739. func GetEdbInfoByClassifyIdAndSource(classifyId, edbInfoType, source int) (items []*EdbClassifyItems, err error) {
  1740. o := orm.NewOrmUsingDB("data")
  1741. sql := ` SELECT edb_info_id,classify_id,edb_name AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type, sort,is_join_permission FROM edb_info WHERE classify_id = ? AND edb_info_type = ? AND source =?`
  1742. pars := []interface{}{classifyId, edbInfoType, source}
  1743. sql += ` order by sort asc,edb_info_id asc `
  1744. _, err = o.Raw(sql, pars).QueryRows(&items)
  1745. return
  1746. }
  1747. type ReplaceEdbInfoItem struct {
  1748. OldEdbInfo *EdbInfo
  1749. NewEdbInfo *EdbInfo
  1750. }
  1751. func GetEdbInfoFieldList(cond string, pars []interface{}, fields []string) (items []*EdbInfo, err error) {
  1752. field := " * "
  1753. if len(fields) > 0 {
  1754. field = strings.Join(fields, ",")
  1755. }
  1756. o := orm.NewOrmUsingDB("data")
  1757. sql := fmt.Sprintf(`SELECT %s FROM edb_info WHERE 1=1 %s `, field, cond)
  1758. _, err = o.Raw(sql, pars).QueryRows(&items)
  1759. return
  1760. }
  1761. // EdbInfoExtra 指标额外数据-extra字段
  1762. type EdbInfoExtra struct {
  1763. ApiExtraPars string `description:"API-额外参数(如同花顺日期序列)"`
  1764. }
  1765. func GetEdbInfoListByCond(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  1766. o := orm.NewOrmUsingDB("data")
  1767. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1768. if condition != "" {
  1769. sql += condition
  1770. }
  1771. sql += ` ORDER BY create_time DESC `
  1772. _, err = o.Raw(sql, pars).QueryRows(&list)
  1773. return
  1774. }
  1775. func GetThsHfEdbInfoMaxAndMinInfoByMongo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  1776. mogDataObj := new(mgo.EdbDataThsHf)
  1777. pipeline := []bson.M{
  1778. {"$match": bson.M{"edb_code": edbCode}},
  1779. {"$group": bson.M{
  1780. "_id": nil,
  1781. "min_date": bson.M{"$min": "$data_time"},
  1782. "max_date": bson.M{"$max": "$data_time"},
  1783. "min_value": bson.M{"$min": "$value"},
  1784. "max_value": bson.M{"$max": "$value"},
  1785. }},
  1786. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  1787. }
  1788. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  1789. if err != nil {
  1790. fmt.Println("EdbDataThsHf getEdbDataThsHfList Err:" + err.Error())
  1791. return
  1792. }
  1793. if !result.MaxDate.IsZero() {
  1794. whereQuery := bson.M{"edb_code": edbCode, "data_time": result.MaxDate}
  1795. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  1796. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  1797. if tmpErr != nil {
  1798. err = tmpErr
  1799. return
  1800. }
  1801. result.LatestValue = latestValue.Value
  1802. result.EndValue = latestValue.Value
  1803. }
  1804. item = &EdbInfoMaxAndMinInfo{
  1805. MinDate: result.MinDate.Format(utils.FormatDate),
  1806. MaxDate: result.MaxDate.Format(utils.FormatDate),
  1807. MinValue: result.MinValue,
  1808. MaxValue: result.MaxValue,
  1809. LatestValue: result.LatestValue,
  1810. }
  1811. return
  1812. }
  1813. func getThsHfAllDataByMongo(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1814. dataList = make([]*EdbData, 0)
  1815. mogDataObj := mgo.EdbDataThsHf{}
  1816. // 构建查询条件
  1817. queryConditions := bson.M{
  1818. "edb_info_id": edbInfoId,
  1819. }
  1820. // 开始日期
  1821. dateCondition, err := mgo.BuildDateCondition(startDataTime, "")
  1822. if err != nil {
  1823. return
  1824. }
  1825. if len(dateCondition) > 0 {
  1826. queryConditions["data_time"] = dateCondition
  1827. }
  1828. // 获取列表数据
  1829. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"-data_time"})
  1830. if tmpErr != nil {
  1831. err = tmpErr
  1832. return
  1833. }
  1834. for k, v := range tmpDataList {
  1835. dataList = append(dataList, &EdbData{
  1836. EdbDataId: k + 1,
  1837. EdbInfoId: v.EdbInfoId,
  1838. DataTime: v.DataTime.Format(utils.FormatDate),
  1839. Value: v.Value,
  1840. })
  1841. }
  1842. return
  1843. }