edb_info.go 59 KB

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