edb_info.go 64 KB

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