edb_info.go 79 KB

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