edb_info.go 74 KB

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