chart_common.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers/data_manage/correlation"
  5. "eta/eta_api/controllers/data_manage/cross_variety"
  6. "eta/eta_api/controllers/data_manage/future_good"
  7. "eta/eta_api/controllers/data_manage/line_equation"
  8. "eta/eta_api/controllers/data_manage/line_feature"
  9. "eta/eta_api/controllers/data_manage/range_analysis"
  10. "eta/eta_api/models"
  11. "eta/eta_api/models/data_manage"
  12. "eta/eta_api/models/system"
  13. "eta/eta_api/services/alarm_msg"
  14. "eta/eta_api/services/data"
  15. "eta/eta_api/services/data/excel"
  16. "eta/eta_api/utils"
  17. "fmt"
  18. "strings"
  19. "time"
  20. )
  21. // CommonChartInfoDetailFromUniqueCode
  22. // @Title 根据编码获取图表详情
  23. // @Description 根据编码获取图表详情接口
  24. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  25. // @Param IsCache query bool true "是否走缓存,默认false"
  26. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  27. // @router /chart_info/common/detail/from_unique_code [get]
  28. func (this *ChartInfoController) CommonChartInfoDetailFromUniqueCode() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. sysUser := this.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. uniqueCode := this.GetString("UniqueCode")
  42. if uniqueCode == "" {
  43. br.Msg = "参数错误"
  44. br.ErrMsg = "参数错误,uniqueCode is empty"
  45. return
  46. }
  47. //是否走缓存
  48. isCache, _ := this.GetBool("IsCache")
  49. status := true
  50. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  51. if err != nil {
  52. if err.Error() == utils.ErrNoRow() {
  53. status = false
  54. } else {
  55. br.Msg = "获取失败"
  56. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  57. return
  58. }
  59. }
  60. if chartInfo == nil {
  61. status = false
  62. }
  63. if !status {
  64. resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  65. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  66. resp.EdbInfoList = endInfoList
  67. resp.ChartInfo = chartInfo
  68. resp.Status = false
  69. br.Data = resp
  70. br.Ret = 200
  71. br.Success = true
  72. br.Msg = "获取成功"
  73. return
  74. }
  75. switch chartInfo.Source {
  76. case utils.CHART_SOURCE_DEFAULT:
  77. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  78. if !isOk {
  79. if strings.Contains(errMsg, utils.ErrNoRow()) {
  80. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  81. resp.EdbInfoList = endInfoList
  82. resp.ChartInfo = chartInfo
  83. resp.Status = false
  84. br.Data = resp
  85. br.Ret = 200
  86. br.Success = true
  87. br.Msg = "获取成功"
  88. return
  89. }
  90. br.Msg = msg
  91. br.ErrMsg = errMsg
  92. return
  93. }
  94. br.Ret = 200
  95. br.Success = true
  96. br.Msg = "获取成功"
  97. br.Data = resp
  98. case utils.CHART_SOURCE_FUTURE_GOOD:
  99. resp, isOk, msg, errMsg := future_good.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  100. if !isOk {
  101. if strings.Contains(errMsg, utils.ErrNoRow()) {
  102. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  103. resp.EdbInfoList = endInfoList
  104. resp.ChartInfo = chartInfo
  105. resp.Status = false
  106. br.Data = resp
  107. br.Ret = 200
  108. br.Success = true
  109. br.Msg = "获取成功"
  110. return
  111. }
  112. br.Msg = msg
  113. br.ErrMsg = errMsg
  114. return
  115. }
  116. br.Ret = 200
  117. br.Success = true
  118. br.Msg = "获取成功"
  119. br.Data = resp
  120. case utils.CHART_SOURCE_FUTURE_GOOD_PROFIT:
  121. resp, isOk, msg, errMsg := future_good.GetFutureGoodProfitChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  122. if !isOk {
  123. if strings.Contains(errMsg, utils.ErrNoRow()) {
  124. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  125. resp.EdbInfoList = endInfoList
  126. resp.ChartInfo = chartInfo
  127. resp.Status = false
  128. br.Data = resp
  129. br.Ret = 200
  130. br.Success = true
  131. br.Msg = "获取成功"
  132. return
  133. }
  134. br.Msg = msg
  135. br.ErrMsg = errMsg
  136. return
  137. }
  138. br.Ret = 200
  139. br.Success = true
  140. br.Msg = "获取成功"
  141. br.Data = resp
  142. case utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION:
  143. resp, isOk, msg, errMsg := correlation.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  144. if !isOk {
  145. if strings.Contains(errMsg, utils.ErrNoRow()) {
  146. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  147. resp.EdbInfoList = endInfoList
  148. resp.ChartInfo = chartInfo
  149. resp.Status = false
  150. br.Data = resp
  151. br.Ret = 200
  152. br.Success = true
  153. br.Msg = "获取成功"
  154. return
  155. }
  156. br.Msg = msg
  157. br.ErrMsg = errMsg
  158. return
  159. }
  160. br.Ret = 200
  161. br.Success = true
  162. br.Msg = "获取成功"
  163. br.Data = resp
  164. case utils.CHART_SOURCE_LINE_EQUATION:
  165. resp, isOk, msg, errMsg := line_equation.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  166. if !isOk {
  167. if strings.Contains(errMsg, utils.ErrNoRow()) {
  168. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  169. resp.EdbInfoList = endInfoList
  170. resp.ChartInfo = chartInfo
  171. resp.Status = false
  172. br.Data = resp
  173. br.Ret = 200
  174. br.Success = true
  175. br.Msg = "获取成功"
  176. return
  177. }
  178. br.Msg = msg
  179. br.ErrMsg = errMsg
  180. return
  181. }
  182. br.Ret = 200
  183. br.Success = true
  184. br.Msg = "获取成功"
  185. br.Data = resp
  186. case utils.CHART_SOURCE_LINE_FEATURE_STANDARD_DEVIATION, utils.CHART_SOURCE_LINE_FEATURE_PERCENTILE, utils.CHART_SOURCE_LINE_FEATURE_FREQUENCY:
  187. resp, isOk, msg, errMsg := line_feature.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  188. if !isOk {
  189. if strings.Contains(errMsg, utils.ErrNoRow()) {
  190. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  191. resp.EdbInfoList = endInfoList
  192. resp.ChartInfo = chartInfo
  193. resp.Status = false
  194. br.Data = resp
  195. br.Ret = 200
  196. br.Success = true
  197. br.Msg = "获取成功"
  198. return
  199. }
  200. br.Msg = msg
  201. br.ErrMsg = errMsg
  202. return
  203. }
  204. br.Ret = 200
  205. br.Success = true
  206. br.Msg = "获取成功"
  207. br.Data = resp
  208. case utils.CHART_SOURCE_CROSS_HEDGING:
  209. resp, isOk, msg, errMsg := cross_variety.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  210. if !isOk {
  211. if strings.Contains(errMsg, utils.ErrNoRow()) {
  212. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  213. resp.EdbInfoList = endInfoList
  214. resp.ChartInfo = chartInfo
  215. resp.Status = false
  216. br.Data = resp
  217. br.Ret = 200
  218. br.Success = true
  219. br.Msg = "获取成功"
  220. return
  221. }
  222. br.Msg = msg
  223. br.ErrMsg = errMsg
  224. return
  225. }
  226. br.Ret = 200
  227. br.Success = true
  228. br.Msg = "获取成功"
  229. br.Data = resp
  230. case utils.CHART_SOURCE_BALANCE_EXCEL:
  231. resp, isOk, msg, errMsg := getBalanceChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  232. if !isOk {
  233. if strings.Contains(errMsg, utils.ErrNoRow()) {
  234. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  235. resp.EdbInfoList = endInfoList
  236. resp.ChartInfo = chartInfo
  237. resp.Status = false
  238. br.Data = resp
  239. br.Ret = 200
  240. br.Success = true
  241. br.Msg = "获取成功"
  242. return
  243. }
  244. br.Msg = msg
  245. br.ErrMsg = errMsg
  246. return
  247. }
  248. br.Ret = 200
  249. br.Success = true
  250. br.Msg = "获取成功"
  251. br.Data = resp
  252. case utils.CHART_SOURCE_RANGE_ANALYSIS:
  253. resp, isOk, msg, errMsg := range_analysis.GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  254. if !isOk {
  255. if strings.Contains(errMsg, utils.ErrNoRow()) {
  256. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  257. resp.EdbInfoList = endInfoList
  258. resp.ChartInfo = chartInfo
  259. resp.Status = false
  260. br.Data = resp
  261. br.Ret = 200
  262. br.Success = true
  263. br.Msg = "获取成功"
  264. return
  265. }
  266. br.Msg = msg
  267. br.ErrMsg = errMsg
  268. return
  269. }
  270. br.Ret = 200
  271. br.Success = true
  272. br.Msg = "获取成功"
  273. br.Data = resp
  274. default:
  275. br.Msg = "错误的图表"
  276. br.ErrMsg = "错误的图表"
  277. return
  278. }
  279. }
  280. func getBalanceChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  281. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  282. defer func() {
  283. if !isOk {
  284. if errMsg != "" {
  285. uniqueCode := ""
  286. if chartInfo != nil {
  287. uniqueCode = chartInfo.UniqueCode
  288. }
  289. tmpTip := fmt.Sprintf("查询图表详情失败,getBalanceChartInfoDetailFromUniqueCode UniqueCode:%s,err:%s", uniqueCode, errMsg)
  290. utils.FileLog.Info(tmpTip)
  291. go alarm_msg.SendAlarmMsg(tmpTip, 3)
  292. }
  293. }
  294. }()
  295. resp, isOk, msg, errMsg = data.CheckBalanceChartCacheAndPermission(chartInfo, isCache, sysUser)
  296. if isOk {
  297. return
  298. }
  299. msg = `获取失败`
  300. // 相关联指标
  301. mappingListTmp, dataListMap, err, errMsg := excel.GetBalanceExcelChartSingle(chartInfo.ChartInfoId, 0, "")
  302. if err != nil {
  303. errMsg = "获取失败"
  304. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
  305. return
  306. }
  307. var chartInfoResp *data_manage.ChartInfoDetailResp
  308. chartInfoResp, err, errMsg = data.GetBalanceExcelChartDetail(chartInfo, mappingListTmp, sysUser, dataListMap)
  309. if err != nil {
  310. msg = "查询图表详情失败"
  311. errMsg = "查询图表详情失败,Err:" + err.Error()
  312. return
  313. }
  314. resp = &data_manage.ChartInfoDetailFromUniqueCodeResp{
  315. ChartInfo: chartInfoResp.ChartInfo,
  316. Status: true,
  317. EdbInfoList: chartInfoResp.EdbInfoList,
  318. XEdbIdValue: chartInfoResp.XEdbIdValue,
  319. YDataList: chartInfoResp.YDataList,
  320. XDataList: chartInfoResp.XDataList,
  321. BarChartInfo: chartInfoResp.BarChartInfo,
  322. CorrelationChartInfo: chartInfoResp.CorrelationChartInfo,
  323. DataResp: chartInfoResp.DataResp,
  324. }
  325. // 将数据加入缓存
  326. if utils.Re == nil {
  327. cacheData, _ := json.Marshal(resp)
  328. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  329. utils.Rc.Put(key, cacheData, 2*time.Hour)
  330. }
  331. isOk = true
  332. return
  333. }