chart_series.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/global"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "time"
  8. )
  9. type ChartSeries struct {
  10. ChartSeriesId int `orm:"column(chart_series_id);pk" gorm:"primaryKey" `
  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. sql := "SELECT * FROM chart_series WHERE chart_info_id = ?"
  31. err = global.DmSQL["data"].Raw(sql, chartInfoId).Scan(&items).Error
  32. return
  33. }
  34. // EditChartSeriesAndEdbMapping
  35. func EditChartSeriesAndEdbMapping(extraConfigStr string, chartInfoId int) (err error) {
  36. to := global.DmSQL["data"].Begin()
  37. defer func() {
  38. if err != nil {
  39. _ = to.Rollback()
  40. } else {
  41. _ = to.Commit()
  42. }
  43. }()
  44. // 获取已经存在的系列
  45. series, err := GetChartSeriesByChartInfoId(chartInfoId)
  46. if err != nil {
  47. return
  48. }
  49. //整理成系列map
  50. seriesMap := make(map[int]struct{})
  51. seriesDeleteMap := make(map[int]struct{})
  52. for _, v := range series {
  53. seriesMap[v.ChartSeriesId] = struct{}{}
  54. seriesDeleteMap[v.ChartSeriesId] = struct{}{}
  55. }
  56. var sectionExtraConfig ChartSectionAllExtraConf
  57. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
  58. if err != nil {
  59. err = fmt.Errorf("截面组合图配置异常")
  60. return
  61. }
  62. // 删除所有的指标映射
  63. err = to.Exec("DELETE FROM chart_series_edb_mapping WHERE chart_info_id = ?", chartInfoId).Error
  64. if err != nil {
  65. return
  66. }
  67. seriesList := sectionExtraConfig.SeriesList
  68. for _, v := range seriesList {
  69. seriesId := v.ChartSeriesId
  70. tmp := &ChartSeries{
  71. ChartSeriesId: v.ChartSeriesId,
  72. SeriesName: v.SeriesName,
  73. SeriesNameEn: v.SeriesNameEn,
  74. ChartInfoId: chartInfoId,
  75. ChartStyle: v.ChartStyle,
  76. ChartColor: v.ChartColor,
  77. ChartWidth: v.ChartWidth,
  78. IsPoint: v.IsPoint,
  79. IsNumber: v.IsNumber,
  80. IsAxis: v.IsAxis,
  81. MaxData: v.MaxData,
  82. MinData: v.MinData,
  83. ModifyTime: time.Now(),
  84. }
  85. if _, ok := seriesMap[v.ChartSeriesId]; !ok {
  86. if seriesId > 0 {
  87. err = fmt.Errorf("系列ID错误")
  88. return
  89. }
  90. //新增
  91. tmp.CreateTime = time.Now()
  92. e := to.Create(tmp).Error
  93. if e != nil {
  94. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  95. return
  96. }
  97. seriesId = tmp.ChartSeriesId
  98. } else {
  99. //编辑
  100. delete(seriesDeleteMap, v.ChartSeriesId)
  101. e := to.Updates(tmp).Error
  102. if e != nil {
  103. err = fmt.Errorf("UpdateChartSeries Err:" + e.Error())
  104. return
  105. }
  106. }
  107. addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0)
  108. for _, edbItem := range v.EdbInfoList {
  109. dateConfStrByte, e := json.Marshal(edbItem.DateConf)
  110. if e != nil {
  111. err = e
  112. return
  113. }
  114. dateConfStr := string(dateConfStrByte)
  115. edbTmp := &ChartSeriesEdbMapping{
  116. ChartSeriesId: seriesId, //todo 系列ID
  117. ChartInfoId: chartInfoId, //todo 表图ID
  118. EdbInfoId: edbItem.EdbInfoId,
  119. //EdbAliasName: "",
  120. //EdbAliasNameEn: "",
  121. DateConfName: edbItem.DateConfName,
  122. DateConf: dateConfStr,
  123. DateConfType: edbItem.DateConfType,
  124. CreateTime: time.Now(),
  125. ModifyTime: time.Now(),
  126. }
  127. addSeriesEdbList = append(addSeriesEdbList, edbTmp)
  128. }
  129. if len(addSeriesEdbList) > 0 {
  130. err = to.CreateInBatches(addSeriesEdbList, utils.MultiAddNum).Error
  131. if err != nil {
  132. err = fmt.Errorf("AddChartSeries Err:" + err.Error())
  133. return
  134. }
  135. }
  136. }
  137. //删除旧的系列和ID
  138. seriesIds := make([]int, 0)
  139. for id, _ := range seriesDeleteMap {
  140. seriesIds = append(seriesIds, id)
  141. }
  142. if len(seriesIds) > 0 {
  143. sql := `DELETE FROM chart_series WHERE chart_series_id IN (` + utils.GetOrmInReplace(len(seriesIds)) + `) and chart_info_id=?`
  144. err = to.Exec(sql, seriesIds, chartInfoId).Error
  145. if err != nil {
  146. err = fmt.Errorf("删除系列失败 Err:" + err.Error())
  147. return
  148. }
  149. sql = `DELETE FROM chart_series_edb_mapping WHERE chart_series_id IN (` + utils.GetOrmInReplace(len(seriesIds)) + `) and chart_info_id=?`
  150. err = to.Exec(sql, seriesIds, chartInfoId).Error
  151. if err != nil {
  152. err = fmt.Errorf("删除系列指标 Err:" + err.Error())
  153. return
  154. }
  155. }
  156. return
  157. }
  158. func AddChartSeriesAndEdbMapping(extraConfigStr string, chartInfoId int) (err error) {
  159. to := global.DmSQL["data"].Begin()
  160. defer func() {
  161. if err != nil {
  162. _ = to.Rollback()
  163. } else {
  164. _ = to.Commit()
  165. }
  166. }()
  167. var sectionExtraConfig ChartSectionAllExtraConf
  168. err = json.Unmarshal([]byte(extraConfigStr), &sectionExtraConfig)
  169. if err != nil {
  170. err = fmt.Errorf("截面组合图配置异常")
  171. return
  172. }
  173. seriesList := sectionExtraConfig.SeriesList
  174. for _, v := range seriesList {
  175. tmp := &ChartSeries{
  176. SeriesName: v.SeriesName,
  177. SeriesNameEn: v.SeriesNameEn,
  178. ChartInfoId: chartInfoId,
  179. ChartStyle: v.ChartStyle,
  180. ChartColor: v.ChartColor,
  181. ChartWidth: v.ChartWidth,
  182. IsPoint: v.IsPoint,
  183. IsNumber: v.IsNumber,
  184. IsAxis: v.IsAxis,
  185. MaxData: v.MaxData,
  186. MinData: v.MinData,
  187. IsOrder: false, //todo 是否排序
  188. CreateTime: time.Now(),
  189. ModifyTime: time.Now(),
  190. }
  191. e := to.Create(tmp).Error
  192. if e != nil {
  193. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  194. return
  195. }
  196. seriesId := tmp.ChartSeriesId
  197. addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0)
  198. for _, edbItem := range v.EdbInfoList {
  199. dateConfStrByte, e := json.Marshal(edbItem.DateConf)
  200. if e != nil {
  201. err = e
  202. return
  203. }
  204. dateConfStr := string(dateConfStrByte)
  205. edbTmp := &ChartSeriesEdbMapping{
  206. ChartSeriesId: seriesId, //todo 系列ID
  207. ChartInfoId: chartInfoId, //todo 表图ID
  208. EdbInfoId: edbItem.EdbInfoId,
  209. //EdbAliasName: "",
  210. //EdbAliasNameEn: "",
  211. DateConfName: edbItem.DateConfName,
  212. DateConfType: edbItem.DateConfType,
  213. DateConf: dateConfStr,
  214. CreateTime: time.Now(),
  215. ModifyTime: time.Now(),
  216. }
  217. addSeriesEdbList = append(addSeriesEdbList, edbTmp)
  218. }
  219. if len(addSeriesEdbList) > 0 {
  220. e = to.CreateInBatches(addSeriesEdbList, utils.MultiAddNum).Error
  221. if e != nil {
  222. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  223. return
  224. }
  225. }
  226. }
  227. return
  228. }
  229. func DeleteChartSeriesAndEdbMapping(chartInfoId int) (err error) {
  230. to := global.DmSQL["data"].Begin()
  231. defer func() {
  232. if err != nil {
  233. _ = to.Rollback()
  234. } else {
  235. _ = to.Commit()
  236. }
  237. }()
  238. sql := ` DELETE FROM chart_series WHERE chart_info_id=? `
  239. err = to.Exec(sql, chartInfoId).Error
  240. if err != nil {
  241. return
  242. }
  243. sql = ` DELETE FROM chart_series_edb_mapping WHERE chart_info_id=? `
  244. err = to.Exec(sql, chartInfoId).Error
  245. return
  246. }
  247. func CopyChartSeriesAndEdbMapping(seriesList []*ChartSeries, seriesEdbInfoList []*ChartSeriesEdbMapping, chartInfoId int) (err error) {
  248. to := global.DmSQL["data"].Begin()
  249. defer func() {
  250. if err != nil {
  251. _ = to.Rollback()
  252. } else {
  253. _ = to.Commit()
  254. }
  255. }()
  256. seriesEdbMap := make(map[int][]*ChartSeriesEdbMapping)
  257. for _, v := range seriesEdbInfoList {
  258. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], v)
  259. }
  260. for _, v := range seriesList {
  261. tmp := &ChartSeries{
  262. SeriesName: v.SeriesName,
  263. SeriesNameEn: v.SeriesNameEn,
  264. ChartInfoId: chartInfoId,
  265. ChartStyle: v.ChartStyle,
  266. ChartColor: v.ChartColor,
  267. ChartWidth: v.ChartWidth,
  268. IsPoint: v.IsPoint,
  269. IsNumber: v.IsNumber,
  270. IsAxis: v.IsAxis,
  271. MaxData: v.MaxData,
  272. MinData: v.MinData,
  273. CreateTime: time.Now(),
  274. ModifyTime: time.Now(),
  275. }
  276. e := to.Create(tmp).Error
  277. if e != nil {
  278. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  279. return
  280. }
  281. seriesId := tmp.ChartSeriesId
  282. addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0)
  283. mappingList, ok := seriesEdbMap[v.ChartSeriesId]
  284. if ok {
  285. for _, edbItem := range mappingList {
  286. edbTmp := &ChartSeriesEdbMapping{
  287. ChartSeriesId: seriesId, //todo 系列ID
  288. ChartInfoId: chartInfoId, //todo 表图ID
  289. EdbInfoId: edbItem.EdbInfoId,
  290. //EdbAliasName: "",
  291. //EdbAliasNameEn: "",
  292. DateConfName: edbItem.DateConfName,
  293. DateConfType: edbItem.DateConfType,
  294. DateConf: edbItem.DateConf,
  295. CreateTime: time.Now(),
  296. ModifyTime: time.Now(),
  297. }
  298. addSeriesEdbList = append(addSeriesEdbList, edbTmp)
  299. }
  300. if len(addSeriesEdbList) > 0 {
  301. e = to.CreateInBatches(addSeriesEdbList, utils.MultiAddNum).Error
  302. if e != nil {
  303. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  304. return
  305. }
  306. }
  307. }
  308. }
  309. return
  310. }
  311. type ChartSectionSeriesValSortAsc []ChartSectionSeriesValSort
  312. type ChartSectionSeriesValSortDesc []ChartSectionSeriesValSort
  313. type ChartSectionSeriesValSort struct {
  314. Index int
  315. Value float64
  316. }
  317. func (s ChartSectionSeriesValSortAsc) Len() int {
  318. return len(s)
  319. }
  320. func (s ChartSectionSeriesValSortAsc) Less(i, j int) bool {
  321. return s[i].Value < s[j].Value // 升序排序,如果想要降序则改为 s[i].Value > s[j].Value
  322. }
  323. func (s ChartSectionSeriesValSortAsc) Swap(i, j int) {
  324. s[i], s[j] = s[j], s[i]
  325. }
  326. func (s ChartSectionSeriesValSortDesc) Len() int {
  327. return len(s)
  328. }
  329. func (s ChartSectionSeriesValSortDesc) Less(i, j int) bool {
  330. return s[i].Value > s[j].Value // 升序排序,如果想要降序则改为 s[i].Value > s[j].Value
  331. }
  332. func (s ChartSectionSeriesValSortDesc) Swap(i, j int) {
  333. s[i], s[j] = s[j], s[i]
  334. }