excel_edb_mapping.go 9.5 KB

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