excel_edb_mapping.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package excel
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "time"
  7. )
  8. type ExcelEdbMapping struct {
  9. ExcelEdbMappingId int `orm:"column(excel_edb_mapping_id);pk" gorm:"primaryKey" `
  10. ExcelInfoId int `description:"excel的id"`
  11. Source int `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
  12. EdbInfoId int `description:"计算指标id"`
  13. CreateTime time.Time `description:"创建时间"`
  14. ModifyTime time.Time `description:"修改时间"`
  15. }
  16. func (e *ExcelEdbMapping) Add() (err error) {
  17. err = global.DmSQL["data"].Create(e).Error
  18. return
  19. }
  20. func (e *ExcelEdbMapping) Update(cols []string) (err error) {
  21. err = global.DmSQL["data"].Select(cols).Updates(e).Error
  22. return
  23. }
  24. func GetExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMapping, err error) {
  25. sql := ` SELECT * FROM excel_edb_mapping AS a
  26. join edb_info as b on a.edb_info_id = b.edb_info_id
  27. WHERE 1=1 AND a.excel_info_id = ? `
  28. err = global.DmSQL["data"].Raw(sql, excelInfoId).Find(&items).Error
  29. return
  30. }
  31. type ExcelEdbMappingItem struct {
  32. EdbInfoId int `description:"指标id"`
  33. UniqueCode string `description:"唯一编码"`
  34. EdbName string `description:"指标名称"`
  35. ClassifyId int `description:"分类id"`
  36. Frequency string `description:"频度"`
  37. Unit string `description:"单位"`
  38. CalculateFormula string `json:"-"`
  39. DateSequenceStr string `description:"日期序列公式"`
  40. DataSequenceStr string `description:"数据序列公式"`
  41. }
  42. type CalculateFormula struct {
  43. DateSequenceStr string `json:"DateSequenceStr"`
  44. DataSequenceStr string `json:"DataSequenceStr"`
  45. }
  46. func GetAllExcelEdbMappingItemByExcelInfoId(excelInfoId int) (items []*ExcelEdbMappingItem, err error) {
  47. 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
  48. JOIN excel_edb_mapping AS b ON a.edb_info_id=b.edb_info_id
  49. WHERE b.excel_info_id = ? ORDER BY b.excel_edb_mapping_id ASC `
  50. err = global.DmSQL["data"].Raw(sql, excelInfoId).Find(&items).Error
  51. return
  52. }
  53. type ExcelEdbMappingWithParentIdItem struct {
  54. ExcelInfoId int
  55. ParentId int
  56. }
  57. func GetNoCustomAnalysisExcelEdbMapping(edbInfoId int) (items []ExcelEdbMappingWithParentIdItem, err error) {
  58. sql := ` SELECT b.excel_info_id, b.parent_id FROM excel_edb_mapping a
  59. join excel_info b on a.excel_info_id=b.excel_info_id
  60. WHERE edb_info_id=? AND a.source != 4 AND b.is_delete = 0`
  61. err = global.DmSQL["data"].Raw(sql, edbInfoId).Find(&items).Error
  62. return
  63. }
  64. func GetAllExcelEdbMappingByExcelInfoId(excelInfoId int) (items []*ExcelEdbMapping, err error) {
  65. sql := `SELECT a.* FROM excel_edb_mapping a
  66. WHERE a.excel_info_id = ? ORDER BY a.excel_edb_mapping_id ASC `
  67. err = global.DmSQL["data"].Raw(sql, excelInfoId).Find(&items).Error
  68. return
  69. }
  70. func GetAllExcelEdbMappingByExcelInfoIds(excelInfoIds []int) (items []*ExcelEdbMapping, err error) {
  71. sql := `SELECT a.* FROM excel_edb_mapping a
  72. WHERE a.excel_info_id in (` + utils.GetOrmInReplace(len(excelInfoIds)) + `) ORDER BY a.excel_edb_mapping_id ASC `
  73. err = global.DmSQL["data"].Raw(sql, excelInfoIds).Find(&items).Error
  74. return
  75. }
  76. func GetExcelEdbMappingByEdbInfoIdAndSource(edbInfoId int, sources []int) (items []*ExcelEdbMapping, err error) {
  77. sql := ` SELECT * FROM excel_edb_mapping WHERE 1=1 AND edb_info_id = ? AND source in (` + utils.GetOrmInReplace(len(sources)) + `) `
  78. err = global.DmSQL["data"].Raw(sql, edbInfoId, sources).Find(&items).Error
  79. return
  80. }
  81. func DeleteCustomAnalysisExcelEdbMappingByEdbInfoId(edbInfoId int) (err error) {
  82. sql := `DELETE FROM excel_edb_mapping WHERE source = ? AND edb_info_id = ? LIMIT 1`
  83. err = global.DmSQL["data"].Exec(sql, utils.CUSTOM_ANALYSIS_TABLE, edbInfoId).Error
  84. return
  85. }
  86. func GetExcelEdbMappingItemByExcelInfoIdOrKeyword(excelInfoId int, keyword string) (items []*ExcelEdbMappingItem, err error) {
  87. cond := `b.excel_info_id = ?`
  88. pars := make([]interface{}, 0)
  89. pars = append(pars, excelInfoId)
  90. if keyword != "" {
  91. cond += ` AND (a.edb_code LIKE ? OR a.edb_name LIKE ?)`
  92. pars = append(pars, keyword, keyword)
  93. }
  94. sql := fmt.Sprintf(`SELECT
  95. a.edb_info_id,
  96. a.unique_code,
  97. a.edb_name,
  98. a.classify_id,
  99. a.frequency,
  100. a.unit,
  101. calculate_formula
  102. FROM
  103. edb_info AS a
  104. JOIN excel_edb_mapping AS b ON a.edb_info_id = b.edb_info_id
  105. WHERE
  106. %s
  107. ORDER BY
  108. b.excel_edb_mapping_id ASC`, cond)
  109. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  110. return
  111. }