chart_series.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/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. func CopyChartSeriesAndEdbMapping(seriesList []*ChartSeries, seriesEdbInfoList []*ChartSeriesEdbMapping, chartInfoId int) (err error) {
  261. o := orm.NewOrmUsingDB("data")
  262. to, err := o.Begin()
  263. if err != nil {
  264. return
  265. }
  266. defer func() {
  267. if err != nil {
  268. _ = to.Rollback()
  269. } else {
  270. _ = to.Commit()
  271. }
  272. }()
  273. seriesEdbMap := make(map[int][]*ChartSeriesEdbMapping)
  274. for _, v := range seriesEdbInfoList {
  275. seriesEdbMap[v.ChartSeriesId] = append(seriesEdbMap[v.ChartSeriesId], v)
  276. }
  277. for _, v := range seriesList {
  278. tmp := &ChartSeries{
  279. SeriesName: v.SeriesName,
  280. SeriesNameEn: v.SeriesNameEn,
  281. ChartInfoId: chartInfoId,
  282. ChartStyle: v.ChartStyle,
  283. ChartColor: v.ChartColor,
  284. ChartWidth: v.ChartWidth,
  285. IsPoint: v.IsPoint,
  286. IsNumber: v.IsNumber,
  287. IsAxis: v.IsAxis,
  288. MaxData: v.MaxData,
  289. MinData: v.MinData,
  290. CreateTime: time.Now(),
  291. ModifyTime: time.Now(),
  292. }
  293. seriesIdTmp, e := to.Insert(tmp)
  294. if e != nil {
  295. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  296. return
  297. }
  298. seriesId := int(seriesIdTmp)
  299. addSeriesEdbList := make([]*ChartSeriesEdbMapping, 0)
  300. mappingList, ok := seriesEdbMap[v.ChartSeriesId]
  301. if ok {
  302. for _, edbItem := range mappingList {
  303. edbTmp := &ChartSeriesEdbMapping{
  304. ChartSeriesId: seriesId, //todo 系列ID
  305. ChartInfoId: chartInfoId, //todo 表图ID
  306. EdbInfoId: edbItem.EdbInfoId,
  307. //EdbAliasName: "",
  308. //EdbAliasNameEn: "",
  309. DateConfName: edbItem.DateConfName,
  310. DateConfType: edbItem.DateConfType,
  311. DateConf: edbItem.DateConf,
  312. CreateTime: time.Now(),
  313. ModifyTime: time.Now(),
  314. }
  315. addSeriesEdbList = append(addSeriesEdbList, edbTmp)
  316. }
  317. if len(addSeriesEdbList) > 0 {
  318. _, e = to.InsertMulti(len(addSeriesEdbList), addSeriesEdbList)
  319. if e != nil {
  320. err = fmt.Errorf("AddChartSeries Err:" + e.Error())
  321. return
  322. }
  323. }
  324. }
  325. }
  326. return
  327. }
  328. type ChartSectionSeriesValSortAsc []ChartSectionSeriesValSort
  329. type ChartSectionSeriesValSortDesc []ChartSectionSeriesValSort
  330. type ChartSectionSeriesValSort struct {
  331. Index int
  332. Value float64
  333. }
  334. func (s ChartSectionSeriesValSortAsc) Len() int {
  335. return len(s)
  336. }
  337. func (s ChartSectionSeriesValSortAsc) Less(i, j int) bool {
  338. return s[i].Value < s[j].Value // 升序排序,如果想要降序则改为 s[i].Value > s[j].Value
  339. }
  340. func (s ChartSectionSeriesValSortAsc) Swap(i, j int) {
  341. s[i], s[j] = s[j], s[i]
  342. }
  343. func (s ChartSectionSeriesValSortDesc) Len() int {
  344. return len(s)
  345. }
  346. func (s ChartSectionSeriesValSortDesc) Less(i, j int) bool {
  347. return s[i].Value > s[j].Value // 升序排序,如果想要降序则改为 s[i].Value > s[j].Value
  348. }
  349. func (s ChartSectionSeriesValSortDesc) Swap(i, j int) {
  350. s[i], s[j] = s[j], s[i]
  351. }