edb_info.go 58 KB

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