excel_edb_mapping.go 7.4 KB

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