chart_info.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. package range_analysis
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/controllers"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/data_manage"
  7. "eta/eta_mobile/models/data_manage/chart_theme"
  8. "eta/eta_mobile/models/system"
  9. "eta/eta_mobile/services/data"
  10. "eta/eta_mobile/services/data/data_manage_permission"
  11. rangeServ "eta/eta_mobile/services/data/range_analysis"
  12. "eta/eta_mobile/utils"
  13. "fmt"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // RangeChartChartInfoController 区间计算图表管理
  20. type RangeChartChartInfoController struct {
  21. controllers.BaseAuthController
  22. }
  23. // Preview
  24. // @Title 区间计算图表-预览数据
  25. // @Description 区间计算图表-获取预览数据
  26. // @Param StartDate query string true "自定义开始日期"
  27. // @Param EndDate query string true "自定义结束日期"
  28. // @Param LeadValue query int true "领先值"
  29. // @Param LeadUnit query string true "领先单位:年,天,月,季,周"
  30. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  31. // @router /chart_info/preview [post]
  32. func (this *RangeChartChartInfoController) Preview() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. this.Data["json"] = br
  36. this.ServeJSON()
  37. }()
  38. sysUser := this.SysUser
  39. if sysUser == nil {
  40. br.Msg = "请登录"
  41. br.ErrMsg = "请登录,SysUser Is Empty"
  42. br.Ret = 408
  43. return
  44. }
  45. var req data_manage.ChartRangeAnalysisPreviewReq
  46. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  47. br.Msg = "参数解析异常!"
  48. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  49. return
  50. }
  51. // 基础校验
  52. if len(req.ChartEdbInfoList) == 0 {
  53. br.Msg = "请选择指标"
  54. return
  55. }
  56. err, msg, isSendEmail := rangeServ.CheckChartRangeExtraConfig(req.ExtraConfig)
  57. if err != nil {
  58. br.Msg = msg
  59. br.ErrMsg = err.Error()
  60. br.IsSendEmail = isSendEmail
  61. return
  62. }
  63. // 获取指标信息
  64. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  65. edbInfoMappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  66. for _, v := range req.ChartEdbInfoList {
  67. edbInfoMapping, e := data_manage.GetChartEdbMappingByEdbInfoId(v.EdbInfoId)
  68. if e != nil {
  69. br.Msg = "获取失败"
  70. br.ErrMsg = "获取区间计算图表, A指标mapping信息失败, Err:" + e.Error()
  71. return
  72. }
  73. if v.EdbAliasName != "" {
  74. edbInfoMapping.EdbAliasName = v.EdbAliasName
  75. }
  76. edbInfoMapping.IsAxis = v.IsAxis
  77. edbInfoMappingList = append(edbInfoMappingList, edbInfoMapping)
  78. }
  79. chartInfo := new(data_manage.ChartInfoView)
  80. chartInfo.ChartType = 1
  81. chartInfo.Source = utils.CHART_SOURCE_RANGE_ANALYSIS
  82. chartTheme, err := chart_theme.GetSystemChartTheme(chartInfo.ChartType)
  83. if err != nil {
  84. br.Msg = "获取失败"
  85. br.ErrMsg = "获取图表主题失败, Err:" + err.Error()
  86. return
  87. }
  88. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  89. chartInfo.ChartThemeStyle = chartTheme.Config
  90. // 获取图表x轴y轴
  91. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(0, req.DateType, req.StartYear, req.StartDate, req.EndDate, edbInfoMappingList, &req.ExtraConfig)
  92. if e != nil {
  93. br.Msg = "获取失败"
  94. br.ErrMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  95. return
  96. }
  97. if req.ExtraConfig.DataConvertType > 0 && req.ExtraConfig.DataConvertConf.Unit != "" && req.ExtraConfig.DataConvertConf.Unit != "无" {
  98. chartInfo.Unit = req.ExtraConfig.DataConvertConf.Unit
  99. chartInfo.UnitEn = req.ExtraConfig.DataConvertConf.EnUnit
  100. }
  101. //添加配置信息
  102. if req.ExtraConfig.MultipleGraphConfigId == 0 {
  103. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  104. //MultipleGraphConfigId: 0,
  105. SysUserId: sysUser.AdminId,
  106. SysUserRealName: sysUser.RealName,
  107. ModifyTime: time.Now(),
  108. CreateTime: time.Now(),
  109. }
  110. err = data_manage.AddMultipleGraphConfig(multipleGraphConfig)
  111. if err != nil {
  112. br.Msg = "获取失败"
  113. br.ErrMsg = "新增区间计算图表配置失败, Err: " + err.Error()
  114. return
  115. }
  116. dataResp.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  117. }
  118. resp := new(data_manage.ChartInfoDetailResp)
  119. resp.ChartInfo = chartInfo
  120. resp.EdbInfoList = edbList
  121. resp.XEdbIdValue = xEdbIdValue
  122. resp.DataResp = dataResp
  123. br.Data = resp
  124. br.Ret = 200
  125. br.Success = true
  126. br.Msg = "获取成功"
  127. }
  128. // Detail
  129. // @Title 获取图表详情
  130. // @Description 获取图表详情接口
  131. // @Param ChartInfoId query int true "图表id"
  132. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  133. // @Param StartDate query string true "自定义开始日期"
  134. // @Param EndDate query string true "自定义结束日期"
  135. // @Param Calendar query string true "公历/农历"
  136. // @Param SeasonStartDate query string true "季节性图开始日期"
  137. // @Param SeasonEndDate query string true "季节性图结束日期"
  138. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  139. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  140. // @Success 200 {object} data_manage.ChartInfoDetailResp
  141. // @router /chart_info/detail [get]
  142. func (this *RangeChartChartInfoController) Detail() {
  143. br := new(models.BaseResponse).Init()
  144. defer func() {
  145. this.Data["json"] = br
  146. this.ServeJSON()
  147. }()
  148. sysUser := this.SysUser
  149. if sysUser == nil {
  150. br.Msg = "请登录"
  151. br.ErrMsg = "请登录,SysUser Is Empty"
  152. br.Ret = 408
  153. return
  154. }
  155. chartInfoId, _ := this.GetInt("ChartInfoId")
  156. if chartInfoId <= 0 {
  157. br.Msg = "参数有误"
  158. return
  159. }
  160. dateType, _ := this.GetInt("DateType")
  161. fmt.Println("dateType:", dateType)
  162. startDate := this.GetString("StartDate")
  163. endDate := this.GetString("EndDate")
  164. startYear, _ := this.GetInt("StartYear")
  165. var err error
  166. chartInfo := new(data_manage.ChartInfoView)
  167. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  168. if err != nil {
  169. if err.Error() == utils.ErrNoRow() {
  170. br.Msg = "图被删除,请刷新页面"
  171. br.ErrMsg = "图被删除,请刷新页面,Err:" + err.Error()
  172. return
  173. }
  174. br.Msg = "获取失败"
  175. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  176. return
  177. }
  178. if dateType <= 0 {
  179. dateType = chartInfo.DateType
  180. }
  181. if startDate == "" {
  182. startDate = chartInfo.StartDate
  183. }
  184. if endDate == "" {
  185. endDate = chartInfo.EndDate
  186. }
  187. if startYear <= 0 {
  188. startYear = chartInfo.StartYear
  189. }
  190. // 获取主题样式
  191. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, chartInfo.ChartType)
  192. if err != nil {
  193. br.Msg = "获取失败"
  194. br.ErrMsg = "获取主题信息失败,Err:" + err.Error()
  195. return
  196. }
  197. chartInfo.ChartThemeStyle = chartTheme.Config
  198. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  199. // 获取指标信息
  200. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  201. edbInfoMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  202. if err != nil {
  203. br.Msg = "获取失败"
  204. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  205. return
  206. }
  207. // 获取图表数据
  208. if len(edbInfoMappingList) == 0 {
  209. br.Msg = "获取失败"
  210. br.ErrMsg = "图表没有指标,无法计算"
  211. return
  212. }
  213. // 区间计算图表配置校验
  214. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  215. if chartInfo.ExtraConfig == `` {
  216. br.Msg = "配置信息错误"
  217. return
  218. }
  219. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  220. if err != nil {
  221. br.Msg = "配置信息错误"
  222. br.ErrMsg = "图表配置信息错误,Err:" + err.Error()
  223. return
  224. }
  225. // 获取图表x轴y轴
  226. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, &extraConfig)
  227. if e != nil {
  228. br.Msg = "获取失败"
  229. br.ErrMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  230. return
  231. }
  232. // 判断是否加入我的图库
  233. if chartInfoId > 0 && chartInfo != nil {
  234. {
  235. var myChartCondition string
  236. var myChartPars []interface{}
  237. myChartCondition += ` AND a.admin_id=? `
  238. myChartPars = append(myChartPars, sysUser.AdminId)
  239. myChartCondition += ` AND a.chart_info_id=? `
  240. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  241. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  242. if err != nil && err.Error() != utils.ErrNoRow() {
  243. br.Msg = "获取失败"
  244. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  245. return
  246. }
  247. if myChartList != nil && len(myChartList) > 0 {
  248. chartInfo.IsAdd = true
  249. chartInfo.MyChartId = myChartList[0].MyChartId
  250. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  251. }
  252. }
  253. }
  254. //图表操作权限
  255. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  256. //判断是否需要展示英文标识
  257. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
  258. // todo 中英文处理
  259. if extraConfig.DataConvertType > 0 && extraConfig.DataConvertConf.Unit != "" && extraConfig.DataConvertConf.Unit != "无" {
  260. chartInfo.Unit = extraConfig.DataConvertConf.Unit
  261. chartInfo.UnitEn = extraConfig.DataConvertConf.EnUnit
  262. }
  263. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  264. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  265. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  266. // 另存为
  267. chartInfo.Button = data_manage.ChartViewButton{
  268. IsEdit: chartInfo.IsEdit,
  269. IsEnChart: chartInfo.IsEnChart,
  270. IsAdd: chartInfo.IsAdd,
  271. IsCopy: chartInfo.IsEdit,
  272. IsSetName: chartInfo.IsSetName,
  273. }
  274. // 指标权限
  275. {
  276. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  277. classifyMap := make(map[int]*data_manage.EdbClassify)
  278. // 分类
  279. {
  280. classifyIdList := make([]int, 0)
  281. for _, v := range edbList {
  282. classifyIdList = append(classifyIdList, v.ClassifyId)
  283. }
  284. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  285. if tmpErr != nil {
  286. br.Msg = "获取失败"
  287. br.ErrMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  288. return
  289. }
  290. for _, v := range classifyList {
  291. classifyMap[v.ClassifyId] = v
  292. }
  293. }
  294. // 遍历到校验map
  295. for _, v := range edbList {
  296. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  297. ClassifyId: v.ClassifyId,
  298. IsJoinPermission: v.IsJoinPermission,
  299. EdbInfoId: v.EdbInfoId,
  300. }
  301. }
  302. // 获取所有有权限的指标和分类
  303. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  304. if err != nil {
  305. br.Msg = "获取失败"
  306. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  307. return
  308. }
  309. for _, v := range edbList {
  310. // 数据权限
  311. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  312. if !ok {
  313. continue
  314. }
  315. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  316. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  317. }
  318. }
  319. }
  320. resp := new(data_manage.ChartInfoDetailResp)
  321. resp.ChartInfo = chartInfo
  322. resp.EdbInfoList = edbList
  323. resp.XEdbIdValue = xEdbIdValue
  324. resp.DataResp = dataResp
  325. br.Ret = 200
  326. br.Success = true
  327. br.Msg = "获取成功"
  328. br.Data = resp
  329. }
  330. // DetailFromUniqueCode
  331. // @Title 根据编码获取图表详情
  332. // @Description 根据编码获取图表详情接口
  333. // @Param UniqueCode query int true "图表唯一编码,如果是管理后台访问,传固定字符串:7c69b590249049942070ae9dcd5bf6dc"
  334. // @Param IsCache query bool true "是否走缓存,默认false"
  335. // @Success 200 {object} data_manage.ChartInfoDetailFromUniqueCodeResp
  336. // @router /chart_info/detail/from_unique_code [get]
  337. func (this *RangeChartChartInfoController) DetailFromUniqueCode() {
  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. adminId := sysUser.AdminId
  351. uniqueCode := this.GetString("UniqueCode")
  352. if uniqueCode == "" {
  353. br.Msg = "参数错误"
  354. br.ErrMsg = "参数错误,uniqueCode is empty"
  355. return
  356. }
  357. //是否走缓存
  358. isCache, _ := this.GetBool("IsCache")
  359. resp := new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  360. status := true
  361. chartInfo, err := data_manage.GetChartInfoViewByUniqueCode(uniqueCode)
  362. if err != nil {
  363. if err.Error() == utils.ErrNoRow() {
  364. status = false
  365. } else {
  366. br.Msg = "获取失败"
  367. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  368. return
  369. }
  370. }
  371. if chartInfo == nil {
  372. status = false
  373. }
  374. if !status {
  375. endInfoList := make([]*data_manage.ChartEdbInfoMapping, 0)
  376. resp.EdbInfoList = endInfoList
  377. resp.ChartInfo = chartInfo
  378. resp.Status = false
  379. br.Ret = 200
  380. br.Success = true
  381. br.Msg = "获取成功"
  382. br.Data = resp
  383. return
  384. }
  385. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  386. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  387. if utils.Re == nil && isCache {
  388. if utils.Re == nil && utils.Rc.IsExist(key) {
  389. if d, e := utils.Rc.RedisBytes(key); e == nil {
  390. err := json.Unmarshal(d, &resp)
  391. if err == nil && resp != nil {
  392. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  393. var myCond string
  394. var myPars []interface{}
  395. myCond += ` AND a.admin_id=? `
  396. myPars = append(myPars, adminId)
  397. myCond += ` AND a.chart_info_id=? `
  398. myPars = append(myPars, chartInfo.ChartInfoId)
  399. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  400. if err != nil && err.Error() != utils.ErrNoRow() {
  401. br.Msg = "获取失败"
  402. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  403. return
  404. }
  405. resp.ChartInfo.IsAdd = false
  406. resp.ChartInfo.MyChartId = 0
  407. resp.ChartInfo.MyChartClassifyId = ""
  408. if myList != nil && len(myList) > 0 {
  409. resp.ChartInfo.IsAdd = true
  410. resp.ChartInfo.MyChartId = myList[0].MyChartId
  411. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  412. }
  413. br.Ret = 200
  414. br.Success = true
  415. br.Msg = "获取成功"
  416. br.Data = resp
  417. return
  418. }
  419. }
  420. }
  421. }
  422. resp, isOk, msg, errMsg := GetChartInfoDetailFromUniqueCode(chartInfo, isCache, sysUser)
  423. if !isOk {
  424. br.Msg = msg
  425. br.ErrMsg = errMsg
  426. return
  427. }
  428. br.Ret = 200
  429. br.Success = true
  430. br.Msg = "获取成功"
  431. br.Data = resp
  432. }
  433. // GetChartInfoDetailFromUniqueCode 根据编码获取图表详情
  434. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  435. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  436. adminId := sysUser.AdminId
  437. // 指标数据map
  438. edbClassifyPermissionMap := make(map[int]data_manage_permission.EdbClassifyPermission)
  439. defer func() {
  440. if isOk {
  441. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  442. {
  443. //判断是否加入我的图库
  444. var myChartCondition string
  445. var myChartPars []interface{}
  446. myChartCondition += ` AND a.admin_id=? `
  447. myChartPars = append(myChartPars, adminId)
  448. myChartCondition += ` AND a.chart_info_id=? `
  449. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  450. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  451. if err != nil && err.Error() != utils.ErrNoRow() {
  452. msg = "获取失败"
  453. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  454. return
  455. }
  456. if myChartList != nil && len(myChartList) > 0 {
  457. chartInfo.IsAdd = true
  458. chartInfo.MyChartId = myChartList[0].MyChartId
  459. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  460. }
  461. }
  462. resp.ChartInfo.HaveOperaAuth = true
  463. // 指标权限
  464. {
  465. classifyMap := make(map[int]*data_manage.EdbClassify)
  466. // 分类
  467. {
  468. classifyIdList := make([]int, 0)
  469. for _, v := range resp.EdbInfoList {
  470. classifyIdList = append(classifyIdList, v.ClassifyId)
  471. }
  472. classifyList, tmpErr := data_manage.GetEdbClassifyByIdList(classifyIdList)
  473. if tmpErr != nil {
  474. errMsg = "获取分类列表失败,Err:" + tmpErr.Error()
  475. return
  476. }
  477. for _, v := range classifyList {
  478. classifyMap[v.ClassifyId] = v
  479. }
  480. }
  481. // 指标
  482. if len(edbClassifyPermissionMap) < 0 {
  483. edbInfoIdList := make([]int, 0)
  484. for _, v := range resp.EdbInfoList {
  485. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  486. }
  487. edbInfoList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdList)
  488. if tmpErr != nil {
  489. errMsg = "获取指标列表失败,Err:" + tmpErr.Error()
  490. return
  491. }
  492. for _, v := range edbInfoList {
  493. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  494. ClassifyId: v.ClassifyId,
  495. IsJoinPermission: v.IsJoinPermission,
  496. EdbInfoId: v.EdbInfoId,
  497. }
  498. }
  499. }
  500. // 获取所有有权限的指标和分类
  501. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(sysUser.AdminId, 0, 0)
  502. if err != nil {
  503. errMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  504. return
  505. }
  506. for _, v := range resp.EdbInfoList {
  507. // 数据权限
  508. edbItem, ok := edbClassifyPermissionMap[v.EdbInfoId]
  509. if !ok {
  510. continue
  511. }
  512. if currClassify, ok := classifyMap[edbItem.ClassifyId]; ok {
  513. v.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(edbItem.IsJoinPermission, currClassify.IsJoinPermission, edbItem.EdbInfoId, edbItem.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  514. }
  515. }
  516. }
  517. }
  518. }()
  519. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  520. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  521. if utils.Re == nil && isCache {
  522. if utils.Re == nil && utils.Rc.IsExist(key) {
  523. if chartData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  524. err := json.Unmarshal(chartData, &resp)
  525. if err != nil || resp == nil {
  526. return
  527. }
  528. isOk = true
  529. fmt.Println("source redis")
  530. return
  531. }
  532. }
  533. }
  534. // 获取主题样式
  535. chartTheme, err := data.GetChartThemeConfig(chartInfo.ChartThemeId, utils.CHART_SOURCE_DEFAULT, 1)
  536. if err != nil {
  537. msg = "获取失败"
  538. errMsg = "获取主题信息失败,Err:" + err.Error()
  539. return
  540. }
  541. chartInfo.ChartThemeStyle = chartTheme.Config
  542. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  543. chartInfoId := chartInfo.ChartInfoId
  544. // 获取指标信息
  545. //chartInfo.CorrelationLeadUnit = req.LeadUnit
  546. edbInfoMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  547. if err != nil {
  548. msg = "获取失败"
  549. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  550. return
  551. }
  552. dateType := chartInfo.DateType
  553. // 开始/结束日期
  554. startYear := chartInfo.StartYear
  555. startDate := chartInfo.StartDate
  556. endDate := chartInfo.EndDate
  557. // 区间计算图表配置校验
  558. var extraConfig data_manage.ChartRangeAnalysisExtraConf
  559. if chartInfo.ExtraConfig == `` {
  560. msg = "配置信息错误"
  561. return
  562. }
  563. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig)
  564. if err != nil {
  565. msg = "配置信息错误"
  566. errMsg = "图表配置信息错误,Err:" + err.Error()
  567. return
  568. }
  569. // 获取图表数据
  570. if len(edbInfoMappingList) == 0 {
  571. msg = "获取失败"
  572. errMsg = "图表没有指标,无法计算"
  573. return
  574. }
  575. // 获取图表x轴y轴
  576. edbList, xEdbIdValue, dataResp, e := rangeServ.GetChartDataByEdbInfoList(chartInfoId, dateType, startYear, startDate, endDate, edbInfoMappingList, &extraConfig)
  577. if e != nil {
  578. msg = "获取失败"
  579. errMsg = "获取区间计算图表, 图表计算值失败, Err:" + e.Error()
  580. return
  581. }
  582. if chartInfoId > 0 && chartInfo != nil {
  583. //判断是否加入我的图库
  584. {
  585. var myChartCondition string
  586. var myChartPars []interface{}
  587. myChartCondition += ` AND a.admin_id=? `
  588. myChartPars = append(myChartPars, sysUser.AdminId)
  589. myChartCondition += ` AND a.chart_info_id=? `
  590. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  591. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  592. if err != nil && err.Error() != utils.ErrNoRow() {
  593. msg = "获取失败"
  594. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  595. return
  596. }
  597. if myChartList != nil && len(myChartList) > 0 {
  598. chartInfo.IsAdd = true
  599. chartInfo.MyChartId = myChartList[0].MyChartId
  600. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  601. }
  602. }
  603. }
  604. //图表操作权限
  605. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  606. //判断是否需要展示英文标识
  607. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
  608. chartInfo.UnitEn = ""
  609. // 另存为
  610. chartInfo.Button = data_manage.ChartViewButton{
  611. IsEdit: chartInfo.IsEdit,
  612. IsEnChart: chartInfo.IsEnChart,
  613. IsAdd: chartInfo.IsAdd,
  614. IsCopy: chartInfo.IsEdit,
  615. IsSetName: chartInfo.IsSetName,
  616. }
  617. // 图表的指标来源
  618. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  619. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  620. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  621. resp.ChartInfo = chartInfo
  622. resp.EdbInfoList = edbList
  623. resp.XEdbIdValue = xEdbIdValue
  624. resp.DataResp = dataResp
  625. resp.Status = true
  626. // 遍历到校验map
  627. for _, v := range edbList {
  628. edbClassifyPermissionMap[v.EdbInfoId] = data_manage_permission.EdbClassifyPermission{
  629. ClassifyId: v.ClassifyId,
  630. IsJoinPermission: v.IsJoinPermission,
  631. EdbInfoId: v.EdbInfoId,
  632. }
  633. }
  634. // 将数据加入缓存
  635. if utils.Re == nil {
  636. d, _ := json.Marshal(resp)
  637. _ = utils.Rc.Put(key, d, 2*time.Hour)
  638. }
  639. isOk = true
  640. return
  641. }
  642. // List
  643. // @Title 区间计算图表列表接口
  644. // @Description 区间计算图表列表接口
  645. // @Param PageSize query int true "每页数据条数"
  646. // @Param CurrentIndex query int true "当前页页码,从1开始"
  647. // @Param ChartClassifyId query int true "分类id"
  648. // @Param Keyword query string true "搜索关键词"
  649. // @Param IsShowMe query bool true "是否只看我的,true、false"
  650. // @Param Source query int true "图表类型,3:区间计算,4:滚动区间计算"
  651. // @Success 200 {object} data_manage.ChartListResp
  652. // @router /chart_info/list [get]
  653. func (this *RangeChartChartInfoController) List() {
  654. br := new(models.BaseResponse).Init()
  655. defer func() {
  656. this.Data["json"] = br
  657. this.ServeJSON()
  658. }()
  659. sysUser := this.SysUser
  660. if sysUser == nil {
  661. br.Msg = "请登录"
  662. br.ErrMsg = "请登录,SysUser Is Empty"
  663. br.Ret = 408
  664. return
  665. }
  666. chartClassifyId, _ := this.GetInt("ChartClassifyId")
  667. pageSize, _ := this.GetInt("PageSize")
  668. currentIndex, _ := this.GetInt("CurrentIndex")
  669. keyword := this.GetString("KeyWord")
  670. var total int
  671. page := paging.GetPaging(currentIndex, pageSize, total)
  672. var startSize int
  673. if pageSize <= 0 {
  674. pageSize = utils.PageSize20
  675. }
  676. if currentIndex <= 0 {
  677. currentIndex = 1
  678. }
  679. startSize = paging.StartIndex(currentIndex, pageSize)
  680. source := utils.CHART_SOURCE_RANGE_ANALYSIS
  681. var condition string
  682. var pars []interface{}
  683. // 普通图表
  684. condition += ` AND source = ? `
  685. pars = append(pars, source)
  686. if chartClassifyId > 0 {
  687. chartClassifyId, err := data_manage.GetChartClassify(chartClassifyId)
  688. if err != nil && err.Error() != utils.ErrNoRow() {
  689. br.Msg = "获取图表信息失败"
  690. br.ErrMsg = "获取信息失败,GetChartClassify,Err:" + err.Error()
  691. return
  692. }
  693. condition += " AND chart_classify_id IN(" + chartClassifyId + ") "
  694. }
  695. if keyword != "" {
  696. //将关键词按照空格分割
  697. keywords := strings.Split(keyword, " ")
  698. condition += ` AND ( chart_name LIKE '%` + keywords[0] + `%' `
  699. for k, key := range keywords {
  700. if k == 0 {
  701. continue
  702. }
  703. condition += ` OR chart_name LIKE '%` + key + `%' `
  704. }
  705. condition += ` )`
  706. }
  707. //只看我的
  708. isShowMe, _ := this.GetBool("IsShowMe")
  709. if isShowMe {
  710. condition += ` AND sys_user_id = ? `
  711. pars = append(pars, sysUser.AdminId)
  712. }
  713. // 获取当前账号的不可见指标
  714. noPermissionChartIdList := make([]int, 0)
  715. {
  716. obj := data_manage.EdbInfoNoPermissionAdmin{}
  717. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  718. if err != nil && err.Error() != utils.ErrNoRow() {
  719. br.Msg = "获取失败"
  720. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  721. return
  722. }
  723. for _, v := range confList {
  724. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  725. }
  726. }
  727. lenNoPermissionChartIdList := len(noPermissionChartIdList)
  728. if lenNoPermissionChartIdList > 0 {
  729. condition += ` AND chart_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionChartIdList) + `) `
  730. pars = append(pars, noPermissionChartIdList)
  731. }
  732. //获取图表信息
  733. list, err := data_manage.GetChartListByCondition(condition, pars, startSize, pageSize)
  734. if err != nil && err.Error() != utils.ErrNoRow() {
  735. br.Success = true
  736. br.Msg = "获取图表信息失败"
  737. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  738. return
  739. }
  740. myChartList, err := data_manage.GetMyChartListByAdminId(sysUser.AdminId)
  741. if err != nil && err.Error() != utils.ErrNoRow() {
  742. br.Msg = "获取图表信息失败"
  743. br.ErrMsg = "获取我的图表信息失败,Err:" + err.Error()
  744. return
  745. }
  746. myChartMap := make(map[int]*data_manage.MyChartView)
  747. for _, v := range myChartList {
  748. myChartMap[v.ChartInfoId] = v
  749. }
  750. listLen := len(list)
  751. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  752. if listLen > 0 {
  753. chartInfoIds := ""
  754. for _, v := range list {
  755. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  756. }
  757. if chartInfoIds != "" {
  758. chartInfoIds = strings.Trim(chartInfoIds, ",")
  759. //判断是否需要展示英文标识
  760. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  761. if e != nil {
  762. br.Msg = "获取失败"
  763. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  764. return
  765. }
  766. for _, v := range edbList {
  767. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  768. }
  769. }
  770. }
  771. for i := 0; i < listLen; i++ {
  772. //判断是否需要展示英文标识
  773. if edbTmpList, ok := chartEdbMap[list[i].ChartInfoId]; ok {
  774. list[i].IsEnChart = data.CheckIsEnChart(list[i].ChartNameEn, edbTmpList, list[i].Source, list[i].ChartType)
  775. }
  776. if existItem, ok := myChartMap[list[i].ChartInfoId]; ok {
  777. list[i].IsAdd = true
  778. list[i].MyChartId = existItem.MyChartId
  779. list[i].MyChartClassifyId = existItem.MyChartClassifyId
  780. }
  781. }
  782. resp := new(data_manage.ChartListResp)
  783. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  784. items := make([]*data_manage.ChartInfoView, 0)
  785. resp.Paging = page
  786. resp.List = items
  787. br.Ret = 200
  788. br.Success = true
  789. br.Msg = "获取成功"
  790. return
  791. }
  792. dataCount, err := data_manage.GetChartListCountByCondition(condition, pars)
  793. if err != nil && err.Error() != utils.ErrNoRow() {
  794. br.Msg = "获取指标信息失败"
  795. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  796. return
  797. }
  798. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  799. resp.Paging = page
  800. resp.List = list
  801. br.Ret = 200
  802. br.Success = true
  803. br.Msg = "获取成功"
  804. br.Data = resp
  805. }
  806. // Copy
  807. // @Title 复制并新增图表接口
  808. // @Description 新增图表接口
  809. // @Params request body data_manage.CopyAddChartInfoReq true "type json string"
  810. // @Success 200 {object} data_manage.AddChartInfoResp
  811. // @router /chart_info/copy [post]
  812. func (this *RangeChartChartInfoController) Copy() {
  813. br := new(models.BaseResponse).Init()
  814. defer func() {
  815. if br.ErrMsg == "" {
  816. br.IsSendEmail = false
  817. }
  818. this.Data["json"] = br
  819. this.ServeJSON()
  820. }()
  821. sysUser := this.SysUser
  822. if sysUser == nil {
  823. br.Msg = "请登录"
  824. br.ErrMsg = "请登录,SysUser Is Empty"
  825. br.Ret = 408
  826. return
  827. }
  828. var req data_manage.CopyAddChartInfoReq
  829. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  830. if err != nil {
  831. br.Msg = "参数解析异常!"
  832. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  833. return
  834. }
  835. if req.ChartInfoId <= 0 {
  836. br.Msg = "参数有误"
  837. br.ErrMsg = fmt.Sprintf("参数有误, ChartInfoId: %d", req.ChartInfoId)
  838. return
  839. }
  840. req.ChartName = strings.TrimSpace(req.ChartName)
  841. if req.ChartName == "" {
  842. br.Msg = "请输入图表名称"
  843. return
  844. }
  845. if req.ChartClassifyId <= 0 {
  846. br.Msg = "请选择图表分类"
  847. return
  848. }
  849. deleteCache := true
  850. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  851. defer func() {
  852. if deleteCache {
  853. utils.Rc.Delete(cacheKey)
  854. }
  855. }()
  856. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  857. deleteCache = false
  858. br.Msg = "系统处理中,请稍后重试!"
  859. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  860. return
  861. }
  862. chartSource := utils.CHART_SOURCE_RANGE_ANALYSIS
  863. // 校验分类、图表名称
  864. {
  865. var cond string
  866. var pars []interface{}
  867. switch this.Lang {
  868. case utils.EnLangVersion:
  869. cond += " AND chart_name_en = ? AND source = ? "
  870. default:
  871. cond += " AND chart_name = ? AND source = ? "
  872. }
  873. pars = append(pars, req.ChartName, chartSource)
  874. count, e := data_manage.GetChartInfoCountByCondition(cond, pars)
  875. if e != nil {
  876. br.Msg = "保存失败"
  877. br.ErrMsg = fmt.Sprintf("获取同名图表失败, Err: %v", e)
  878. return
  879. }
  880. if count > 0 {
  881. br.Msg = "图表名称已存在, 请重新填写"
  882. return
  883. }
  884. _, e = data_manage.GetChartClassifyById(req.ChartClassifyId)
  885. if e != nil {
  886. if e.Error() == utils.ErrNoRow() {
  887. br.Msg = "分类不存在"
  888. return
  889. }
  890. br.Msg = "保存失败"
  891. br.ErrMsg = fmt.Sprintf("获取图表分类失败, Err: %v", e)
  892. return
  893. }
  894. }
  895. // 图表信息
  896. originChart, e := data_manage.GetChartInfoById(req.ChartInfoId)
  897. if e != nil {
  898. if e.Error() == utils.ErrNoRow() {
  899. br.Msg = "原图表不存在"
  900. return
  901. }
  902. br.Msg = "保存失败"
  903. br.ErrMsg = fmt.Sprintf("获取原图表信息失败, Err: %v", e)
  904. return
  905. }
  906. if originChart.Source == utils.CHART_SOURCE_ROLLING_CORRELATION {
  907. br.Msg = `滚动区间计算图不支持另存为`
  908. br.IsSendEmail = false
  909. return
  910. }
  911. // 普通区间计算图表
  912. chartInfo := new(data_manage.ChartInfo)
  913. newChart, err, errMsg, isSendEmail := rangeServ.CopyChartInfo(req.ChartClassifyId, req.ChartName, originChart, sysUser, this.Lang)
  914. chartInfo = newChart
  915. if err != nil {
  916. br.Msg = "保存失败"
  917. if errMsg != `` {
  918. br.Msg = errMsg
  919. }
  920. br.ErrMsg = err.Error()
  921. br.IsSendEmail = isSendEmail
  922. return
  923. }
  924. // 新增操作日志
  925. {
  926. chartLog := new(data_manage.ChartInfoLog)
  927. chartLog.ChartInfoId = chartInfo.ChartInfoId
  928. chartLog.ChartName = req.ChartName
  929. chartLog.ChartClassifyId = req.ChartClassifyId
  930. chartLog.SysUserId = sysUser.AdminId
  931. chartLog.SysUserRealName = sysUser.RealName
  932. chartLog.UniqueCode = chartInfo.UniqueCode
  933. chartLog.CreateTime = time.Now()
  934. chartLog.Content = string(this.Ctx.Input.RequestBody)
  935. chartLog.Status = "复制区间计算图表"
  936. chartLog.Method = this.Ctx.Input.URI()
  937. go data_manage.AddChartInfoLog(chartLog)
  938. }
  939. br.Ret = 200
  940. br.Success = true
  941. br.Msg = "保存成功"
  942. br.Data = data_manage.AddChartInfoResp{
  943. ChartInfoId: chartInfo.ChartInfoId,
  944. UniqueCode: chartInfo.UniqueCode,
  945. ChartType: chartInfo.ChartType,
  946. }
  947. br.IsAddLog = true
  948. }
  949. // Refresh
  950. // @Title 图表刷新接口
  951. // @Description 图表刷新接口
  952. // @Param ChartInfoId query int true "图表id"
  953. // @Param UniqueCode query string true "唯一code"
  954. // @Success Ret=200 刷新成功
  955. // @router /chart_info/refresh [get]
  956. func (this *RangeChartChartInfoController) Refresh() {
  957. br := new(models.BaseResponse).Init()
  958. defer func() {
  959. this.Data["json"] = br
  960. this.ServeJSON()
  961. }()
  962. sysUser := this.SysUser
  963. if sysUser == nil {
  964. br.Msg = "请登录"
  965. br.ErrMsg = "请登录,SysUser Is Empty"
  966. br.Ret = 408
  967. return
  968. }
  969. chartInfoId, _ := this.GetInt("ChartInfoId")
  970. uniqueCode := this.GetString("UniqueCode")
  971. if chartInfoId <= 0 && uniqueCode == `` {
  972. br.Msg = "参数错误"
  973. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  974. return
  975. }
  976. var chartInfo *data_manage.ChartInfo
  977. var err error
  978. if chartInfoId > 0 {
  979. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  980. } else {
  981. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  982. }
  983. if err != nil {
  984. if err.Error() == utils.ErrNoRow() {
  985. br.Msg = "图表已被删除,无需刷新"
  986. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  987. return
  988. }
  989. br.Msg = "刷新失败"
  990. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  991. return
  992. }
  993. // 刷新区间计算图表
  994. isAsync, e := rangeServ.ChartInfoRefresh(chartInfo.ChartInfoId, chartInfo.UniqueCode)
  995. if e != nil {
  996. br.Msg = "刷新失败"
  997. br.ErrMsg = "刷新区间计算图表失败, Err:" + err.Error()
  998. return
  999. }
  1000. // 多因子区间计算异步刷新, 前端提示
  1001. if isAsync {
  1002. br.Ret = 200
  1003. br.Success = true
  1004. br.Msg = "刷新时间较长, 请10分钟后查看"
  1005. return
  1006. }
  1007. //清除图表缓存
  1008. {
  1009. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  1010. _ = utils.Rc.Delete(key)
  1011. }
  1012. br.Ret = 200
  1013. br.Success = true
  1014. br.Msg = "刷新成功"
  1015. }
  1016. // BaseInfoEdit
  1017. // @Title 编辑图表基础信息接口
  1018. // @Description 编辑图表基础信息接口
  1019. // @Param request body data_manage.EditChartInfoBaseReq true "type json string"
  1020. // @Success Ret=200 编辑成功
  1021. // @router /chart_info/base/edit [post]
  1022. func (this *RangeChartChartInfoController) BaseInfoEdit() {
  1023. br := new(models.BaseResponse).Init()
  1024. defer func() {
  1025. this.Data["json"] = br
  1026. this.ServeJSON()
  1027. }()
  1028. sysUser := this.SysUser
  1029. if sysUser == nil {
  1030. br.Msg = "请登录"
  1031. br.ErrMsg = "请登录,SysUser Is Empty"
  1032. br.Ret = 408
  1033. return
  1034. }
  1035. var req data_manage.EditChartRangeBaseReq
  1036. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1037. if err != nil {
  1038. br.Msg = "参数解析异常!"
  1039. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1040. return
  1041. }
  1042. req.ChartName = strings.Trim(req.ChartName, " ")
  1043. if req.ChartInfoId <= 0 {
  1044. br.Msg = "请选择图表"
  1045. return
  1046. }
  1047. if req.ChartName == "" {
  1048. br.Msg = "请输入图表名称"
  1049. return
  1050. }
  1051. chartItem, e := data_manage.GetChartInfoById(req.ChartInfoId)
  1052. if e != nil {
  1053. if e.Error() == utils.ErrNoRow() {
  1054. br.Msg = "图表已被删除, 请刷新页面"
  1055. return
  1056. }
  1057. br.Msg = "获取图表信息失败"
  1058. br.ErrMsg = "获取图表信息失败, Err: " + e.Error()
  1059. return
  1060. }
  1061. if chartItem.Source != utils.CHART_SOURCE_RANGE_ANALYSIS {
  1062. br.Msg = "该图不是区间计算图表"
  1063. return
  1064. }
  1065. var condition string
  1066. var pars []interface{}
  1067. condition += " AND chart_info_id <> ? AND source = ? "
  1068. pars = append(pars, req.ChartInfoId, utils.CHART_SOURCE_RANGE_ANALYSIS)
  1069. switch this.Lang {
  1070. case utils.EnLangVersion:
  1071. condition += " AND chart_name_en = ? "
  1072. default:
  1073. condition += " AND chart_name = ? "
  1074. }
  1075. pars = append(pars, req.ChartName)
  1076. existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  1077. if err != nil {
  1078. if err.Error() != utils.ErrNoRow() {
  1079. br.Msg = "判断英文图表名称是否存在失败"
  1080. br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
  1081. return
  1082. }
  1083. }
  1084. if err == nil && existItem.ChartInfoId > 0 {
  1085. br.Msg = existItem.ChartName + ":" + req.ChartName + "图表名称已存在"
  1086. return
  1087. }
  1088. switch this.Lang {
  1089. case utils.EnLangVersion:
  1090. chartItem.ChartNameEn = req.ChartName
  1091. default:
  1092. chartItem.ChartName = req.ChartName
  1093. }
  1094. chartItem.ModifyTime = time.Now().Local()
  1095. if e := chartItem.Update([]string{"ChartName", "ChartNameEn", "ModifyTime"}); e != nil {
  1096. br.Msg = "操作失败"
  1097. br.ErrMsg = "更新图表信息失败, Err: " + e.Error()
  1098. return
  1099. }
  1100. //添加es数据
  1101. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1102. //修改my eta es数据
  1103. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1104. //新增操作日志
  1105. {
  1106. chartLog := new(data_manage.ChartInfoLog)
  1107. chartLog.ChartName = chartItem.ChartName
  1108. chartLog.ChartInfoId = req.ChartInfoId
  1109. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  1110. chartLog.SysUserId = sysUser.AdminId
  1111. chartLog.SysUserRealName = sysUser.RealName
  1112. chartLog.UniqueCode = chartItem.UniqueCode
  1113. chartLog.CreateTime = time.Now()
  1114. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1115. chartLog.Status = "编辑区间计算图表基础信息"
  1116. chartLog.Method = this.Ctx.Input.URL()
  1117. go data_manage.AddChartInfoLog(chartLog)
  1118. }
  1119. // 清除缓存
  1120. if utils.Re == nil && utils.Rc != nil {
  1121. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
  1122. _ = utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
  1123. }
  1124. br.Ret = 200
  1125. br.Success = true
  1126. br.Msg = "编辑成功"
  1127. br.IsAddLog = true
  1128. }
  1129. // @Title 保存图表接口
  1130. // @Description 保存图表接口
  1131. // @Param request body data_manage.SaveChartInfoReq true "type json string"
  1132. // @Success Ret=200 返回图表id
  1133. // @router /chart_info/save [post]
  1134. func (this *RangeChartChartInfoController) ChartInfoSave() {
  1135. br := new(models.BaseResponse).Init()
  1136. defer func() {
  1137. this.Data["json"] = br
  1138. this.ServeJSON()
  1139. }()
  1140. sysUser := this.SysUser
  1141. if sysUser == nil {
  1142. br.Msg = "请登录"
  1143. br.ErrMsg = "请登录,SysUser Is Empty"
  1144. br.Ret = 408
  1145. return
  1146. }
  1147. var req data_manage.SaveChartRangeReq
  1148. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1149. if err != nil {
  1150. br.Msg = "参数解析异常!"
  1151. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1152. return
  1153. }
  1154. if req.ChartInfoId <= 0 {
  1155. br.Msg = "参数错误!"
  1156. return
  1157. }
  1158. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  1159. if err != nil {
  1160. if err.Error() == utils.ErrNoRow() {
  1161. br.Msg = "图表已被删除,请刷新页面!"
  1162. br.ErrMsg = "图表已被删除,请刷新页面,ChartInfoId:" + strconv.Itoa(req.ChartInfoId)
  1163. return
  1164. }
  1165. br.Msg = "获取图表信息失败!"
  1166. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1167. return
  1168. }
  1169. //图表操作权限
  1170. //ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId)
  1171. //if !ok {
  1172. // br.Msg = "没有该图表的操作权限"
  1173. // br.ErrMsg = "没有该图表的操作权限"
  1174. // return
  1175. //}
  1176. err = data_manage.EditRangeChartInfo(&req)
  1177. if err != nil {
  1178. br.Msg = "保存失败"
  1179. br.ErrMsg = "保存失败,Err:" + err.Error()
  1180. return
  1181. }
  1182. key := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  1183. if utils.Re == nil && utils.Rc != nil {
  1184. utils.Rc.Delete(key)
  1185. }
  1186. //修改es数据
  1187. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1188. //修改my eta es数据
  1189. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1190. //新增操作日志
  1191. {
  1192. chartLog := new(data_manage.ChartInfoLog)
  1193. chartLog.ChartName = chartItem.ChartName
  1194. chartLog.ChartInfoId = req.ChartInfoId
  1195. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  1196. chartLog.SysUserId = sysUser.AdminId
  1197. chartLog.SysUserRealName = sysUser.RealName
  1198. chartLog.UniqueCode = chartItem.UniqueCode
  1199. chartLog.CreateTime = time.Now()
  1200. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1201. chartLog.Status = "修改配置项"
  1202. chartLog.Method = this.Ctx.Input.URI()
  1203. go data_manage.AddChartInfoLog(chartLog)
  1204. }
  1205. br.Ret = 200
  1206. br.Success = true
  1207. br.Msg = "保存成功"
  1208. br.IsAddLog = true
  1209. }