chart_series.go 8.2 KB

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