excel_edb_mapping.go 6.6 KB

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