edb_info.go 58 KB

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