edb_info.go 66 KB

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