edb_info.go 76 KB

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