edb_info.go 65 KB

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