edb_info.go 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  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(fromEdbInfoIdList []int) (list []*EdbInfo, err error) {
  688. num := len(fromEdbInfoIdList)
  689. if num <= 0 {
  690. return
  691. }
  692. edbEdbInfoIdList := make([]int, 0)
  693. sql := ` SELECT DISTINCT b.edb_info_id FROM edb_info_calculate_mapping AS a
  694. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  695. WHERE a.from_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  696. ORDER BY b.edb_info_id ASC `
  697. err = global.DmSQL["data"].Raw(sql, fromEdbInfoIdList).Scan(&edbEdbInfoIdList).Error
  698. if err != nil {
  699. return
  700. }
  701. // 查询来的实际id集合
  702. num = len(edbEdbInfoIdList)
  703. if num <= 0 {
  704. return
  705. }
  706. sql = ` SELECT * FROM edb_info WHERE edb_info_id IN (` + utils.GetOrmInReplace(num) + `) ORDER BY edb_info_id ASC `
  707. err = global.DmSQL["data"].Raw(sql, edbEdbInfoIdList).Find(&list).Error
  708. return
  709. }
  710. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  711. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  712. if err != nil && !utils.IsErrNoRow(err) {
  713. return
  714. }
  715. for _, item := range calculateList {
  716. if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
  717. continue
  718. }
  719. if item.EdbType == 1 {
  720. baseEdbInfoArr = append(baseEdbInfoArr, item)
  721. } else {
  722. calculateInfoArr = append(calculateInfoArr, item)
  723. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  724. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  725. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  726. }
  727. }
  728. return
  729. }
  730. func ModifyEdbInfoDataStatus(edbInfoId int64, source, subSource int, edbCode string) (err error) {
  731. sql := ``
  732. tableName := GetEdbDataTableName(source, subSource)
  733. sql = ` UPDATE %s SET edb_info_id=?,modify_time=NOW() WHERE edb_code=? `
  734. sql = fmt.Sprintf(sql, tableName)
  735. err = global.DmSQL["data"].Exec(sql, edbInfoId, edbCode).Error
  736. return
  737. }
  738. // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  739. func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
  740. sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
  741. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  742. return
  743. }
  744. // GetFirstEdbInfoByPublicClassifyId
  745. // @Description: 获取当前公共分类下,且排序数相同 的排序第一条的数据
  746. // @param classifyId
  747. // @return item
  748. // @return err
  749. func GetFirstEdbInfoByPublicClassifyId(classifyId int) (item *EdbInfo, err error) {
  750. 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`
  751. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  752. return
  753. }
  754. // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
  755. func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  756. sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=?`
  757. if prevEdbInfoId > 0 {
  758. sql += ` AND ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  759. } else {
  760. sql += ` AND ( sort > ? )`
  761. }
  762. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort).Error
  763. return
  764. }
  765. // UpdateEdbInfoPublicSortByClassifyId 根据公共分类id更新公共排序
  766. func UpdateEdbInfoPublicSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  767. sql := ` update edb_info set sort = ` + updateSort + ` WHERE edb_public_classify_id=? AND public_status = 3 `
  768. if prevEdbInfoId > 0 {
  769. sql += ` AND ( public_sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and public_sort=` + fmt.Sprint(nowSort) + ` )) `
  770. } else {
  771. sql += ` AND ( public_sort > ? )`
  772. }
  773. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort).Error
  774. return
  775. }
  776. // Update 更新指标基础信息
  777. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  778. err = global.DmSQL["data"].Select(cols).Updates(edbInfo).Error
  779. return
  780. }
  781. // UpdateById 更新指标基础信息
  782. func (m *EdbInfo) UpdateById(edbInfoId int, updateMap map[string]interface{}) (err error) {
  783. err = global.DmSQL["data"].Model(m).Where("edb_info_id = ? ", edbInfoId).Updates(updateMap).Error
  784. return
  785. }
  786. type EdbInfoDataResp struct {
  787. EdbInfo *EdbInfo
  788. DataList []*EdbDataList
  789. }
  790. type EdbInfoDataFullClassifyResp struct {
  791. EdbInfo *EdbInfoFullClassify
  792. DataList []*EdbDataList
  793. }
  794. type EdbInfoReplaceReq struct {
  795. OldEdbInfoId int `description:"原指标ID"`
  796. NewEdbInfoId int `description:"替换为指标ID"`
  797. }
  798. // ReplaceChartEdb 替换图表中的指标
  799. func ReplaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo, userId int) (relationEdbInfoIdList []int, replaceChartTotal, replaceCalculateTotal int, err error) {
  800. var errmsg string
  801. relationEdbInfoIdList = make([]int, 0) // 关联的指标id
  802. logMsg := `` // 记录替换的日志
  803. to := global.DmSQL["data"].Begin()
  804. defer func() {
  805. if err != nil {
  806. _ = to.Rollback()
  807. } else {
  808. _ = to.Commit()
  809. if logMsg != `` {
  810. utils.FileLog.Info(fmt.Sprintf("替换指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  811. }
  812. }
  813. if errmsg != "" {
  814. fmt.Println("errmsg:" + errmsg)
  815. }
  816. if err != nil && errmsg != "" {
  817. go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
  818. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  819. }
  820. }()
  821. //替换自己创建的图表
  822. {
  823. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  824. csql := `SELECT a.* FROM chart_edb_mapping AS a
  825. JOIN chart_info AS b on a.chart_info_id=b.chart_info_id
  826. WHERE b.sys_user_id = ? AND a.edb_info_id=? `
  827. err = to.Raw(csql, userId, oldEdbInfo.EdbInfoId).Scan(&chartEdbMappingList).Error
  828. if err != nil {
  829. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  830. return
  831. }
  832. replaceChartTotal = len(chartEdbMappingList)
  833. // 查询所有的相关的图表信息
  834. chartInfoIds := make([]string, 0)
  835. if len(chartEdbMappingList) > 0 {
  836. for _, chartEdbMapping := range chartEdbMappingList {
  837. chartInfoIds = append(chartInfoIds, strconv.Itoa(chartEdbMapping.ChartInfoId))
  838. }
  839. }
  840. if len(chartInfoIds) > 0 {
  841. chartInfoList, e := GetChartInfoListByChartIdList(chartInfoIds)
  842. if e != nil {
  843. err = e
  844. errmsg = "获取图表信息失败:Err:" + e.Error()
  845. return
  846. }
  847. for _, chartInfo := range chartInfoList {
  848. updateStr := make([]string, 0)
  849. if chartInfo.EdbInfoIds != "" && strings.Contains(chartInfo.EdbInfoIds, strconv.Itoa(oldEdbInfo.EdbInfoId)) {
  850. //需要更换
  851. //解析字符串
  852. edbInfoIds := strings.Split(chartInfo.EdbInfoIds, ",")
  853. for i, edbInfoId := range edbInfoIds {
  854. if edbInfoId == strconv.Itoa(oldEdbInfo.EdbInfoId) {
  855. edbInfoIds[i] = strconv.Itoa(newEdbInfo.EdbInfoId)
  856. }
  857. }
  858. chartInfo.EdbInfoIds = strings.Join(edbInfoIds, ",")
  859. updateStr = append(updateStr, "EdbInfoIds")
  860. }
  861. if chartInfo.ExtraConfig != "" || chartInfo.BarConfig != "" {
  862. //判断是否是拟合方程或者散点图截面图
  863. if chartInfo.Source == utils.CHART_SOURCE_FUTURE_GOOD {
  864. // 商品价格曲线图的一些配置
  865. var barConfig FutureGoodBarChartInfoReq
  866. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  867. if err != nil {
  868. errmsg = "商品价格曲线图配置异常 json.Unmarshal:Err:" + err.Error()
  869. return
  870. }
  871. if barConfig.BaseEdbInfoId == oldEdbInfo.EdbInfoId {
  872. barConfig.BaseEdbInfoId = newEdbInfo.EdbInfoId
  873. }
  874. for k, item := range barConfig.EdbInfoIdList {
  875. if item.EdbInfoId == oldEdbInfo.EdbInfoId && item.Source == 1 {
  876. barConfig.EdbInfoIdList[k].EdbInfoId = newEdbInfo.EdbInfoId
  877. }
  878. }
  879. // 更新图表配置信息
  880. // 重新序列化
  881. configJson, e := json.Marshal(barConfig)
  882. if e != nil {
  883. err = e
  884. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  885. return
  886. }
  887. configStr := string(configJson)
  888. chartInfo.BarConfig = configStr
  889. updateStr = append(updateStr, "BarConfig")
  890. } else if chartInfo.Source == utils.CHART_SOURCE_FUTURE_GOOD_PROFIT {
  891. // 商品利润曲线图的一些配置
  892. var extraConf FutureGoodProfitChartInfoReq
  893. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
  894. if err != nil {
  895. errmsg = "商品利润曲线图配置异常 json.Unmarshal:Err:" + err.Error()
  896. return
  897. }
  898. if extraConf.BaseEdbInfoId == oldEdbInfo.EdbInfoId {
  899. extraConf.BaseEdbInfoId = newEdbInfo.EdbInfoId
  900. }
  901. for k, v := range extraConf.EdbInfoIdList {
  902. if v == oldEdbInfo.EdbInfoId {
  903. extraConf.EdbInfoIdList[k] = newEdbInfo.EdbInfoId
  904. }
  905. }
  906. // 更新图表配置信息
  907. // 重新序列化
  908. configJson, e := json.Marshal(extraConf)
  909. if e != nil {
  910. err = e
  911. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  912. return
  913. }
  914. configStr := string(configJson)
  915. chartInfo.ExtraConfig = configStr
  916. updateStr = append(updateStr, "ExtraConfig")
  917. } else if chartInfo.Source == utils.CHART_SOURCE_LINE_EQUATION {
  918. //解析配置内容
  919. var lineChartInfoConfig request.LineChartInfoReq
  920. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &lineChartInfoConfig)
  921. if err != nil {
  922. errmsg = "获取图表配置信息失败 json.Unmarshal:Err:" + err.Error()
  923. return
  924. }
  925. xEdbInfoIdList := lineChartInfoConfig.XEdbInfoIdList
  926. yEdbInfoIdList := lineChartInfoConfig.YEdbInfoIdList
  927. if len(xEdbInfoIdList) > 0 {
  928. // 循环判断是否存在
  929. for k, xEdbInfoId := range xEdbInfoIdList {
  930. if xEdbInfoId == oldEdbInfo.EdbInfoId {
  931. xEdbInfoIdList[k] = newEdbInfo.EdbInfoId
  932. }
  933. }
  934. }
  935. if len(yEdbInfoIdList) > 0 {
  936. // 循环判断是否存在
  937. for k, yEdbInfoId := range yEdbInfoIdList {
  938. if yEdbInfoId == oldEdbInfo.EdbInfoId {
  939. yEdbInfoIdList[k] = newEdbInfo.EdbInfoId
  940. }
  941. }
  942. }
  943. // 替换新的指标信息
  944. lineChartInfoConfig.XEdbInfoIdList = xEdbInfoIdList
  945. lineChartInfoConfig.YEdbInfoIdList = yEdbInfoIdList
  946. // 重新序列化
  947. lineChartInfoConfigJson, e := json.Marshal(lineChartInfoConfig)
  948. if e != nil {
  949. err = e
  950. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  951. return
  952. }
  953. lineChartInfoConfigStr := string(lineChartInfoConfigJson)
  954. // 更新图表配置信息
  955. chartInfo.ExtraConfig = lineChartInfoConfigStr
  956. updateStr = append(updateStr, "ExtraConfig")
  957. } else if chartInfo.ChartType == utils.CHART_TYPE_SECTION_SCATTER {
  958. //解析配置内容
  959. var tmpExtraConfig SectionScatterReq
  960. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &tmpExtraConfig)
  961. if err != nil {
  962. errmsg = "获取截面散点图配置信息失败 json.Unmarshal:Err:" + err.Error()
  963. return
  964. }
  965. //找到指标信息,并替换
  966. if len(tmpExtraConfig.SeriesList) > 0 {
  967. for k1, series := range tmpExtraConfig.SeriesList {
  968. for k2, item := range series.EdbInfoList {
  969. if item.XEdbInfoId == oldEdbInfo.EdbInfoId {
  970. tmpExtraConfig.SeriesList[k1].EdbInfoList[k2].XEdbInfoId = newEdbInfo.EdbInfoId
  971. }
  972. if item.YEdbInfoId == oldEdbInfo.EdbInfoId {
  973. tmpExtraConfig.SeriesList[k1].EdbInfoList[k2].YEdbInfoId = newEdbInfo.EdbInfoId
  974. }
  975. }
  976. }
  977. }
  978. // 重新序列化
  979. sectionScatterConfigJson, e := json.Marshal(tmpExtraConfig)
  980. if e != nil {
  981. err = e
  982. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  983. return
  984. }
  985. sectionScatterConfigStr := string(sectionScatterConfigJson)
  986. // 更新图表配置信息
  987. chartInfo.ExtraConfig = sectionScatterConfigStr
  988. updateStr = append(updateStr, "ExtraConfig")
  989. } else if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  990. //解析配置内容
  991. var tmpExtraConfig ChartSectionExtraConf
  992. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &tmpExtraConfig)
  993. if err != nil {
  994. errmsg = "获取截面散点图配置信息失败 json.Unmarshal:Err:" + err.Error()
  995. return
  996. }
  997. //找到指标信息,并替换
  998. if len(tmpExtraConfig.DateConfList) > 0 {
  999. for k1, item := range tmpExtraConfig.DateConfList {
  1000. if item.EdbInfoId > 0 && item.EdbInfoId == oldEdbInfo.EdbInfoId {
  1001. tmpExtraConfig.DateConfList[k1].EdbInfoId = newEdbInfo.EdbInfoId
  1002. }
  1003. }
  1004. }
  1005. // 重新序列化
  1006. configJson, e := json.Marshal(tmpExtraConfig)
  1007. if e != nil {
  1008. err = e
  1009. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  1010. return
  1011. }
  1012. configStr := string(configJson)
  1013. // 更新图表配置信息
  1014. chartInfo.ExtraConfig = configStr
  1015. updateStr = append(updateStr, "ExtraConfig")
  1016. }
  1017. }
  1018. if len(updateStr) > 0 {
  1019. updateStr = append(updateStr, "ModifyTime")
  1020. chartInfo.ModifyTime = time.Now()
  1021. err = chartInfo.Update(updateStr)
  1022. if err != nil {
  1023. errmsg = "更新图表信息失败:chartInfo.Update Err: " + err.Error()
  1024. return
  1025. }
  1026. }
  1027. }
  1028. }
  1029. // 查询我的所有图表信息
  1030. if len(chartEdbMappingList) > 0 {
  1031. chartInfoIdList := make([]string, 0)
  1032. for _, mv := range chartEdbMappingList {
  1033. //获取图表所有指标信息
  1034. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  1035. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  1036. err = to.Raw(csql, mv.ChartInfoId).Scan(&chartEdbList).Error
  1037. if err != nil {
  1038. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  1039. return
  1040. }
  1041. // 查询图表信息
  1042. var minData, maxData float64
  1043. minData = newEdbInfo.MinValue
  1044. maxData = newEdbInfo.MaxValue
  1045. for _, cv := range chartEdbList {
  1046. if mv.IsAxis == cv.IsAxis {
  1047. if minData > cv.MinData {
  1048. minData = cv.MinData
  1049. }
  1050. if maxData < cv.MaxData {
  1051. maxData = cv.MaxData
  1052. }
  1053. }
  1054. }
  1055. //修改图表关联指标
  1056. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  1057. err = to.Exec(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Error
  1058. if err != nil {
  1059. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  1060. return
  1061. }
  1062. rsql = ` UPDATE chart_series_edb_mapping SET edb_info_id=? WHERE edb_info_id=? and chart_info_id=?`
  1063. err = to.Exec(rsql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoType, mv.ChartInfoId).Error
  1064. if err != nil {
  1065. errmsg = "更新图库系列指标信息失败:Err:" + err.Error()
  1066. return
  1067. }
  1068. chartInfoIdList = append(chartInfoIdList, fmt.Sprint(mv.ChartInfoId))
  1069. }
  1070. logMsg += `涉及到的图表id:` + strings.Join(chartInfoIdList, ",") + ";"
  1071. }
  1072. }
  1073. //替换计算指标
  1074. {
  1075. //获取所有包含的计算指标
  1076. mappingList := make([]*EdbInfoCalculateMapping, 0)
  1077. queryList := make([]*EdbInfoCalculateMapping, 0)
  1078. msql := ` SELECT a.* FROM edb_info_calculate_mapping AS a
  1079. JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  1080. WHERE b.sys_user_id = ? AND a.from_edb_info_id = ?`
  1081. err = to.Raw(msql, userId, oldEdbInfo.EdbInfoId).Find(&queryList).Error
  1082. if err != nil {
  1083. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  1084. return
  1085. }
  1086. exists := make(map[int]bool)
  1087. for _, v := range queryList {
  1088. if exists[v.EdbInfoId] {
  1089. continue
  1090. }
  1091. exists[v.EdbInfoId] = true
  1092. mappingList = append(mappingList, v)
  1093. }
  1094. replaceCalculateTotal = len(mappingList)
  1095. //计算指标
  1096. if len(mappingList) > 0 {
  1097. edbInfoIdList := make([]string, 0)
  1098. for _, mv := range mappingList {
  1099. //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
  1100. if mv.EdbInfoId == newEdbInfo.EdbInfoId {
  1101. continue
  1102. }
  1103. //替换原指标
  1104. 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=? `
  1105. err = to.Exec(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Error
  1106. if err != nil {
  1107. return
  1108. }
  1109. relationEdbInfoIdList = append(relationEdbInfoIdList, mv.EdbInfoId)
  1110. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(mv.EdbInfoId))
  1111. }
  1112. logMsg += `涉及到的指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1113. }
  1114. }
  1115. // 替换预测指标中的来源指标
  1116. {
  1117. //获取所有包含的计算指标
  1118. predictEdbConfList := make([]*PredictEdbConf, 0)
  1119. queryList := make([]*PredictEdbConf, 0)
  1120. msql := ` SELECT a.* FROM predict_edb_conf AS a
  1121. JOIN edb_info AS b on a.predict_edb_info_id = b.edb_info_id
  1122. WHERE source_edb_info_id = ?`
  1123. err = to.Raw(msql, oldEdbInfo.EdbInfoId).Scan(&predictEdbConfList).Error
  1124. if err != nil {
  1125. errmsg = "获取关联的预测指标信息失败:Err:" + err.Error()
  1126. return
  1127. }
  1128. exists := make(map[int]bool)
  1129. for _, v := range queryList {
  1130. if exists[v.PredictEdbInfoId] {
  1131. continue
  1132. }
  1133. exists[v.PredictEdbInfoId] = true
  1134. predictEdbConfList = append(predictEdbConfList, v)
  1135. //替换预测指标的来源指标
  1136. msql = `UPDATE predict_edb_conf SET source_edb_info_id=? WHERE config_id=? `
  1137. err = to.Exec(msql, newEdbInfo.EdbInfoId, v.ConfigId).Error
  1138. if err != nil {
  1139. return
  1140. }
  1141. }
  1142. replaceCalculateTotal += len(predictEdbConfList)
  1143. if len(predictEdbConfList) > 0 {
  1144. edbInfoIdList := make([]string, 0)
  1145. for _, v := range predictEdbConfList {
  1146. relationEdbInfoIdList = append(relationEdbInfoIdList, v.PredictEdbInfoId)
  1147. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(v.PredictEdbInfoId))
  1148. }
  1149. logMsg += `涉及到的预测指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1150. }
  1151. }
  1152. return
  1153. }
  1154. type EdbInfoView struct {
  1155. EdbInfoId int `orm:"column(edb_info_id);pk" gorm:"primaryKey" `
  1156. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  1157. SourceName string `description:"来源名称"`
  1158. Source int `description:"来源id"`
  1159. EdbCode string `description:"指标编码"`
  1160. EdbName string `description:"指标名称"`
  1161. EdbNameSource string `description:"指标名称来源"`
  1162. Frequency string `description:"频率"`
  1163. Unit string `description:"单位"`
  1164. StartDate string `description:"起始日期"`
  1165. EndDate string `description:"终止日期"`
  1166. ClassifyId int `description:"分类id"`
  1167. SysUserId int
  1168. SysUserRealName string
  1169. UniqueCode string `description:"指标唯一编码"`
  1170. CreateTime string
  1171. ModifyTime string
  1172. MinValue float64 `description:"指标最小值"`
  1173. MaxValue float64 `description:"指标最大值"`
  1174. CalculateFormula string `description:"计算公式"`
  1175. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  1176. Sort int `description:"排序字段"`
  1177. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  1178. LatestDate string `description:"数据最新日期(实际日期)"`
  1179. LatestValue float64 `description:"数据最新值(实际值)"`
  1180. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  1181. SubSourceName string `description:"子数据来源名称"`
  1182. IndicatorCode string `description:"指标代码"`
  1183. StockCode string `description:"证券代码"`
  1184. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  1185. }
  1186. // EdbInfoFilterDataResp 搜索指标列表数据返回
  1187. type EdbInfoFilterDataResp struct {
  1188. Paging *paging.PagingItem
  1189. List []*EdbInfoList
  1190. }
  1191. // GetEdbInfoFilterList 获取指指标列表数据接口
  1192. func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
  1193. o := global.DmSQL["data"]
  1194. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  1195. err = o.Raw(sql, pars...).Scan(&total).Error
  1196. if err != nil {
  1197. return
  1198. }
  1199. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  1200. pars = append(pars, startSize, pageSize)
  1201. err = o.Raw(sql, pars...).Scan(&list).Error
  1202. return
  1203. }
  1204. // EdbInfoEs 写入es的指标数据结构
  1205. type EdbInfoEs struct {
  1206. EdbInfoId int `orm:"column(edb_info_id);pk" gorm:"primaryKey" `
  1207. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  1208. SourceName string `description:"来源名称"`
  1209. Source int `description:"来源id"`
  1210. EdbCode string `description:"指标编码"`
  1211. EdbNameEn string `description:"英文指标名称"`
  1212. EdbName string `description:"指标名称"`
  1213. Frequency string `description:"频率"`
  1214. FrequencyEn string `description:"英文频率"`
  1215. Unit string `description:"单位"`
  1216. UnitEn string `description:"英文单位"`
  1217. StartDate string `description:"起始日期"`
  1218. EndDate string `description:"终止日期"`
  1219. LatestDate string `description:"数据最新日期(实际日期)"`
  1220. LatestValue float64 `description:"数据最新值(实际值)"`
  1221. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  1222. ClassifyId int `description:"分类id"`
  1223. UniqueCode string `description:"指标唯一编码"`
  1224. SysUserId int `description:"创建人id"`
  1225. SysUserRealName string `description:"创建人姓名"`
  1226. ModifyTime string `description:"最新修改时间"`
  1227. CreateTime string `description:"创建时间"`
  1228. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  1229. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  1230. ChartImage string `description:"图表图片"`
  1231. RuleType int `description:"预测规则,1:最新,2:固定值"`
  1232. FixedValue float64 `description:"固定值"`
  1233. IsEnEdb bool `description:"是否展示英文标识"`
  1234. DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
  1235. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  1236. SubSourceName string `description:"子数据来源名称"`
  1237. IndicatorCode string `description:"指标代码"`
  1238. StockCode string `description:"证券代码"`
  1239. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  1240. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  1241. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  1242. IsSupplierStop int `description:"是否供应商停更:1:停更,0:未停更"`
  1243. CollectClassifyIdList []int `description:"所属收藏分类id列表" gorm:"-"`
  1244. SharedUserIdList []int `description:"共享用户id列表" gorm:"-"`
  1245. PublicStatus int `description:"公开状态;0:未公开;1:审批中;2:已驳回;3:已公开"`
  1246. EdbPublicClassifyId int `description:"指标公开分类id"`
  1247. PublicTime *time.Time `description:"设置公开的时间"`
  1248. }
  1249. // GetEsEdbInfo 获取指指标列表数据接口
  1250. func GetEsEdbInfo(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoEs, err error) {
  1251. o := global.DmSQL["data"]
  1252. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  1253. err = o.Raw(sql, pars...).Scan(&total).Error
  1254. if err != nil {
  1255. return
  1256. }
  1257. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  1258. pars = append(pars, startSize, pageSize)
  1259. err = o.Raw(sql, pars...).Scan(&list).Error
  1260. return
  1261. }
  1262. // SetEdbInfoImageReq 设置指标图片
  1263. type SetEdbInfoImageReq struct {
  1264. EdbInfoId int `description:"指标ID"`
  1265. ImageUrl string `description:"指标图片地址"`
  1266. }
  1267. // GetNextEdbInfoByCondition 根据条件获取下一个指标
  1268. func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
  1269. o := global.DmSQL["data"]
  1270. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1271. if condition != "" {
  1272. sql += condition
  1273. }
  1274. sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
  1275. err = o.Raw(sql, pars...).First(&item).Error
  1276. return
  1277. }
  1278. // GetNextEdbInfo 根据分类id获取下一个 指标
  1279. func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
  1280. o := global.DmSQL["data"]
  1281. sql := ` SELECT b.* FROM edb_classify AS a
  1282. INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
  1283. WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
  1284. AND a.classify_type=? AND b.edb_info_type=?
  1285. ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
  1286. LIMIT 1 `
  1287. err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).Scan(&item).Error
  1288. return
  1289. }
  1290. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  1291. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  1292. num := len(edbCodeList)
  1293. if num <= 0 {
  1294. return
  1295. }
  1296. o := global.DmSQL["data"]
  1297. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  1298. err = o.Raw(sql, source, edbCodeList).Scan(&items).Error
  1299. return
  1300. }
  1301. // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
  1302. func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
  1303. o := global.DmSQL["data"]
  1304. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  1305. if condition != "" {
  1306. sql += condition
  1307. }
  1308. err = o.Raw(sql, pars...).Scan(&count).Error
  1309. return
  1310. }
  1311. // GetEdbInfoListByCondition 根据条件获取指标列表数据
  1312. func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int, orderDesc string) (items []*EdbInfoList, err error) {
  1313. o := global.DmSQL["data"]
  1314. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1315. if condition != "" {
  1316. sql += condition
  1317. }
  1318. if orderDesc == `` {
  1319. orderDesc = ` ASC `
  1320. }
  1321. sql += ` ORDER BY edb_info_id `
  1322. sql += orderDesc
  1323. sql += ` LIMIT ?,? `
  1324. pars = append(pars, startSize)
  1325. pars = append(pars, pageSize)
  1326. err = o.Raw(sql, pars...).Scan(&items).Error
  1327. return
  1328. }
  1329. // GetEdbInfoListByEdbInfoId
  1330. // @Description: 根据指标id列表获取列表信息
  1331. // @param edbIdList
  1332. // @return items
  1333. // @return err
  1334. func GetEdbInfoListByEdbInfoId(edbIdList []int) (items []*EdbInfo, err error) {
  1335. num := len(edbIdList)
  1336. if num <= 0 {
  1337. return
  1338. }
  1339. o := global.DmSQL["data"]
  1340. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1341. err = o.Raw(sql, edbIdList).Scan(&items).Error
  1342. return
  1343. }
  1344. // GetEdbInfoListByUserId
  1345. // @Description: 根据指标id列表获取列表信息
  1346. // @param userIdList []int
  1347. // @param edbInfoType int
  1348. // @return items
  1349. // @return err
  1350. func GetEdbInfoListByUserId(userIdList []int, edbInfoType int) (items []*EdbInfo, err error) {
  1351. num := len(userIdList)
  1352. if num <= 0 {
  1353. return
  1354. }
  1355. o := global.DmSQL["data"]
  1356. sql := ` SELECT * FROM edb_info WHERE edb_info_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  1357. err = o.Raw(sql, edbInfoType, userIdList).Scan(&items).Error
  1358. return
  1359. }
  1360. // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
  1361. func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1362. num := len(edbIdList)
  1363. if num <= 0 {
  1364. return
  1365. }
  1366. o := global.DmSQL["data"]
  1367. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1368. err = o.Exec(sql, userId, userName, edbIdList).Error
  1369. return
  1370. }
  1371. // ModifyEdbInfoUserIdByOldUserId
  1372. // @Description: 根据旧用户id修改新用户id
  1373. // @author: Roc
  1374. // @datetime 2024-03-25 19:17:09
  1375. // @param edbIdList []string
  1376. // @param userId int
  1377. // @param userName string
  1378. // @return err error
  1379. func ModifyEdbInfoUserIdByOldUserId(oldUserIdList []int, edbInfoType, userId int, userName string) (err error) {
  1380. num := len(oldUserIdList)
  1381. if num <= 0 {
  1382. return
  1383. }
  1384. o := global.DmSQL["data"]
  1385. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) and edb_info_type = ?`
  1386. err = o.Exec(sql, userId, userName, oldUserIdList, edbInfoType).Error
  1387. return
  1388. }
  1389. // MoveEdbChartList 图表/指标信息
  1390. type MoveEdbChartList struct {
  1391. DataId string `description:"指标/图表唯一id"`
  1392. Code string `description:"指标code"`
  1393. Name string `description:"指标/图表名称"`
  1394. NameEn string `description:"指标/图表名称"`
  1395. ClassifyName string `description:"分类名称"`
  1396. ClassifyNameEn string `description:"分类名称"`
  1397. CreateUserId int `description:"创建人id"`
  1398. CreateUserName string `description:"创建人名称"`
  1399. }
  1400. // MoveEdbChartListResp 更改创建人返回数据
  1401. type MoveEdbChartListResp struct {
  1402. Paging *paging.PagingItem
  1403. List []MoveEdbChartList
  1404. }
  1405. type EdbChartClassifyResp struct {
  1406. List []*EdbChartClassify
  1407. }
  1408. type EdbChartClassify struct {
  1409. ClassifyId int
  1410. ClassifyName string
  1411. ParentId int
  1412. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  1413. Child []*EdbChartClassify
  1414. }
  1415. // EdbInfoDataSeasonalResp 指标季节性数据返回
  1416. type EdbInfoDataSeasonalResp struct {
  1417. EdbInfo *EdbInfo
  1418. DataList interface{}
  1419. }
  1420. // SetEdbDataInsertConfigReq 设置插入规则的请求
  1421. type SetEdbDataInsertConfigReq struct {
  1422. EdbInfoId int `description:"指标ID"`
  1423. Date string `description:"日期"`
  1424. Value string `description:"值"`
  1425. }
  1426. // GetEdbInfoByClassifyId 用于分类展示
  1427. func GetEdbInfoByClassifyId(classifyId, edbInfoType, edbType, adminId int, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList []int) (items []*EdbClassifyItems, err error) {
  1428. o := global.DmSQL["data"]
  1429. 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 = ?`
  1430. pars := []interface{}{classifyId, edbInfoType}
  1431. // 如果筛选了指标类型,那么就根据条件来
  1432. if edbType > 0 {
  1433. sql += ` AND edb_type = ? `
  1434. pars = append(pars, edbType)
  1435. }
  1436. // 如果筛选了用户id
  1437. if adminId > 0 {
  1438. sql += ` AND sys_user_id = ? `
  1439. pars = append(pars, adminId)
  1440. }
  1441. noPermissionEdbInfoIdNum := len(noPermissionEdbInfoIdList)
  1442. if noPermissionEdbInfoIdNum > 0 {
  1443. sql += ` AND edb_info_id NOT IN (` + utils.GetOrmInReplace(noPermissionEdbInfoIdNum) + `) `
  1444. pars = append(pars, noPermissionEdbInfoIdList)
  1445. }
  1446. noPermissionClassifyInfoIdNum := len(noPermissionEdbClassifyIdList)
  1447. if noPermissionClassifyInfoIdNum > 0 {
  1448. sql += ` AND classify_id NOT IN (` + utils.GetOrmInReplace(noPermissionClassifyInfoIdNum) + `) `
  1449. pars = append(pars, noPermissionEdbClassifyIdList)
  1450. }
  1451. sql += ` order by sort asc,edb_info_id asc `
  1452. err = o.Raw(sql, pars...).Scan(&items).Error
  1453. return
  1454. }
  1455. type EdbAndClassify struct {
  1456. EdbInfoId int `description:"指标id"`
  1457. EdbCode string `description:"指标code"`
  1458. UniqueCode string `description:"唯一code"`
  1459. EdbName string `description:"指标名称"`
  1460. ClassifyId int `description:"所属分类id"`
  1461. FirstClassifyId int `description:"第1级的分类id"`
  1462. FirstClassifyName string `description:"第1级的分类名称"`
  1463. SecondClassifyId int `description:"第2级的分类id"`
  1464. SecondClassifyName string `description:"第2级的分类名称"`
  1465. }
  1466. // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
  1467. func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
  1468. num := len(edbIdList)
  1469. if num <= 0 {
  1470. return
  1471. }
  1472. o := global.DmSQL["data"]
  1473. sql := ` SELECT a.edb_info_id,a.edb_code,a.unique_code,a.edb_name,a.classify_id FROM edb_info AS a
  1474. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1475. err = o.Raw(sql, edbIdList).Scan(&items).Error
  1476. return
  1477. }
  1478. // TraceEdbInfoResp 指标追溯数据返回
  1479. type TraceEdbInfoResp struct {
  1480. EdbInfoId int `description:"指标id"`
  1481. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  1482. EdbName string `description:"指标名称"`
  1483. EdbNameEn string `description:"指标英文名称"`
  1484. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  1485. RuleTitle string `description:"指标规则"`
  1486. RuleTitleEn string `description:"指标规则(英文)"`
  1487. UniqueCode string `description:"唯一编码"`
  1488. ClassifyId int `description:"分类ID"`
  1489. Child []TraceEdbInfoResp `description:"下级来源"`
  1490. IsStop int8 `description:"是否终止"`
  1491. EdbInfo *EdbInfo `description:"指标信息" json:"-"`
  1492. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  1493. }
  1494. // BeforeAndAfterDateDataResp 前后几期数据
  1495. type BeforeAndAfterDateDataResp struct {
  1496. List []*EdbDataList `description:"list"`
  1497. Date string `description:"实际日期"`
  1498. ShowValue string `description:"展示值"`
  1499. }
  1500. // GetEdbInfoAdminList
  1501. func GetEdbInfoAdminList(edbType int) (list []int, err error) {
  1502. o := global.DmSQL["data"]
  1503. sql := ` SELECT sys_user_id FROM edb_info WHERE edb_info_type = ? GROUP BY sys_user_id `
  1504. err = o.Raw(sql, edbType).Scan(&list).Error
  1505. return
  1506. }
  1507. type EdbInfoExistCheckResp struct {
  1508. IndexExist bool `description:"指标是否存在:true,存在,false:不存在"`
  1509. ExistStockCodeArr []string `description:"存在的证券代码"`
  1510. ExistEdbCodeArr []string `description:"存在的指标代码"`
  1511. ExistClassifyId []int `description:"存在的分类id"`
  1512. ExistIndexId []int `description:"存在的指标id"`
  1513. ExistEdbInfo []*EdbInfo `description:"存在的指标"`
  1514. }
  1515. type BatchAddEdbInfoReq struct {
  1516. BatchList []*BatchAddEdbInfo
  1517. }
  1518. type BatchAddEdbInfo struct {
  1519. Source int `description:"来源id"`
  1520. EdbName string `description:"指标名称"`
  1521. Frequency string `description:"频率"`
  1522. Unit string `description:"单位"`
  1523. ClassifyId int `description:"分类id"`
  1524. StockCode string `description:"证券代码"`
  1525. EdbCode string `description:"指标编码"`
  1526. ApiExtraPars string `description:"API额外参数"`
  1527. }
  1528. func GetEdbInfoWsdMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  1529. o := global.DmSQL["data"]
  1530. sql := ``
  1531. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  1532. 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=? `
  1533. sql = fmt.Sprintf(sql, tableName)
  1534. err = o.Raw(sql, edbCode).Scan(&item).Error
  1535. if err != nil {
  1536. return
  1537. }
  1538. var latest_value float64
  1539. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  1540. sql = fmt.Sprintf(sql, tableName)
  1541. err = o.Raw(sql, edbCode).Scan(&latest_value).Error
  1542. item.LatestValue = latest_value
  1543. return
  1544. }
  1545. // GetEdbInfoByNames 根据名称获取普通的指标
  1546. func GetEdbInfoByNames(edbNames string) (items []*EdbInfoList, err error) {
  1547. o := global.DmSQL["data"]
  1548. sql := ` SELECT * FROM edb_info WHERE edb_name IN (` + edbNames + `) AND edb_info_type = 0 `
  1549. err = o.Raw(sql).Scan(&items).Error
  1550. return
  1551. }
  1552. // BaseRefreshEdbInfo
  1553. // @Description: 刷新配置的基础指标信息结构体
  1554. type BaseRefreshEdbInfo struct {
  1555. EdbInfoId int
  1556. ClassifyId int `description:"钢联化工指标分类id"`
  1557. IndexCode string `description:"指标编码"`
  1558. IndexName string `description:"指标名称"`
  1559. EndDate string `description:"最新日期"`
  1560. EndValue string `description:"最新值"`
  1561. SysUserId int `description:"创建人id"`
  1562. SysUserRealName string `description:"创建人姓名"`
  1563. Frequency string `description:"频度"`
  1564. IsStop int `description:"是否停更:1:停更,0:未停更"`
  1565. TerminalCode string `description:"终端编码"`
  1566. RefreshTime string `description:"刷新时间"`
  1567. }
  1568. // RefreshBaseEdbInfoResp
  1569. // @Description: 刷新数据源的数据返回
  1570. type RefreshBaseEdbInfoResp struct {
  1571. Paging *paging.PagingItem
  1572. List []*BaseRefreshEdbInfo
  1573. }
  1574. // GetEdbBaseInfoList
  1575. // @Description: 获取指标库数据列表
  1576. // @author: Roc
  1577. // @datetime 2024-01-10 14:28:22
  1578. // @param condition string
  1579. // @param pars []interface{}
  1580. // @param orderBy string
  1581. // @param startSize int
  1582. // @param pageSize int
  1583. // @return total int
  1584. // @return items []*BaseRefreshEdbInfo
  1585. // @return err error
  1586. func GetEdbBaseInfoList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRefreshEdbInfo, err error) {
  1587. o := global.DmSQL["data"]
  1588. // 数量汇总
  1589. totalSql := ` SELECT count(1) FROM edb_info WHERE 1=1 `
  1590. if condition != "" {
  1591. totalSql += condition
  1592. }
  1593. err = o.Raw(totalSql, pars...).Scan(&total).Error
  1594. if err != nil {
  1595. return
  1596. }
  1597. // 列表数据
  1598. 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 `
  1599. if condition != "" {
  1600. sql += condition
  1601. }
  1602. if orderBy != "" {
  1603. sql += ` ORDER BY ` + orderBy
  1604. } else {
  1605. sql += ` ORDER BY edb_info_id ASC `
  1606. }
  1607. sql += ` LIMIT ?,? `
  1608. pars = append(pars, startSize, pageSize)
  1609. err = o.Raw(sql, pars...).Scan(&items).Error
  1610. return
  1611. }
  1612. // ModifyEdbInfoUpdateStatus
  1613. // @Description: 修改指标库停更状态
  1614. // @author: Roc
  1615. // @datetime 2024-01-08 16:23:31
  1616. // @param edbIdList []int
  1617. // @param indexCodeList []string
  1618. // @param isStop int
  1619. // @return err error
  1620. func ModifyEdbInfoUpdateStatus(edbIdList []int, isStop int, calculateEdbInfoIds []int) (err error) {
  1621. idNum := len(edbIdList)
  1622. if idNum <= 0 {
  1623. return
  1624. }
  1625. o := global.DmSQL["data"].Begin()
  1626. defer func() {
  1627. if err != nil {
  1628. _ = o.Rollback()
  1629. return
  1630. }
  1631. _ = o.Commit()
  1632. }()
  1633. // 更改指标的更新状态
  1634. sql := ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  1635. err = o.Exec(sql, isStop, edbIdList).Error
  1636. if err != nil {
  1637. return
  1638. }
  1639. if len(calculateEdbInfoIds) > 0 {
  1640. // 批量更新相关联的指标ID
  1641. sql = ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) `
  1642. err = o.Exec(sql, isStop, calculateEdbInfoIds).Error
  1643. if err != nil {
  1644. return
  1645. }
  1646. }
  1647. return
  1648. }
  1649. func GetEdbInfoCount(source int, edbCode string) (count int, err error) {
  1650. o := global.DmSQL["data"]
  1651. sql := ` SELECT COUNT(1) AS count FROM edb_info
  1652. WHERE source=? AND edb_code = ?`
  1653. err = o.Raw(sql, source, edbCode).Scan(&count).Error
  1654. return
  1655. }
  1656. // GetEdbInfoByNameArr 根据名称获取指标
  1657. func GetEdbInfoByNameArr(names []string, edbInfoType int) (items []*EdbInfo, err error) {
  1658. if len(names) == 0 {
  1659. return
  1660. }
  1661. o := global.DmSQL["data"]
  1662. 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)))
  1663. err = o.Raw(sql, names, edbInfoType).Scan(&items).Error
  1664. return
  1665. }
  1666. // GetEdbCodesBySource 根据来源获取指标编码
  1667. func GetEdbCodesBySource(source int) (items []*EdbInfo, err error) {
  1668. o := global.DmSQL["data"]
  1669. sql := ` SELECT edb_info_id, edb_code, unique_code, classify_id FROM edb_info WHERE source = ? `
  1670. err = o.Raw(sql, source).Scan(&items).Error
  1671. return
  1672. }
  1673. // GetAllEdbDataListData
  1674. // @Description: 获取指标的所有数据
  1675. // @author: Roc
  1676. // @datetime 2024-05-07 15:18:27
  1677. // @param edbInfoId int
  1678. // @param source int
  1679. // @param subSource int
  1680. // @param startDataTime string
  1681. // @return dataList []*EdbData
  1682. // @return err error
  1683. func GetAllEdbDataListData(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1684. // 自有数据需要额外处理(从mongo获取)
  1685. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  1686. return getAllDataByMongo(edbInfoId, source, subSource, startDataTime)
  1687. }
  1688. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  1689. return getThsHfAllDataByMongo(edbInfoId, source, subSource, startDataTime)
  1690. }
  1691. // 默认走mysql
  1692. return getAllDataByMysql(edbInfoId, source, subSource, startDataTime)
  1693. }
  1694. // getAllDataByMysql
  1695. // @Description: 从mysql获取指标的所有数据
  1696. // @author: Roc
  1697. // @datetime 2024-05-07 15:18:41
  1698. // @param edbInfoId int
  1699. // @param source int
  1700. // @param subSource int
  1701. // @param startDataTime string
  1702. // @return dataList []*data_manage.EdbData
  1703. // @return err error
  1704. func getAllDataByMysql(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1705. dataList = make([]*EdbData, 0)
  1706. var dataCondition string
  1707. var dataPars []interface{}
  1708. dataCondition += ` AND edb_info_id=? `
  1709. dataPars = append(dataPars, edbInfoId)
  1710. // 结束日期
  1711. if startDataTime != "" {
  1712. dataCondition += ` AND data_time > ? `
  1713. dataPars = append(dataPars, startDataTime)
  1714. }
  1715. // 获取列表数据
  1716. dataList, err = GetAllEdbDataListByCondition(dataCondition, dataPars, source, subSource)
  1717. if err != nil {
  1718. return
  1719. }
  1720. return
  1721. }
  1722. // getAllDataByMongo
  1723. // @Description: 从mongo获取指标的所有数据
  1724. // @author: Roc
  1725. // @datetime 2024-05-07 15:18:53
  1726. // @param edbInfoId int
  1727. // @param source int
  1728. // @param subSource int
  1729. // @param startDataTime string
  1730. // @return dataList []*data_manage.EdbData
  1731. // @return err error
  1732. func getAllDataByMongo(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1733. dataList = make([]*EdbData, 0)
  1734. mogDataObj := mgo.EdbDataBusiness{}
  1735. // 构建查询条件
  1736. queryConditions := bson.M{
  1737. "edb_info_id": edbInfoId,
  1738. }
  1739. // 开始日期
  1740. dateCondition, err := mgo.BuildDateCondition(startDataTime, "")
  1741. if err != nil {
  1742. return
  1743. }
  1744. if len(dateCondition) > 0 {
  1745. queryConditions["data_time"] = dateCondition
  1746. }
  1747. // 获取列表数据
  1748. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"-data_time"})
  1749. if tmpErr != nil {
  1750. err = tmpErr
  1751. return
  1752. }
  1753. for k, v := range tmpDataList {
  1754. dataList = append(dataList, &EdbData{
  1755. EdbDataId: k + 1,
  1756. EdbInfoId: v.EdbInfoId,
  1757. DataTime: v.DataTime.Format(utils.FormatDate),
  1758. Value: v.Value,
  1759. })
  1760. }
  1761. return
  1762. }
  1763. // GetEdbClassifyIdListBySource 获取普通指标的分类列表
  1764. func GetEdbClassifyIdListBySource(source int) (items []int, err error) {
  1765. o := global.DmSQL["data"]
  1766. sql := `SELECT distinct classify_id FROM edb_info WHERE source = ? and edb_info_type=0`
  1767. err = o.Raw(sql, source).Scan(&items).Error
  1768. return
  1769. }
  1770. // GetEdbInfoByClassifyIdAndSource 用于分类展示
  1771. func GetEdbInfoByClassifyIdAndSource(classifyId, edbInfoType, source int) (items []*EdbClassifyItems, err error) {
  1772. o := global.DmSQL["data"]
  1773. 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 =?`
  1774. pars := []interface{}{classifyId, edbInfoType, source}
  1775. sql += ` order by sort asc,edb_info_id asc `
  1776. err = o.Raw(sql, pars...).Scan(&items).Error
  1777. return
  1778. }
  1779. type ReplaceEdbInfoItem struct {
  1780. OldEdbInfo *EdbInfo
  1781. NewEdbInfo *EdbInfo
  1782. UserId int
  1783. }
  1784. type EdbInfoEditRecord struct {
  1785. EdbInfoId int `description:"指标ID"`
  1786. EdbName string `description:"指标名称"`
  1787. Frequency string `description:"频率"`
  1788. Unit string `description:"单位"`
  1789. ClassifyId int `description:"分类id"`
  1790. CalculateFormula string `description:"计算公式"`
  1791. OperateUserId int `description:"操作人id"`
  1792. OperateUserRealName string `description:"操作人姓名"`
  1793. }
  1794. func ModifyEdbInfoBaseTimeById(edbInfoId int, cTime time.Time) (err error) {
  1795. o := global.DmSQL["data"]
  1796. // 更新修改时间
  1797. sql := ` UPDATE edb_info SET base_modify_time = ? WHERE edb_info_id = ? `
  1798. err = o.Exec(sql, cTime, edbInfoId).Error
  1799. return
  1800. }
  1801. func GetEdbInfoFieldList(cond string, pars []interface{}, fields []string) (items []*EdbInfo, err error) {
  1802. field := " * "
  1803. if len(fields) > 0 {
  1804. field = strings.Join(fields, ",")
  1805. }
  1806. o := global.DmSQL["data"]
  1807. sql := fmt.Sprintf(`SELECT %s FROM edb_info WHERE 1=1 %s `, field, cond)
  1808. err = o.Raw(sql, pars...).Scan(&items).Error
  1809. return
  1810. }
  1811. // EdbInfoExtra 指标额外数据-extra字段
  1812. type EdbInfoExtra struct {
  1813. ApiExtraPars string `description:"API-额外参数(如同花顺日期序列)"`
  1814. }
  1815. func GetEdbInfoListByCond(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  1816. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1817. if condition != "" {
  1818. sql += condition
  1819. }
  1820. sql += ` ORDER BY create_time DESC `
  1821. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  1822. return
  1823. }
  1824. func GetThsHfEdbInfoMaxAndMinInfoByMongo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  1825. mogDataObj := new(mgo.EdbDataThsHf)
  1826. pipeline := []bson.M{
  1827. {"$match": bson.M{"edb_code": edbCode}},
  1828. {"$group": bson.M{
  1829. "_id": nil,
  1830. "min_date": bson.M{"$min": "$data_time"},
  1831. "max_date": bson.M{"$max": "$data_time"},
  1832. "min_value": bson.M{"$min": "$value"},
  1833. "max_value": bson.M{"$max": "$value"},
  1834. }},
  1835. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  1836. }
  1837. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  1838. if err != nil {
  1839. fmt.Println("EdbDataThsHf getEdbDataThsHfList Err:" + err.Error())
  1840. return
  1841. }
  1842. if !result.MaxDate.IsZero() {
  1843. whereQuery := bson.M{"edb_code": edbCode, "data_time": result.MaxDate}
  1844. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  1845. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  1846. if tmpErr != nil {
  1847. err = tmpErr
  1848. return
  1849. }
  1850. result.LatestValue = latestValue.Value
  1851. result.EndValue = latestValue.Value
  1852. }
  1853. item = &EdbInfoMaxAndMinInfo{
  1854. MinDate: result.MinDate.Format(utils.FormatDate),
  1855. MaxDate: result.MaxDate.Format(utils.FormatDate),
  1856. MinValue: result.MinValue,
  1857. MaxValue: result.MaxValue,
  1858. LatestValue: result.LatestValue,
  1859. }
  1860. return
  1861. }
  1862. func getThsHfAllDataByMongo(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1863. dataList = make([]*EdbData, 0)
  1864. mogDataObj := mgo.EdbDataThsHf{}
  1865. // 构建查询条件
  1866. queryConditions := bson.M{
  1867. "edb_info_id": edbInfoId,
  1868. }
  1869. // 开始日期
  1870. dateCondition, err := mgo.BuildDateCondition(startDataTime, "")
  1871. if err != nil {
  1872. return
  1873. }
  1874. if len(dateCondition) > 0 {
  1875. queryConditions["data_time"] = dateCondition
  1876. }
  1877. // 获取列表数据
  1878. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"-data_time"})
  1879. if tmpErr != nil {
  1880. err = tmpErr
  1881. return
  1882. }
  1883. for k, v := range tmpDataList {
  1884. dataList = append(dataList, &EdbData{
  1885. EdbDataId: k + 1,
  1886. EdbInfoId: v.EdbInfoId,
  1887. DataTime: v.DataTime.Format(utils.FormatDate),
  1888. Value: v.Value,
  1889. })
  1890. }
  1891. return
  1892. }
  1893. // GetEdbInfoByTypes 根据类型获取所有指标
  1894. func GetEdbInfoByTypes(edbInfoType, edbType int) (items []*EdbClassifyItems, err error) {
  1895. o := global.DmSQL["data"]
  1896. 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`
  1897. err = o.Raw(sql, edbInfoType, edbType).Scan(&items).Error
  1898. return
  1899. }
  1900. // GetIsJoinPermissionEdbInfoIdList
  1901. // @Description: 获取已经加入指标管控的指标id列表
  1902. // @param edbInfoType
  1903. // @param edbType
  1904. // @return list
  1905. // @return err
  1906. func GetIsJoinPermissionEdbInfoIdList(edbInfoType, edbType int) (list []int, err error) {
  1907. pars := make([]interface{}, 0)
  1908. sql := ` SELECT edb_info_id FROM edb_info WHERE 1=1 AND is_join_permission = 1 `
  1909. if edbInfoType >= 0 {
  1910. sql += ` AND edb_info_type = ? `
  1911. pars = append(pars, edbInfoType)
  1912. }
  1913. if edbInfoType > 0 {
  1914. sql += ` AND edb_type = ? `
  1915. pars = append(pars, edbType)
  1916. }
  1917. sql += ` ORDER BY create_time DESC `
  1918. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  1919. return
  1920. }
  1921. // UpdateClassifyIdByEdbInfoId
  1922. // @Description: 根据指标id更新所属分类id
  1923. // @author: Roc
  1924. // @datetime 2024-11-29 17:54:44
  1925. // @param edbInfoId int
  1926. // @param edbClassifyId int
  1927. // @return err error
  1928. func UpdateClassifyIdByEdbInfoId(edbInfoId, edbClassifyId int) (err error) {
  1929. sql := ` UPDATE edb_info SET classify_id = ? WHERE edb_info_id=? `
  1930. err = global.DmSQL["data"].Exec(sql, edbClassifyId, edbInfoId).Error
  1931. return
  1932. }
  1933. // UpdateClassifyIdByEdbInfoIdList
  1934. // @Description: 根据指标id列表更新所属分类id
  1935. // @author: Roc
  1936. // @datetime 2024-12-10 13:18:52
  1937. // @param edbInfoId int
  1938. // @param edbClassifyId int
  1939. // @return err error
  1940. func UpdateClassifyIdByEdbInfoIdList(edbInfoIdList []int, edbClassifyId int) (err error) {
  1941. if len(edbInfoIdList) <= 0 {
  1942. return
  1943. }
  1944. sql := ` UPDATE edb_info SET classify_id = ? WHERE edb_info_id in (?) `
  1945. err = global.DmSQL["data"].Exec(sql, edbClassifyId, edbInfoIdList).Error
  1946. return
  1947. }