chart_info.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_ETA_mobile_api/controllers"
  6. "hongze/hongze_ETA_mobile_api/models/data_manage"
  7. "hongze/hongze_ETA_mobile_api/models/system"
  8. "hongze/hongze_ETA_mobile_api/services/data"
  9. "hongze/hongze_ETA_mobile_api/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 图表管理
  15. type ChartInfoController struct {
  16. controllers.BaseAuthController
  17. }
  18. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  19. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  20. adminId := sysUser.AdminId
  21. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  22. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  23. if utils.Re == nil && isCache {
  24. if utils.Re == nil && utils.Rc.IsExist(key) {
  25. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  26. err := json.Unmarshal(data, &resp)
  27. if err == nil && resp != nil {
  28. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  29. var myCond string
  30. var myPars []interface{}
  31. myCond += ` AND a.admin_id=? `
  32. myPars = append(myPars, adminId)
  33. myCond += ` AND a.chart_info_id=? `
  34. myPars = append(myPars, chartInfo.ChartInfoId)
  35. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  36. if err != nil && err.Error() != utils.ErrNoRow() {
  37. msg = "获取失败"
  38. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  39. return
  40. }
  41. resp.ChartInfo.IsAdd = false
  42. resp.ChartInfo.MyChartId = 0
  43. resp.ChartInfo.MyChartClassifyId = ""
  44. if myList != nil && len(myList) > 0 {
  45. resp.ChartInfo.IsAdd = true
  46. resp.ChartInfo.MyChartId = myList[0].MyChartId
  47. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  48. }
  49. isOk = true
  50. fmt.Println("source redis")
  51. return
  52. }
  53. }
  54. }
  55. }
  56. chartInfoId := chartInfo.ChartInfoId
  57. dateType := chartInfo.DateType
  58. if dateType <= 0 {
  59. dateType = 3
  60. }
  61. startDate := chartInfo.StartDate
  62. endDate := chartInfo.EndDate
  63. seasonStartDate := chartInfo.SeasonStartDate
  64. seasonEndDate := chartInfo.SeasonEndDate
  65. calendar := chartInfo.Calendar
  66. chartType := chartInfo.ChartType
  67. if calendar == "" {
  68. calendar = "公历"
  69. }
  70. fmt.Println("a dateType:", dateType)
  71. switch dateType {
  72. case 1:
  73. startDate = "2000-01-01"
  74. endDate = ""
  75. case 2:
  76. startDate = "2010-01-01"
  77. endDate = ""
  78. case 3:
  79. startDate = "2015-01-01"
  80. endDate = ""
  81. case 4:
  82. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  83. startDate = "2021-01-01"
  84. endDate = ""
  85. case 5:
  86. startDate = startDate + "-01"
  87. endDate = endDate + "-01"
  88. case 6:
  89. //startDate = startDate + "-01"
  90. endDate = ""
  91. case 7:
  92. startDate = "2018-01-01"
  93. endDate = ""
  94. case 8:
  95. startDate = "2019-01-01"
  96. endDate = ""
  97. case 9:
  98. startDate = "2020-01-01"
  99. endDate = ""
  100. endDate = ""
  101. case 11:
  102. startDate = "2022-01-01"
  103. endDate = ""
  104. }
  105. // 兼容日期错误
  106. {
  107. if strings.Count(startDate, "-") == 1 {
  108. startDate = startDate + "-01"
  109. }
  110. if strings.Count(endDate, "-") == 1 {
  111. endDate = endDate + "-01"
  112. }
  113. }
  114. if chartType == 2 {
  115. if seasonStartDate != "" {
  116. startDate = seasonStartDate + "-01-01"
  117. } else {
  118. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  119. startDate = strconv.Itoa(fivePre) + "-01-01"
  120. }
  121. if seasonEndDate != "" {
  122. endDate = seasonEndDate + "-12-31"
  123. } else {
  124. //endDate = time.Now().Format(utils.FormatDate)
  125. }
  126. }
  127. mappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  128. if err != nil {
  129. msg = "获取失败"
  130. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  131. return
  132. }
  133. extraConfigStr := chartInfo.ExtraConfig //图表额外数据参数
  134. var barConfig data_manage.BarChartInfoReq
  135. // 柱方图的一些配置
  136. if chartInfo != nil && chartInfo.ChartType == 7 {
  137. if chartInfo.BarConfig == `` {
  138. msg = "柱方图未配置"
  139. errMsg = "柱方图未配置"
  140. return
  141. }
  142. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  143. if err != nil {
  144. msg = "柱方图配置异常"
  145. errMsg = "柱方图配置异常"
  146. return
  147. }
  148. extraConfigStr = chartInfo.BarConfig
  149. }
  150. // 获取图表中的指标数据
  151. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr)
  152. if err != nil {
  153. msg = "获取失败"
  154. if errMsg != `` {
  155. msg = errMsg
  156. }
  157. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  158. return
  159. }
  160. warnEdbList := make([]string, 0)
  161. for _, v := range edbList {
  162. if v.IsNullData {
  163. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  164. }
  165. }
  166. if len(warnEdbList) > 0 {
  167. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  168. }
  169. /*
  170. ChartClassify []*ChartClassifyView
  171. */
  172. chartClassify, _ := data_manage.GetChartClassifyViewById(chartInfo.ChartClassifyId)
  173. if chartClassify != nil {
  174. chartClassifyParent, _ := data_manage.GetChartClassifyViewById(chartClassify.ParentId)
  175. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartClassifyParent)
  176. }
  177. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartClassify)
  178. resp.EdbInfoList = edbList
  179. //判断是否需要展示英文标识
  180. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  181. //判断是否加入我的图库
  182. {
  183. var myChartCondition string
  184. var myChartPars []interface{}
  185. myChartCondition += ` AND a.admin_id=? `
  186. myChartPars = append(myChartPars, adminId)
  187. myChartCondition += ` AND a.chart_info_id=? `
  188. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  189. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  190. if err != nil && err.Error() != utils.ErrNoRow() {
  191. msg = "获取失败"
  192. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  193. return
  194. }
  195. if myChartList != nil && len(myChartList) > 0 {
  196. chartInfo.IsAdd = true
  197. chartInfo.MyChartId = myChartList[0].MyChartId
  198. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  199. }
  200. }
  201. //图表操作权限
  202. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId)
  203. resp.ChartInfo = chartInfo
  204. resp.XEdbIdValue = xEdbIdValue
  205. resp.YDataList = yDataList
  206. resp.Status = true
  207. resp.BarChartInfo = barConfig
  208. resp.DataResp = dataResp
  209. // 将数据加入缓存
  210. if utils.Re == nil {
  211. data, _ := json.Marshal(resp)
  212. utils.Rc.Put(key, data, 2*time.Hour)
  213. }
  214. isOk = true
  215. return
  216. }