edb_info.go 77 KB

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