excel_edb_mapping.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package excel
  2. import (
  3. "eta/eta_chart_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. // ExcelEdbMapping excel与指标的关系表
  9. type ExcelEdbMapping struct {
  10. ExcelEdbMappingId int `orm:"column(excel_edb_mapping_id);pk"`
  11. ExcelInfoId int `description:"excel的id"`
  12. Source int `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
  13. EdbInfoId int `description:"计算指标id"`
  14. CreateTime time.Time `description:"创建时间"`
  15. ModifyTime time.Time `description:"修改时间"`
  16. }
  17. // AddExcelEdbMappingMulti 批量添加excel与指标的关系
  18. func AddExcelEdbMappingMulti(items []*ExcelEdbMapping) (err error) {
  19. o := orm.NewOrmUsingDB("data")
  20. _, err = o.InsertMulti(len(items), items)
  21. return
  22. }
  23. // Add 添加excel与指标的关系
  24. func (e *ExcelEdbMapping) Add() (err error) {
  25. o := orm.NewOrmUsingDB("data")
  26. _, err = o.Insert(e)
  27. return
  28. }
  29. // GetExcelEdbMappingByEdbInfoId 根据指标id获取配置关系
  30. func GetExcelEdbMappingByEdbInfoId(edbInfoId int) (item *ExcelEdbMapping, err error) {
  31. o := orm.NewOrmUsingDB("data")
  32. sql := ` SELECT * FROM excel_edb_mapping WHERE 1=1 AND edb_info_id = ? `
  33. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  34. return
  35. }
  36. // GetExcelEdbMappingByExcelInfoId 根据excel的id获取配置关系
  37. func GetExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMapping, err error) {
  38. o := orm.NewOrmUsingDB("data")
  39. sql := ` SELECT * FROM excel_edb_mapping AS a
  40. join edb_info as b on a.edb_info_id = b.edb_info_id
  41. WHERE 1=1 AND a.excel_info_id = ? `
  42. _, err = o.Raw(sql, excelInfoId).QueryRows(&items)
  43. return
  44. }
  45. type ExcelEdbMappingItem struct {
  46. EdbInfoId int `description:"指标id"`
  47. UniqueCode string `description:"唯一编码"`
  48. EdbName string `description:"指标名称"`
  49. ClassifyId int `description:"分类id"`
  50. Frequency string `description:"频度"`
  51. Unit string `description:"单位"`
  52. CalculateFormula string `json:"-"`
  53. DateSequenceStr string `description:"日期序列公式"`
  54. DataSequenceStr string `description:"数据序列公式"`
  55. }
  56. // CalculateFormula 计算公式
  57. type CalculateFormula struct {
  58. DateSequenceStr string `json:"DateSequenceStr"`
  59. DataSequenceStr string `json:"DataSequenceStr"`
  60. }
  61. // GetAllExcelEdbMappingItemByExcelInfoId 根据品种id获取所有的指标结果集
  62. func GetAllExcelEdbMappingItemByExcelInfoId(excelInfoId int) (items []*ExcelEdbMappingItem, err error) {
  63. o := orm.NewOrmUsingDB("data")
  64. sql := `SELECT a.edb_info_id,a.unique_code,a.edb_name,a.classify_id,a.frequency,a.unit,calculate_formula FROM edb_info AS a
  65. JOIN excel_edb_mapping AS b ON a.edb_info_id=b.edb_info_id
  66. WHERE b.excel_info_id = ? ORDER BY b.excel_edb_mapping_id ASC `
  67. _, err = o.Raw(sql, excelInfoId).QueryRows(&items)
  68. return
  69. }
  70. // GetNoCustomAnalysisExcelEdbMappingCount 根据指标id获取非自定义分析的关联关系
  71. func GetNoCustomAnalysisExcelEdbMappingCount(edbInfoId int) (count int, err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. sql := ` SELECT COUNT(1) AS count FROM excel_edb_mapping a
  74. join excel_info b on a.excel_info_id=b.excel_info_id
  75. WHERE edb_info_id=? AND a.source != 4 AND b.is_delete = 0`
  76. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  77. return
  78. }
  79. // GetAllExcelEdbMappingByExcelInfoId 根据excel的id获取所有的指标
  80. func GetAllExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMapping, err error) {
  81. o := orm.NewOrmUsingDB("data")
  82. sql := `SELECT a.* FROM excel_edb_mapping a
  83. WHERE a.excel_info_id = ? ORDER BY a.excel_edb_mapping_id ASC `
  84. _, err = o.Raw(sql, excelInfoId).QueryRows(&items)
  85. return
  86. }
  87. // DeleteCustomAnalysisExcelEdbMappingByEdbInfoId
  88. // @Description: 根据指标id删除与自定义分析表格的关系
  89. // @author: Roc
  90. // @datetime 2023-11-02 13:20:02
  91. // @param excelInfoId int
  92. // @return err error
  93. func DeleteCustomAnalysisExcelEdbMappingByEdbInfoId(excelInfoId int) (err error) {
  94. o := orm.NewOrmUsingDB("data")
  95. sql := `DELETE FROM excel_edb_mapping WHERE source = ? AND edb_info_id = ? LIMIT 1`
  96. _, err = o.Raw(sql, utils.CUSTOM_ANALYSIS_TABLE, excelInfoId).Exec()
  97. return
  98. }
  99. // GetExcelEdbMappingItemByExcelInfoIdOrKeyword 根据表格ID或关键词获取指标
  100. func GetExcelEdbMappingItemByExcelInfoIdOrKeyword(excelInfoId int, keyword string) (items []*ExcelEdbMappingItem, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. cond := `b.excel_info_id = ?`
  103. pars := make([]interface{}, 0)
  104. pars = append(pars, excelInfoId)
  105. if keyword != "" {
  106. cond += ` AND (a.edb_code LIKE ? OR a.edb_name LIKE ?)`
  107. pars = append(pars, keyword, keyword)
  108. }
  109. sql := fmt.Sprintf(`SELECT
  110. a.edb_info_id,
  111. a.unique_code,
  112. a.edb_name,
  113. a.classify_id,
  114. a.frequency,
  115. a.unit,
  116. calculate_formula
  117. FROM
  118. edb_info AS a
  119. JOIN excel_edb_mapping AS b ON a.edb_info_id = b.edb_info_id
  120. WHERE
  121. %s
  122. ORDER BY
  123. b.excel_edb_mapping_id ASC`, cond)
  124. _, err = o.Raw(sql, pars).QueryRows(&items)
  125. return
  126. }