chart_info.go 14 KB

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