chart_info.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. package data_manage
  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/system"
  8. "eta/eta_mobile/services/data"
  9. "eta/eta_mobile/utils"
  10. "fmt"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // 图表管理
  17. type ChartInfoController struct {
  18. controllers.BaseAuthController
  19. }
  20. func GetChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  21. resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)
  22. adminId := sysUser.AdminId
  23. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  24. key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
  25. if utils.Re == nil && isCache {
  26. if utils.Re == nil && utils.Rc.IsExist(key) {
  27. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  28. err := json.Unmarshal(data, &resp)
  29. if err == nil && resp != nil {
  30. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  31. var myCond string
  32. var myPars []interface{}
  33. myCond += ` AND a.admin_id=? `
  34. myPars = append(myPars, adminId)
  35. myCond += ` AND a.chart_info_id=? `
  36. myPars = append(myPars, chartInfo.ChartInfoId)
  37. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  38. if err != nil && err.Error() != utils.ErrNoRow() {
  39. msg = "获取失败"
  40. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  41. return
  42. }
  43. resp.ChartInfo.IsAdd = false
  44. resp.ChartInfo.MyChartId = 0
  45. resp.ChartInfo.MyChartClassifyId = ""
  46. if myList != nil && len(myList) > 0 {
  47. resp.ChartInfo.IsAdd = true
  48. resp.ChartInfo.MyChartId = myList[0].MyChartId
  49. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  50. }
  51. isOk = true
  52. fmt.Println("source redis")
  53. return
  54. }
  55. }
  56. }
  57. }
  58. chartInfoId := chartInfo.ChartInfoId
  59. dateType := chartInfo.DateType
  60. if dateType <= 0 {
  61. dateType = 3
  62. }
  63. startDate := chartInfo.StartDate
  64. endDate := chartInfo.EndDate
  65. seasonStartDate := chartInfo.SeasonStartDate
  66. seasonEndDate := chartInfo.SeasonEndDate
  67. calendar := chartInfo.Calendar
  68. chartType := chartInfo.ChartType
  69. if calendar == "" {
  70. calendar = "公历"
  71. }
  72. fmt.Println("a dateType:", dateType)
  73. switch dateType {
  74. case 1:
  75. startDate = "2000-01-01"
  76. endDate = ""
  77. case 2:
  78. startDate = "2010-01-01"
  79. endDate = ""
  80. case 3:
  81. startDate = "2015-01-01"
  82. endDate = ""
  83. case 4:
  84. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  85. startDate = "2021-01-01"
  86. endDate = ""
  87. case 5:
  88. startDate = startDate + "-01"
  89. endDate = endDate + "-01"
  90. case 6:
  91. //startDate = startDate + "-01"
  92. endDate = ""
  93. case 7:
  94. startDate = "2018-01-01"
  95. endDate = ""
  96. case 8:
  97. startDate = "2019-01-01"
  98. endDate = ""
  99. case 9:
  100. startDate = "2020-01-01"
  101. endDate = ""
  102. endDate = ""
  103. case 11:
  104. startDate = "2022-01-01"
  105. endDate = ""
  106. }
  107. // 兼容日期错误
  108. {
  109. if strings.Count(startDate, "-") == 1 {
  110. startDate = startDate + "-01"
  111. }
  112. if strings.Count(endDate, "-") == 1 {
  113. endDate = endDate + "-01"
  114. }
  115. }
  116. if chartType == 2 {
  117. if seasonStartDate != "" {
  118. startDate = seasonStartDate + "-01"
  119. } else {
  120. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  121. startDate = strconv.Itoa(fivePre) + "-01-01"
  122. }
  123. if seasonEndDate != "" {
  124. seasonEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, seasonEndDate+"-01", time.Local)
  125. if tmpErr != nil {
  126. msg = "获取失败"
  127. errMsg = "获取图表,指标信息失败,Err:" + tmpErr.Error()
  128. return
  129. }
  130. endDate = seasonEndDateTime.AddDate(0, 1, -1).Format(utils.FormatDate)
  131. } else {
  132. //endDate = time.Now().Format(utils.FormatDate)
  133. }
  134. }
  135. mappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  136. if err != nil {
  137. msg = "获取失败"
  138. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  139. return
  140. }
  141. extraConfigStr := chartInfo.ExtraConfig //图表额外数据参数
  142. var barConfig data_manage.BarChartInfoReq
  143. // 柱方图的一些配置
  144. if chartInfo != nil && chartInfo.ChartType == 7 {
  145. if chartInfo.BarConfig == `` {
  146. msg = "柱方图未配置"
  147. errMsg = "柱方图未配置"
  148. return
  149. }
  150. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  151. if err != nil {
  152. msg = "柱方图配置异常"
  153. errMsg = "柱方图配置异常"
  154. return
  155. }
  156. extraConfigStr = chartInfo.BarConfig
  157. }
  158. // 获取图表中的指标数据
  159. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr)
  160. if err != nil {
  161. msg = "获取失败"
  162. if errMsg != `` {
  163. msg = errMsg
  164. }
  165. errMsg = "获取图表,指标信息失败,Err:" + err.Error()
  166. return
  167. }
  168. warnEdbList := make([]string, 0)
  169. for _, v := range edbList {
  170. if v.IsNullData {
  171. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  172. }
  173. }
  174. if len(warnEdbList) > 0 {
  175. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  176. }
  177. /*
  178. ChartClassify []*ChartClassifyView
  179. */
  180. chartClassify, _ := data_manage.GetChartClassifyViewById(chartInfo.ChartClassifyId)
  181. if chartClassify != nil {
  182. chartClassifyParent, _ := data_manage.GetChartClassifyViewById(chartClassify.ParentId)
  183. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartClassifyParent)
  184. }
  185. chartInfo.ChartClassify = append(chartInfo.ChartClassify, chartClassify)
  186. resp.EdbInfoList = edbList
  187. //判断是否需要展示英文标识
  188. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  189. //判断是否加入我的图库
  190. {
  191. var myChartCondition string
  192. var myChartPars []interface{}
  193. myChartCondition += ` AND a.admin_id=? `
  194. myChartPars = append(myChartPars, adminId)
  195. myChartCondition += ` AND a.chart_info_id=? `
  196. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  197. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  198. if err != nil && err.Error() != utils.ErrNoRow() {
  199. msg = "获取失败"
  200. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  201. return
  202. }
  203. if myChartList != nil && len(myChartList) > 0 {
  204. chartInfo.IsAdd = true
  205. chartInfo.MyChartId = myChartList[0].MyChartId
  206. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  207. }
  208. }
  209. //图表操作权限
  210. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId)
  211. chartInfo.Button = data_manage.ChartViewButton{
  212. IsEdit: chartInfo.IsEdit,
  213. IsEnChart: chartInfo.IsEnChart,
  214. IsAdd: chartInfo.IsAdd,
  215. IsCopy: true,
  216. IsSetName: chartInfo.IsSetName,
  217. }
  218. resp.ChartInfo = chartInfo
  219. resp.XEdbIdValue = xEdbIdValue
  220. resp.YDataList = yDataList
  221. resp.Status = true
  222. resp.BarChartInfo = barConfig
  223. resp.DataResp = dataResp
  224. // 将数据加入缓存
  225. if utils.Re == nil {
  226. data, _ := json.Marshal(resp)
  227. utils.Rc.Put(key, data, 2*time.Hour)
  228. }
  229. isOk = true
  230. return
  231. }
  232. // @Title 编辑图表英文信息接口
  233. // @Description 编辑图表英文信息接口
  234. // @Param request body data_manage.EditChartEnInfoReq true "type json string"
  235. // @Success Ret=200 编辑成功
  236. // @router /chart_info/en/edit [post]
  237. func (this *ChartInfoController) ChartEnInfoEdit() {
  238. br := new(models.BaseResponse).Init()
  239. defer func() {
  240. this.Data["json"] = br
  241. this.ServeJSON()
  242. }()
  243. sysUser := this.SysUser
  244. if sysUser == nil {
  245. br.Msg = "请登录"
  246. br.ErrMsg = "请登录,SysUser Is Empty"
  247. br.Ret = 408
  248. return
  249. }
  250. var req data_manage.EditChartEnInfoReq
  251. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  252. if err != nil {
  253. br.Msg = "参数解析异常!"
  254. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  255. return
  256. }
  257. req.ChartNameEn = strings.Trim(req.ChartNameEn, " ")
  258. if req.ChartInfoId <= 0 {
  259. br.Msg = "请选择图表"
  260. return
  261. }
  262. //判断指标名称是否存在
  263. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  264. if err != nil {
  265. if err.Error() == utils.ErrNoRow() {
  266. br.Msg = "图表已被删除,请刷新页面"
  267. br.ErrMsg = "图表已被删除,请刷新页面"
  268. return
  269. }
  270. br.Msg = "获取图表信息失败"
  271. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  272. return
  273. }
  274. // 判断是否有传入对应的指标配置
  275. noEdbInfoType := []int{10}
  276. if len(req.ChartEdbInfoList) <= 0 && !utils.InArrayByInt(noEdbInfoType, chartItem.ChartType) {
  277. br.Msg = "请选择指标!"
  278. return
  279. }
  280. var edbCondition string
  281. var edbPars []interface{}
  282. for _, v := range req.ChartEdbInfoList {
  283. edbInfoId := v.EdbInfoId
  284. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  285. if err != nil {
  286. if err.Error() == utils.ErrNoRow() {
  287. br.Msg = "图表不存在!"
  288. br.ErrMsg = "图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  289. return
  290. } else {
  291. br.Msg = "获取图表信息失败!"
  292. br.ErrMsg = "获取图表的指标信息失败,Err:" + err.Error()
  293. return
  294. }
  295. }
  296. if edbInfo == nil {
  297. br.Msg = "指标不存在!"
  298. br.ErrMsg = "指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  299. return
  300. }
  301. //判断指标名称是否重复
  302. if v.EdbNameEn != "" {
  303. edbCondition = ""
  304. edbPars = make([]interface{}, 0)
  305. edbCondition += " AND edb_info_id<>? "
  306. edbPars = append(edbPars, edbInfo.EdbInfoId)
  307. /*edbCondition += " AND source=? "
  308. edbPars = append(edbPars, edbInfo.Source)*/
  309. edbCondition += " AND edb_name_en=? "
  310. edbPars = append(edbPars, v.EdbNameEn)
  311. edbExist, err := data_manage.GetEdbInfoByCondition(edbCondition, edbPars)
  312. if err != nil {
  313. if err.Error() != utils.ErrNoRow() {
  314. br.Msg = "判断英文指标名称是否存在失败"
  315. br.ErrMsg = "判断英文指标名称是否存在失败,Err:" + err.Error()
  316. return
  317. }
  318. }
  319. if err == nil && edbExist.EdbInfoId > 0 {
  320. br.Msg = edbExist.EdbName + ":" + v.EdbNameEn + "指标名称已存在"
  321. br.ErrMsg = "英文指标名称已存在,请重新填写"
  322. br.IsSendEmail = false
  323. return
  324. }
  325. }
  326. }
  327. if req.ChartNameEn != "" || req.ExtraConfig != `` {
  328. var condition string
  329. var pars []interface{}
  330. condition += " AND chart_info_id<>? "
  331. pars = append(pars, req.ChartInfoId)
  332. /*condition += " AND chart_classify_id=? "
  333. pars = append(pars, chartItem.ChartClassifyId)*/
  334. condition += " AND chart_name_en=? "
  335. pars = append(pars, req.ChartNameEn)
  336. existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
  337. if err != nil {
  338. if err.Error() != utils.ErrNoRow() {
  339. br.Msg = "判断英文图表名称是否存在失败"
  340. br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
  341. return
  342. }
  343. }
  344. if err == nil && existItem.ChartInfoId > 0 {
  345. br.Msg = existItem.ChartName + ":" + req.ChartNameEn + "图表名称已存在"
  346. return
  347. }
  348. }
  349. err = data_manage.EditChartEnInfoAndEdbEnInfo(&req)
  350. if err != nil {
  351. br.Msg = "保存失败"
  352. br.ErrMsg = "保存失败,Err:" + err.Error()
  353. return
  354. }
  355. //添加es数据
  356. go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  357. //修改my eta es数据
  358. go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  359. //edbinfo 修改es信息
  360. for _, v := range req.ChartEdbInfoList {
  361. edbInfoId := v.EdbInfoId
  362. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  363. if err != nil {
  364. if err.Error() == utils.ErrNoRow() {
  365. br.Msg = "图表不存在!"
  366. br.ErrMsg = "图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
  367. return
  368. } else {
  369. br.Msg = "获取图表信息失败!"
  370. br.ErrMsg = "获取图表的指标信息失败,Err:" + err.Error()
  371. return
  372. }
  373. }
  374. if edbInfo != nil {
  375. go data.AddOrEditEdbInfoToEs(edbInfoId)
  376. }
  377. }
  378. //新增操作日志
  379. {
  380. chartLog := new(data_manage.ChartInfoLog)
  381. chartLog.ChartName = chartItem.ChartName
  382. chartLog.ChartInfoId = req.ChartInfoId
  383. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  384. chartLog.SysUserId = sysUser.AdminId
  385. chartLog.SysUserRealName = sysUser.RealName
  386. chartLog.UniqueCode = chartItem.UniqueCode
  387. chartLog.CreateTime = time.Now()
  388. chartLog.Content = string(this.Ctx.Input.RequestBody)
  389. chartLog.Status = "编辑图表英文信息"
  390. chartLog.Method = this.Ctx.Input.URL()
  391. go data_manage.AddChartInfoLog(chartLog)
  392. }
  393. //清除缓存
  394. if utils.Re == nil && utils.Rc != nil {
  395. utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
  396. utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
  397. }
  398. br.Ret = 200
  399. br.Success = true
  400. br.Msg = "编辑成功"
  401. br.IsAddLog = true
  402. }
  403. // ChartInfoMove
  404. // @Title 移动图表接口
  405. // @Description 移动图表接口
  406. // @Success 200 {object} data_manage.MoveChartInfoReq
  407. // @router /chart_info/move [post]
  408. func (this *ChartInfoController) ChartInfoMove() {
  409. br := new(models.BaseResponse).Init()
  410. defer func() {
  411. this.Data["json"] = br
  412. this.ServeJSON()
  413. }()
  414. sysUser := this.SysUser
  415. if sysUser == nil {
  416. br.Msg = "请登录"
  417. br.ErrMsg = "请登录,SysUser Is Empty"
  418. br.Ret = 408
  419. return
  420. }
  421. var req data_manage.MoveChartInfoReq
  422. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  423. if err != nil {
  424. br.Msg = "参数解析异常!"
  425. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  426. return
  427. }
  428. if req.ChartInfoId <= 0 {
  429. br.Msg = "参数错误"
  430. br.ErrMsg = "图表id小于等于0"
  431. return
  432. }
  433. if req.ChartClassifyId <= 0 {
  434. br.Msg = "请选择分类"
  435. return
  436. }
  437. //判断分类是否存在
  438. count, _ := data_manage.GetChartClassifyCountById(req.ChartClassifyId)
  439. if count <= 0 {
  440. br.Msg = "分类已被删除,不可移动,请刷新页面"
  441. return
  442. }
  443. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  444. if err != nil {
  445. br.Msg = "移动失败"
  446. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  447. return
  448. }
  449. if chartInfo.Source != utils.CHART_SOURCE_DEFAULT {
  450. br.Msg = "图表异常"
  451. br.ErrMsg = "分类异常,不是ETA图库的图表"
  452. return
  453. }
  454. //如果改变了分类,那么移动该图表数据
  455. if chartInfo.ChartClassifyId != req.ChartClassifyId {
  456. //查询需要修改的分类下是否存在同一个图表名称
  457. tmpChartInfo, tmpErr := data_manage.GetChartInfoByClassifyIdAndName(req.ChartClassifyId, chartInfo.ChartName)
  458. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  459. br.Msg = "移动失败"
  460. br.ErrMsg = "移动失败,Err:" + tmpErr.Error()
  461. return
  462. }
  463. if tmpChartInfo != nil {
  464. br.Msg = "移动失败,同一个分类下图表名称不允许重复"
  465. br.ErrMsg = "移动失败,同一个分类下图表名称不允许重复"
  466. return
  467. }
  468. err = data_manage.MoveChartInfo(req.ChartInfoId, req.ChartClassifyId)
  469. if err != nil {
  470. br.Msg = "移动失败"
  471. br.ErrMsg = "移动失败,Err:" + err.Error()
  472. return
  473. }
  474. }
  475. //移动排序
  476. updateCol := make([]string, 0)
  477. //如果有传入 上一个兄弟节点分类id
  478. if req.PrevChartInfoId > 0 {
  479. prevChartInfo, err := data_manage.GetChartInfoById(req.PrevChartInfoId)
  480. if err != nil {
  481. br.Msg = "移动失败"
  482. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  483. return
  484. }
  485. //如果是移动在两个兄弟节点之间
  486. if req.NextChartInfoId > 0 {
  487. //下一个兄弟节点
  488. nextChartInfo, err := data_manage.GetChartInfoById(req.NextChartInfoId)
  489. if err != nil {
  490. br.Msg = "移动失败"
  491. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  492. return
  493. }
  494. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  495. if prevChartInfo.Sort == nextChartInfo.Sort || prevChartInfo.Sort == chartInfo.Sort {
  496. //变更兄弟节点的排序
  497. updateSortStr := `sort + 2`
  498. _ = data_manage.UpdateChartInfoSortByClassifyId(prevChartInfo.ChartClassifyId, prevChartInfo.Sort, prevChartInfo.ChartInfoId, []int{chartInfo.Source}, updateSortStr)
  499. } else {
  500. //如果下一个兄弟的排序权重正好是上个兄弟节点的下一层,那么需要再加一层了
  501. if nextChartInfo.Sort-prevChartInfo.Sort == 1 {
  502. //变更兄弟节点的排序
  503. updateSortStr := `sort + 1`
  504. _ = data_manage.UpdateChartInfoSortByClassifyId(prevChartInfo.ChartClassifyId, prevChartInfo.Sort, prevChartInfo.ChartInfoId, []int{chartInfo.Source}, updateSortStr)
  505. }
  506. }
  507. }
  508. chartInfo.Sort = prevChartInfo.Sort + 1
  509. chartInfo.ModifyTime = time.Now()
  510. updateCol = append(updateCol, "Sort", "ModifyTime")
  511. } else {
  512. firstClassify, err := data_manage.GetFirstChartInfoByClassifyId(req.ChartClassifyId)
  513. if err != nil && err.Error() != utils.ErrNoRow() {
  514. br.Msg = "移动失败"
  515. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  516. return
  517. }
  518. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  519. if firstClassify != nil && firstClassify.Sort == 0 {
  520. updateSortStr := ` sort + 1 `
  521. _ = data_manage.UpdateChartInfoSortByClassifyId(firstClassify.ChartClassifyId, 0, firstClassify.ChartInfoId-1, []int{chartInfo.Source}, updateSortStr)
  522. }
  523. chartInfo.Sort = 0 //那就是排在第一位
  524. chartInfo.ModifyTime = time.Now()
  525. updateCol = append(updateCol, "Sort", "ModifyTime")
  526. }
  527. //更新
  528. if len(updateCol) > 0 {
  529. err = chartInfo.Update(updateCol)
  530. if err != nil {
  531. br.Msg = "移动失败"
  532. br.ErrMsg = "修改失败,Err:" + err.Error()
  533. return
  534. }
  535. }
  536. if err != nil {
  537. br.Msg = "移动失败"
  538. br.ErrMsg = "修改失败,Err:" + err.Error()
  539. return
  540. }
  541. //添加es数据
  542. go data.EsAddOrEditChartInfo(req.ChartInfoId)
  543. //修改my eta es数据
  544. go data.EsAddOrEditMyChartInfoByChartInfoId(req.ChartInfoId)
  545. //新增操作日志
  546. {
  547. chartLog := new(data_manage.ChartInfoLog)
  548. chartLog.ChartName = chartInfo.ChartName
  549. chartLog.ChartInfoId = req.ChartInfoId
  550. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  551. chartLog.SysUserId = sysUser.AdminId
  552. chartLog.SysUserRealName = sysUser.RealName
  553. chartLog.UniqueCode = chartInfo.UniqueCode
  554. chartLog.CreateTime = time.Now()
  555. chartLog.Content = string(this.Ctx.Input.RequestBody)
  556. chartLog.Status = "移动图表"
  557. chartLog.Method = this.Ctx.Input.URL()
  558. go data_manage.AddChartInfoLog(chartLog)
  559. }
  560. br.Ret = 200
  561. br.Success = true
  562. br.Msg = "移动成功"
  563. }
  564. // ChartInfoDetail
  565. // @Title 获取图表详情
  566. // @Description 获取图表详情接口
  567. // @Param ChartInfoId query int true "图表id"
  568. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  569. // @Param StartDate query string true "自定义开始日期"
  570. // @Param EndDate query string true "自定义结束日期"
  571. // @Param Calendar query string true "公历/农历"
  572. // @Param SeasonStartDate query string true "季节性图开始日期"
  573. // @Param SeasonEndDate query string true "季节性图结束日期"
  574. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  575. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  576. // @Success 200 {object} data_manage.ChartInfoDetailResp
  577. // @router /chart_info/detail [get]
  578. func (this *ChartInfoController) ChartInfoDetail() {
  579. br := new(models.BaseResponse).Init()
  580. defer func() {
  581. this.Data["json"] = br
  582. this.ServeJSON()
  583. }()
  584. sysUser := this.SysUser
  585. if sysUser == nil {
  586. br.Msg = "请登录"
  587. br.ErrMsg = "请登录,SysUser Is Empty"
  588. br.Ret = 408
  589. return
  590. }
  591. chartInfoId, _ := this.GetInt("ChartInfoId")
  592. dateType, _ := this.GetInt("DateType")
  593. fmt.Println("dateType:", dateType)
  594. if dateType <= 0 {
  595. dateType = 3
  596. }
  597. startDate := this.GetString("StartDate")
  598. endDate := this.GetString("EndDate")
  599. seasonStartDate := this.GetString("SeasonStartDate")
  600. seasonEndDate := this.GetString("SeasonEndDate")
  601. edbInfoId := this.GetString("EdbInfoId")
  602. chartType, _ := this.GetInt("ChartType")
  603. fmt.Println("seasonStartDate:", seasonStartDate)
  604. fmt.Println("seasonEndDate:", seasonEndDate)
  605. calendar := this.GetString("Calendar")
  606. if calendar == "" {
  607. calendar = "公历"
  608. }
  609. switch dateType {
  610. case 1:
  611. startDate = "2000-01-01"
  612. case 2:
  613. startDate = "2010-01-01"
  614. case 3:
  615. startDate = "2015-01-01"
  616. case 4:
  617. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  618. startDate = "2021-01-01"
  619. case 5:
  620. startDate = startDate + "-01"
  621. endDate = endDate + "-01"
  622. case 6:
  623. startDate = startDate + "-01"
  624. case 7:
  625. startDate = "2018-01-01"
  626. case 8:
  627. startDate = "2019-01-01"
  628. case 9:
  629. startDate = "2020-01-01"
  630. case 11:
  631. startDate = "2022-01-01"
  632. }
  633. var err error
  634. chartInfo := new(data_manage.ChartInfoView)
  635. if chartInfoId > 0 {
  636. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  637. if err != nil {
  638. if err.Error() == utils.ErrNoRow() {
  639. br.Msg = "图被删除,请刷新页面"
  640. br.ErrMsg = "图被删除,请刷新页面,Err:" + err.Error()
  641. return
  642. }
  643. br.Msg = "获取失败"
  644. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  645. return
  646. }
  647. chartType = chartInfo.ChartType
  648. }
  649. if chartType == 2 {
  650. if seasonStartDate != "" {
  651. startDate = seasonStartDate + "-01"
  652. } else {
  653. fivePre := time.Now().AddDate(-4, 0, 0).Year()
  654. startDate = strconv.Itoa(fivePre) + "-01-01"
  655. }
  656. if seasonEndDate != "" {
  657. seasonEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, seasonEndDate+"-01", time.Local)
  658. if tmpErr != nil {
  659. br.Msg = "获取失败"
  660. br.ErrMsg = "获取图表,指标信息失败,Err:" + tmpErr.Error()
  661. return
  662. }
  663. endDate = seasonEndDateTime.AddDate(0, 1, -1).Format(utils.FormatDate)
  664. } else {
  665. endDate = "" //time.Now().AddDate(2, 0, 0).Format(utils.FormatDate)
  666. }
  667. }
  668. mappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  669. if chartInfoId > 0 {
  670. mappingList, err = data_manage.GetChartEdbMappingList(chartInfoId)
  671. if err != nil {
  672. br.Msg = "获取失败"
  673. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  674. return
  675. }
  676. } else {
  677. if edbInfoId != "" {
  678. mappingList, err = data_manage.GetChartEdbMappingListByEdbInfoId(edbInfoId)
  679. if err != nil {
  680. br.Msg = "获取失败"
  681. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  682. return
  683. }
  684. }
  685. }
  686. // 图表额外数据参数
  687. extraConfigStr := chartInfo.ExtraConfig
  688. // 柱方图的一些配置
  689. var barConfig data_manage.BarChartInfoReq
  690. if chartInfo != nil && chartInfo.ChartType == 7 {
  691. if chartInfo.BarConfig == `` {
  692. br.Msg = "柱方图未配置"
  693. br.ErrMsg = "柱方图未配置"
  694. return
  695. }
  696. err := json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  697. if err != nil {
  698. br.Msg = "柱方图配置异常"
  699. br.ErrMsg = "柱方图配置异常"
  700. return
  701. }
  702. extraConfigStr = chartInfo.BarConfig
  703. }
  704. // 获取图表中的指标数据
  705. edbList, xEdbIdValue, yDataList, dataResp, err, errMsg := data.GetChartEdbData(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr)
  706. if err != nil {
  707. br.Msg = "获取失败"
  708. if errMsg != `` {
  709. br.Msg = errMsg
  710. }
  711. br.ErrMsg = "获取图表,指标信息失败,Err:" + err.Error()
  712. return
  713. }
  714. warnEdbList := make([]string, 0)
  715. for _, v := range edbList {
  716. if v.IsNullData {
  717. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  718. }
  719. }
  720. if len(warnEdbList) > 0 {
  721. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  722. }
  723. if chartInfoId > 0 && chartInfo != nil {
  724. //判断是否加入我的图库
  725. {
  726. var myChartCondition string
  727. var myChartPars []interface{}
  728. myChartCondition += ` AND a.admin_id=? `
  729. myChartPars = append(myChartPars, sysUser.AdminId)
  730. myChartCondition += ` AND a.chart_info_id=? `
  731. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  732. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  733. if err != nil && err.Error() != utils.ErrNoRow() {
  734. br.Msg = "获取失败"
  735. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  736. return
  737. }
  738. if myChartList != nil && len(myChartList) > 0 {
  739. chartInfo.IsAdd = true
  740. chartInfo.MyChartId = myChartList[0].MyChartId
  741. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  742. }
  743. }
  744. }
  745. //图表操作权限
  746. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId)
  747. //判断是否需要展示英文标识
  748. chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  749. chartInfo.Button = data_manage.ChartViewButton{
  750. IsEdit: chartInfo.IsEdit,
  751. IsEnChart: chartInfo.IsEnChart,
  752. IsAdd: chartInfo.IsAdd,
  753. IsCopy: true,
  754. IsSetName: chartInfo.IsSetName,
  755. }
  756. resp := new(data_manage.ChartInfoDetailResp)
  757. resp.ChartInfo = chartInfo
  758. resp.EdbInfoList = edbList
  759. resp.XEdbIdValue = xEdbIdValue
  760. resp.YDataList = yDataList
  761. resp.BarChartInfo = barConfig
  762. resp.DataResp = dataResp
  763. br.Ret = 200
  764. br.Success = true
  765. br.Msg = "获取成功"
  766. br.Data = resp
  767. }
  768. // ChartInfoEdbInfoDetail
  769. // @Title 图表-获取指标详情
  770. // @Description 图表-获取指标详情接口
  771. // @Param EdbInfoId query int true "指标ID"
  772. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今"
  773. // @Param StartDate query string true "自定义开始日期"
  774. // @Param EndDate query string true "自定义结束日期"
  775. // @Param EdbInfoType query int true "1:标准指标,0:领先指标,默认为标准指标"
  776. // @Param LeadValue query int true "领先值"
  777. // @Param LeadUnit query string true "领先单位:天,月,季,周"
  778. // @Success 200 {object} data_manage.ChartEdbInfoDetailResp
  779. // @router /chart_info/edb_info/detail [get]
  780. func (this *ChartInfoController) ChartInfoEdbInfoDetail() {
  781. br := new(models.BaseResponse).Init()
  782. defer func() {
  783. this.Data["json"] = br
  784. this.ServeJSON()
  785. }()
  786. sysUser := this.SysUser
  787. if sysUser == nil {
  788. br.Msg = "请登录"
  789. br.ErrMsg = "请登录,SysUser Is Empty"
  790. br.Ret = 408
  791. return
  792. }
  793. edbInfoId, _ := this.GetInt("EdbInfoId")
  794. if edbInfoId <= 0 {
  795. br.Msg = "参数错误"
  796. return
  797. }
  798. dateType, _ := this.GetInt("DateType")
  799. if dateType <= 0 {
  800. dateType = 3
  801. }
  802. edbInfoType, _ := this.GetInt("EdbInfoType")
  803. if edbInfoType < 0 {
  804. edbInfoType = 1
  805. }
  806. leadValue, _ := this.GetInt("LeadValue")
  807. if leadValue < 0 {
  808. leadValue = 0
  809. }
  810. leadUnit := this.GetString("LeadUnit")
  811. startDate := this.GetString("StartDate")
  812. endDate := this.GetString("EndDate")
  813. isTimeBetween := false //是否是时间区间
  814. switch dateType {
  815. case 1:
  816. startDate = "2000-01-01"
  817. endDate = ""
  818. case 2:
  819. startDate = "2010-01-01"
  820. endDate = ""
  821. case 3:
  822. startDate = "2015-01-01"
  823. endDate = ""
  824. case 4:
  825. //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
  826. startDate = "2021-01-01"
  827. endDate = ""
  828. case 5:
  829. startDate = startDate + "-01"
  830. endDate = endDate + "-01"
  831. isTimeBetween = true
  832. case 6:
  833. //startDate = startDate + "-01"
  834. endDate = ""
  835. case 7:
  836. startDate = "2018-01-01"
  837. endDate = ""
  838. case 8:
  839. startDate = "2019-01-01"
  840. endDate = ""
  841. case 9:
  842. startDate = "2020-01-01"
  843. endDate = ""
  844. case 11:
  845. startDate = "2022-01-01"
  846. endDate = ""
  847. default:
  848. startDate = startDate + "-01"
  849. endDate = endDate + "-01"
  850. }
  851. // 兼容日期错误
  852. {
  853. if strings.Count(startDate, "-") == 1 {
  854. startDate = startDate + "-01"
  855. }
  856. if strings.Count(endDate, "-") == 1 {
  857. endDate = endDate + "-01"
  858. }
  859. }
  860. if startDate == "" {
  861. br.Msg = "参数错误"
  862. br.Msg = "参数错误,无效的查询日期"
  863. return
  864. }
  865. var startDateReal string
  866. var diffSeconds int64
  867. if leadValue > 0 && leadUnit != "" {
  868. var startTimeRealTemp time.Time
  869. startDateParse, err := time.Parse(utils.FormatDate, startDate)
  870. if err != nil {
  871. br.Msg = "获取数据失败"
  872. br.ErrMsg = "获取数据失败,日期格式错误:startDate:" + startDate
  873. return
  874. }
  875. switch leadUnit {
  876. case "天":
  877. startTimeRealTemp = startDateParse.AddDate(0, 0, -leadValue)
  878. case "月":
  879. startTimeRealTemp = startDateParse.AddDate(0, -leadValue, 0)
  880. case "季":
  881. startTimeRealTemp = startDateParse.AddDate(0, -3*leadValue, 0)
  882. case "周":
  883. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*leadValue)
  884. case "年":
  885. startTimeRealTemp = startDateParse.AddDate(-leadValue, 0, 0)
  886. }
  887. if startTimeRealTemp.Before(startDateParse) {
  888. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  889. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  890. } else {
  891. startDateReal = startDate
  892. diffSeconds = 0
  893. }
  894. } else {
  895. startDateReal = startDate
  896. }
  897. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  898. if err != nil {
  899. br.Msg = "获取失败"
  900. br.Msg = "获取失败,Err:" + err.Error()
  901. return
  902. }
  903. var dataList []*data_manage.EdbDataList
  904. var minData, maxData float64
  905. switch edbInfo.EdbInfoType {
  906. case 0: //普通源指标
  907. dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfoId, startDateReal, endDate)
  908. if err != nil {
  909. br.Msg = "获取失败"
  910. br.Msg = "获取失败,Err:" + err.Error()
  911. return
  912. }
  913. //查询区间内最大最小值
  914. minData, maxData, err = data_manage.GetEdbDataListMinAndMax(edbInfo.Source, edbInfoId, startDateReal, endDate)
  915. if err != nil {
  916. br.Msg = "获取失败"
  917. br.Msg = "获取指标最新的最大最小值失败,Err:" + err.Error()
  918. return
  919. }
  920. case 1: //预测指标
  921. tmpDataList, sourceEdbInfoItem, _, err, _ := data.GetPredictDataListByPredictEdbInfo(edbInfo, startDateReal, endDate, isTimeBetween)
  922. if err != nil {
  923. br.Msg = "获取失败"
  924. br.Msg = "获取失败,Err:" + err.Error()
  925. return
  926. }
  927. dataList = tmpDataList
  928. // 有预测数据,且为普通的预测指标
  929. if len(dataList) > 0 && edbInfo.EdbInfoType == 0 {
  930. //查询区间内最大最小值
  931. minData, maxData, err = data_manage.GetEdbDataListMinAndMax(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDateReal, endDate)
  932. if err != nil {
  933. br.Msg = "获取失败"
  934. br.Msg = "获取指标最新的最大最小值失败,Err:" + err.Error()
  935. return
  936. }
  937. tmpValue := dataList[len(dataList)-1]
  938. // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
  939. if edbInfo.MaxValue < tmpValue.Value {
  940. maxData = tmpValue.Value
  941. }
  942. // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
  943. if edbInfo.MinValue > tmpValue.Value {
  944. minData = tmpValue.Value
  945. }
  946. }
  947. }
  948. if diffSeconds != 0 {
  949. dataListLen := len(dataList)
  950. for i := 0; i < dataListLen; i++ {
  951. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  952. }
  953. day, err := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  954. if err != nil {
  955. br.Msg = "获取失败"
  956. br.Msg = "获取领先滞后的的日期间隔失败,Err:" + err.Error()
  957. return
  958. }
  959. latestDateTime, err := time.ParseInLocation(utils.FormatDate, edbInfo.LatestDate, time.Local)
  960. if err != nil {
  961. br.Msg = "获取失败"
  962. br.Msg = "指标日期转换失败,Err:" + err.Error()
  963. return
  964. }
  965. edbInfo.LatestDate = latestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  966. }
  967. mapping := new(data_manage.ChartEdbInfoMapping)
  968. mapping.EdbInfoId = edbInfo.EdbInfoId
  969. mapping.EdbInfoCategoryType = edbInfo.EdbInfoType
  970. mapping.SourceName = edbInfo.SourceName
  971. mapping.Source = edbInfo.Source
  972. mapping.EdbCode = edbInfo.EdbCode
  973. mapping.EdbName = edbInfo.EdbName
  974. mapping.EdbType = edbInfo.EdbType
  975. mapping.Frequency = edbInfo.Frequency
  976. mapping.Unit = edbInfo.Unit
  977. mapping.StartDate = edbInfo.StartDate
  978. mapping.EndDate = edbInfo.EndDate
  979. mapping.ChartEdbMappingId = 0
  980. mapping.ChartInfoId = 0
  981. mapping.MaxData = maxData
  982. mapping.MinData = minData
  983. mapping.IsOrder = false
  984. mapping.IsAxis = 1
  985. mapping.EdbInfoType = 1
  986. mapping.LeadValue = 0
  987. mapping.LeadUnit = ""
  988. mapping.LeadUnitEn = ""
  989. mapping.ChartStyle = ""
  990. mapping.ChartColor = ""
  991. mapping.PredictChartColor = ""
  992. mapping.ChartWidth = 0
  993. mapping.LatestDate = edbInfo.LatestDate
  994. mapping.DataList = dataList
  995. resp := new(data_manage.ChartEdbInfoDetailResp)
  996. resp.EdbInfo = mapping
  997. br.Ret = 200
  998. br.Success = true
  999. br.Msg = "获取成功"
  1000. br.Data = resp
  1001. }
  1002. // CopyChartInfo
  1003. // @Title 复制并新增图表接口
  1004. // @Description 新增图表接口
  1005. // @Param request body data_manage.CopyAddChartInfoReq true "type json string"
  1006. // @Success 200 {object} data_manage.AddChartInfoResp
  1007. // @router /chart_info/copy [post]
  1008. func (this *ChartInfoController) CopyChartInfo() {
  1009. br := new(models.BaseResponse).Init()
  1010. defer func() {
  1011. this.Data["json"] = br
  1012. this.ServeJSON()
  1013. }()
  1014. sysUser := this.SysUser
  1015. if sysUser == nil {
  1016. br.Msg = "请登录"
  1017. br.ErrMsg = "请登录,SysUser Is Empty"
  1018. br.Ret = 408
  1019. return
  1020. }
  1021. deleteCache := true
  1022. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  1023. defer func() {
  1024. if deleteCache {
  1025. utils.Rc.Delete(cacheKey)
  1026. }
  1027. }()
  1028. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1029. deleteCache = false
  1030. br.Msg = "系统处理中,请稍后重试!"
  1031. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1032. return
  1033. }
  1034. var req data_manage.CopyAddChartInfoReq
  1035. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1036. if err != nil {
  1037. br.Msg = "参数解析异常!"
  1038. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1039. return
  1040. }
  1041. // 获取原图表信息
  1042. oldChartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  1043. if err != nil {
  1044. br.Msg = "获取原图表信息失败"
  1045. br.ErrMsg = "获取原图表信息失败,Err:" + err.Error()
  1046. return
  1047. }
  1048. //校验图表名称是否重复
  1049. req.ChartName = strings.Trim(req.ChartName, " ")
  1050. if req.ChartName == "" {
  1051. br.Msg = "请填写图表名称!"
  1052. return
  1053. }
  1054. if req.ChartClassifyId <= 0 {
  1055. br.Msg = "分类参数错误!"
  1056. return
  1057. }
  1058. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1059. if err != nil {
  1060. if err.Error() == utils.ErrNoRow() {
  1061. br.Msg = "分类不存在"
  1062. br.ErrMsg = "分类不存在"
  1063. return
  1064. }
  1065. br.Msg = "获取分类信息失败"
  1066. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1067. return
  1068. }
  1069. if chartClassify == nil {
  1070. br.Msg = "分类不存在"
  1071. br.ErrMsg = "分类不存在"
  1072. return
  1073. }
  1074. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  1075. //if err != nil && err.Error() != utils.ErrNoRow() {
  1076. // br.Msg = "保存失败"
  1077. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  1078. // return
  1079. //}
  1080. //if count > 0 {
  1081. // br.Msg = "所选指标已存在相同指标"
  1082. // return
  1083. //}
  1084. //判断图表是否存在
  1085. var condition string
  1086. var pars []interface{}
  1087. condition += " AND chart_classify_id=? "
  1088. pars = append(pars, req.ChartClassifyId)
  1089. condition += " AND chart_name=? AND source = ? "
  1090. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  1091. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1092. if err != nil {
  1093. br.Msg = "判断图表名称是否存在失败"
  1094. br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  1095. return
  1096. }
  1097. if count > 0 {
  1098. br.Msg = "图表已存在,请重新填写"
  1099. br.IsSendEmail = false
  1100. return
  1101. }
  1102. //获取原图表关联的指标信息列表
  1103. edbMappingList, err := data_manage.GetChartEdbMappingList(req.ChartInfoId)
  1104. if err != nil {
  1105. br.Msg = "获取图表关联的指标信息失败!"
  1106. return
  1107. }
  1108. //添加图表
  1109. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1110. chartInfo := &data_manage.ChartInfo{
  1111. ChartInfoId: 0,
  1112. ChartName: req.ChartName,
  1113. ChartClassifyId: req.ChartClassifyId,
  1114. SysUserId: sysUser.AdminId,
  1115. SysUserRealName: sysUser.RealName,
  1116. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  1117. CreateTime: time.Now(),
  1118. ModifyTime: time.Now(),
  1119. DateType: oldChartInfo.DateType,
  1120. StartDate: oldChartInfo.StartDate,
  1121. EndDate: oldChartInfo.EndDate,
  1122. IsSetName: oldChartInfo.IsSetName,
  1123. EdbInfoIds: oldChartInfo.EdbInfoIds,
  1124. ChartType: oldChartInfo.ChartType,
  1125. Calendar: oldChartInfo.Calendar,
  1126. SeasonStartDate: oldChartInfo.SeasonStartDate,
  1127. SeasonEndDate: oldChartInfo.SeasonEndDate,
  1128. ChartImage: oldChartInfo.ChartImage,
  1129. BarConfig: oldChartInfo.BarConfig,
  1130. //Sort: sort,
  1131. LeftMin: oldChartInfo.LeftMin,
  1132. LeftMax: oldChartInfo.LeftMax,
  1133. RightMin: oldChartInfo.RightMin,
  1134. RightMax: oldChartInfo.RightMax,
  1135. Disabled: oldChartInfo.Disabled,
  1136. Source: oldChartInfo.Source,
  1137. ExtraConfig: oldChartInfo.ExtraConfig,
  1138. }
  1139. newId, err := data_manage.AddChartInfo(chartInfo)
  1140. if err != nil {
  1141. br.Msg = "保存失败"
  1142. br.ErrMsg = "保存失败,Err:" + err.Error()
  1143. return
  1144. }
  1145. chartInfo.ChartInfoId = int(newId)
  1146. // 添加图表与指标的关联关系
  1147. {
  1148. mapList := make([]*data_manage.ChartEdbMapping, 0)
  1149. for _, v := range edbMappingList {
  1150. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1151. mapItem := &data_manage.ChartEdbMapping{
  1152. //ChartEdbMappingId: 0,
  1153. ChartInfoId: chartInfo.ChartInfoId,
  1154. EdbInfoId: v.EdbInfoId,
  1155. CreateTime: time.Now(),
  1156. ModifyTime: time.Now(),
  1157. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  1158. MaxData: v.MaxData,
  1159. MinData: v.MinData,
  1160. IsOrder: v.IsOrder,
  1161. IsAxis: v.IsAxis,
  1162. EdbInfoType: v.EdbInfoType,
  1163. LeadValue: v.LeadValue,
  1164. LeadUnit: v.LeadUnit,
  1165. ChartStyle: v.ChartStyle,
  1166. ChartColor: v.ChartColor,
  1167. ChartWidth: v.ChartWidth,
  1168. Source: v.Source,
  1169. }
  1170. mapList = append(mapList, mapItem)
  1171. }
  1172. err = data_manage.AddChartEdbMapping(mapList)
  1173. if err != nil {
  1174. br.Msg = "保存失败"
  1175. br.ErrMsg = "保存失败,Err:" + err.Error()
  1176. return
  1177. }
  1178. }
  1179. //添加es数据
  1180. go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1181. //修改my eta es数据
  1182. go data.EsAddOrEditMyChartInfoByChartInfoId(chartInfo.ChartInfoId)
  1183. //新增操作日志
  1184. {
  1185. chartLog := new(data_manage.ChartInfoLog)
  1186. chartLog.ChartInfoId = chartInfo.ChartInfoId
  1187. chartLog.ChartName = req.ChartName
  1188. chartLog.ChartClassifyId = req.ChartClassifyId
  1189. chartLog.SysUserId = sysUser.AdminId
  1190. chartLog.SysUserRealName = sysUser.RealName
  1191. chartLog.UniqueCode = chartInfo.UniqueCode
  1192. chartLog.CreateTime = time.Now()
  1193. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1194. chartLog.Status = "新增图表"
  1195. chartLog.Method = this.Ctx.Input.URI()
  1196. go data_manage.AddChartInfoLog(chartLog)
  1197. }
  1198. br.Ret = 200
  1199. br.Success = true
  1200. br.Msg = "保存成功"
  1201. br.Data = data_manage.AddChartInfoResp{
  1202. ChartInfoId: chartInfo.ChartInfoId,
  1203. UniqueCode: chartInfo.UniqueCode,
  1204. ChartType: chartInfo.ChartType,
  1205. }
  1206. br.IsAddLog = true
  1207. }
  1208. // ChartInfoSearchByEs
  1209. // @Title 图表模糊搜索(从es获取)
  1210. // @Description 图表模糊搜索(从es获取)
  1211. // @Param Keyword query string true "图表名称"
  1212. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1213. // @Success 200 {object} data_manage.ChartInfo
  1214. // @router /chart_info/search_by_es [get]
  1215. func (this *ChartInfoController) ChartInfoSearchByEs() {
  1216. br := new(models.BaseResponse).Init()
  1217. defer func() {
  1218. this.Data["json"] = br
  1219. this.ServeJSON()
  1220. }()
  1221. sysUser := this.SysUser
  1222. if sysUser == nil {
  1223. br.Msg = "请登录"
  1224. br.ErrMsg = "请登录,SysUser Is Empty"
  1225. br.Ret = 408
  1226. return
  1227. }
  1228. pageSize, _ := this.GetInt("PageSize")
  1229. currentIndex, _ := this.GetInt("CurrentIndex")
  1230. var startSize int
  1231. if pageSize <= 0 {
  1232. pageSize = utils.PageSize20
  1233. }
  1234. if currentIndex <= 0 {
  1235. currentIndex = 1
  1236. }
  1237. startSize = paging.StartIndex(currentIndex, pageSize)
  1238. keyword := this.GetString("Keyword")
  1239. //只看我的
  1240. isShowMe, _ := this.GetBool("IsShowMe")
  1241. showSysId := 0
  1242. if isShowMe {
  1243. showSysId = sysUser.AdminId
  1244. }
  1245. var searchList []*data_manage.ChartInfo
  1246. var total int64
  1247. var err error
  1248. // 获取当前账号的不可见指标
  1249. noPermissionChartIdList := make([]int, 0)
  1250. {
  1251. obj := data_manage.EdbInfoNoPermissionAdmin{}
  1252. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  1253. if err != nil && err.Error() != utils.ErrNoRow() {
  1254. br.Msg = "获取失败"
  1255. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1256. return
  1257. }
  1258. for _, v := range confList {
  1259. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  1260. }
  1261. }
  1262. if keyword != "" {
  1263. searchList, total, err = data.EsSearchChartInfo(keyword, showSysId, []int{utils.CHART_SOURCE_DEFAULT}, noPermissionChartIdList, startSize, pageSize)
  1264. } else {
  1265. total, searchList, err = data_manage.ChartInfoSearchByEmptyKeyWord(showSysId, []int{utils.CHART_SOURCE_DEFAULT}, noPermissionChartIdList)
  1266. if err != nil && err.Error() != utils.ErrNoRow() {
  1267. br.Msg = "获取失败"
  1268. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1269. return
  1270. }
  1271. }
  1272. finalList := make([]*data_manage.ChartInfoMore, 0)
  1273. if len(searchList) > 0 {
  1274. chartInfoIds := ""
  1275. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  1276. for _, v := range searchList {
  1277. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  1278. }
  1279. if chartInfoIds != "" {
  1280. chartInfoIds = strings.Trim(chartInfoIds, ",")
  1281. //判断是否需要展示英文标识
  1282. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  1283. if e != nil {
  1284. br.Msg = "获取失败"
  1285. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  1286. return
  1287. }
  1288. for _, v := range edbList {
  1289. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  1290. }
  1291. }
  1292. for _, v := range searchList {
  1293. tmp := new(data_manage.ChartInfoMore)
  1294. tmp.ChartInfo = *v
  1295. //判断是否需要展示英文标识
  1296. if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
  1297. tmp.IsEnChart = data.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
  1298. }
  1299. finalList = append(finalList, tmp)
  1300. }
  1301. }
  1302. //新增搜索词记录
  1303. {
  1304. searchKeyword := new(data_manage.SearchKeyword)
  1305. searchKeyword.KeyWord = keyword
  1306. searchKeyword.CreateTime = time.Now()
  1307. go data_manage.AddSearchKeyword(searchKeyword)
  1308. }
  1309. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1310. resp := data_manage.ChartInfoListByEsResp{
  1311. Paging: page,
  1312. List: finalList,
  1313. }
  1314. br.Ret = 200
  1315. br.Success = true
  1316. br.Msg = "获取成功"
  1317. br.Data = resp
  1318. }
  1319. // @Title 图表刷新接口
  1320. // @Description 图表刷新接口
  1321. // @Param ChartInfoId query int true "图表id"
  1322. // @Param UniqueCode query string true "唯一code"
  1323. // @Success Ret=200 刷新成功
  1324. // @router /chart_info/refresh [get]
  1325. func (this *ChartInfoController) ChartInfoRefresh() {
  1326. br := new(models.BaseResponse).Init()
  1327. defer func() {
  1328. this.Data["json"] = br
  1329. this.ServeJSON()
  1330. }()
  1331. sysUser := this.SysUser
  1332. if sysUser == nil {
  1333. br.Msg = "请登录"
  1334. br.ErrMsg = "请登录,SysUser Is Empty"
  1335. br.Ret = 408
  1336. return
  1337. }
  1338. chartInfoId, _ := this.GetInt("ChartInfoId")
  1339. uniqueCode := this.GetString("UniqueCode")
  1340. if chartInfoId <= 0 && uniqueCode == `` {
  1341. br.Msg = "参数错误"
  1342. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  1343. return
  1344. }
  1345. var chartInfo *data_manage.ChartInfo
  1346. var err error
  1347. if chartInfoId > 0 {
  1348. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  1349. } else {
  1350. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  1351. }
  1352. if err != nil {
  1353. if err.Error() == utils.ErrNoRow() {
  1354. br.Msg = "图表已被删除,无需刷新"
  1355. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1356. return
  1357. }
  1358. br.Msg = "刷新失败"
  1359. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1360. return
  1361. }
  1362. err = data.ChartInfoRefreshV2(chartInfo.ChartInfoId)
  1363. if err != nil {
  1364. br.Msg = "刷新失败"
  1365. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  1366. return
  1367. }
  1368. /*
  1369. //err = data.ChartInfoRefresh(chartInfo.ChartInfoId)
  1370. //if err != nil {
  1371. // br.Msg = "刷新失败"
  1372. // br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  1373. // return
  1374. //}
  1375. */
  1376. //清除图表缓存
  1377. {
  1378. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  1379. _ = utils.Rc.Delete(key)
  1380. }
  1381. br.Ret = 200
  1382. br.Success = true
  1383. br.Msg = "刷新成功"
  1384. }
  1385. // BatchChartInfoRefresh
  1386. // @Title 批量刷新图表接口
  1387. // @Description 批量刷新图表接口
  1388. // @Param request body data_manage.BatchChartRefreshReq true "type json string"
  1389. // @Success Ret=200 刷新成功
  1390. // @router /chart_info/batch_refresh [post]
  1391. func (this *ChartInfoController) BatchChartInfoRefresh() {
  1392. br := new(models.BaseResponse).Init()
  1393. defer func() {
  1394. this.Data["json"] = br
  1395. this.ServeJSON()
  1396. }()
  1397. sysUser := this.SysUser
  1398. if sysUser == nil {
  1399. br.Msg = "请登录"
  1400. br.ErrMsg = "请登录,SysUser Is Empty"
  1401. br.Ret = 408
  1402. return
  1403. }
  1404. var req data_manage.BatchChartRefreshReq
  1405. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1406. if err != nil {
  1407. br.Msg = "参数解析异常!"
  1408. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1409. return
  1410. }
  1411. //获取所有的图表列表
  1412. _, chartInfoList, err := data_manage.GetChartInfoListByUniqueCodeSlice(req.ChartInfoCode)
  1413. if len(chartInfoList) <= 0 {
  1414. br.Ret = 200
  1415. br.Success = true
  1416. br.Msg = "刷新成功"
  1417. return
  1418. }
  1419. err = data.BatchChartInfoRefreshV2(chartInfoList)
  1420. if err != nil {
  1421. br.Msg = "刷新失败"
  1422. br.ErrMsg = "刷新图表关联指标信息失败,Err:" + err.Error()
  1423. return
  1424. }
  1425. //清除图表缓存
  1426. for _, v := range chartInfoList {
  1427. key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
  1428. _ = utils.Rc.Delete(key)
  1429. }
  1430. br.Ret = 200
  1431. br.Success = true
  1432. br.Msg = "刷新成功"
  1433. }