edb_info.go 58 KB

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