edb_info.go 77 KB

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