chart_info.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_forum_admin/models"
  6. "eta/eta_forum_admin/services"
  7. "eta/eta_forum_admin/services/area_graph"
  8. "eta/eta_forum_admin/utils"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type ChartInfoController struct {
  15. BaseAuthController
  16. }
  17. // ChartInfoDetail
  18. // @Title 获取图表详情
  19. // @Description 获取图表详情接口
  20. // @Param ChartInfoId query int true "图表id"
  21. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  22. // @Param StartDate query string true "自定义开始日期"
  23. // @Param EndDate query string true "自定义结束日期"
  24. // @Param Calendar query string true "公历/农历"
  25. // @Param SeasonStartDate query string true "季节性图开始日期"
  26. // @Param SeasonEndDate query string true "季节性图结束日期"
  27. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  28. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  29. // @Success 200 {object} models.ChartInfoDetailResp
  30. // @router /detail [get]
  31. func (this *ChartInfoController) ChartInfoDetail() {
  32. br := new(models.BaseResponse).Init()
  33. defer func() {
  34. this.Data["json"] = br
  35. this.ServeJSON()
  36. }()
  37. sysUser := this.SysUser
  38. if sysUser == nil {
  39. br.Msg = "请登录"
  40. br.ErrMsg = "请登录,SysUser Is Empty"
  41. br.Ret = 408
  42. return
  43. }
  44. chartInfoId, _ := this.GetInt("ChartInfoId")
  45. var err error
  46. chartInfo := new(models.ChartInfoView)
  47. chartInfo.HaveOperaAuth = true
  48. if chartInfoId <= 0 {
  49. br.Msg = "请选择图表"
  50. return
  51. }
  52. chartInfo, err = models.GetChartInfoViewById(chartInfoId)
  53. if err != nil {
  54. if err.Error() == utils.ErrNoRow() {
  55. br.Msg = "该图表已删除,自动查看下一图表"
  56. br.ErrMsg = "该图表已删除,自动查看下一图表,Err:" + err.Error()
  57. br.Ret = 406
  58. return
  59. }
  60. br.Msg = "获取失败"
  61. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  62. return
  63. }
  64. chartType := chartInfo.ChartType
  65. startDate := chartInfo.StartDate
  66. endDate := chartInfo.EndDate
  67. seasonStartDate := chartInfo.SeasonStartDate
  68. seasonEndDate := chartInfo.SeasonEndDate
  69. startYear := chartInfo.StartYear
  70. calendar := this.GetString("Calendar")
  71. if calendar == "" {
  72. calendar = chartInfo.Calendar
  73. }
  74. if calendar == "" {
  75. calendar = "公历"
  76. }
  77. dateType := chartInfo.DateType
  78. // 获取主题样式
  79. chartTheme, err := services.GetChartThemeConfig(chartInfo.ChartThemeId, chartInfo.Source, chartInfo.ChartType)
  80. if err != nil {
  81. br.Msg = "获取失败"
  82. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  83. return
  84. }
  85. chartInfo.ChartThemeStyle = chartTheme.Config
  86. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  87. resp := new(models.ChartInfoDetailResp)
  88. mappingList := make([]*models.ChartEdbInfoMapping, 0)
  89. mappingList, err = models.GetChartEdbMappingList(chartInfoId)
  90. if err != nil {
  91. br.Msg = "获取失败"
  92. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  93. return
  94. }
  95. // 图表额外数据参数
  96. extraConfigStr := chartInfo.ExtraConfig
  97. // 柱方图的一些配置
  98. var barConfig models.BarChartInfoReq
  99. if chartInfo != nil && chartInfo.ChartType == 7 {
  100. if chartInfo.BarConfig == `` {
  101. br.Msg = "柱方图未配置"
  102. br.ErrMsg = "柱方图未配置"
  103. return
  104. }
  105. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  106. if err != nil {
  107. br.Msg = "柱方图配置异常"
  108. br.ErrMsg = "柱方图配置异常"
  109. return
  110. }
  111. extraConfigStr = chartInfo.BarConfig
  112. }
  113. if chartType == 2 {
  114. startDate = seasonStartDate
  115. endDate = seasonEndDate
  116. if dateType <= 0 {
  117. if startDate != "" {
  118. dateType = 5
  119. } else {
  120. dateType = utils.DateTypeNYears
  121. }
  122. }
  123. } else {
  124. if dateType <= 0 {
  125. dateType = 3
  126. }
  127. }
  128. yearMax := 0
  129. if dateType == utils.DateTypeNYears {
  130. for _, v := range mappingList {
  131. if v.LatestDate != "" {
  132. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  133. if tErr != nil {
  134. br.Msg = "获取失败"
  135. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  136. return
  137. }
  138. if lastDateT.Year() > yearMax {
  139. yearMax = lastDateT.Year()
  140. }
  141. }
  142. }
  143. }
  144. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)
  145. if chartInfo.ChartType == 2 {
  146. chartInfo.StartDate = startDate
  147. chartInfo.EndDate = endDate
  148. chartInfo.Calendar = calendar
  149. }
  150. // 获取图表中的指标数据
  151. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := services.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig)
  152. if err != nil {
  153. br.Msg = "获取失败"
  154. if errMsg != `` {
  155. br.Msg = errMsg
  156. }
  157. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  158. return
  159. }
  160. // 单位
  161. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  162. chartInfo.Unit = yDataList[0].Unit
  163. chartInfo.UnitEn = yDataList[0].UnitEn
  164. }
  165. warnEdbList := make([]string, 0)
  166. for _, v := range edbList {
  167. if v.IsNullData {
  168. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  169. }
  170. }
  171. if len(warnEdbList) > 0 {
  172. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  173. }
  174. // 面积图 面积堆积 数据处理
  175. if chartInfo.ChartType == utils.CHART_TYPE_AREA {
  176. err, errMsg = fillAreaGraphData(extraConfigStr, edbList)
  177. if err != nil {
  178. br.Msg = "面积图处理失败"
  179. br.ErrMsg = errMsg
  180. return
  181. }
  182. }
  183. // 图表的指标来源
  184. sourceNameList, sourceNameEnList := services.GetEdbSourceByEdbInfoIdList(edbList)
  185. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  186. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  187. //判断是否需要展示英文标识
  188. chartInfo.IsEnChart = services.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  189. resp.EdbInfoList = edbList
  190. resp.XEdbIdValue = xEdbIdValue
  191. resp.YDataList = yDataList
  192. resp.DataResp = dataResp
  193. chartInfo.Button = models.ChartViewButton{
  194. IsEdit: chartInfo.IsEdit,
  195. IsEnChart: chartInfo.IsEnChart,
  196. IsAdd: chartInfo.IsAdd,
  197. IsCopy: true,
  198. IsSetName: chartInfo.IsSetName,
  199. }
  200. resp.ChartInfo = chartInfo
  201. resp.BarChartInfo = barConfig
  202. br.Ret = 200
  203. br.Success = true
  204. br.Msg = "获取成功"
  205. br.Data = resp
  206. }
  207. func fillAreaGraphData(extraConfigStr string, edbDataList []*models.ChartEdbInfoMapping) (err error, errMsg string) {
  208. var tmpConfig models.AreaExtraConf
  209. if extraConfigStr != `` {
  210. err = json.Unmarshal([]byte(extraConfigStr), &tmpConfig)
  211. if err != nil {
  212. errMsg = "面积图配置异常"
  213. err = errors.New(errMsg)
  214. return
  215. }
  216. if tmpConfig.StandardEdbInfoId <= 0 {
  217. utils.FileLog.Info("面积图未开启面积堆积")
  218. return
  219. }
  220. }
  221. if tmpConfig.IsHeap == 1 {
  222. standardIndexMap := make(map[string]*models.EdbDataList)
  223. var startDate, endDate string
  224. for _, v := range edbDataList {
  225. // 判断是否为基准指标
  226. if v.EdbInfoId == tmpConfig.StandardEdbInfoId {
  227. if dataList, ok := v.DataList.([]*models.EdbDataList); ok {
  228. startDate = dataList[0].DataTime
  229. endDate = dataList[len(dataList)-1].DataTime
  230. for _, dataObject := range dataList {
  231. standardIndexMap[dataObject.DataTime] = dataObject
  232. }
  233. }
  234. break
  235. }
  236. }
  237. strategy, err := area_graph.CreateStrategy(tmpConfig.NullDealWay)
  238. if err != nil {
  239. return err, "创建空值处理器失败"
  240. }
  241. err = strategy.Deal(tmpConfig, edbDataList, standardIndexMap, startDate, endDate)
  242. if err != nil {
  243. return err, err.Error()
  244. }
  245. // 时间戳处理
  246. for _, mapping := range edbDataList {
  247. if dataList, ok := mapping.DataList.([]*models.EdbDataList); ok {
  248. for _, dataInfo := range dataList {
  249. toFormatTime := utils.StringToFormatTime(dataInfo.DataTime, utils.FormatDate)
  250. dataInfo.DataTimestamp = toFormatTime.UnixMilli()
  251. }
  252. }
  253. }
  254. }
  255. return nil, ""
  256. }
  257. // ChartInfoSearchByEs
  258. // @Title 图表模糊搜索(从es获取)
  259. // @Description 图表模糊搜索(从es获取)
  260. // @Param Keyword query string true "图表名称"
  261. // @Param IsShowMe query bool true "是否只看我的,true、false"
  262. // @Success 200 {object} models.ChartInfo
  263. // @router /search_by_es [get]
  264. func (this *ChartInfoController) ChartInfoSearchByEs() {
  265. br := new(models.BaseResponse).Init()
  266. defer func() {
  267. this.Data["json"] = br
  268. this.ServeJSON()
  269. }()
  270. sysUser := this.SysUser
  271. if sysUser == nil {
  272. br.Msg = "请登录"
  273. br.ErrMsg = "请登录,SysUser Is Empty"
  274. br.Ret = 408
  275. return
  276. }
  277. pageSize, _ := this.GetInt("PageSize")
  278. currentIndex, _ := this.GetInt("CurrentIndex")
  279. var startSize int
  280. if pageSize <= 0 {
  281. pageSize = utils.PageSize20
  282. }
  283. if currentIndex <= 0 {
  284. currentIndex = 1
  285. }
  286. startSize = paging.StartIndex(currentIndex, pageSize)
  287. keyword := this.GetString("Keyword")
  288. //只看我的
  289. sysUserIds := this.GetString("SysUserIds")
  290. adminIdsSlice := make([]int, 0)
  291. if sysUserIds != "" {
  292. adminIds := strings.Split(sysUserIds, ",")
  293. if len(adminIds) == 0 {
  294. br.Msg = "请选择正确的创建人"
  295. return
  296. }
  297. for _, adminId := range adminIds {
  298. adminIdInt, e := strconv.Atoi(adminId)
  299. if e != nil {
  300. br.Msg = "请选择正确的创建人"
  301. return
  302. }
  303. adminIdsSlice = append(adminIdsSlice, adminIdInt)
  304. }
  305. }
  306. var searchList []*models.ChartInfo
  307. var total int64
  308. var err error
  309. if keyword != "" {
  310. searchList, total, err = services.EsSearchChartInfo(keyword, adminIdsSlice, []int{utils.CHART_SOURCE_DEFAULT}, startSize, pageSize)
  311. } else {
  312. total, searchList, err = models.ChartInfoSearchByEmptyKeyWord(adminIdsSlice, []int{utils.CHART_SOURCE_DEFAULT}, startSize, pageSize)
  313. if err != nil && err.Error() != utils.ErrNoRow() {
  314. br.Msg = "获取失败"
  315. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  316. return
  317. }
  318. }
  319. finalList := make([]*models.ChartInfoMore, 0)
  320. if len(searchList) > 0 {
  321. // 涉及分类id
  322. classifyIdList := make([]int, 0)
  323. // 图表id
  324. chartInfoIdList := make([]int, 0)
  325. chartInfoIds := ""
  326. chartEdbMap := make(map[int][]*models.ChartEdbInfoMapping)
  327. for _, v := range searchList {
  328. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  329. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  330. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  331. }
  332. if chartInfoIds != "" {
  333. chartInfoIds = strings.Trim(chartInfoIds, ",")
  334. //判断是否需要展示英文标识
  335. edbList, e := models.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  336. if e != nil {
  337. br.Msg = "获取失败"
  338. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  339. return
  340. }
  341. for _, v := range edbList {
  342. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  343. }
  344. }
  345. // 当前列表中的分类map
  346. chartClassifyMap := make(map[int]*models.ChartClassify)
  347. // 图表分类
  348. {
  349. chartClassifyList, err := models.GetChartClassifyByIdList(classifyIdList)
  350. if err != nil {
  351. br.Msg = "获取失败"
  352. br.ErrMsg = "获取图表分类失败,Err:" + err.Error()
  353. return
  354. }
  355. for _, v := range chartClassifyList {
  356. chartClassifyMap[v.ChartClassifyId] = v
  357. }
  358. }
  359. for _, v := range searchList {
  360. tmp := new(models.ChartInfoMore)
  361. tmp.ChartInfo = *v
  362. //判断是否需要展示英文标识
  363. if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
  364. tmp.IsEnChart = services.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
  365. }
  366. // 图表权限
  367. finalList = append(finalList, tmp)
  368. }
  369. }
  370. //新增搜索词记录
  371. {
  372. searchKeyword := new(models.SearchKeyword)
  373. searchKeyword.KeyWord = keyword
  374. searchKeyword.CreateTime = time.Now()
  375. go models.AddSearchKeyword(searchKeyword)
  376. }
  377. page := paging.GetPaging(currentIndex, pageSize, int(total))
  378. resp := models.ChartInfoListByEsResp{
  379. Paging: page,
  380. List: finalList,
  381. }
  382. br.Ret = 200
  383. br.Success = true
  384. br.Msg = "获取成功"
  385. br.Data = resp
  386. }
  387. // ChartList
  388. // @Title 根据分类创建人姓名等获取图表列表
  389. // @Description 根据分类创建人姓名等获取图表列表
  390. // @Param SysUserIds query string true "根据创建人查询"
  391. // @Param ChartClassifyIds query string true "图片分类id"
  392. // @Success 200 {object} models.ChartClassifyListResp
  393. // @router /list [get]
  394. func (this *ChartInfoController) ChartList() {
  395. br := new(models.BaseResponse).Init()
  396. defer func() {
  397. this.Data["json"] = br
  398. this.ServeJSON()
  399. }()
  400. sysUser := this.SysUser
  401. if sysUser == nil {
  402. br.Msg = "请登录"
  403. br.ErrMsg = "请登录,SysUser Is Empty"
  404. br.Ret = 408
  405. return
  406. }
  407. resp := new(models.ChartClassifyListResp)
  408. pageSize, _ := this.GetInt("PageSize")
  409. currentIndex, _ := this.GetInt("CurrentIndex")
  410. chartClassifyIds := this.GetString("ChartClassifyIds")
  411. sysUserIds := this.GetString("SysUserIds")
  412. chartName := this.GetString("ChartName")
  413. var startSize int
  414. if pageSize <= 0 {
  415. pageSize = utils.PageSize20
  416. }
  417. if currentIndex <= 0 {
  418. currentIndex = 1
  419. }
  420. startSize = utils.StartIndex(currentIndex, pageSize)
  421. condition := ""
  422. pars := make([]interface{}, 0)
  423. if chartClassifyIds != "" {
  424. classifyIds := strings.Split(chartClassifyIds, ",")
  425. if len(classifyIds) == 0 {
  426. br.Msg = "请选择正确的分类"
  427. return
  428. }
  429. classifyIdSlice := make([]int, 0)
  430. for _, id := range classifyIds {
  431. tmp, e := strconv.Atoi(id)
  432. if e != nil {
  433. br.Msg = "请选择正确的分类"
  434. return
  435. }
  436. classifyIdSlice = append(classifyIdSlice, tmp)
  437. }
  438. condition += " AND chart_classify_id in (" + utils.GetOrmInReplace(len(classifyIdSlice)) + ") "
  439. pars = append(pars, classifyIdSlice)
  440. }
  441. if sysUserIds != "" {
  442. adminIds := strings.Split(sysUserIds, ",")
  443. if len(adminIds) == 0 {
  444. br.Msg = "请选择正确的创建人"
  445. return
  446. }
  447. adminIdsSlice := make([]int, 0)
  448. for _, adminId := range adminIds {
  449. adminIdInt, e := strconv.Atoi(adminId)
  450. if e != nil {
  451. br.Msg = "请选择正确的创建人"
  452. return
  453. }
  454. adminIdsSlice = append(adminIdsSlice, adminIdInt)
  455. }
  456. condition += " AND sys_user_id in (" + utils.GetOrmInReplace(len(adminIds)) + ") "
  457. pars = append(pars, adminIdsSlice)
  458. }
  459. if chartName != "" {
  460. condition += " AND chart_name LIKE ? "
  461. pars = append(pars, utils.GetLikeKeyword(chartName))
  462. }
  463. allChartInfo, err := models.GetChartInfoByConditionPage(utils.CHART_SOURCE_DEFAULT, condition, pars, startSize, pageSize)
  464. if err != nil && err.Error() != utils.ErrNoRow() {
  465. br.Msg = "获取失败"
  466. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  467. return
  468. }
  469. total, err := models.GetChartInfoTotalByCondition(utils.CHART_SOURCE_DEFAULT, condition, pars)
  470. if err != nil {
  471. br.Msg = "获取失败"
  472. br.ErrMsg = "获取图表总数失败,Err:" + err.Error()
  473. return
  474. }
  475. if len(allChartInfo) > 0 {
  476. /*adminIds := make([]int, 0)
  477. for _, v := range allChartInfo {
  478. adminIds = append(adminIds, v.SysUserId)
  479. }
  480. adminCompanyMap, er := services.GetCompanyNameByAdmins(adminIds)
  481. if er != nil {
  482. br.Msg = "获取失败"
  483. br.ErrMsg = "获取机构失败,Err:" + er.Error()
  484. return
  485. }*/
  486. for k, v := range allChartInfo {
  487. v.Children = make([]*models.ChartClassifyItems, 0)
  488. //if n, ok := adminCompanyMap[v.SysUserId]; ok {
  489. allChartInfo[k].Company = utils.HzCompanyName
  490. //}
  491. }
  492. }
  493. page := paging.GetPaging(currentIndex, pageSize, int(total))
  494. resp.AllNodes = allChartInfo
  495. resp.Paging = page
  496. br.Ret = 200
  497. br.Success = true
  498. br.Msg = "获取成功"
  499. br.Data = resp
  500. }