edb_info.go 76 KB

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