edb_info.go 73 KB

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