edb_info.go 76 KB

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