edb_info.go 65 KB

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