edb_info.go 57 KB

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