chart_series.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package models
  2. import (
  3. "eta/eta_forum_hub/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type ChartSeries struct {
  9. ChartSeriesId int `orm:"column(chart_series_id);pk"`
  10. SeriesName string `description:"系列名称"`
  11. SeriesNameEn string `description:"系列英文名称"`
  12. ChartInfoId int `description:"图表ID"`
  13. ChartStyle string `description:"图表类型"`
  14. ChartColor string `description:"颜色"`
  15. ChartWidth int `description:"线条大小"`
  16. IsPoint int `description:"是否用数据点展示(0 否,1是)"`
  17. IsNumber int `description:"是否用数值展示(0 否,1是)"`
  18. IsAxis int `description:"1:左轴,0:右轴"`
  19. MaxData float64 `description:"上限"`
  20. MinData float64 `description:"下限"`
  21. IsOrder bool `description:"true:正序,false:逆序"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. }
  25. func (c *ChartSeries) TableName() string {
  26. return "chart_series"
  27. }
  28. func GetChartSeriesByChartInfoId(chartInfoId int) (items []*ChartSeries, err error) {
  29. o := orm.NewOrm()
  30. sql := "SELECT * FROM chart_series WHERE chart_info_id = ?"
  31. _, err = o.Raw(sql, chartInfoId).QueryRows(&items)
  32. return
  33. }
  34. // EditChartSeriesAndEdbMapping
  35. func EditChartSeriesAndEdbMapping(seriesList []*ChartSeries, seriesEdbInfoList []*ChartSeriesEdbMapping, chartInfoId int, newEdbInfoMap map[int]int) (err error) {
  36. o := orm.NewOrm()
  37. to, err := o.Begin()
  38. if err != nil {
  39. return
  40. }
  41. defer func() {
  42. if err != nil {
  43. _ = to.Rollback()
  44. } else {
  45. _ = to.Commit()
  46. }
  47. }()
  48. // 获取已经存在的系列
  49. series, err := GetChartSeriesByChartInfoId(chartInfoId)
  50. if err != nil {
  51. return
  52. }
  53. //整理成系列map
  54. seriesMap := make(map[string]int)
  55. seriesDeleteMap := make(map[string]struct{})
  56. for _, v := range series {
  57. seriesMap[v.SeriesName] = v.ChartSeriesId
  58. seriesDeleteMap[v.SeriesName] = struct{}{}
  59. }
  60. // 删除所有的指标映射
  61. _, err = o.Raw("DELETE FROM chart_series_edb_mapping WHERE chart_info_id = ?", chartInfoId).Exec()
  62. if err != nil {
  63. return
  64. }
  65. seriesEdbMap := make(map[int][]*ChartSeriesEdbMapping)
  66. for _, v := range seriesEdbInfoList {
  67. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], v)
  68. }
  69. for _, v := range seriesList {
  70. tmp := &ChartSeries{
  71. SeriesName: v.SeriesName,
  72. SeriesNameEn: v.SeriesNameEn,
  73. ChartInfoId: chartInfoId,
  74. ChartStyle: v.ChartStyle,
  75. ChartColor: v.ChartColor,
  76. ChartWidth: v.ChartWidth,
  77. IsPoint: v.IsPoint,
  78. IsNumber: v.IsNumber,
  79. IsAxis: v.IsAxis,
  80. MaxData: v.MaxData,
  81. MinData: v.MinData,
  82. ModifyTime: time.Now(),
  83. }
  84. seriesId, ok := seriesMap[v.SeriesName]
  85. if !ok {
  86. //新增
  87. tmp.CreateTime = time.Now()
  88. seriesIdTmp, e := to.Insert(tmp)
  89. if e != nil {
  90. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  91. return
  92. }
  93. seriesId = int(seriesIdTmp)
  94. } else {
  95. //编辑
  96. tmp.ChartSeriesId = seriesId
  97. delete(seriesDeleteMap, v.SeriesName)
  98. _, e := to.Update(tmp)
  99. if e != nil {
  100. err = fmt.Errorf("UpdateChartSeries Err:" + e.Error())
  101. return
  102. }
  103. }
  104. addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0)
  105. mappingList, ok := seriesEdbMap[v.ChartSeriesId]
  106. if ok {
  107. for _, edbItem := range mappingList {
  108. newEdbInfoId, ok1 := newEdbInfoMap[edbItem.EdbInfoId]
  109. if ok1 {
  110. edbTmp := &ChartSeriesEdbMapping{
  111. ChartSeriesId: seriesId, //todo 系列ID
  112. ChartInfoId: chartInfoId, //todo 表图ID
  113. EdbInfoId: newEdbInfoId,
  114. //EdbAliasName: "",
  115. //EdbAliasNameEn: "",
  116. DateConfName: edbItem.DateConfName,
  117. DateConf: edbItem.DateConf,
  118. DateConfType: edbItem.DateConfType,
  119. CreateTime: time.Now(),
  120. ModifyTime: time.Now(),
  121. }
  122. addSeriesEdbList = append(addSeriesEdbList, edbTmp)
  123. }
  124. }
  125. if len(addSeriesEdbList) > 0 {
  126. _, err = to.InsertMulti(len(addSeriesEdbList), addSeriesEdbList)
  127. if err != nil {
  128. err = fmt.Errorf("AddChartSeries Err:" + err.Error())
  129. return
  130. }
  131. }
  132. }
  133. }
  134. //删除旧的系列和ID
  135. seriesNames := make([]string, 0)
  136. for name, _ := range seriesDeleteMap {
  137. seriesNames = append(seriesNames, name)
  138. }
  139. if len(seriesNames) > 0 {
  140. sql := `DELETE FROM chart_series WHERE series_name IN (` + utils.GetOrmInReplace(len(seriesNames)) + `) and chart_info_id=?`
  141. _, err = to.Raw(sql, seriesNames, chartInfoId).Exec()
  142. if err != nil {
  143. err = fmt.Errorf("删除系列失败 Err:" + err.Error())
  144. return
  145. }
  146. }
  147. return
  148. }
  149. func AddChartSeriesAndEdbMapping(seriesList []*ChartSeries, seriesEdbInfoList []*ChartSeriesEdbMapping, chartInfoId int, newEdbInfoMap map[int]int) (err error) {
  150. o := orm.NewOrm()
  151. to, err := o.Begin()
  152. if err != nil {
  153. return
  154. }
  155. defer func() {
  156. if err != nil {
  157. _ = to.Rollback()
  158. } else {
  159. _ = to.Commit()
  160. }
  161. }()
  162. seriesEdbMap := make(map[int][]*ChartSeriesEdbMapping)
  163. for _, v := range seriesEdbInfoList {
  164. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], v)
  165. }
  166. for _, v := range seriesList {
  167. tmp := &ChartSeries{
  168. SeriesName: v.SeriesName,
  169. SeriesNameEn: v.SeriesNameEn,
  170. ChartInfoId: chartInfoId,
  171. ChartStyle: v.ChartStyle,
  172. ChartColor: v.ChartColor,
  173. ChartWidth: v.ChartWidth,
  174. IsPoint: v.IsPoint,
  175. IsNumber: v.IsNumber,
  176. IsAxis: v.IsAxis,
  177. MaxData: v.MaxData,
  178. MinData: v.MinData,
  179. CreateTime: time.Now(),
  180. ModifyTime: time.Now(),
  181. }
  182. seriesIdTmp, e := to.Insert(tmp)
  183. if e != nil {
  184. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  185. return
  186. }
  187. seriesId := int(seriesIdTmp)
  188. addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0)
  189. mappingList, ok := seriesEdbMap[v.ChartSeriesId]
  190. if ok {
  191. for _, edbItem := range mappingList {
  192. newEdbInfoId, ok1 := newEdbInfoMap[edbItem.EdbInfoId]
  193. if ok1 {
  194. edbTmp := &ChartSeriesEdbMapping{
  195. ChartSeriesId: seriesId, //todo 系列ID
  196. ChartInfoId: chartInfoId, //todo 表图ID
  197. EdbInfoId: newEdbInfoId,
  198. //EdbAliasName: "",
  199. //EdbAliasNameEn: "",
  200. DateConfName: edbItem.DateConfName,
  201. DateConfType: edbItem.DateConfType,
  202. DateConf: edbItem.DateConf,
  203. CreateTime: time.Now(),
  204. ModifyTime: time.Now(),
  205. }
  206. addSeriesEdbList = append(addSeriesEdbList, edbTmp)
  207. }
  208. }
  209. if len(addSeriesEdbList) > 0 {
  210. _, e = to.InsertMulti(len(addSeriesEdbList), addSeriesEdbList)
  211. if e != nil {
  212. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  213. return
  214. }
  215. }
  216. }
  217. }
  218. return
  219. }
  220. func DeleteChartSeriesAndEdbMapping(chartInfoId int) (err error) {
  221. o := orm.NewOrm()
  222. to, err := o.Begin()
  223. if err != nil {
  224. return
  225. }
  226. defer func() {
  227. if err != nil {
  228. _ = to.Rollback()
  229. } else {
  230. _ = to.Commit()
  231. }
  232. }()
  233. sql := ` DELETE FROM chart_series WHERE chart_info_id=? `
  234. _, err = to.Raw(sql, chartInfoId).Exec()
  235. if err != nil {
  236. return
  237. }
  238. sql = ` DELETE FROM chart_series_edb_mapping WHERE chart_info_id=? `
  239. _, err = to.Raw(sql, chartInfoId).Exec()
  240. return
  241. }