edb_info.go 68 KB

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