excel_edb_mapping.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package excel
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "time"
  7. )
  8. // ExcelEdbMapping excel与指标的关系表
  9. type ExcelEdbMapping struct {
  10. ExcelEdbMappingId int `orm:"column(excel_edb_mapping_id);pk" gorm:"primaryKey" `
  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. // Add 添加excel与指标的关系
  18. func (e *ExcelEdbMapping) Add() (err error) {
  19. err = global.DmSQL["data"].Create(e).Error
  20. return
  21. }
  22. // Update 更新 excel表格基础信息
  23. func (e *ExcelEdbMapping) Update(cols []string) (err error) {
  24. err = global.DmSQL["data"].Select(cols).Updates(e).Error
  25. return
  26. }
  27. // GetExcelEdbMappingByExcelInfoId 根据excel的id获取配置关系
  28. func GetExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMapping, err error) {
  29. sql := ` SELECT * FROM excel_edb_mapping AS a
  30. join edb_info as b on a.edb_info_id = b.edb_info_id
  31. WHERE 1=1 AND a.excel_info_id = ? `
  32. err = global.DmSQL["data"].Raw(sql, excelInfoId).Find(&items).Error
  33. return
  34. }
  35. type ExcelEdbMappingItem struct {
  36. EdbInfoId int `description:"指标id"`
  37. UniqueCode string `description:"唯一编码"`
  38. EdbName string `description:"指标名称"`
  39. ClassifyId int `description:"分类id"`
  40. Frequency string `description:"频度"`
  41. Unit string `description:"单位"`
  42. CalculateFormula string `json:"-"`
  43. DateSequenceStr string `description:"日期序列公式"`
  44. DataSequenceStr string `description:"数据序列公式"`
  45. }
  46. // CalculateFormula 计算公式
  47. type CalculateFormula struct {
  48. DateSequenceStr string `json:"DateSequenceStr"`
  49. DataSequenceStr string `json:"DataSequenceStr"`
  50. }
  51. // GetAllExcelEdbMappingItemByExcelInfoId 根据品种id获取所有的指标结果集
  52. func GetAllExcelEdbMappingItemByExcelInfoId(excelInfoId int) (items []*ExcelEdbMappingItem, err error) {
  53. 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
  54. JOIN excel_edb_mapping AS b ON a.edb_info_id=b.edb_info_id
  55. WHERE b.excel_info_id = ? ORDER BY b.excel_edb_mapping_id ASC `
  56. err = global.DmSQL["data"].Raw(sql, excelInfoId).Find(&items).Error
  57. return
  58. }
  59. type ExcelEdbMappingWithParentIdItem struct {
  60. ExcelInfoId int
  61. ParentId int
  62. }
  63. func GetNoCustomAnalysisExcelEdbMapping(edbInfoId int) (items []ExcelEdbMappingWithParentIdItem, err error) {
  64. sql := ` SELECT b.excel_info_id, b.parent_id FROM excel_edb_mapping a
  65. join excel_info b on a.excel_info_id=b.excel_info_id
  66. WHERE edb_info_id=? AND a.source != 4 AND b.is_delete = 0`
  67. err = global.DmSQL["data"].Raw(sql, edbInfoId).Find(&items).Error
  68. return
  69. }
  70. // GetAllExcelEdbMappingByExcelInfoId 根据excel的id获取所有的指标
  71. func GetAllExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMapping, err error) {
  72. sql := `SELECT a.* FROM excel_edb_mapping a
  73. WHERE a.excel_info_id = ? ORDER BY a.excel_edb_mapping_id ASC `
  74. err = global.DmSQL["data"].Raw(sql, excelInfoId).Find(&items).Error
  75. return
  76. }
  77. // GetAllExcelEdbMappingByExcelInfoIds 根据excel的id获取所有的指标
  78. func GetAllExcelEdbMappingByExcelInfoIds(excelInfoIds []int) (items []*ExcelEdbMapping, err error) {
  79. sql := `SELECT a.* FROM excel_edb_mapping a
  80. WHERE a.excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `) ORDER BY a.excel_edb_mapping_id ASC `
  81. err = global.DmSQL["data"].Raw(sql, excelInfoIds).Find(&items).Error
  82. return
  83. }
  84. // GetExcelEdbMappingByEdbInfoIdAndSource 根据指标id获取配置关系
  85. func GetExcelEdbMappingByEdbInfoIdAndSource(edbInfoId int, sources []int) (items []*ExcelEdbMapping, err error) {
  86. sql := ` SELECT * FROM excel_edb_mapping WHERE 1=1 AND edb_info_id = ? AND source in (` + utils.GetOrmInReplace(len(sources)) + `) `
  87. err = global.DmSQL["data"].Raw(sql, edbInfoId, sources).Find(&items).Error
  88. return
  89. }
  90. // GetUserExcelEdbMappingByEdbInfoIdAndSource 根据指标id获取所属用户的配置关系
  91. func GetUserExcelEdbMappingByEdbInfoIdAndSource(userId, edbInfoId int, sources []int) (items []*ExcelEdbMapping, err error) {
  92. sql := ` SELECT a.* FROM excel_edb_mapping AS a
  93. JOIN excel_info AS b on a.excel_info_id = b.excel_info_id
  94. WHERE 1=1 AND a.sys_user_id = ? AND b.edb_info_id = ? AND b.source in (` + utils.GetOrmInReplace(len(sources)) + `) `
  95. err = global.DmSQL["data"].Raw(sql, userId, edbInfoId, sources).Find(&items).Error
  96. return
  97. }
  98. // DeleteCustomAnalysisExcelEdbMappingByEdbInfoId
  99. // @Description: 根据指标id删除与自定义分析表格的关系
  100. // @author: Roc
  101. // @datetime 2023-11-02 13:20:02
  102. // @param edbInfoId int
  103. // @return err error
  104. func DeleteCustomAnalysisExcelEdbMappingByEdbInfoId(edbInfoId int) (err error) {
  105. sql := `DELETE FROM excel_edb_mapping WHERE source = ? AND edb_info_id = ? LIMIT 1`
  106. err = global.DmSQL["data"].Exec(sql, utils.CUSTOM_ANALYSIS_TABLE, edbInfoId).Error
  107. return
  108. }
  109. // GetExcelEdbMappingItemByExcelInfoIdOrKeyword 根据表格ID或关键词获取指标
  110. func GetExcelEdbMappingItemByExcelInfoIdOrKeyword(excelInfoId int, keyword string) (items []*ExcelEdbMappingItem, err error) {
  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 = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  134. return
  135. }