edb_info.go 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  1. package data_manage
  2. import (
  3. "eta/eta_api/models/mgo"
  4. "eta/eta_api/services/alarm_msg"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "strings"
  11. "time"
  12. )
  13. type EdbInfo struct {
  14. EdbInfoId int `orm:"column(edb_info_id);pk"`
  15. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  16. SourceName string `description:"来源名称"`
  17. Source int `description:"来源id"`
  18. EdbCode string `description:"指标编码"`
  19. EdbName string `description:"指标名称"`
  20. EdbNameEn string `description:"英文指标名称"`
  21. EdbNameSource string `description:"指标名称来源"`
  22. Frequency string `description:"频率"`
  23. Unit string `description:"单位"`
  24. UnitEn string `description:"英文单位"`
  25. StartDate string `description:"起始日期"`
  26. EndDate string `description:"终止日期"`
  27. ClassifyId int `description:"分类id"`
  28. SysUserId int
  29. SysUserRealName string
  30. UniqueCode string `description:"指标唯一编码"`
  31. CreateTime time.Time
  32. ModifyTime time.Time
  33. BaseModifyTime time.Time
  34. MinValue float64 `description:"指标最小值"`
  35. MaxValue float64 `description:"指标最大值"`
  36. CalculateFormula string `description:"计算公式"`
  37. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  38. Sort int `description:"排序字段"`
  39. LatestDate string `description:"数据最新日期(实际日期)"`
  40. LatestValue float64 `description:"数据最新值(实际值)"`
  41. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  42. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  43. MoveFrequency string `description:"移动频度"`
  44. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  45. ServerUrl string `description:"服务器地址"`
  46. ChartImage string `description:"图表图片"`
  47. Calendar string `description:"公历/农历" orm:"default(公历);"`
  48. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  49. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  50. TerminalCode string `description:"终端编码,用于配置在机器上"`
  51. DataUpdateTime string `description:"最近一次数据发生变化的时间"`
  52. ErDataUpdateDate string `description:"本次更新,数据发生变化的最早日期"`
  53. SourceIndexName string `description:"数据源中的指标名称"`
  54. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  55. SubSourceName string `description:"子数据来源名称"`
  56. IndicatorCode string `description:"指标代码"`
  57. StockCode string `description:"证券代码"`
  58. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  59. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  60. Extra string `description:"指标额外配置"`
  61. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  62. }
  63. type EdbInfoFullClassify struct {
  64. *EdbInfo
  65. CorrelationStr string `description:"相关性系数字符串"`
  66. ClassifyList []*EdbClassifyIdItems
  67. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  68. }
  69. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  70. o := orm.NewOrmUsingDB("data")
  71. lastId, err = o.Insert(item)
  72. return
  73. }
  74. type BaseEdbNameItem struct {
  75. EdbInfoId int `description:"指标id"`
  76. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  77. SourceName string `description:"来源名称"`
  78. Source int `description:"来源id"`
  79. EdbCode string `description:"指标编码"`
  80. EdbName string `description:"指标名称"`
  81. EdbNameEn string `description:"指标英文名称"`
  82. Frequency string `description:"频率"`
  83. Unit string `description:"单位"`
  84. UnitEn string `description:"英文单位"`
  85. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  86. }
  87. type BaseEdbInfoResp struct {
  88. List []*BaseEdbNameItem
  89. }
  90. // GetEdbInfoAll 用于分类展示
  91. func GetEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  92. o := orm.NewOrmUsingDB("data")
  93. 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 = ? order by sort asc,edb_info_id asc`
  94. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  95. return
  96. }
  97. // GetPredictEdbInfoAll 用于分类展示(预测指标)
  98. func GetPredictEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  99. o := orm.NewOrmUsingDB("data")
  100. 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 FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
  101. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  102. return
  103. }
  104. // 用于分类展示
  105. func GetEdbInfoAllList() (items []*EdbInfo, err error) {
  106. o := orm.NewOrmUsingDB("data")
  107. sql := ` SELECT * FROM edb_info `
  108. _, err = o.Raw(sql).QueryRows(&items)
  109. return
  110. }
  111. // 指标检索数据
  112. type EdbInfoSearch struct {
  113. EdbCode string `description:"指标编码"`
  114. StartDate string `description:"起始日期"`
  115. EndDate string `description:"终止日期"`
  116. EdbName string `description:"指标名称"`
  117. Unit string `description:"单位"`
  118. Frequency string `description:"频率"`
  119. DataList []*EdbInfoSearchData
  120. StockList []*StockInfo `description:"时序数据"`
  121. }
  122. type StockInfo struct {
  123. StockCode string
  124. EdbCode string
  125. DataList []*EdbInfoSearchData
  126. }
  127. type EdbInfoSearchData struct {
  128. DataTime string `description:"数据日期"`
  129. Value float64 `description:"数据"`
  130. }
  131. type BaseIndexInfo struct {
  132. IndexName string `description:"数据日期"`
  133. Unit string `description:"单位"`
  134. Frequency string `description:"频率"`
  135. }
  136. type EdbInfoSearchResp struct {
  137. SearchItem *EdbInfoSearch `description:"指标分类"`
  138. Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
  139. ClassifyList []*EdbClassifySimplify
  140. StockSearchList []*EdbInfoSearch
  141. }
  142. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  143. o := orm.NewOrmUsingDB("data")
  144. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  145. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  146. return
  147. }
  148. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  149. o := orm.NewOrmUsingDB("data")
  150. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  151. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  152. return
  153. }
  154. // GetEdbInfoByUniqueCode
  155. // @Description: 根据uniqueCode获取指标详情
  156. // @author: Roc
  157. // @datetime 2024-01-18 13:40:01
  158. // @param uniqueCode string
  159. // @return item *EdbInfo
  160. // @return err error
  161. func GetEdbInfoByUniqueCode(uniqueCode string) (item *EdbInfo, err error) {
  162. o := orm.NewOrmUsingDB("data")
  163. sql := ` SELECT * FROM edb_info WHERE unique_code=? `
  164. err = o.Raw(sql, uniqueCode).QueryRow(&item)
  165. return
  166. }
  167. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  168. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  169. num := len(edbInfoIdList)
  170. if num <= 0 {
  171. return
  172. }
  173. o := orm.NewOrmUsingDB("data")
  174. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  175. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  176. return
  177. }
  178. type AddEdbInfoReq struct {
  179. Source int `description:"来源id"`
  180. EdbCode string `description:"指标编码"`
  181. EdbName string `description:"指标名称"`
  182. Frequency string `description:"频率"`
  183. Unit string `description:"单位"`
  184. ClassifyId int `description:"分类id"`
  185. StartDate string `description:"起始日期"`
  186. EndDate string `description:"终止日期"`
  187. }
  188. func DeleteEdbInfoAndData(edbInfoId, source, subSource int) (err error) {
  189. o := orm.NewOrmUsingDB("data")
  190. to, err := o.Begin()
  191. if err != nil {
  192. return
  193. }
  194. defer func() {
  195. if err != nil {
  196. _ = to.Rollback()
  197. } else {
  198. _ = to.Commit()
  199. }
  200. }()
  201. // 删除指标信息
  202. sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
  203. _, err = to.Raw(sql, edbInfoId).Exec()
  204. if err != nil {
  205. return
  206. }
  207. // 删除指标的明细数据
  208. if source == utils.DATA_SOURCE_BUSINESS {
  209. err = deleteAllEdbDataByMongo(to, edbInfoId, source, subSource)
  210. } else {
  211. err = deleteAllEdbDataByMysql(to, edbInfoId, source, subSource)
  212. }
  213. if err != nil {
  214. return
  215. }
  216. //calculateTableName := GetEdbInfoCalculateTableName(source)
  217. //if calculateTableName != "" {
  218. // sql = ` DELETE FROM %s WHERE edb_info_id=? `
  219. // sql = fmt.Sprintf(sql, calculateTableName)
  220. // _, err = to.Raw(sql, edbInfoId).Exec()
  221. // return
  222. //}
  223. //return
  224. // 删除计算指标的关系
  225. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  226. _, err = to.Raw(sql, edbInfoId).Exec()
  227. if err != nil {
  228. return
  229. }
  230. // 删除预测指标的配置
  231. sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id=? `
  232. _, err = to.Raw(sql, edbInfoId).Exec()
  233. if err != nil {
  234. return
  235. }
  236. // 删除跨品种分析标签绑定的指标
  237. sql = ` DELETE FROM chart_tag_variety WHERE edb_info_id=?`
  238. _, err = to.Raw(sql, edbInfoId).Exec()
  239. if err != nil {
  240. return
  241. }
  242. return
  243. }
  244. // deleteAllEdbDataByMysql
  245. // @Description: 删除指标的明细数据(mysql)
  246. // @author: Roc
  247. // @datetime 2024-05-09 13:31:54
  248. // @param to orm.TxOrmer
  249. // @param edbInfoId int
  250. // @param source int
  251. // @param subSource int
  252. // @return err error
  253. func deleteAllEdbDataByMysql(to orm.TxOrmer, edbInfoId, source, subSource int) (err error) {
  254. tableName := GetEdbDataTableName(source, subSource)
  255. if tableName == `` {
  256. return
  257. }
  258. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id=? `, tableName)
  259. _, err = to.Raw(sql, edbInfoId).Exec()
  260. return
  261. }
  262. // deleteAllEdbDataByMongo
  263. // @Description: 删除指标的明细数据(mongo)
  264. // @author: Roc
  265. // @datetime 2024-05-09 13:31:37
  266. // @param to orm.TxOrmer
  267. // @param edbInfoId int
  268. // @param source int
  269. // @param subSource int
  270. // @return err error
  271. func deleteAllEdbDataByMongo(to orm.TxOrmer, edbInfoId, source, subSource int) (err error) {
  272. mogDataObj := mgo.EdbDataBusiness{}
  273. err = mogDataObj.RemoveMany(bson.M{"edb_info_id": edbInfoId})
  274. return
  275. }
  276. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  277. o := orm.NewOrmUsingDB("data")
  278. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  279. if condition != "" {
  280. sql += condition
  281. }
  282. err = o.Raw(sql, pars).QueryRow(&count)
  283. return
  284. }
  285. type EditEdbInfoReq struct {
  286. EdbInfoId int `description:"指标ID"`
  287. EdbName string `description:"指标名称"`
  288. Frequency string `description:"频率"`
  289. Unit string `description:"单位"`
  290. ClassifyId int `description:"分类id"`
  291. CalculateFormula string `description:"计算公式"`
  292. }
  293. type EditEdbEnInfoReq struct {
  294. EdbInfoId int `description:"指标ID"`
  295. EdbNameEn string `description:"英文指标名称"`
  296. UnitEn string `description:"英文单位"`
  297. }
  298. // EditEdbBaseInfoReq
  299. // @Description: 修改指标基础信息请求参数
  300. type EditEdbBaseInfoReq struct {
  301. EdbInfoId int `description:"指标ID"`
  302. EdbName string `description:"指标名称"`
  303. Unit string `description:"指标单位"`
  304. }
  305. func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
  306. o := orm.NewOrmUsingDB("data")
  307. sql := ` UPDATE edb_info
  308. SET
  309. edb_name =?,
  310. edb_name_source =?,
  311. frequency = ?,
  312. unit = ?,
  313. classify_id = ?,
  314. modify_time = NOW()
  315. WHERE edb_info_id = ?`
  316. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.EdbInfoId).Exec()
  317. return
  318. }
  319. // ModifyEdbEnInfo 修改指标英文信息
  320. func ModifyEdbEnInfo(item *EditEdbEnInfoReq) (err error) {
  321. o := orm.NewOrmUsingDB("data")
  322. sql := ` UPDATE edb_info
  323. SET
  324. edb_name_en =?,
  325. unit_en = ?,
  326. modify_time = NOW()
  327. WHERE edb_info_id = ?`
  328. _, err = o.Raw(sql, item.EdbNameEn, item.UnitEn, item.EdbInfoId).Exec()
  329. return
  330. }
  331. type EdbInfoList struct {
  332. EdbInfoId int `orm:"column(edb_info_id);pk"`
  333. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  334. SourceName string `description:"来源名称"`
  335. Source int `description:"来源id"`
  336. EdbCode string `description:"指标编码"`
  337. EdbNameEn string `description:"英文指标名称"`
  338. EdbName string `description:"指标名称"`
  339. Frequency string `description:"频率"`
  340. FrequencyEn string `description:"英文频率"`
  341. Unit string `description:"单位"`
  342. UnitEn string `description:"英文单位"`
  343. StartDate string `description:"起始日期"`
  344. EndDate string `description:"终止日期"`
  345. LatestDate string `description:"数据最新日期(实际日期)"`
  346. LatestValue float64 `description:"数据最新值(实际值)"`
  347. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  348. ClassifyId int `description:"分类id"`
  349. UniqueCode string `description:"指标唯一编码"`
  350. SysUserId int `description:"创建人id"`
  351. SysUserRealName string `description:"创建人姓名"`
  352. ModifyTime string `description:"最新修改时间"`
  353. CreateTime string `description:"创建时间"`
  354. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  355. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  356. ChartImage string `description:"图表图片"`
  357. RuleType int `description:"预测规则,1:最新,2:固定值"`
  358. FixedValue float64 `description:"固定值"`
  359. DataList []*EdbData `description:"实际指标数据"`
  360. PredictDataList []*EdbData `description:"预测指标数据"`
  361. Button EdbClassifyItemsButton `description:"操作权限"`
  362. IsEnEdb bool `description:"是否展示英文标识"`
  363. DataInsertConfig EdbDataInsertConfigItem `description:"指标数据插入配置"`
  364. DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
  365. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  366. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  367. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  368. SubSourceName string `description:"子数据来源名称"`
  369. IndicatorCode string `description:"指标代码"`
  370. StockCode string `description:"证券代码"`
  371. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  372. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  373. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  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"`
  382. EdbInfoId int
  383. DataTime string
  384. Value float64
  385. }
  386. type EdbInfoListResp struct {
  387. Paging *paging.PagingItem
  388. Item *EdbInfoList
  389. ClassifyList []*EdbClassifyIdItems
  390. }
  391. func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  392. o := orm.NewOrmUsingDB("data")
  393. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  394. if condition != "" {
  395. sql += condition
  396. }
  397. err = o.Raw(sql, pars).QueryRow(&item)
  398. return
  399. }
  400. func GetEdbDataCountByCondition(condition string, pars []interface{}, source, subSource int) (count int, err error) {
  401. o := orm.NewOrmUsingDB("data")
  402. tableName := GetEdbDataTableName(source, subSource)
  403. sql := ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  404. sql = fmt.Sprintf(sql, tableName)
  405. if condition != "" {
  406. sql += condition
  407. }
  408. err = o.Raw(sql, pars).QueryRow(&count)
  409. return
  410. }
  411. func GetEdbDataListByCondition(condition string, pars []interface{}, source, subSource, pageSize, startSize int) (item []*EdbData, err error) {
  412. o := orm.NewOrmUsingDB("data")
  413. tableName := GetEdbDataTableName(source, subSource)
  414. sql := ` SELECT * FROM %s WHERE 1=1 `
  415. sql = fmt.Sprintf(sql, tableName)
  416. if condition != "" {
  417. sql += condition
  418. }
  419. sql += ` ORDER BY data_time DESC `
  420. sql += ` LIMIT ?,? `
  421. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  422. return
  423. }
  424. // GetAllEdbDataListByCondition 根据条件获取所有的数据
  425. func GetAllEdbDataListByCondition(condition string, pars []interface{}, source, subSource int) (item []*EdbData, err error) {
  426. o := orm.NewOrmUsingDB("data")
  427. tableName := GetEdbDataTableName(source, subSource)
  428. sql := ` SELECT * FROM %s WHERE 1=1 `
  429. sql = fmt.Sprintf(sql, tableName)
  430. if condition != "" {
  431. sql += condition
  432. }
  433. sql += ` ORDER BY data_time DESC `
  434. _, err = o.Raw(sql, pars).QueryRows(&item)
  435. return
  436. }
  437. func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
  438. o := orm.NewOrmUsingDB("data")
  439. sql := ` SELECT * FROM edb_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
  440. err = o.Raw(sql).QueryRow(&item)
  441. return
  442. }
  443. func ModifyEdbInfoModifyTime(edbInfoId int64) (err error) {
  444. o := orm.NewOrmUsingDB("data")
  445. sql := ` UPDATE edb_info SET modify_time = NOW() WHERE edb_info_id = ? `
  446. _, err = o.Raw(sql, edbInfoId).Exec()
  447. return
  448. }
  449. type MoveEdbInfoReq struct {
  450. EdbInfoId int `description:"指标ID"`
  451. PrevEdbInfoId int `description:"上一个指标ID"`
  452. NextEdbInfoId int `description:"下一个指标ID"`
  453. ClassifyId int `description:"分类id"`
  454. }
  455. func MoveEdbInfo(edbInfoId, classifyId int) (err error) {
  456. o := orm.NewOrmUsingDB("data")
  457. sql := ` UPDATE edb_info
  458. SET
  459. classify_id = ?
  460. WHERE edb_info_id = ?`
  461. _, err = o.Raw(sql, classifyId, edbInfoId).Exec()
  462. return
  463. }
  464. // GetEdbInfoByName 根据名称获取普通的指标
  465. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  466. o := orm.NewOrmUsingDB("data")
  467. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 0 `
  468. _, err = o.Raw(sql, edbName).QueryRows(&items)
  469. return
  470. }
  471. // GetPredictEdbInfoByName 根据指标名称获取预测指标
  472. func GetPredictEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  473. o := orm.NewOrmUsingDB("data")
  474. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 1 `
  475. _, err = o.Raw(sql, edbName).QueryRows(&items)
  476. return
  477. }
  478. func GetEdbInfoByEnName(edbNameEn string) (items []*EdbInfoList, err error) {
  479. o := orm.NewOrmUsingDB("data")
  480. sql := ` SELECT * FROM edb_info WHERE edb_name_en=? AND edb_info_type = 0 `
  481. _, err = o.Raw(sql, edbNameEn).QueryRows(&items)
  482. return
  483. }
  484. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  485. o := orm.NewOrmUsingDB("data")
  486. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  487. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  488. return
  489. }
  490. func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
  491. o := orm.NewOrmUsingDB("data")
  492. sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
  493. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  494. return
  495. }
  496. type ChartEdbInfo struct {
  497. EdbInfoId int `description:"指标id"`
  498. EdbName string `description:"指标名称"`
  499. SourceName string `json:"-"`
  500. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  501. }
  502. func EdbInfoSearchByKeyWord(keyword string) (searchList []*ChartEdbInfo, err error) {
  503. pars := make([]interface{}, 0)
  504. o := orm.NewOrmUsingDB("data")
  505. sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  506. if keyword != "" {
  507. sql += ` AND (edb_name LIKE ? OR edb_code LIKE ? ) `
  508. pars = append(pars, utils.GetLikeKeyword(keyword), utils.GetLikeKeyword(keyword))
  509. }
  510. sql += ` ORDER BY create_time DESC `
  511. _, err = o.Raw(sql, pars...).QueryRows(&searchList)
  512. return
  513. }
  514. type EdbInfoMaxAndMinInfo struct {
  515. MinDate string `description:"最小日期"`
  516. MaxDate string `description:"最大日期"`
  517. MinValue float64 `description:"最小值"`
  518. MaxValue float64 `description:"最大值"`
  519. LatestValue float64 `description:"最新值"`
  520. }
  521. // GetEdbInfoMaxAndMinInfo
  522. // @Description: 获取指标最大最小值
  523. // @author: Roc
  524. // @datetime 2024-05-07 15:34:00
  525. // @param source int
  526. // @param subSource int
  527. // @param edbCode string
  528. // @return item *EdbInfoMaxAndMinInfo
  529. // @return err error
  530. func GetEdbInfoMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  531. // 自有数据需要额外处理(从mongo获取)
  532. if source == utils.DATA_SOURCE_BUSINESS {
  533. return GetEdbInfoMaxAndMinInfoByMongo(source, subSource, edbCode)
  534. }
  535. // 默认走mysql
  536. return GetEdbInfoMaxAndMinInfoByMysql(source, subSource, edbCode)
  537. }
  538. // GetEdbInfoMaxAndMinInfoByMysql
  539. // @Description: 从mysql中获取指标最大最小值
  540. // @author: Roc
  541. // @datetime 2024-05-07 15:33:43
  542. // @param source int
  543. // @param subSource int
  544. // @param edbCode string
  545. // @return item *EdbInfoMaxAndMinInfo
  546. // @return err error
  547. func GetEdbInfoMaxAndMinInfoByMysql(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  548. o := orm.NewOrmUsingDB("data")
  549. sql := ``
  550. tableName := GetEdbDataTableName(source, subSource)
  551. 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=? `
  552. sql = fmt.Sprintf(sql, tableName)
  553. err = o.Raw(sql, edbCode).QueryRow(&item)
  554. if err != nil {
  555. return
  556. }
  557. var latest_value float64
  558. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  559. sql = fmt.Sprintf(sql, tableName)
  560. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  561. item.LatestValue = latest_value
  562. return
  563. }
  564. // GetEdbInfoMaxAndMinInfoByMongo
  565. // @Description: 从mongo中获取指标最大最小值
  566. // @author: Roc
  567. // @datetime 2024-05-07 15:33:53
  568. // @param source int
  569. // @param subSource int
  570. // @param edbCode string
  571. // @return item *EdbInfoMaxAndMinInfo
  572. // @return err error
  573. func GetEdbInfoMaxAndMinInfoByMongo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  574. mogDataObj := new(mgo.EdbDataBusiness)
  575. pipeline := []bson.M{
  576. {"$match": bson.M{"edb_code": edbCode}},
  577. {"$group": bson.M{
  578. "_id": nil,
  579. "min_date": bson.M{"$min": "$data_time"},
  580. "max_date": bson.M{"$max": "$data_time"},
  581. "min_value": bson.M{"$min": "$value"},
  582. "max_value": bson.M{"$max": "$value"},
  583. }},
  584. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  585. }
  586. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  587. if err != nil {
  588. fmt.Println("EdbDataBusiness getEdbDataBusinessList Err:" + err.Error())
  589. return
  590. }
  591. if !result.MaxDate.IsZero() {
  592. whereQuery := bson.M{"edb_code": edbCode, "data_time": result.MaxDate}
  593. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  594. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  595. if tmpErr != nil {
  596. err = tmpErr
  597. return
  598. }
  599. result.LatestValue = latestValue.Value
  600. result.EndValue = latestValue.Value
  601. }
  602. item = &EdbInfoMaxAndMinInfo{
  603. MinDate: result.MinDate.Format(utils.FormatDate),
  604. MaxDate: result.MaxDate.Format(utils.FormatDate),
  605. MinValue: result.MinValue,
  606. MaxValue: result.MaxValue,
  607. LatestValue: result.LatestValue,
  608. }
  609. return
  610. }
  611. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  612. o := orm.NewOrmUsingDB("data")
  613. 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=? `
  614. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  615. return
  616. }
  617. func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  618. o := orm.NewOrmUsingDB("data")
  619. sql := ` SELECT * FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  620. if condition != "" {
  621. sql += condition
  622. }
  623. sql += ` ORDER BY create_time DESC `
  624. _, err = o.Raw(sql, pars).QueryRows(&list)
  625. return
  626. }
  627. type AddEdbInfoResp struct {
  628. EdbInfoId int `description:"指标ID"`
  629. UniqueCode string `description:"指标唯一编码"`
  630. ClassifyId int `description:"分类id"`
  631. ExistEdbName []string `description:"重复指标名称"`
  632. }
  633. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  634. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  635. o := orm.NewOrmUsingDB("data")
  636. //calculateTableName := GetEdbInfoCalculateTableName(source)
  637. //if calculateTableName == "" {
  638. // err = errors.New("无效的表名")
  639. // return
  640. //}
  641. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  642. //sql = fmt.Sprintf(sql, calculateTableName)
  643. if condition != "" {
  644. sql += condition
  645. }
  646. _, err = o.Raw(sql, pars).QueryRows(&items)
  647. return
  648. }
  649. // GetEdbInfoAllCalculateByEdbInfoIdList 根据指标id集合 获取基础指标对应的所有计算指标
  650. func GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList []int) (list []*EdbInfo, err error) {
  651. num := len(edbInfoIdList)
  652. if num <= 0 {
  653. return
  654. }
  655. o := orm.NewOrmUsingDB("data")
  656. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  657. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  658. WHERE a.from_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  659. GROUP BY a.edb_info_id
  660. ORDER BY a.edb_info_id ASC `
  661. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  662. return
  663. }
  664. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  665. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  666. if err != nil && err.Error() != utils.ErrNoRow() {
  667. return
  668. }
  669. for _, item := range calculateList {
  670. if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
  671. continue
  672. }
  673. if item.EdbType == 1 {
  674. baseEdbInfoArr = append(baseEdbInfoArr, item)
  675. } else {
  676. calculateInfoArr = append(calculateInfoArr, item)
  677. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  678. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  679. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  680. }
  681. }
  682. return
  683. }
  684. func ModifyEdbInfoDataStatus(edbInfoId int64, source, subSource int, edbCode string) (err error) {
  685. o := orm.NewOrmUsingDB("data")
  686. sql := ``
  687. tableName := GetEdbDataTableName(source, subSource)
  688. sql = ` UPDATE %s SET edb_info_id=?,modify_time=NOW() WHERE edb_code=? `
  689. sql = fmt.Sprintf(sql, tableName)
  690. _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
  691. return
  692. }
  693. // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  694. func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
  695. o := orm.NewOrmUsingDB("data")
  696. sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
  697. err = o.Raw(sql, classifyId).QueryRow(&item)
  698. return
  699. }
  700. // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
  701. func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  702. o := orm.NewOrmUsingDB("data")
  703. sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=?`
  704. if prevEdbInfoId > 0 {
  705. sql += ` AND ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  706. } else {
  707. sql += ` AND ( sort > ? )`
  708. }
  709. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  710. return
  711. }
  712. // Update 更新指标基础信息
  713. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  714. o := orm.NewOrmUsingDB("data")
  715. _, err = o.Update(edbInfo, cols...)
  716. return
  717. }
  718. type EdbInfoDataResp struct {
  719. EdbInfo *EdbInfo
  720. DataList []*EdbDataList
  721. }
  722. type EdbInfoDataFullClassifyResp struct {
  723. EdbInfo *EdbInfoFullClassify
  724. DataList []*EdbDataList
  725. }
  726. type EdbInfoReplaceReq struct {
  727. OldEdbInfoId int `description:"原指标ID"`
  728. NewEdbInfoId int `description:"替换为指标ID"`
  729. }
  730. // EdbInfoReplaceV2 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  731. //func EdbInfoReplaceV2(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (mappingList,replaceChartTotal, replaceCalculateTotal int, err error) {
  732. // defer func() {
  733. // if err != nil {
  734. // fmt.Println("ERR:", err.Error())
  735. // }
  736. // }()
  737. //
  738. // // 替换关联表中的source_edb_info_id
  739. // mappingList, replaceChartTotal, replaceCalculateTotal, err := replaceChartEdb(oldEdbInfo, newEdbInfo)
  740. // if err != nil {
  741. // return
  742. // }
  743. // err = replaceRelationEdbInfo(mappingList, oldEdbInfo, newEdbInfo, sysAdminId, sysAdminRealName)
  744. // return
  745. //}
  746. // ReplaceChartEdb 替换图表中的指标
  747. func ReplaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (relationEdbInfoIdList []int, replaceChartTotal, replaceCalculateTotal int, err error) {
  748. var errmsg string
  749. relationEdbInfoIdList = make([]int, 0) // 关联的指标id
  750. logMsg := `` // 记录替换的日志
  751. o := orm.NewOrmUsingDB("data")
  752. to, err := o.Begin()
  753. if err != nil {
  754. return
  755. }
  756. defer func() {
  757. if err != nil {
  758. _ = to.Rollback()
  759. } else {
  760. _ = to.Commit()
  761. if logMsg != `` {
  762. utils.FileLog.Info(fmt.Sprintf("替换指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  763. }
  764. }
  765. if errmsg != "" {
  766. fmt.Println("errmsg:" + errmsg)
  767. }
  768. if err != nil && errmsg != "" {
  769. go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
  770. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  771. }
  772. }()
  773. //替换图表
  774. {
  775. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  776. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  777. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  778. if err != nil {
  779. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  780. return
  781. }
  782. replaceChartTotal = len(chartEdbMappingList)
  783. if len(chartEdbMappingList) > 0 {
  784. chartInfoIdList := make([]string, 0)
  785. for _, mv := range chartEdbMappingList {
  786. //获取图表所有指标信息
  787. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  788. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  789. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  790. if err != nil {
  791. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  792. return
  793. }
  794. var minData, maxData float64
  795. minData = newEdbInfo.MinValue
  796. maxData = newEdbInfo.MaxValue
  797. for _, cv := range chartEdbList {
  798. if mv.IsAxis == cv.IsAxis {
  799. if minData > cv.MinData {
  800. minData = cv.MinData
  801. }
  802. if maxData < cv.MaxData {
  803. maxData = cv.MaxData
  804. }
  805. }
  806. }
  807. //修改图表关联指标
  808. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  809. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  810. if err != nil {
  811. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  812. return
  813. }
  814. chartInfoIdList = append(chartInfoIdList, fmt.Sprint(mv.ChartInfoId))
  815. }
  816. logMsg += `涉及到的图表id:` + strings.Join(chartInfoIdList, ",") + ";"
  817. }
  818. }
  819. //替换计算指标
  820. {
  821. //获取所有包含的计算指标
  822. mappingList := make([]*EdbInfoCalculateMapping, 0)
  823. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  824. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  825. if err != nil {
  826. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  827. return
  828. }
  829. replaceCalculateTotal = len(mappingList)
  830. //计算指标
  831. if len(mappingList) > 0 {
  832. edbInfoIdList := make([]string, 0)
  833. for _, mv := range mappingList {
  834. //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
  835. if mv.EdbInfoId == newEdbInfo.EdbInfoId {
  836. continue
  837. }
  838. //替换原指标
  839. 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=? `
  840. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  841. if err != nil {
  842. return
  843. }
  844. relationEdbInfoIdList = append(relationEdbInfoIdList, mv.EdbInfoId)
  845. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(mv.EdbInfoId))
  846. }
  847. logMsg += `涉及到的指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  848. }
  849. }
  850. // 替换预测指标中的来源指标
  851. {
  852. //获取所有包含的计算指标
  853. predictEdbConfList := make([]*PredictEdbConf, 0)
  854. msql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? GROUP BY predict_edb_info_id `
  855. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&predictEdbConfList)
  856. if err != nil {
  857. errmsg = "获取关联的预测指标信息失败:Err:" + err.Error()
  858. return
  859. }
  860. replaceCalculateTotal += len(predictEdbConfList)
  861. //替换预测指标的来源指标
  862. msql = `UPDATE predict_edb_conf SET source_edb_info_id=? WHERE source_edb_info_id=? `
  863. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).Exec()
  864. if err != nil {
  865. return
  866. }
  867. if len(predictEdbConfList) > 0 {
  868. edbInfoIdList := make([]string, 0)
  869. for _, v := range predictEdbConfList {
  870. relationEdbInfoIdList = append(relationEdbInfoIdList, v.PredictEdbInfoId)
  871. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(v.PredictEdbInfoId))
  872. }
  873. logMsg += `涉及到的预测指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  874. }
  875. }
  876. return
  877. }
  878. type EdbInfoView struct {
  879. EdbInfoId int `orm:"column(edb_info_id);pk"`
  880. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  881. SourceName string `description:"来源名称"`
  882. Source int `description:"来源id"`
  883. EdbCode string `description:"指标编码"`
  884. EdbName string `description:"指标名称"`
  885. EdbNameSource string `description:"指标名称来源"`
  886. Frequency string `description:"频率"`
  887. Unit string `description:"单位"`
  888. StartDate string `description:"起始日期"`
  889. EndDate string `description:"终止日期"`
  890. ClassifyId int `description:"分类id"`
  891. SysUserId int
  892. SysUserRealName string
  893. UniqueCode string `description:"指标唯一编码"`
  894. CreateTime string
  895. ModifyTime string
  896. MinValue float64 `description:"指标最小值"`
  897. MaxValue float64 `description:"指标最大值"`
  898. CalculateFormula string `description:"计算公式"`
  899. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  900. Sort int `description:"排序字段"`
  901. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  902. LatestDate string `description:"数据最新日期(实际日期)"`
  903. LatestValue float64 `description:"数据最新值(实际值)"`
  904. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  905. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  906. SubSourceName string `description:"子数据来源名称"`
  907. IndicatorCode string `description:"指标代码"`
  908. StockCode string `description:"证券代码"`
  909. }
  910. // 获取指标的所有计算指标,以及计算指标所依赖计算指标
  911. func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  912. o := orm.NewOrmUsingDB("data")
  913. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  914. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  915. if err != nil {
  916. return
  917. }
  918. return
  919. }
  920. func GetAllCalculate(edbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  921. calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
  922. if err != nil && err.Error() != utils.ErrNoRow() {
  923. return
  924. }
  925. for _, item := range calculateList {
  926. _, ok := edbInfoMap[item.EdbInfoId]
  927. if !ok {
  928. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  929. mappingList = append(mappingList, item)
  930. newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId, edbInfoMap)
  931. mappingList = append(mappingList, newCalculateInfoArr...)
  932. }
  933. }
  934. return
  935. }
  936. // GetAllCalculateByEdbInfoIdV2 获取计算指标的所有所依赖的指标(基础指标+计算指标)
  937. func GetAllCalculateByEdbInfoIdV2(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  938. o := orm.NewOrmUsingDB("data")
  939. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE edb_info_id=? GROUP BY from_edb_info_id `
  940. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  941. if err != nil {
  942. return
  943. }
  944. return
  945. }
  946. // GetAllCalculateV2 获取所有的指标(如果下层指标包含了待替换指标,那么就不加入)
  947. func GetAllCalculateV2(oldEdbInfoId, replaceEdbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  948. calculateList, err := GetAllCalculateByEdbInfoId(oldEdbInfoId)
  949. if err != nil && err.Error() != utils.ErrNoRow() {
  950. return
  951. }
  952. tmpMappingList := make([]*EdbInfoCalculateMapping, 0)
  953. for _, item := range calculateList {
  954. //如果计算指标里面存在即将替换的指标id,那么退出当前循环,进入下一循环
  955. if item.EdbInfoId == replaceEdbInfoId {
  956. err = fmt.Errorf("指标重复1")
  957. return
  958. }
  959. //获取该指标的依赖指标列表,如果依赖指标列表中存在即将替换的指标,那么过滤该指标
  960. childCalculateList, tmpErr := GetAllCalculateByEdbInfoIdV2(item.EdbInfoId)
  961. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  962. err = tmpErr
  963. return
  964. }
  965. for _, childCalculate := range childCalculateList {
  966. if childCalculate.FromEdbInfoId == replaceEdbInfoId {
  967. err = fmt.Errorf("指标重复2")
  968. return
  969. }
  970. }
  971. _, ok := edbInfoMap[item.EdbInfoId]
  972. if !ok {
  973. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  974. newCalculateInfoArr, tmpErr := GetAllCalculateV2(item.EdbInfoId, replaceEdbInfoId, edbInfoMap)
  975. if tmpErr != nil {
  976. err = tmpErr
  977. continue
  978. } else {
  979. mappingList = append(mappingList, item)
  980. tmpMappingList = append(tmpMappingList, newCalculateInfoArr...)
  981. }
  982. }
  983. }
  984. mappingList = append(mappingList, tmpMappingList...)
  985. return
  986. }
  987. // EdbInfoFilterDataResp 搜索指标列表数据返回
  988. type EdbInfoFilterDataResp struct {
  989. Paging *paging.PagingItem
  990. List []*EdbInfoList
  991. }
  992. // GetEdbInfoFilterList 获取指指标列表数据接口
  993. func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
  994. o := orm.NewOrmUsingDB("data")
  995. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  996. err = o.Raw(sql, pars).QueryRow(&total)
  997. if err != nil {
  998. return
  999. }
  1000. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  1001. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  1002. return
  1003. }
  1004. // SetEdbInfoImageReq 设置指标图片
  1005. type SetEdbInfoImageReq struct {
  1006. EdbInfoId int `description:"指标ID"`
  1007. ImageUrl string `description:"指标图片地址"`
  1008. }
  1009. // GetNextEdbInfoByCondition 根据条件获取下一个指标
  1010. func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
  1011. o := orm.NewOrmUsingDB("data")
  1012. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1013. if condition != "" {
  1014. sql += condition
  1015. }
  1016. sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
  1017. err = o.Raw(sql, pars).QueryRow(&item)
  1018. return
  1019. }
  1020. // GetNextEdbInfo 根据分类id获取下一个 指标
  1021. func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
  1022. o := orm.NewOrmUsingDB("data")
  1023. sql := ` SELECT b.* FROM edb_classify AS a
  1024. INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
  1025. WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
  1026. AND a.classify_type=? AND b.edb_info_type=?
  1027. ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
  1028. LIMIT 1 `
  1029. err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).QueryRow(&item)
  1030. return
  1031. }
  1032. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  1033. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  1034. num := len(edbCodeList)
  1035. if num <= 0 {
  1036. return
  1037. }
  1038. o := orm.NewOrmUsingDB("data")
  1039. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  1040. _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
  1041. return
  1042. }
  1043. // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
  1044. func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
  1045. o := orm.NewOrmUsingDB("data")
  1046. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  1047. if condition != "" {
  1048. sql += condition
  1049. }
  1050. err = o.Raw(sql, pars).QueryRow(&count)
  1051. return
  1052. }
  1053. // GetEdbInfoListByCondition 根据条件获取指标列表数据
  1054. func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int, orderDesc string) (items []*EdbInfoList, err error) {
  1055. o := orm.NewOrmUsingDB("data")
  1056. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1057. if condition != "" {
  1058. sql += condition
  1059. }
  1060. if orderDesc == `` {
  1061. orderDesc = ` ASC `
  1062. }
  1063. sql += ` ORDER BY edb_info_id `
  1064. sql += orderDesc
  1065. sql += ` LIMIT ?,? `
  1066. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1067. return
  1068. }
  1069. // GetEdbInfoListV2ByCondition 根据条件获取指标列表数据(EdbInfo原始数据结构)
  1070. func GetEdbInfoListV2ByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfo, err error) {
  1071. o := orm.NewOrmUsingDB("data")
  1072. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1073. if condition != "" {
  1074. sql += condition
  1075. }
  1076. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  1077. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1078. return
  1079. }
  1080. // GetEdbInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
  1081. func GetEdbInfoListGroupByUserId(edbIdList []string) (items []*EdbInfo, err error) {
  1082. num := len(edbIdList)
  1083. if num <= 0 {
  1084. return
  1085. }
  1086. o := orm.NewOrmUsingDB("data")
  1087. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
  1088. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1089. return
  1090. }
  1091. // GetEdbInfoListByEdbInfoId
  1092. // @Description: 根据指标id列表获取列表信息
  1093. // @param edbIdList
  1094. // @return items
  1095. // @return err
  1096. func GetEdbInfoListByEdbInfoId(edbIdList []string) (items []*EdbInfo, err error) {
  1097. num := len(edbIdList)
  1098. if num <= 0 {
  1099. return
  1100. }
  1101. o := orm.NewOrmUsingDB("data")
  1102. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1103. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1104. return
  1105. }
  1106. // GetEdbInfoListByUserId
  1107. // @Description: 根据指标id列表获取列表信息
  1108. // @param userIdList []int
  1109. // @param edbInfoType int
  1110. // @return items
  1111. // @return err
  1112. func GetEdbInfoListByUserId(userIdList []int, edbInfoType int) (items []*EdbInfo, err error) {
  1113. num := len(userIdList)
  1114. if num <= 0 {
  1115. return
  1116. }
  1117. o := orm.NewOrmUsingDB("data")
  1118. sql := ` SELECT * FROM edb_info WHERE edb_info_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  1119. _, err = o.Raw(sql, edbInfoType, userIdList).QueryRows(&items)
  1120. return
  1121. }
  1122. // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
  1123. func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1124. num := len(edbIdList)
  1125. if num <= 0 {
  1126. return
  1127. }
  1128. o := orm.NewOrmUsingDB("data")
  1129. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1130. _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
  1131. return
  1132. }
  1133. // ModifyEdbInfoUserIdByOldUserId
  1134. // @Description: 根据旧用户id修改新用户id
  1135. // @author: Roc
  1136. // @datetime 2024-03-25 19:17:09
  1137. // @param edbIdList []string
  1138. // @param userId int
  1139. // @param userName string
  1140. // @return err error
  1141. func ModifyEdbInfoUserIdByOldUserId(oldUserIdList []int, edbInfoType, userId int, userName string) (err error) {
  1142. num := len(oldUserIdList)
  1143. if num <= 0 {
  1144. return
  1145. }
  1146. o := orm.NewOrmUsingDB("data")
  1147. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) and edb_info_type = ?`
  1148. _, err = o.Raw(sql, userId, userName, oldUserIdList, edbInfoType).Exec()
  1149. return
  1150. }
  1151. // MoveEdbChartList 图表/指标信息
  1152. type MoveEdbChartList struct {
  1153. DataId string `description:"指标/图表唯一id"`
  1154. Code string `description:"指标code"`
  1155. Name string `description:"指标/图表名称"`
  1156. NameEn string `description:"指标/图表名称"`
  1157. ClassifyName string `description:"分类名称"`
  1158. ClassifyNameEn string `description:"分类名称"`
  1159. CreateUserId int `description:"创建人id"`
  1160. CreateUserName string `description:"创建人名称"`
  1161. }
  1162. // MoveEdbChartListResp 更改创建人返回数据
  1163. type MoveEdbChartListResp struct {
  1164. Paging *paging.PagingItem
  1165. List []MoveEdbChartList
  1166. }
  1167. type EdbChartClassifyResp struct {
  1168. List []*EdbChartClassify
  1169. }
  1170. type EdbChartClassify struct {
  1171. ClassifyId int
  1172. ClassifyName string
  1173. ParentId int
  1174. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  1175. Child []*EdbChartClassify
  1176. }
  1177. // EdbInfoDataSeasonalResp 指标季节性数据返回
  1178. type EdbInfoDataSeasonalResp struct {
  1179. EdbInfo *EdbInfo
  1180. DataList interface{}
  1181. }
  1182. // SetEdbDataInsertConfigReq 设置插入规则的请求
  1183. type SetEdbDataInsertConfigReq struct {
  1184. EdbInfoId int `description:"指标ID"`
  1185. Date string `description:"日期"`
  1186. Value string `description:"值"`
  1187. }
  1188. // GetEdbInfoByClassifyId 用于分类展示
  1189. func GetEdbInfoByClassifyId(classifyId, edbInfoType, adminId int) (items []*EdbClassifyItems, err error) {
  1190. o := orm.NewOrmUsingDB("data")
  1191. 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 = ?`
  1192. pars := []interface{}{classifyId, edbInfoType}
  1193. // 如果筛选了用户id
  1194. if adminId > 0 {
  1195. sql += ` AND sys_user_id = ? `
  1196. pars = append(pars, adminId)
  1197. }
  1198. sql += ` order by sort asc,edb_info_id asc `
  1199. _, err = o.Raw(sql, pars).QueryRows(&items)
  1200. return
  1201. }
  1202. type EdbAndClassify struct {
  1203. EdbInfoId int `description:"指标id"`
  1204. EdbCode string `description:"指标code"`
  1205. UniqueCode string `description:"唯一code"`
  1206. EdbName string `description:"指标名称"`
  1207. ClassifyId int `description:"所属分类id"`
  1208. FirstClassifyId int `description:"第1级的分类id"`
  1209. FirstClassifyName string `description:"第1级的分类名称"`
  1210. SecondClassifyId int `description:"第2级的分类id"`
  1211. SecondClassifyName string `description:"第2级的分类名称"`
  1212. }
  1213. // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
  1214. func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
  1215. num := len(edbIdList)
  1216. if num <= 0 {
  1217. return
  1218. }
  1219. o := orm.NewOrmUsingDB("data")
  1220. sql := ` SELECT a.edb_info_id,a.edb_code,a.unique_code,a.edb_name,a.classify_id FROM edb_info AS a
  1221. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1222. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1223. return
  1224. }
  1225. // TraceEdbInfoResp 指标追溯数据返回
  1226. type TraceEdbInfoResp struct {
  1227. EdbInfoId int `description:"指标id"`
  1228. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  1229. EdbName string `description:"指标名称"`
  1230. EdbNameEn string `description:"指标英文名称"`
  1231. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  1232. RuleTitle string `description:"指标规则"`
  1233. RuleTitleEn string `description:"指标规则(英文)"`
  1234. UniqueCode string `description:"唯一编码"`
  1235. ClassifyId int `description:"分类ID"`
  1236. Child []TraceEdbInfoResp `description:"下级来源"`
  1237. IsStop int8 `description:"是否终止"`
  1238. EdbInfo *EdbInfo `description:"指标信息" json:"-"`
  1239. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  1240. }
  1241. // BeforeAndAfterDateDataResp 前后几期数据
  1242. type BeforeAndAfterDateDataResp struct {
  1243. List []*EdbDataList `description:"list"`
  1244. Date string `description:"实际日期"`
  1245. ShowValue string `description:"展示值"`
  1246. }
  1247. // GetEdbInfoAdminList
  1248. func GetEdbInfoAdminList(edbType int) (list []int, err error) {
  1249. o := orm.NewOrmUsingDB("data")
  1250. sql := ` SELECT sys_user_id FROM edb_info WHERE edb_info_type = ? GROUP BY sys_user_id `
  1251. _, err = o.Raw(sql, edbType).QueryRows(&list)
  1252. return
  1253. }
  1254. type EdbInfoExistCheckResp struct {
  1255. IndexExist bool `description:"指标是否存在:true,存在,false:不存在"`
  1256. ExistStockCodeArr []string `description:"存在的证券代码"`
  1257. ExistEdbCodeArr []string `description:"存在的指标代码"`
  1258. ExistClassifyId []int `description:"存在的分类id"`
  1259. ExistIndexId []int `description:"存在的指标id"`
  1260. ExistEdbInfo []*EdbInfo `description:"存在的指标"`
  1261. }
  1262. type BatchAddEdbInfoReq struct {
  1263. BatchList []*BatchAddEdbInfo
  1264. }
  1265. type BatchAddEdbInfo struct {
  1266. Source int `description:"来源id"`
  1267. EdbName string `description:"指标名称"`
  1268. Frequency string `description:"频率"`
  1269. Unit string `description:"单位"`
  1270. ClassifyId int `description:"分类id"`
  1271. StockCode string `description:"证券代码"`
  1272. EdbCode string `description:"指标编码"`
  1273. }
  1274. func GetEdbInfoWsdMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  1275. o := orm.NewOrmUsingDB("data")
  1276. sql := ``
  1277. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  1278. 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=? `
  1279. sql = fmt.Sprintf(sql, tableName)
  1280. err = o.Raw(sql, edbCode).QueryRow(&item)
  1281. if err != nil {
  1282. return
  1283. }
  1284. var latest_value float64
  1285. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  1286. sql = fmt.Sprintf(sql, tableName)
  1287. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  1288. item.LatestValue = latest_value
  1289. return
  1290. }
  1291. // GetEdbInfoByNames 根据名称获取普通的指标
  1292. func GetEdbInfoByNames(edbNames string) (items []*EdbInfoList, err error) {
  1293. o := orm.NewOrmUsingDB("data")
  1294. sql := ` SELECT * FROM edb_info WHERE edb_name IN (` + edbNames + `) AND edb_info_type = 0 `
  1295. _, err = o.Raw(sql).QueryRows(&items)
  1296. return
  1297. }
  1298. func GetSmmEdbInfoMaxAndMinInfo(edbCodes []string) (item *EdbInfoMaxAndMinInfo, err error) {
  1299. o := orm.NewOrmUsingDB("data")
  1300. sql := ``
  1301. 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 edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `)`
  1302. err = o.Raw(sql, edbCodes).QueryRow(&item)
  1303. if err != nil {
  1304. return
  1305. }
  1306. var latest_value float64
  1307. sql = ` SELECT value AS latest_value FROM edb_data_ys WHERE edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `) ORDER BY data_time DESC LIMIT 1 `
  1308. err = o.Raw(sql, edbCodes).QueryRow(&latest_value)
  1309. item.LatestValue = latest_value
  1310. return
  1311. }
  1312. type EdbInfoSmmExistCheckResp struct {
  1313. EdbInfoExistCheckResp
  1314. ExistAll bool `description:"是否全部重复"`
  1315. }
  1316. // GetEdbBaseInfoList
  1317. // @Description: 获取指标库数据列表
  1318. // @author: Roc
  1319. // @datetime 2024-01-10 14:28:22
  1320. // @param condition string
  1321. // @param pars []interface{}
  1322. // @param orderBy string
  1323. // @param startSize int
  1324. // @param pageSize int
  1325. // @return total int
  1326. // @return items []*BaseRefreshEdbInfo
  1327. // @return err error
  1328. func GetEdbBaseInfoList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRefreshEdbInfo, err error) {
  1329. o := orm.NewOrmUsingDB("data")
  1330. // 数量汇总
  1331. totalSql := ` SELECT count(1) FROM edb_info WHERE 1=1 `
  1332. if condition != "" {
  1333. totalSql += condition
  1334. }
  1335. err = o.Raw(totalSql, pars).QueryRow(&total)
  1336. if err != nil {
  1337. return
  1338. }
  1339. // 列表数据
  1340. 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 `
  1341. if condition != "" {
  1342. sql += condition
  1343. }
  1344. if orderBy != "" {
  1345. sql += ` ORDER BY ` + orderBy
  1346. } else {
  1347. sql += ` ORDER BY edb_info_id ASC `
  1348. }
  1349. sql += ` LIMIT ?,? `
  1350. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1351. return
  1352. }
  1353. // ModifyEdbInfoUpdateStatus
  1354. // @Description: 修改指标库停更状态
  1355. // @author: Roc
  1356. // @datetime 2024-01-08 16:23:31
  1357. // @param edbIdList []int
  1358. // @param indexCodeList []string
  1359. // @param isStop int
  1360. // @return err error
  1361. func ModifyEdbInfoUpdateStatus(edbIdList []int, isStop int) (err error) {
  1362. idNum := len(edbIdList)
  1363. if idNum <= 0 {
  1364. return
  1365. }
  1366. o, err := orm.NewOrmUsingDB("data").Begin()
  1367. if err != nil {
  1368. return
  1369. }
  1370. defer func() {
  1371. if err != nil {
  1372. _ = o.Rollback()
  1373. return
  1374. }
  1375. _ = o.Commit()
  1376. }()
  1377. // 更改指标的更新状态
  1378. sql := ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  1379. _, err = o.Raw(sql, isStop, edbIdList).Exec()
  1380. if err != nil {
  1381. return
  1382. }
  1383. return
  1384. }
  1385. func GetEdbInfoCount(source int, edbCode string) (count int, err error) {
  1386. o := orm.NewOrmUsingDB("data")
  1387. sql := ` SELECT COUNT(1) AS count FROM edb_info
  1388. WHERE source=? AND edb_code = ?`
  1389. err = o.Raw(sql, source, edbCode).QueryRow(&count)
  1390. return
  1391. }
  1392. // GetEdbInfoByNameArr 根据名称获取指标
  1393. func GetEdbInfoByNameArr(names []string, edbInfoType int) (items []*EdbInfo, err error) {
  1394. if len(names) == 0 {
  1395. return
  1396. }
  1397. o := orm.NewOrmUsingDB("data")
  1398. 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)))
  1399. _, err = o.Raw(sql, names, edbInfoType).QueryRows(&items)
  1400. return
  1401. }
  1402. type EdbInfoEditRecord struct {
  1403. EdbInfoId int `description:"指标ID"`
  1404. EdbName string `description:"指标名称"`
  1405. Frequency string `description:"频率"`
  1406. Unit string `description:"单位"`
  1407. ClassifyId int `description:"分类id"`
  1408. CalculateFormula string `description:"计算公式"`
  1409. OperateUserId int `description:"操作人id"`
  1410. OperateUserRealName string `description:"操作人姓名"`
  1411. }
  1412. func ModifyEdbInfoBaseTimeById(edbInfoId int, cTime time.Time) (err error) {
  1413. o := orm.NewOrmUsingDB("data")
  1414. // 更新修改时间
  1415. sql := ` UPDATE edb_info SET base_modify_time = ? WHERE edb_info_id = ? `
  1416. _, err = o.Raw(sql, cTime, edbInfoId).Exec()
  1417. return
  1418. }
  1419. // GetEdbCodesBySource 根据来源获取指标编码
  1420. func GetEdbCodesBySource(source int) (items []*EdbInfo, err error) {
  1421. o := orm.NewOrmUsingDB("data")
  1422. sql := ` SELECT edb_info_id, edb_code, unique_code, classify_id FROM edb_info WHERE source = ? `
  1423. _, err = o.Raw(sql, source).QueryRows(&items)
  1424. return
  1425. }
  1426. // GetAllEdbDataListData
  1427. // @Description: 获取指标的所有数据
  1428. // @author: Roc
  1429. // @datetime 2024-05-07 15:18:27
  1430. // @param edbInfoId int
  1431. // @param source int
  1432. // @param subSource int
  1433. // @param startDataTime string
  1434. // @return dataList []*EdbData
  1435. // @return err error
  1436. func GetAllEdbDataListData(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1437. // 自有数据需要额外处理(从mongo获取)
  1438. if source == utils.DATA_SOURCE_BUSINESS {
  1439. return getAllDataByMongo(edbInfoId, source, subSource, startDataTime)
  1440. }
  1441. // 默认走mysql
  1442. return getAllDataByMysql(edbInfoId, source, subSource, startDataTime)
  1443. }
  1444. // getAllDataByMysql
  1445. // @Description: 从mysql获取指标的所有数据
  1446. // @author: Roc
  1447. // @datetime 2024-05-07 15:18:41
  1448. // @param edbInfoId int
  1449. // @param source int
  1450. // @param subSource int
  1451. // @param startDataTime string
  1452. // @return dataList []*data_manage.EdbData
  1453. // @return err error
  1454. func getAllDataByMysql(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1455. dataList = make([]*EdbData, 0)
  1456. var dataCondition string
  1457. var dataPars []interface{}
  1458. dataCondition += ` AND edb_info_id=? `
  1459. dataPars = append(dataPars, edbInfoId)
  1460. // 结束日期
  1461. if startDataTime != "" {
  1462. dataCondition += ` AND data_time > ? `
  1463. dataPars = append(dataPars, startDataTime)
  1464. }
  1465. // 获取列表数据
  1466. dataList, err = GetAllEdbDataListByCondition(dataCondition, dataPars, source, subSource)
  1467. if err != nil {
  1468. return
  1469. }
  1470. return
  1471. }
  1472. // getAllDataByMongo
  1473. // @Description: 从mongo获取指标的所有数据
  1474. // @author: Roc
  1475. // @datetime 2024-05-07 15:18:53
  1476. // @param edbInfoId int
  1477. // @param source int
  1478. // @param subSource int
  1479. // @param startDataTime string
  1480. // @return dataList []*data_manage.EdbData
  1481. // @return err error
  1482. func getAllDataByMongo(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1483. dataList = make([]*EdbData, 0)
  1484. mogDataObj := mgo.EdbDataBusiness{}
  1485. // 构建查询条件
  1486. queryConditions := bson.M{
  1487. "edb_info_id": edbInfoId,
  1488. }
  1489. // 开始日期
  1490. dateCondition, err := mgo.BuildDateCondition(startDataTime, "")
  1491. if err != nil {
  1492. return
  1493. }
  1494. if len(dateCondition) > 0 {
  1495. queryConditions["data_time"] = dateCondition
  1496. }
  1497. // 获取列表数据
  1498. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"-data_time"})
  1499. if tmpErr != nil {
  1500. err = tmpErr
  1501. return
  1502. }
  1503. for k, v := range tmpDataList {
  1504. dataList = append(dataList, &EdbData{
  1505. EdbDataId: k + 1,
  1506. EdbInfoId: v.EdbInfoId,
  1507. DataTime: v.DataTime.Format(utils.FormatDate),
  1508. Value: v.Value,
  1509. })
  1510. }
  1511. return
  1512. }
  1513. type ReplaceEdbInfoItem struct {
  1514. OldEdbInfo *EdbInfo
  1515. NewEdbInfo *EdbInfo
  1516. }