edb_info.go 67 KB

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