edb_info.go 69 KB

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