edb_info.go 73 KB

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