chart_info.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. errMsg = "面积图配置异常"
  218. err = errors.New(errMsg)
  219. return
  220. }
  221. }
  222. if tmpConfig.IsHeap == 1 {
  223. standardIndexMap := make(map[string]*models.EdbDataList)
  224. var startDate, endDate string
  225. for _, v := range edbDataList {
  226. // 判断是否为基准指标
  227. if v.EdbInfoId == tmpConfig.StandardEdbInfoId {
  228. if dataList, ok := v.DataList.([]*models.EdbDataList); ok {
  229. startDate = dataList[0].DataTime
  230. endDate = dataList[len(dataList)-1].DataTime
  231. for _, dataObject := range dataList {
  232. standardIndexMap[dataObject.DataTime] = dataObject
  233. }
  234. }
  235. break
  236. }
  237. }
  238. strategy, err := area_graph.CreateStrategy(tmpConfig.NullDealWay)
  239. if err != nil {
  240. return err, "创建空值处理器失败"
  241. }
  242. err = strategy.Deal(tmpConfig, edbDataList, standardIndexMap, startDate, endDate)
  243. if err != nil {
  244. return err, err.Error()
  245. }
  246. }
  247. return nil, ""
  248. }
  249. // ChartInfoSearchByEs
  250. // @Title 图表模糊搜索(从es获取)
  251. // @Description 图表模糊搜索(从es获取)
  252. // @Param Keyword query string true "图表名称"
  253. // @Param IsShowMe query bool true "是否只看我的,true、false"
  254. // @Success 200 {object} models.ChartInfo
  255. // @router /search_by_es [get]
  256. func (this *ChartInfoController) ChartInfoSearchByEs() {
  257. br := new(models.BaseResponse).Init()
  258. defer func() {
  259. this.Data["json"] = br
  260. this.ServeJSON()
  261. }()
  262. sysUser := this.SysUser
  263. if sysUser == nil {
  264. br.Msg = "请登录"
  265. br.ErrMsg = "请登录,SysUser Is Empty"
  266. br.Ret = 408
  267. return
  268. }
  269. pageSize, _ := this.GetInt("PageSize")
  270. currentIndex, _ := this.GetInt("CurrentIndex")
  271. var startSize int
  272. if pageSize <= 0 {
  273. pageSize = utils.PageSize20
  274. }
  275. if currentIndex <= 0 {
  276. currentIndex = 1
  277. }
  278. startSize = paging.StartIndex(currentIndex, pageSize)
  279. keyword := this.GetString("Keyword")
  280. //只看我的
  281. sysUserIds := this.GetString("SysUserIds")
  282. adminIdsSlice := make([]int, 0)
  283. if sysUserIds != "" {
  284. adminIds := strings.Split(sysUserIds, ",")
  285. if len(adminIds) == 0 {
  286. br.Msg = "请选择正确的创建人"
  287. return
  288. }
  289. for _, adminId := range adminIds {
  290. adminIdInt, e := strconv.Atoi(adminId)
  291. if e != nil {
  292. br.Msg = "请选择正确的创建人"
  293. return
  294. }
  295. adminIdsSlice = append(adminIdsSlice, adminIdInt)
  296. }
  297. }
  298. var searchList []*models.ChartInfo
  299. var total int64
  300. var err error
  301. if keyword != "" {
  302. searchList, total, err = services.EsSearchChartInfo(keyword, adminIdsSlice, []int{utils.CHART_SOURCE_DEFAULT}, startSize, pageSize)
  303. } else {
  304. total, searchList, err = models.ChartInfoSearchByEmptyKeyWord(adminIdsSlice, []int{utils.CHART_SOURCE_DEFAULT}, startSize, pageSize)
  305. if err != nil && err.Error() != utils.ErrNoRow() {
  306. br.Msg = "获取失败"
  307. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  308. return
  309. }
  310. }
  311. finalList := make([]*models.ChartInfoMore, 0)
  312. if len(searchList) > 0 {
  313. // 涉及分类id
  314. classifyIdList := make([]int, 0)
  315. // 图表id
  316. chartInfoIdList := make([]int, 0)
  317. chartInfoIds := ""
  318. chartEdbMap := make(map[int][]*models.ChartEdbInfoMapping)
  319. for _, v := range searchList {
  320. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  321. classifyIdList = append(classifyIdList, v.ChartClassifyId)
  322. chartInfoIdList = append(chartInfoIdList, v.ChartInfoId)
  323. }
  324. if chartInfoIds != "" {
  325. chartInfoIds = strings.Trim(chartInfoIds, ",")
  326. //判断是否需要展示英文标识
  327. edbList, e := models.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  328. if e != nil {
  329. br.Msg = "获取失败"
  330. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  331. return
  332. }
  333. for _, v := range edbList {
  334. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  335. }
  336. }
  337. // 当前列表中的分类map
  338. chartClassifyMap := make(map[int]*models.ChartClassify)
  339. // 图表分类
  340. {
  341. chartClassifyList, err := models.GetChartClassifyByIdList(classifyIdList)
  342. if err != nil {
  343. br.Msg = "获取失败"
  344. br.ErrMsg = "获取图表分类失败,Err:" + err.Error()
  345. return
  346. }
  347. for _, v := range chartClassifyList {
  348. chartClassifyMap[v.ChartClassifyId] = v
  349. }
  350. }
  351. for _, v := range searchList {
  352. tmp := new(models.ChartInfoMore)
  353. tmp.ChartInfo = *v
  354. //判断是否需要展示英文标识
  355. if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
  356. tmp.IsEnChart = services.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
  357. }
  358. // 图表权限
  359. finalList = append(finalList, tmp)
  360. }
  361. }
  362. //新增搜索词记录
  363. {
  364. searchKeyword := new(models.SearchKeyword)
  365. searchKeyword.KeyWord = keyword
  366. searchKeyword.CreateTime = time.Now()
  367. go models.AddSearchKeyword(searchKeyword)
  368. }
  369. page := paging.GetPaging(currentIndex, pageSize, int(total))
  370. resp := models.ChartInfoListByEsResp{
  371. Paging: page,
  372. List: finalList,
  373. }
  374. br.Ret = 200
  375. br.Success = true
  376. br.Msg = "获取成功"
  377. br.Data = resp
  378. }
  379. // ChartList
  380. // @Title 根据分类创建人姓名等获取图表列表
  381. // @Description 根据分类创建人姓名等获取图表列表
  382. // @Param SysUserIds query string true "根据创建人查询"
  383. // @Param ChartClassifyIds query string true "图片分类id"
  384. // @Success 200 {object} models.ChartClassifyListResp
  385. // @router /list [get]
  386. func (this *ChartInfoController) ChartList() {
  387. br := new(models.BaseResponse).Init()
  388. defer func() {
  389. this.Data["json"] = br
  390. this.ServeJSON()
  391. }()
  392. sysUser := this.SysUser
  393. if sysUser == nil {
  394. br.Msg = "请登录"
  395. br.ErrMsg = "请登录,SysUser Is Empty"
  396. br.Ret = 408
  397. return
  398. }
  399. resp := new(models.ChartClassifyListResp)
  400. pageSize, _ := this.GetInt("PageSize")
  401. currentIndex, _ := this.GetInt("CurrentIndex")
  402. chartClassifyIds := this.GetString("ChartClassifyIds")
  403. sysUserIds := this.GetString("SysUserIds")
  404. chartName := this.GetString("ChartName")
  405. var startSize int
  406. if pageSize <= 0 {
  407. pageSize = utils.PageSize20
  408. }
  409. if currentIndex <= 0 {
  410. currentIndex = 1
  411. }
  412. startSize = utils.StartIndex(currentIndex, pageSize)
  413. condition := ""
  414. pars := make([]interface{}, 0)
  415. if chartClassifyIds != "" {
  416. classifyIds := strings.Split(chartClassifyIds, ",")
  417. if len(classifyIds) == 0 {
  418. br.Msg = "请选择正确的分类"
  419. return
  420. }
  421. classifyIdSlice := make([]int, 0)
  422. for _, id := range classifyIds {
  423. tmp, e := strconv.Atoi(id)
  424. if e != nil {
  425. br.Msg = "请选择正确的分类"
  426. return
  427. }
  428. classifyIdSlice = append(classifyIdSlice, tmp)
  429. }
  430. condition += " AND chart_classify_id in (" + utils.GetOrmInReplace(len(classifyIdSlice)) + ") "
  431. pars = append(pars, classifyIdSlice)
  432. }
  433. if sysUserIds != "" {
  434. adminIds := strings.Split(sysUserIds, ",")
  435. if len(adminIds) == 0 {
  436. br.Msg = "请选择正确的创建人"
  437. return
  438. }
  439. adminIdsSlice := make([]int, 0)
  440. for _, adminId := range adminIds {
  441. adminIdInt, e := strconv.Atoi(adminId)
  442. if e != nil {
  443. br.Msg = "请选择正确的创建人"
  444. return
  445. }
  446. adminIdsSlice = append(adminIdsSlice, adminIdInt)
  447. }
  448. condition += " AND sys_user_id in (" + utils.GetOrmInReplace(len(adminIds)) + ") "
  449. pars = append(pars, adminIdsSlice)
  450. }
  451. if chartName != "" {
  452. condition += " AND chart_name LIKE ? "
  453. pars = append(pars, utils.GetLikeKeyword(chartName))
  454. }
  455. allChartInfo, err := models.GetChartInfoByConditionPage(utils.CHART_SOURCE_DEFAULT, condition, pars, startSize, pageSize)
  456. if err != nil && err.Error() != utils.ErrNoRow() {
  457. br.Msg = "获取失败"
  458. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  459. return
  460. }
  461. total, err := models.GetChartInfoTotalByCondition(utils.CHART_SOURCE_DEFAULT, condition, pars)
  462. if err != nil {
  463. br.Msg = "获取失败"
  464. br.ErrMsg = "获取图表总数失败,Err:" + err.Error()
  465. return
  466. }
  467. if len(allChartInfo) > 0 {
  468. /*adminIds := make([]int, 0)
  469. for _, v := range allChartInfo {
  470. adminIds = append(adminIds, v.SysUserId)
  471. }
  472. adminCompanyMap, er := services.GetCompanyNameByAdmins(adminIds)
  473. if er != nil {
  474. br.Msg = "获取失败"
  475. br.ErrMsg = "获取机构失败,Err:" + er.Error()
  476. return
  477. }*/
  478. for k, v := range allChartInfo {
  479. v.Children = make([]*models.ChartClassifyItems, 0)
  480. //if n, ok := adminCompanyMap[v.SysUserId]; ok {
  481. allChartInfo[k].Company = utils.HzCompanyName
  482. //}
  483. }
  484. }
  485. page := paging.GetPaging(currentIndex, pageSize, int(total))
  486. resp.AllNodes = allChartInfo
  487. resp.Paging = page
  488. br.Ret = 200
  489. br.Success = true
  490. br.Msg = "获取成功"
  491. br.Data = resp
  492. }