excel_edb_mapping.go 6.5 KB

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