chart_info.go 13 KB

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