edb_info.go 75 KB

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