chart_series.go 11 KB

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