edb_info.go 57 KB

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