edb_info.go 73 KB

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