edb_info.go 74 KB

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