edb_info.go 75 KB

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