edb_info.go 79 KB

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