edb_info.go 63 KB

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