edb_info.go 65 KB

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