chart_series.go 9.6 KB

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