edb_info.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta_gn/eta_task/global"
  5. "eta_gn/eta_task/utils"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. )
  10. type EdbInfo struct {
  11. EdbInfoId int `gorm:"column:edb_info_id;primaryKey"` //`orm:"column(edb_info_id);pk"`
  12. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  13. SourceName string `description:"来源名称"`
  14. Source int `description:"来源id"`
  15. EdbCode string `description:"指标编码"`
  16. EdbName string `description:"指标名称"`
  17. EdbNameEn string `description:"英文指标名称"`
  18. EdbNameSource string `description:"指标名称来源"`
  19. Frequency string `description:"频率"`
  20. Unit string `description:"单位"`
  21. UnitEn string `description:"英文单位"`
  22. StartDate time.Time `description:"起始日期"`
  23. EndDate time.Time `description:"终止日期"`
  24. ClassifyId int `description:"分类id"`
  25. SysUserId int
  26. SysUserRealName string
  27. UniqueCode string `description:"指标唯一编码"`
  28. CreateTime time.Time
  29. ModifyTime time.Time
  30. BaseModifyTime time.Time
  31. MinValue float64 `description:"指标最小值"`
  32. MaxValue float64 `description:"指标最大值"`
  33. CalculateFormula string `description:"计算公式"`
  34. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  35. Sort int `description:"排序字段"`
  36. LatestDate string `description:"数据最新日期(实际日期)"`
  37. LatestValue float64 `description:"数据最新值(实际值)"`
  38. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  39. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  40. MoveFrequency string `description:"移动频度"`
  41. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  42. ServerUrl string `description:"服务器地址"`
  43. ChartImage string `description:"图表图片"`
  44. Calendar string `description:"公历/农历" orm:"default(公历);"`
  45. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  46. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  47. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  48. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  49. TerminalCode string `description:"终端编码,用于配置在机器上"`
  50. DataUpdateTime string `description:"最近一次数据发生变化的时间"`
  51. ErDataUpdateDate string `description:"本次更新,数据发生变化的最早日期"`
  52. SourceIndexName string `description:"数据源中的指标名称"`
  53. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  54. SubSourceName string `description:"子数据来源名称"`
  55. IndicatorCode string `description:"指标代码"`
  56. StockCode string `description:"证券代码"`
  57. Extra string `description:"指标额外配置"`
  58. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  59. OriginalEdbCode string `description:"指标原始编码"`
  60. }
  61. type EdbInfoList struct {
  62. EdbInfoId int `gorm:"column:edb_info_id;primaryKey"` // `orm:"column(edb_info_id);pk"`
  63. SourceName string `description:"来源名称"`
  64. Source int `description:"来源id"`
  65. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  66. SubSourceName string `description:"子数据来源名称"`
  67. EdbCode string `description:"指标编码"`
  68. EdbName string `description:"指标名称"`
  69. Frequency string `description:"频率"`
  70. Unit string `description:"单位"`
  71. StartDate time.Time `description:"起始日期"`
  72. EndDate time.Time `description:"终止日期"`
  73. ClassifyId int `description:"分类id"`
  74. UniqueCode string `description:"指标唯一编码"`
  75. CalculateFormula string `description:"计算公式"`
  76. ModifyTime string `description:"更新时间"`
  77. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  78. }
  79. type EdbInfoSearchData struct {
  80. EdbDataId int `description:"指标数据Id"`
  81. DataTime string `description:"数据日期"`
  82. Value float64 `description:"数据"`
  83. }
  84. type EdbInfoSearchDataV1 struct {
  85. DataTime string `description:"数据日期"`
  86. Value string `description:"数据"`
  87. }
  88. func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfoList, err error) {
  89. //o := orm.NewOrm()
  90. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  91. if condition != "" {
  92. sql += condition
  93. }
  94. if order == 1 {
  95. sql += ` ORDER BY end_date ASC `
  96. } else {
  97. sql += ` ORDER BY edb_info_id ASC `
  98. }
  99. //_, err = o.Raw(sql, pars).QueryRows(&item)
  100. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  101. return
  102. }
  103. func ModifyEdbDataInfoDate(edbInfoId int, maxDate string) (err error) {
  104. //o := orm.NewOrm()
  105. sql := ` UPDATE edb_info SET end_date=?,modify_time=NOW() WHERE edb_info_id=? `
  106. //_, err = o.Raw(sql, maxDate, edbInfoId).Exec()
  107. err = global.DEFAULT_DmSQL.Exec(sql, maxDate, edbInfoId).Error
  108. return
  109. }
  110. type EdbInfoMaxAndMinInfo struct {
  111. MinDate string `description:"最小日期"`
  112. MaxDate string `description:"最大日期"`
  113. MinValue float64 `description:"最小值"`
  114. MaxValue float64 `description:"最大值"`
  115. LatestValue float64 `description:"最新值"`
  116. }
  117. func GetEdbInfoMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  118. //o := orm.NewOrm()
  119. sql := ``
  120. tableName := GetEdbDataTableName(source, subSource)
  121. if tableName == "" {
  122. err = errors.New("无效的表名称:source:" + strconv.Itoa(source))
  123. return nil, err
  124. }
  125. 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=? `
  126. sql = fmt.Sprintf(sql, tableName)
  127. //err = o.Raw(sql, edbCode).QueryRow(&item)
  128. err = global.DEFAULT_DmSQL.Raw(sql, edbCode).First(&item).Error
  129. var latest_value float64
  130. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  131. sql = fmt.Sprintf(sql, tableName)
  132. //err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  133. err = global.DEFAULT_DmSQL.Raw(sql, edbCode).First(&latest_value).Error
  134. item.LatestValue = latest_value
  135. return
  136. }
  137. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  138. //o := orm.NewOrm()
  139. 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=? `
  140. //_, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  141. err = global.DEFAULT_DmSQL.Exec(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Error
  142. return
  143. }
  144. //order:1升序,其余值为降序
  145. func GetEdbDataListAll(condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchData, err error) {
  146. //o := orm.NewOrm()
  147. sql := ``
  148. tableName := GetEdbDataTableName(source, subSource)
  149. sql = ` SELECT * FROM %s WHERE 1=1 `
  150. sql = fmt.Sprintf(sql, tableName)
  151. if condition != "" {
  152. sql += condition
  153. }
  154. if order == 1 {
  155. sql += ` ORDER BY data_time ASC `
  156. } else {
  157. sql += ` ORDER BY data_time DESC `
  158. }
  159. //_, err = o.Raw(sql, pars).QueryRows(&item)
  160. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  161. return
  162. }
  163. func GetEdbDataListAllV1(condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchDataV1, err error) {
  164. //o := orm.NewOrm()
  165. sql := ``
  166. tableName := GetEdbDataTableName(source, subSource)
  167. sql = ` SELECT * FROM %s WHERE 1=1 `
  168. sql = fmt.Sprintf(sql, tableName)
  169. if condition != "" {
  170. sql += condition
  171. }
  172. if order == 1 {
  173. sql += ` ORDER BY data_time ASC `
  174. } else {
  175. sql += ` ORDER BY data_time DESC `
  176. }
  177. //_, err = o.Raw(sql, pars).QueryRows(&item)
  178. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  179. return
  180. }
  181. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  182. //o := orm.NewOrm()
  183. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  184. //err = o.Raw(sql, edbInfoId).QueryRow(&item)
  185. err = global.DEFAULT_DmSQL.Raw(sql, edbInfoId).First(&item).Error
  186. return
  187. }
  188. func GetQuarterEdbInfo() (item []*EdbInfo, err error) {
  189. //o := orm.NewOrm()
  190. //sql := ` SELECT c.* FROM chart_info AS a
  191. // INNER JOIN chart_edb_mapping AS b ON a.chart_info_id=b.chart_info_id
  192. // INNER JOIN edb_info AS c ON b.edb_info_id=c.edb_info_id
  193. // WHERE a.chart_type=2
  194. // GROUP BY b.edb_info_id
  195. // ORDER BY b.edb_info_id ASC `
  196. sql := `SELECT c.edb_info_id, c.edb_info_type, c.source_name, c.source, c.edb_code, c.edb_name, c.edb_name_source, c.frequency, c.unit, c.start_date, c.end_date, c.classify_id, c.sys_user_id, c.sys_user_real_name, c.create_time, c.modify_time, c.unique_code, c.min_value, c.max_value, c.calculate_formula, c.edb_type, c.is_update, c.sort, c.latest_date, c.latest_value, c.move_type, c.move_frequency, c.no_update, c.server_url, c.edb_name_en, c.unit_en, c.chart_image, c.calendar, c.empty_type, c.max_empty_type, c.data_date_type, c.manual_save, c.terminal_code, c.data_update_time, c.er_data_update_date, c.source_index_name, c.end_value, c.sub_source, c.sub_source_name, c.indicator_code, c.stock_code, c.extra, c.is_join_permission, c.base_modify_time, c.is_static_data, c.set_update_time
  197. FROM chart_info AS a
  198. INNER JOIN chart_edb_mapping AS b ON a.chart_info_id = b.chart_info_id
  199. INNER JOIN edb_info AS c ON b.edb_info_id = c.edb_info_id
  200. WHERE a.chart_type = 2
  201. GROUP BY b.edb_info_id, c.edb_info_id, c.edb_info_type, c.source_name, c.source, c.edb_code, c.edb_name, c.edb_name_source, c.frequency, c.unit, c.start_date, c.end_date, c.classify_id, c.sys_user_id, c.sys_user_real_name, c.create_time, c.modify_time, c.unique_code, c.min_value, c.max_value, c.calculate_formula, c.edb_type, c.is_update, c.sort, c.latest_date, c.latest_value, c.move_type, c.move_frequency, c.no_update, c.server_url, c.edb_name_en, c.unit_en, c.chart_image, c.calendar, c.empty_type, c.max_empty_type, c.data_date_type, c.manual_save, c.terminal_code, c.data_update_time, c.er_data_update_date, c.source_index_name, c.end_value, c.sub_source, c.sub_source_name, c.indicator_code, c.stock_code, c.extra, c.is_join_permission, c.base_modify_time, c.is_static_data, c.set_update_time
  202. ORDER BY b.edb_info_id ASC `
  203. //_, err = o.Raw(sql).QueryRows(&item)
  204. err = global.DEFAULT_DmSQL.Raw(sql).Find(&item).Error
  205. return
  206. }
  207. func ResetEdbInfoIsUpdate() (err error) {
  208. //o := orm.NewOrm()
  209. sql := ` UPDATE edb_info SET is_update=1 `
  210. //_, err = o.Raw(sql).Exec()
  211. err = global.DEFAULT_DmSQL.Exec(sql).Error
  212. return
  213. }
  214. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  215. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  216. //o := orm.NewOrm()
  217. //calculateTableName := GetEdbInfoCalculateTableName(source)
  218. //if calculateTableName == "" {
  219. // err = errors.New("无效的表名")
  220. // return
  221. //}
  222. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  223. //sql = fmt.Sprintf(sql, calculateTableName)
  224. if condition != "" {
  225. sql += condition
  226. }
  227. //_, err = o.Raw(sql, pars).QueryRows(&items)
  228. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  229. return
  230. }
  231. func DeleteEdbDataByIdAndSource(edbDataId, source, subSource int) (err error) {
  232. sql := ` DELETE FROM %s WHERE edb_data_id=? `
  233. tableName := GetEdbDataTableName(source, subSource)
  234. sql = fmt.Sprintf(sql, tableName)
  235. //o := orm.NewOrm()
  236. //_, err = o.Raw(sql, edbDataId).Exec()
  237. err = global.DEFAULT_DmSQL.Exec(sql, edbDataId).Error
  238. return
  239. }
  240. type EdbInfoClassify struct {
  241. EdbInfoId int `gorm:"column:edb_info_id;primaryKey"` // `orm:"column(edb_info_id);pk"`
  242. SourceName string `description:"来源名称"`
  243. Source int `description:"来源id"`
  244. EdbCode string `description:"指标编码"`
  245. ClassifyId int `description:"分类id"`
  246. SysUserId int
  247. SysUserRealName string
  248. UniqueCode string `description:"指标唯一编码"`
  249. CreateTime time.Time
  250. ModifyTime time.Time
  251. }
  252. // GetAllEdbInfoClassifyListByCondition
  253. // @Description: 获取指标与分类的关系列表
  254. // @author: Roc
  255. // @datetime 2024-02-29 10:55:38
  256. // @param condition string
  257. // @param pars []interface{}
  258. // @return item []*EdbInfoUpdateLog
  259. // @return err error
  260. func GetAllEdbInfoClassifyListByCondition(condition string, pars []interface{}) (item []*EdbInfoClassify, err error) {
  261. //o := orm.NewOrmUsingDB("data")
  262. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  263. if condition != "" {
  264. sql += condition
  265. }
  266. sql += `ORDER BY edb_info_id ASC `
  267. //_, err = o.Raw(sql, pars).QueryRows(&item)
  268. err = global.DmSQL["data"].Raw(sql, pars...).Find(&item).Error
  269. return
  270. }
  271. // GetEdbInfoItemByCodeAndSource
  272. // @Description: 根据指标编码和来源id获取指标信息
  273. // @author: Roc
  274. // @datetime 2024-03-11 16:26:23
  275. // @param source int
  276. // @param edbCode string
  277. // @return item *EdbInfo
  278. // @return err error
  279. func GetEdbInfoItemByCodeAndSource(source int, edbCode string) (item *EdbInfoItem, err error) {
  280. //o := orm.NewOrm()
  281. sql := ` SELECT * FROM edb_info WHERE edb_code=? AND source = ?`
  282. //err = o.Raw(sql, source, edbCode).QueryRow(&item)
  283. err = global.DEFAULT_DmSQL.Raw(sql, source, edbCode).First(&item).Error
  284. return
  285. }
  286. // GetEdbInfoMaxModifyTime
  287. // @Description: 根据指标来源和编码获取该指标数据最晚修改时间
  288. // @author: Roc
  289. // @datetime 2024-03-11 17:01:01
  290. // @param source int
  291. // @param edbCode string
  292. // @return modifyTime string
  293. // @return err error
  294. func GetEdbInfoMaxModifyTime(source, subSource int, edbCode string) (modifyTime string, err error) {
  295. //o := orm.NewOrmUsingDB("data")
  296. tableName := GetEdbDataTableName(source, subSource)
  297. if tableName == "" {
  298. err = errors.New("无效的表名称:source:" + strconv.Itoa(source))
  299. return
  300. }
  301. sql := ` SELECT MAX(modify_time) AS modify_time FROM %s WHERE edb_code=? `
  302. sql = fmt.Sprintf(sql, tableName)
  303. //err = o.Raw(sql, edbCode).QueryRow(&modifyTime)
  304. err = global.DmSQL["data"].Raw(sql, edbCode).Scan(&modifyTime).Error
  305. return
  306. }
  307. func GetEdbInfoPageByCondition(condition string, pars []interface{}, startPage, pageSize int) (item []*EdbInfo, err error) {
  308. //o := orm.NewOrm()
  309. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  310. if condition != "" {
  311. sql += condition
  312. }
  313. sql += ` LIMIT ?,? `
  314. //_, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&item)
  315. pars = append(pars, startPage, pageSize)
  316. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  317. return
  318. }
  319. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (total int64, err error) {
  320. //o := orm.NewOrm()
  321. sql := ` SELECT count(*) FROM edb_info WHERE 1=1 `
  322. if condition != "" {
  323. sql += condition
  324. }
  325. //err = o.Raw(sql, pars).QueryRow(&total)
  326. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&total).Error
  327. return
  328. }
  329. func ModifyEdbUpdateStatus(edbIdList []int, indexCodeList []string, calculateEdbInfoIds []int) (err error) {
  330. idNum := len(edbIdList)
  331. if idNum <= 0 {
  332. return
  333. }
  334. //o, err := orm.NewOrmUsingDB("data").Begin()
  335. o := global.DmSQL["data"].Begin()
  336. if err != nil {
  337. return
  338. }
  339. defer func() {
  340. if err != nil {
  341. _ = o.Rollback()
  342. return
  343. }
  344. _ = o.Commit()
  345. }()
  346. // 更改指标的更新状态
  347. sql := ` UPDATE edb_info SET no_update = 1 WHERE source in (?, ?) AND edb_info_id IN (` + utils.GetOrmInReplace(idNum) + `) AND no_update = 0`
  348. //_, err = o.Raw(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND, edbIdList).Exec()
  349. err = o.Exec(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND, edbIdList).Error
  350. if err != nil {
  351. return
  352. }
  353. // 更改钢联化工指标更新状态
  354. if len(indexCodeList) > 0 {
  355. // 更改数据源的更新状态
  356. sql = ` UPDATE base_from_mysteel_chemical_index SET is_stop = 1 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=0`
  357. //_, err = o.Raw(sql, indexCodeList).Exec()
  358. err = o.Exec(sql, indexCodeList).Error
  359. if err != nil {
  360. return
  361. }
  362. }
  363. // 更新相关的计算指标状态
  364. if len(calculateEdbInfoIds) > 0 {
  365. // 批量更新相关联的指标ID
  366. sql = ` UPDATE edb_info SET no_update = 1 WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) AND no_update = 0`
  367. //_, err = o.Raw(sql, calculateEdbInfoIds).Exec()
  368. err = o.Exec(sql, calculateEdbInfoIds).Error
  369. if err != nil {
  370. return
  371. }
  372. }
  373. return
  374. }
  375. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  376. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  377. num := len(edbInfoIdList)
  378. if num <= 0 {
  379. return
  380. }
  381. //o := orm.NewOrmUsingDB("data")
  382. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  383. //_, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  384. err = global.DmSQL["data"].Raw(sql, edbInfoIdList).Find(&items).Error
  385. return
  386. }
  387. // GetAllBaseEdbInfo
  388. // @Description: 获取所有基础指标(只有国能数据节点的数据)
  389. // @return item
  390. // @return err
  391. func GetAllBaseEdbInfo() (item []*EdbInfo, err error) {
  392. sql := ` SELECT * FROM edb_info WHERE 1=1 AND "edb_info_type" = ? AND "edb_type" = ?`
  393. pars := []interface{}{utils.EDB_INFO_TYPE, utils.EdbTypeBase}
  394. err = global.DmSQL["data"].Raw(sql, pars...).Find(&item).Error
  395. return
  396. }
  397. // AddEdbInfo
  398. // @Description: 新增指标
  399. // @param item
  400. // @return err
  401. func AddEdbInfo(item *EdbInfo) (err error) {
  402. err = global.DmSQL["data"].Create(item).Error
  403. if err != nil {
  404. return
  405. }
  406. return
  407. }
  408. // Update
  409. // @Description: 更新指定参数
  410. // @receiver m
  411. // @param cols
  412. // @return err
  413. func (m *EdbInfo) Update(cols []string) (err error) {
  414. err = global.DmSQL["data"].Select(cols).Updates(m).Error
  415. return
  416. }