excel_edb_mapping.go 5.8 KB

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