chart_series.go 8.2 KB

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