excel_edb_mapping.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package excel
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "time"
  8. )
  9. // ExcelEdbMapping excel与指标的关系表
  10. type ExcelEdbMapping struct {
  11. ExcelEdbMappingId int `orm:"column(excel_edb_mapping_id);pk" gorm:"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. return
  23. }
  24. // Add 添加excel与指标的关系
  25. func (e *ExcelEdbMapping) Add() (err error) {
  26. err = global.DbMap[utils.DbNameIndex].Create(e).Error
  27. return
  28. }
  29. // Update 更新 excel表格基础信息
  30. func (e *ExcelEdbMapping) Update(cols []string) (err error) {
  31. err = global.DbMap[utils.DbNameIndex].Select(cols).Updates(e).Error
  32. return
  33. }
  34. // GetExcelEdbMappingByEdbInfoId 根据指标id获取配置关系
  35. func GetExcelEdbMappingByEdbInfoId(edbInfoId int) (items []*ExcelEdbMapping, err error) {
  36. o := orm.NewOrmUsingDB("data")
  37. sql := ` SELECT * FROM excel_edb_mapping WHERE 1=1 AND edb_info_id = ? `
  38. _, err = o.Raw(sql, edbInfoId).QueryRows(&items)
  39. return
  40. }
  41. // GetExcelEdbMappingByExcelInfoId 根据excel的id获取配置关系
  42. func GetExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMapping, err error) {
  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 = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoId).Find(&items).Error
  47. return
  48. }
  49. type ExcelEdbMappingItem struct {
  50. EdbInfoId int `description:"指标id"`
  51. UniqueCode string `description:"唯一编码"`
  52. EdbName string `description:"指标名称"`
  53. ClassifyId int `description:"分类id"`
  54. Frequency string `description:"频度"`
  55. Unit string `description:"单位"`
  56. CalculateFormula string `json:"-"`
  57. DateSequenceStr string `description:"日期序列公式"`
  58. DataSequenceStr string `description:"数据序列公式"`
  59. }
  60. // CalculateFormula 计算公式
  61. type CalculateFormula struct {
  62. DateSequenceStr string `json:"DateSequenceStr"`
  63. DataSequenceStr string `json:"DataSequenceStr"`
  64. }
  65. // GetAllExcelEdbMappingItemByExcelInfoId 根据品种id获取所有的指标结果集
  66. func GetAllExcelEdbMappingItemByExcelInfoId(excelInfoId int) (items []*ExcelEdbMappingItem, err error) {
  67. 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
  68. JOIN excel_edb_mapping AS b ON a.edb_info_id=b.edb_info_id
  69. WHERE b.excel_info_id = ? ORDER BY b.excel_edb_mapping_id ASC `
  70. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoId).Find(&items).Error
  71. return
  72. }
  73. // GetNoCustomAnalysisExcelEdbMappingCount 根据指标id获取非自定义分析的关联关系
  74. func GetNoCustomAnalysisExcelEdbMappingCount(edbInfoId int) (count int, err error) {
  75. o := orm.NewOrmUsingDB("data")
  76. sql := ` SELECT COUNT(1) AS count FROM excel_edb_mapping a
  77. join excel_info b on a.excel_info_id=b.excel_info_id
  78. WHERE edb_info_id=? AND a.source != 4 AND b.is_delete = 0`
  79. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  80. return
  81. }
  82. type ExcelEdbMappingWithParentIdItem struct {
  83. ExcelInfoId int
  84. ParentId int
  85. }
  86. func GetNoCustomAnalysisExcelEdbMapping(edbInfoId int) (items []ExcelEdbMappingWithParentIdItem, err error) {
  87. sql := ` SELECT b.excel_info_id, b.parent_id FROM excel_edb_mapping a
  88. join excel_info b on a.excel_info_id=b.excel_info_id
  89. WHERE edb_info_id=? AND a.source != 4 AND b.is_delete = 0`
  90. err = global.DbMap[utils.DbNameIndex].Raw(sql, edbInfoId).Find(&items).Error
  91. return
  92. }
  93. // GetAllExcelEdbMappingByExcelInfoId 根据excel的id获取所有的指标
  94. func GetAllExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMapping, err error) {
  95. sql := `SELECT a.* FROM excel_edb_mapping a
  96. WHERE a.excel_info_id = ? ORDER BY a.excel_edb_mapping_id ASC `
  97. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoId).Find(&items).Error
  98. return
  99. }
  100. // GetAllExcelEdbMappingByExcelInfoIds 根据excel的id获取所有的指标
  101. func GetAllExcelEdbMappingByExcelInfoIds(excelInfoIds []int) (items []*ExcelEdbMapping, err error) {
  102. sql := `SELECT a.* FROM excel_edb_mapping a
  103. WHERE a.excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `) ORDER BY a.excel_edb_mapping_id ASC `
  104. err = global.DbMap[utils.DbNameIndex].Raw(sql, excelInfoIds).Find(&items).Error
  105. return
  106. }
  107. // GetExcelEdbMappingByEdbInfoIdAndSource 根据指标id获取配置关系
  108. func GetExcelEdbMappingByEdbInfoIdAndSource(edbInfoId int, sources []int) (items []*ExcelEdbMapping, err error) {
  109. sql := ` SELECT * FROM excel_edb_mapping WHERE 1=1 AND edb_info_id = ? AND source in (` + utils.GetOrmInReplace(len(sources)) + `) `
  110. err = global.DbMap[utils.DbNameIndex].Raw(sql, edbInfoId, sources).Find(&items).Error
  111. return
  112. }
  113. // DeleteCustomAnalysisExcelEdbMappingByEdbInfoId
  114. // @Description: 根据指标id删除与自定义分析表格的关系
  115. // @author: Roc
  116. // @datetime 2023-11-02 13:20:02
  117. // @param edbInfoId int
  118. // @return err error
  119. func DeleteCustomAnalysisExcelEdbMappingByEdbInfoId(edbInfoId int) (err error) {
  120. sql := `DELETE FROM excel_edb_mapping WHERE source = ? AND edb_info_id = ? LIMIT 1`
  121. err = global.DbMap[utils.DbNameIndex].Exec(sql, utils.CUSTOM_ANALYSIS_TABLE, edbInfoId).Error
  122. return
  123. }
  124. // GetExcelEdbMappingItemByExcelInfoIdOrKeyword 根据表格ID或关键词获取指标
  125. func GetExcelEdbMappingItemByExcelInfoIdOrKeyword(excelInfoId int, keyword string) (items []*ExcelEdbMappingItem, err error) {
  126. cond := `b.excel_info_id = ?`
  127. pars := make([]interface{}, 0)
  128. pars = append(pars, excelInfoId)
  129. if keyword != "" {
  130. cond += ` AND (a.edb_code LIKE ? OR a.edb_name LIKE ?)`
  131. pars = append(pars, keyword, keyword)
  132. }
  133. sql := fmt.Sprintf(`SELECT
  134. a.edb_info_id,
  135. a.unique_code,
  136. a.edb_name,
  137. a.classify_id,
  138. a.frequency,
  139. a.unit,
  140. calculate_formula
  141. FROM
  142. edb_info AS a
  143. JOIN excel_edb_mapping AS b ON a.edb_info_id = b.edb_info_id
  144. WHERE
  145. %s
  146. ORDER BY
  147. b.excel_edb_mapping_id ASC`, cond)
  148. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  149. return
  150. }