edb_info.go 75 KB

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