edb_info.go 64 KB

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