balance_table.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/excel"
  7. "eta/eta_api/models/data_manage/excel/request"
  8. "eta/eta_api/models/data_manage/excel/response"
  9. "eta/eta_api/models/system"
  10. "eta/eta_api/services/data"
  11. "eta/eta_api/services/data/data_manage_permission"
  12. excelService "eta/eta_api/services/data/excel"
  13. etaTrialService "eta/eta_api/services/eta_trial"
  14. "eta/eta_api/utils"
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. // GetChildTable
  21. // @Title 获取子表
  22. // @Description 获取子表
  23. // @Param request body request.MixedTableCellDataReq true "type json string"
  24. // @router /excel_info/child_table [get]
  25. func (c *ExcelInfoController) GetChildTable() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. c.Data["json"] = br
  29. c.ServeJSON()
  30. }()
  31. sysUser := c.SysUser
  32. if sysUser == nil {
  33. br.Msg = "请登录"
  34. br.ErrMsg = "请登录,SysUser Is Empty"
  35. br.Ret = 408
  36. return
  37. }
  38. parentId, _ := c.GetInt("ParentId")
  39. if parentId <= 0 {
  40. br.Msg = "请选择父表"
  41. return
  42. }
  43. list := make([]*excel.ExcelInfo, 0)
  44. // 查询所有子表
  45. childList, err := excel.GetChildExcelInfoByParentId(parentId)
  46. if err != nil {
  47. br.Msg = "查询子表失败"
  48. return
  49. }
  50. if len(childList) > 0 {
  51. list = childList
  52. }
  53. resp := &response.BalanceChildTableResp{List: list}
  54. br.Ret = 200
  55. br.Success = true
  56. br.Msg = "查询成功"
  57. br.Data = resp
  58. }
  59. // Rename
  60. // @Title 表格重命名接口
  61. // @Description 表格重命名接口
  62. // @Param request body request.EditExcelInfoReq true "type json string"
  63. // @Success 200 {object} response.AddExcelInfoResp
  64. // @router /excel_info/rename [post]
  65. func (c *ExcelInfoController) Rename() {
  66. br := new(models.BaseResponse).Init()
  67. defer func() {
  68. c.Data["json"] = br
  69. c.ServeJSON()
  70. }()
  71. sysUser := c.SysUser
  72. if sysUser == nil {
  73. br.Msg = "请登录"
  74. br.ErrMsg = "请登录,SysUser Is Empty"
  75. br.Ret = 408
  76. return
  77. }
  78. var req request.RenameExcelInfoReq
  79. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  80. if err != nil {
  81. br.Msg = "参数解析异常!"
  82. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  83. return
  84. }
  85. if req.ExcelInfoId <= 0 {
  86. br.Msg = "请传入ETA表格标识!"
  87. return
  88. }
  89. req.ExcelName = strings.Trim(req.ExcelName, " ")
  90. if req.ExcelName == "" {
  91. br.Msg = "请填写表格名称!"
  92. br.IsSendEmail = false
  93. return
  94. }
  95. excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
  96. if err != nil {
  97. br.Msg = "获取ETA表格失败"
  98. br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
  99. return
  100. }
  101. //判断表格是否存在
  102. var condition string
  103. var pars []interface{}
  104. condition += " AND excel_info_id != ? AND excel_classify_id=? AND excel_name=? AND parent_id=?"
  105. pars = append(pars, req.ExcelInfoId, excelInfo.ExcelClassifyId, req.ExcelName, excelInfo.ParentId)
  106. // 获取分类下是否存在该表格名称
  107. count, err := excel.GetExcelInfoCountByCondition(condition, pars)
  108. if err != nil {
  109. br.Msg = "ETA判断表格名称是否存在失败"
  110. br.ErrMsg = "判断ETA表格名称是否存在失败,Err:" + err.Error()
  111. return
  112. }
  113. if count > 0 {
  114. br.Msg = "ETA表格名称已存在,请重新填写"
  115. br.IsSendEmail = false
  116. return
  117. }
  118. // todo 操作权限校验, 增加协作人判断
  119. {
  120. // 数据权限
  121. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelInfo.ExcelInfoId, excelInfo.ExcelClassifyId, excelInfo.IsJoinPermission, c.SysUser.AdminId)
  122. if err != nil {
  123. br.Msg = "获取ETA表格失败"
  124. br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  125. return
  126. }
  127. button := excelService.GetExcelInfoOpButton(sysUser, excelInfo.SysUserId, excelInfo.Source, haveOperaAuth)
  128. if !button.OpButton {
  129. br.Msg = "无操作权限"
  130. br.Msg = "无操作权限"
  131. br.IsSendEmail = false
  132. return
  133. }
  134. }
  135. excelInfo.ModifyTime = time.Now()
  136. excelInfo.ExcelName = req.ExcelName
  137. excelInfo.UpdateUserId = sysUser.AdminId
  138. excelInfo.UpdateUserRealName = sysUser.RealName
  139. // 自动保存时不会传缩略图,也就不更新这个字段
  140. updateExcelInfoParams := []string{"ModifyTime", "ExcelName", "UpdateUserId", "UpdateUserRealName"}
  141. err = excelInfo.Update(updateExcelInfoParams)
  142. if err != nil {
  143. br.Msg = "保存失败"
  144. br.ErrMsg = "保存失败,Err:" + err.Error()
  145. return
  146. }
  147. resp := response.AddExcelInfoResp{
  148. ExcelInfoId: excelInfo.ExcelInfoId,
  149. UniqueCode: excelInfo.UniqueCode,
  150. }
  151. //删除公共图库那边的缓存
  152. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode)
  153. br.Ret = 200
  154. br.Success = true
  155. br.Msg = "修改成功"
  156. br.Data = resp
  157. br.IsAddLog = true
  158. }
  159. // SaveExcelWorker
  160. // @Title 新增编辑表格协作人接口
  161. // @Description 新增编辑表格协作人接口
  162. // @Param request body request.EditExcelInfoReq true "type json string"
  163. // @Success 200 {object} response.AddExcelInfoResp
  164. // @router /excel_info/worker/save [post]
  165. func (c *ExcelInfoController) SaveExcelWorker() {
  166. br := new(models.BaseResponse).Init()
  167. defer func() {
  168. c.Data["json"] = br
  169. c.ServeJSON()
  170. }()
  171. sysUser := c.SysUser
  172. if sysUser == nil {
  173. br.Msg = "请登录"
  174. br.ErrMsg = "请登录,SysUser Is Empty"
  175. br.Ret = 408
  176. return
  177. }
  178. var req request.SaveExcelInfoWorkerReq
  179. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  180. if err != nil {
  181. br.Msg = "参数解析异常!"
  182. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  183. return
  184. }
  185. if req.ExcelInfoId <= 0 {
  186. br.Msg = "请传入ETA表格标识!"
  187. return
  188. }
  189. excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
  190. if err != nil {
  191. br.Msg = "获取ETA表格失败"
  192. br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
  193. return
  194. }
  195. if excelInfo.SysUserId != sysUser.AdminId {
  196. br.Msg = "您没有权限操作!"
  197. br.ErrMsg = "您没有权限操作!"
  198. return
  199. }
  200. // todo 操作权限校验, 增加协作人判断
  201. {
  202. // 数据权限
  203. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelInfo.ExcelInfoId, excelInfo.ExcelClassifyId, excelInfo.IsJoinPermission, c.SysUser.AdminId)
  204. if err != nil {
  205. br.Msg = "获取ETA表格失败"
  206. br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  207. return
  208. }
  209. button := excelService.GetExcelInfoOpButton(sysUser, excelInfo.SysUserId, excelInfo.Source, haveOperaAuth)
  210. if !button.OpButton {
  211. br.Msg = "无操作权限"
  212. br.Msg = "无操作权限"
  213. br.IsSendEmail = false
  214. return
  215. }
  216. }
  217. obj := new(excel.ExcelWorker)
  218. notDeleteWorkerIds := make([]string, 0)
  219. workerMap := make(map[int]struct{})
  220. if req.SysUserIds != "" {
  221. notDeleteWorkerIds = strings.Split(req.SysUserIds, ",")
  222. }
  223. existList, err := obj.GetByExcelInfoId(req.ExcelInfoId)
  224. if err != nil {
  225. br.Msg = "获取表格协作人失败!"
  226. br.ErrMsg = "获取表格协作人失败,Err:" + err.Error()
  227. return
  228. }
  229. for _, v := range existList {
  230. workerMap[v.SysUserId] = struct{}{}
  231. }
  232. addIds := make([]int, 0)
  233. for _, v := range notDeleteWorkerIds {
  234. id, _ := strconv.Atoi(v)
  235. if _, ok := workerMap[id]; !ok {
  236. addIds = append(addIds, id)
  237. }
  238. }
  239. // 查询协作人姓名
  240. adminList, err := system.GetAdminListByIdListWithoutEnable(addIds)
  241. addList := make([]*excel.ExcelWorker, 0)
  242. for _, v := range adminList {
  243. addList = append(addList, &excel.ExcelWorker{
  244. ExcelInfoId: req.ExcelInfoId,
  245. SysUserId: v.AdminId,
  246. SysUserRealName: v.RealName,
  247. CreateTime: time.Now(),
  248. ModifyTime: time.Now(),
  249. })
  250. }
  251. // 保存协作人
  252. err = obj.AddWorker(req.ExcelInfoId, addList, notDeleteWorkerIds)
  253. if err != nil {
  254. br.Msg = "保存失败"
  255. br.ErrMsg = "保存失败,Err:" + err.Error()
  256. return
  257. }
  258. br.Ret = 200
  259. br.Success = true
  260. br.Msg = "操作成功"
  261. }
  262. // GetWorker
  263. // @Title 查询协作人接口
  264. // @Description 新增编辑表格协作人接口
  265. // @Param request body request.EditExcelInfoReq true "type json string"
  266. // @Success 200 {object} response.AddExcelInfoResp
  267. // @router /excel_info/worker [get]
  268. func (c *ExcelInfoController) GetWorker() {
  269. br := new(models.BaseResponse).Init()
  270. defer func() {
  271. c.Data["json"] = br
  272. c.ServeJSON()
  273. }()
  274. sysUser := c.SysUser
  275. if sysUser == nil {
  276. br.Msg = "请登录"
  277. br.ErrMsg = "请登录,SysUser Is Empty"
  278. br.Ret = 408
  279. return
  280. }
  281. excelInfoId, _ := c.GetInt("ExcelInfoId")
  282. if excelInfoId <= 0 {
  283. br.Msg = "请选择表格"
  284. br.ErrMsg = "ExcelInfoId未传"
  285. br.IsSendEmail = false
  286. return
  287. }
  288. obj := new(excel.ExcelWorker)
  289. list, err := obj.GetByExcelInfoId(excelInfoId)
  290. if err != nil {
  291. br.Msg = "获取表格协作人失败!"
  292. br.ErrMsg = "获取表格协作人失败,Err:" + err.Error()
  293. return
  294. }
  295. data := &response.BalanceTableWorkerResp{List: list}
  296. br.Data = data
  297. br.Ret = 200
  298. br.Success = true
  299. br.Msg = "操作成功"
  300. }
  301. // BalanceChartInfoAdd
  302. // @Title 新增图表接口
  303. // @Description 新增图表接口
  304. // @Param request body data_manage.AddChartInfoReq true "type json string"
  305. // @Success 200 {object} data_manage.AddChartInfoResp
  306. // @router /excel_info/balance/chart_add [post]
  307. func (c *ExcelInfoController) BalanceChartInfoAdd() {
  308. br := new(models.BaseResponse).Init()
  309. defer func() {
  310. c.Data["json"] = br
  311. c.ServeJSON()
  312. }()
  313. sysUser := c.SysUser
  314. if sysUser == nil {
  315. br.Msg = "请登录"
  316. br.ErrMsg = "请登录,SysUser Is Empty"
  317. br.Ret = 408
  318. return
  319. }
  320. deleteCache := true
  321. cacheKey := "CACHE_EXCEL_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  322. defer func() {
  323. if deleteCache {
  324. utils.Rc.Delete(cacheKey)
  325. }
  326. }()
  327. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  328. deleteCache = false
  329. br.Msg = "系统处理中,请稍后重试!"
  330. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  331. return
  332. }
  333. var req request.AddBalanceTableChartReq
  334. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  335. if err != nil {
  336. br.Msg = "参数解析异常!"
  337. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  338. return
  339. }
  340. req.ChartName = strings.Trim(req.ChartName, " ")
  341. if req.ChartName == "" {
  342. br.Msg = "请填写图表名称!"
  343. return
  344. }
  345. chartInfo, err, errMsg, isSendEmail := data.AddBalanceExcelChart(req, sysUser)
  346. if err != nil {
  347. br.Msg = "保存失败"
  348. if errMsg != `` {
  349. br.Msg = errMsg
  350. }
  351. br.ErrMsg = err.Error()
  352. br.IsSendEmail = isSendEmail
  353. return
  354. }
  355. resp := new(data_manage.AddChartInfoResp)
  356. resp.ChartInfoId = chartInfo.ChartInfoId
  357. resp.UniqueCode = chartInfo.UniqueCode
  358. resp.ChartType = req.ChartType
  359. //新增操作日志
  360. {
  361. chartLog := new(data_manage.ChartInfoLog)
  362. chartLog.ChartInfoId = chartInfo.ChartInfoId
  363. chartLog.ChartName = req.ChartName
  364. chartLog.ChartClassifyId = 0
  365. chartLog.SysUserId = sysUser.AdminId
  366. chartLog.SysUserRealName = sysUser.RealName
  367. chartLog.UniqueCode = chartInfo.UniqueCode
  368. chartLog.CreateTime = time.Now()
  369. chartLog.Content = string(c.Ctx.Input.RequestBody)
  370. chartLog.Status = "新增图表"
  371. chartLog.Method = c.Ctx.Input.URI()
  372. go data_manage.AddChartInfoLog(chartLog)
  373. }
  374. // 试用平台更新用户累计新增图表数
  375. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  376. if e != nil {
  377. br.Msg = "操作失败"
  378. br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
  379. return
  380. }
  381. if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
  382. go func() {
  383. var r etaTrialService.EtaTrialUserReq
  384. r.Mobile = adminItem.Mobile
  385. _, _ = etaTrialService.UpdateUserChartNum(r)
  386. }()
  387. }
  388. br.Ret = 200
  389. br.Success = true
  390. br.Msg = "保存成功"
  391. br.Data = resp
  392. br.IsAddLog = true
  393. }
  394. // BalanceChartInfoEdit
  395. // @Title 编辑图表接口
  396. // @Description 新增图表接口
  397. // @Param request body data_manage.AddChartInfoReq true "type json string"
  398. // @Success 200 {object} data_manage.AddChartInfoResp
  399. // @router /excel_info/balance/chart_edit [post]
  400. func (c *ExcelInfoController) BalanceChartInfoEdit() {
  401. br := new(models.BaseResponse).Init()
  402. defer func() {
  403. c.Data["json"] = br
  404. c.ServeJSON()
  405. }()
  406. sysUser := c.SysUser
  407. if sysUser == nil {
  408. br.Msg = "请登录"
  409. br.ErrMsg = "请登录,SysUser Is Empty"
  410. br.Ret = 408
  411. return
  412. }
  413. deleteCache := true
  414. cacheKey := "CACHE_EXCEL_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  415. defer func() {
  416. if deleteCache {
  417. utils.Rc.Delete(cacheKey)
  418. }
  419. }()
  420. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  421. deleteCache = false
  422. br.Msg = "系统处理中,请稍后重试!"
  423. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
  424. return
  425. }
  426. var req request.AddBalanceTableChartReq
  427. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  428. if err != nil {
  429. br.Msg = "参数解析异常!"
  430. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  431. return
  432. }
  433. req.ChartName = strings.Trim(req.ChartName, " ")
  434. if req.ChartName == "" {
  435. br.Msg = "请填写图表名称!"
  436. return
  437. }
  438. if req.ChartInfoId <= 0 {
  439. br.Msg = "请选择图表"
  440. return
  441. }
  442. chartItem, err, errMsg, isSendEmail := data.EditBalanceExcelChart(req, sysUser)
  443. if err != nil {
  444. br.Msg = "保存失败"
  445. if errMsg != `` {
  446. br.Msg = errMsg
  447. }
  448. br.ErrMsg = err.Error()
  449. br.IsSendEmail = isSendEmail
  450. return
  451. }
  452. resp := new(data_manage.AddChartInfoResp)
  453. resp.ChartInfoId = chartItem.ChartInfoId
  454. resp.UniqueCode = chartItem.UniqueCode
  455. resp.ChartType = req.ChartType
  456. //新增操作日志
  457. {
  458. chartLog := new(data_manage.ChartInfoLog)
  459. chartLog.ChartName = chartItem.ChartName
  460. chartLog.ChartInfoId = req.ChartInfoId
  461. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  462. chartLog.SysUserId = sysUser.AdminId
  463. chartLog.SysUserRealName = sysUser.RealName
  464. chartLog.UniqueCode = chartItem.UniqueCode
  465. chartLog.CreateTime = time.Now()
  466. chartLog.Content = string(c.Ctx.Input.RequestBody)
  467. chartLog.Status = "编辑图表"
  468. chartLog.Method = c.Ctx.Input.URL()
  469. go data_manage.AddChartInfoLog(chartLog)
  470. }
  471. br.Ret = 200
  472. br.Success = true
  473. br.Msg = "保存成功"
  474. br.Data = resp
  475. br.IsAddLog = true
  476. }
  477. // GetBalanceChartList
  478. // @Title 获取平衡表表关联的图表
  479. // @Description 获取平衡表表关联的图表
  480. // @Param request body request.MixedTableCellDataReq true "type json string"
  481. // @router /excel_info/balance/chart_list [get]
  482. func (c *ExcelInfoController) GetBalanceChartList() {
  483. br := new(models.BaseResponse).Init()
  484. defer func() {
  485. c.Data["json"] = br
  486. c.ServeJSON()
  487. }()
  488. sysUser := c.SysUser
  489. if sysUser == nil {
  490. br.Msg = "请登录"
  491. br.ErrMsg = "请登录,SysUser Is Empty"
  492. br.Ret = 408
  493. return
  494. }
  495. excelInfoId, _ := c.GetInt("ExcelInfoId")
  496. if excelInfoId <= 0 {
  497. br.Msg = "请选择平衡表"
  498. return
  499. }
  500. // 查询所有子表
  501. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  502. if err != nil {
  503. if err.Error() == utils.ErrNoRow() {
  504. br.Msg = "表格不存在"
  505. return
  506. }
  507. br.Msg = "查询子表失败"
  508. br.ErrMsg = "查询子表失败,Err:" + err.Error()
  509. return
  510. }
  511. list := make([]*data_manage.BalanceChartInfoDetailResp, 0)
  512. chartInfoList, mappingListMap, dataListMap, err, errMsg := excelService.GetBalanceExcelChartList(excelInfo, "")
  513. if err != nil {
  514. if errMsg != "" {
  515. br.Msg = errMsg
  516. br.ErrMsg = err.Error()
  517. return
  518. } else {
  519. br.Msg = "查询图表失败"
  520. br.ErrMsg = "查询图表失败,Err:" + err.Error()
  521. }
  522. return
  523. }
  524. for _, chartInfo := range chartInfoList {
  525. mappingList, ok := mappingListMap[chartInfo.ChartInfoId]
  526. if !ok {
  527. br.Msg = "未找到图表关联的指标信息"
  528. return
  529. }
  530. var chartInfoResp *data_manage.ChartInfoDetailResp
  531. chartInfoResp, err, errMsg = data.GetBalanceExcelChartDetail(chartInfo, mappingList, sysUser, dataListMap)
  532. if err != nil {
  533. br.Msg = "查询图表详情失败"
  534. br.ErrMsg = "查询图表详情失败,Err:" + err.Error()
  535. return
  536. }
  537. chartEdbList := make([]*data_manage.ExcelChartEdbView, 0)
  538. for _, v := range mappingList {
  539. tmp := &data_manage.ExcelChartEdbView{
  540. ExcelChartEdbId: v.ExcelChartEdbId,
  541. DateSequenceStr: v.DateSequence,
  542. DataSequenceStr: v.DataSequence,
  543. FromTag: v.FromTag,
  544. }
  545. chartEdbList = append(chartEdbList, tmp)
  546. }
  547. balanceChartInfoResp := &data_manage.BalanceChartInfoDetailResp{
  548. ChartInfoDetailResp: chartInfoResp,
  549. ExcelEdbList: chartEdbList,
  550. }
  551. list = append(list, balanceChartInfoResp)
  552. }
  553. ret := &data_manage.BalanceTableChartListResp{List: list}
  554. br.Ret = 200
  555. br.Success = true
  556. br.Msg = "查询成功"
  557. br.Data = ret
  558. }
  559. // DeleteBalanceChart
  560. // @Title 删除平衡表图表
  561. // @Description 删除平衡表图表
  562. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  563. // @Success 200 Ret=200 删除成功
  564. // @router /excel_info/balance/chart_del [post]
  565. func (c *ExcelInfoController) DeleteBalanceChart() {
  566. br := new(models.BaseResponse).Init()
  567. defer func() {
  568. c.Data["json"] = br
  569. c.ServeJSON()
  570. }()
  571. sysUser := c.SysUser
  572. if sysUser == nil {
  573. br.Msg = "请登录"
  574. br.ErrMsg = "请登录,SysUser Is Empty"
  575. br.Ret = 408
  576. return
  577. }
  578. var req data_manage.DeleteChartClassifyReq
  579. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  580. if err != nil {
  581. br.Msg = "参数解析异常!"
  582. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  583. return
  584. }
  585. if req.ChartInfoId <= 0 {
  586. br.Msg = "参数错误"
  587. br.IsSendEmail = false
  588. return
  589. }
  590. //删除图表
  591. if req.ChartInfoId > 0 {
  592. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  593. if err != nil {
  594. if err.Error() == utils.ErrNoRow() {
  595. br.Msg = "图表已删除,请刷新页面"
  596. br.ErrMsg = "指标不存在,Err:" + err.Error()
  597. return
  598. } else {
  599. br.Msg = "删除失败"
  600. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  601. return
  602. }
  603. }
  604. if chartInfo == nil {
  605. br.Msg = "图表已删除,请刷新页面"
  606. return
  607. }
  608. // todo 查询权限
  609. /*chartClassifyItem, err := data_manage.GetChartClassifyById(chartInfo.ChartClassifyId)
  610. if err != nil {
  611. br.Msg = "获取失败"
  612. br.ErrMsg = "获取图表分类详情数据失败,Err:" + err.Error()
  613. return
  614. }
  615. // 已授权图表、分类id
  616. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(this.SysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  617. if err != nil {
  618. br.Msg = "获取失败"
  619. br.ErrMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  620. return
  621. }
  622. // 权限校验
  623. {
  624. haveOperaAuth := data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, chartClassifyItem.IsJoinPermission, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  625. //图表操作权限
  626. ok := data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, haveOperaAuth)
  627. if !ok {
  628. br.Msg = "没有该图表的操作权限"
  629. br.ErrMsg = "没有该图表的操作权限"
  630. return
  631. }
  632. }*/
  633. // 获取引用该图表的MyCharts, 用于ES删除
  634. var myCond string
  635. var myPars []interface{}
  636. myCond += ` AND a.chart_info_id = ? `
  637. myPars = append(myPars, req.ChartInfoId)
  638. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  639. if e != nil {
  640. br.Msg = "删除失败"
  641. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  642. return
  643. }
  644. myIds := make([]int, 0)
  645. for _, m := range myCharts {
  646. myIds = append(myIds, m.MyChartId)
  647. }
  648. // 查询图表相关联的数据
  649. edbIds := make([]int, 0)
  650. // 查询图表相关联的指标
  651. edbList, e := excel.GetExcelChartEdbMappingByChartInfoId(req.ChartInfoId)
  652. if e != nil {
  653. br.Msg = "删除失败"
  654. br.ErrMsg = "获取图表相关联的指标失败, Err: " + e.Error()
  655. return
  656. }
  657. for _, v := range edbList {
  658. edbIds = append(edbIds, v.ExcelChartEdbId)
  659. }
  660. //删除图表及关联指标
  661. e = data_manage.DeleteBalanceExcelChartInfoAndData(req.ChartInfoId, edbIds)
  662. if e != nil {
  663. br.Msg = "删除失败"
  664. br.ErrMsg = "删除失败,Err:" + e.Error()
  665. return
  666. }
  667. //删除ES
  668. {
  669. go data.EsDeleteChartInfo(req.ChartInfoId)
  670. // 删除MY ETA 图表 es数据
  671. //go data.EsDeleteMyChartInfoByChartInfoId(req.ChartInfoId)
  672. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  673. }
  674. //新增操作日志
  675. {
  676. chartLog := new(data_manage.ChartInfoLog)
  677. chartLog.ChartName = chartInfo.ChartName
  678. chartLog.ChartInfoId = req.ChartInfoId
  679. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  680. chartLog.SysUserId = sysUser.AdminId
  681. chartLog.SysUserRealName = sysUser.RealName
  682. chartLog.UniqueCode = chartInfo.UniqueCode
  683. chartLog.CreateTime = time.Now()
  684. chartLog.Content = string(c.Ctx.Input.RequestBody)
  685. chartLog.Status = "删除图表"
  686. chartLog.Method = c.Ctx.Input.URI()
  687. go data_manage.AddChartInfoLog(chartLog)
  688. }
  689. }
  690. br.Ret = 200
  691. br.Msg = "删除成功"
  692. br.Success = true
  693. br.IsAddLog = true
  694. }
  695. // BalanceSeasonChartLegendPreview
  696. // @Title 季节性图例预览接口
  697. // @Description 季节性图例预览接口
  698. // @Param request body request.BalanceSeasonChartLegendPreviewReq true "type json string"
  699. // @Success 200 {object} data_manage.BalanceSeasonChartLegendPreviewResp
  700. // @router /excel_info/balance/chartLegend/preview [post]
  701. func (c *ExcelInfoController) BalanceSeasonChartLegendPreview() {
  702. br := new(models.BaseResponse).Init()
  703. defer func() {
  704. c.Data["json"] = br
  705. c.ServeJSON()
  706. }()
  707. sysUser := c.SysUser
  708. if sysUser == nil {
  709. br.Msg = "请登录"
  710. br.ErrMsg = "请登录,SysUser Is Empty"
  711. br.Ret = 408
  712. return
  713. }
  714. var req request.BalanceSeasonChartLegendPreviewReq
  715. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  716. if err != nil {
  717. br.Msg = "参数解析异常!"
  718. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  719. return
  720. }
  721. if len(req.DateArr) == 0 || len(req.DateArr) == 0 || len(req.DateArr) != len(req.DataArr) {
  722. br.Msg = "请输入正确的日期和数据"
  723. return
  724. }
  725. newDataMap := make(map[int]float64)
  726. for i, v := range req.DataArr {
  727. fv, e := strconv.ParseFloat(v, 64)
  728. if e != nil {
  729. br.Msg = "数据格式错误"
  730. return
  731. }
  732. newDataMap[i] = fv
  733. }
  734. //组装成excelEdbData
  735. dataList := make([]*data_manage.EdbDataList, 0)
  736. for i, v := range req.DateArr {
  737. // todo 处理DataTimestamp
  738. dataTime, e := time.ParseInLocation(utils.FormatDate, v, time.Local)
  739. if e != nil {
  740. br.Msg = "日期格式错误"
  741. br.ErrMsg = v + "日期格式错误,Err:" + e.Error()
  742. return
  743. }
  744. timestamp := dataTime.UnixNano() / 1e6
  745. tmp := &data_manage.EdbDataList{
  746. EdbDataId: i,
  747. DataTime: v,
  748. DataTimestamp: timestamp,
  749. Value: newDataMap[i],
  750. }
  751. dataList = append(dataList, tmp)
  752. }
  753. list, err, errMsg := data.GetBalanceExcelSeasonChartLegendPreview(dataList, req.Calendar, req.SeasonExtraConfig)
  754. if err != nil {
  755. if errMsg != "" {
  756. br.Msg = errMsg
  757. } else {
  758. br.Msg = "预览失败"
  759. }
  760. br.ErrMsg = err.Error()
  761. return
  762. }
  763. ret := &data_manage.BalanceSeasonChartLegendPreviewResp{List: list}
  764. br.Data = ret
  765. br.Ret = 200
  766. br.Success = true
  767. br.Msg = "操作成功"
  768. }
  769. // AddStaticExcel
  770. // @Title 新增静态表
  771. // @Description 新增静态表
  772. // @Param request body request.EditExcelInfoReq true "type json string"
  773. // @Success 200 {object} response.AddExcelInfoResp
  774. // @router /excel_info/balance/static/add [post]
  775. func (c *ExcelInfoController) AddStaticExcel() {
  776. br := new(models.BaseResponse).Init()
  777. defer func() {
  778. c.Data["json"] = br
  779. c.ServeJSON()
  780. }()
  781. sysUser := c.SysUser
  782. if sysUser == nil {
  783. br.Msg = "请登录"
  784. br.ErrMsg = "请登录,SysUser Is Empty"
  785. br.Ret = 408
  786. return
  787. }
  788. var req request.AddBalanceStaticExcelInfoReq
  789. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  790. if err != nil {
  791. br.Msg = "参数解析异常!"
  792. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  793. return
  794. }
  795. if req.ExcelInfoId <= 0 {
  796. br.Msg = "请传入ETA表格标识!"
  797. return
  798. }
  799. cacheKey := "CACHE_TABLE_INFO_BALANCE_STATIC_ADD_" + strconv.Itoa(req.ExcelInfoId)
  800. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  801. br.Msg = "系统处理中,请稍后重试!"
  802. br.ErrMsg = "系统处理中,请稍后重试!"
  803. return
  804. }
  805. defer func() {
  806. _ = utils.Rc.Delete(cacheKey)
  807. }()
  808. req.VersionName = strings.Trim(req.VersionName, " ")
  809. if req.VersionName == "" {
  810. br.Msg = "请填写版本名称!"
  811. br.IsSendEmail = false
  812. return
  813. }
  814. // 获取原ETA表格信息
  815. oldExcelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
  816. if err != nil {
  817. br.Msg = "获取ETA表格失败"
  818. return
  819. }
  820. // 查询出每个子表的内容,并将内容转为静态版本
  821. //查询动态表所有的子表,并复制为静态表
  822. condition := " AND parent_id = ? AND balance_type = 0 "
  823. var pars []interface{}
  824. pars = append(pars, req.ExcelInfoId)
  825. childExcelList, err := excel.GetExcelInfoListByCondition(condition, pars)
  826. if err != nil {
  827. br.Msg = "获取子表失败"
  828. br.ErrMsg = fmt.Sprintf("获取子表失败 %s", err.Error())
  829. return
  830. }
  831. for k, childExcelInfo := range childExcelList {
  832. content, e := excelService.TransferBalanceExcelContentToStatic(childExcelInfo, "")
  833. if e != nil {
  834. br.Msg = "动态内容转成静态失败"
  835. br.ErrMsg = fmt.Sprintf("动态内容转成静态失败 %s", e.Error())
  836. return
  837. }
  838. childExcelList[k].Content = content
  839. }
  840. excelInfo, err, errMsg, isSendEmail := data.AddBalanceStaticExcel(oldExcelInfo, req.VersionName, sysUser, 0, req.ExcelInfoId, 1, childExcelList)
  841. if err != nil {
  842. br.Msg = "复制失败"
  843. if errMsg != `` {
  844. br.Msg = errMsg
  845. }
  846. br.ErrMsg = "复制失败,Err:" + err.Error()
  847. br.IsSendEmail = isSendEmail
  848. return
  849. }
  850. resp := new(response.AddExcelInfoResp)
  851. resp.ExcelInfoId = excelInfo.ExcelInfoId
  852. resp.UniqueCode = excelInfo.UniqueCode
  853. br.Ret = 200
  854. br.Success = true
  855. br.Msg = "保存成功"
  856. br.Data = resp
  857. br.IsAddLog = true
  858. }
  859. // BalanceVersionList
  860. // @Title 查询平衡表版本号列表
  861. // @Description 查询平衡表版本号列表
  862. // @Param request body request.EditExcelInfoReq true "type json string"
  863. // @Success 200 {object} response.AddExcelInfoResp
  864. // @router /excel_info/balance/version [get]
  865. func (c *ExcelInfoController) BalanceVersionList() {
  866. br := new(models.BaseResponse).Init()
  867. defer func() {
  868. c.Data["json"] = br
  869. c.ServeJSON()
  870. }()
  871. sysUser := c.SysUser
  872. if sysUser == nil {
  873. br.Msg = "请登录"
  874. br.ErrMsg = "请登录,SysUser Is Empty"
  875. br.Ret = 408
  876. return
  877. }
  878. excelInfoId, _ := c.GetInt("ExcelInfoId")
  879. if excelInfoId <= 0 {
  880. br.Msg = "请选择表格"
  881. br.ErrMsg = "ExcelInfoId未传"
  882. br.IsSendEmail = false
  883. return
  884. }
  885. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  886. if err != nil {
  887. br.Msg = "获取ETA表格失败"
  888. br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
  889. return
  890. }
  891. if excelInfo.Source != utils.BALANCE_TABLE {
  892. br.Msg = "请选择平衡表"
  893. return
  894. }
  895. if excelInfo.BalanceType != 0 && excelInfo.ParentId != 0 {
  896. br.Msg = "请选择动态平衡表"
  897. return
  898. }
  899. list := make([]*response.BalanceTableVersionListItem, 0)
  900. list = append(list, &response.BalanceTableVersionListItem{
  901. ExcelInfoId: excelInfo.ExcelInfoId,
  902. UniqueCode: excelInfo.UniqueCode,
  903. BalanceType: excelInfo.BalanceType,
  904. RelExcelInfoId: excelInfo.RelExcelInfoId,
  905. VersionName: "动态表", //todo 有个默认的版本名称
  906. })
  907. //查询动态表所有的子表,并复制为静态表
  908. condition := " AND rel_excel_info_id=? AND parent_id = 0 AND balance_type = 1 "
  909. var pars []interface{}
  910. pars = append(pars, excelInfoId)
  911. staticList, err := excel.GetNoContentExcelInfoListByConditionNoPage(condition, pars)
  912. if err != nil {
  913. br.Msg = "获取子表失败"
  914. br.ErrMsg = fmt.Sprintf("获取子表失败 %s", err.Error())
  915. return
  916. }
  917. for _, staticInfo := range staticList {
  918. tmp := &response.BalanceTableVersionListItem{
  919. ExcelInfoId: staticInfo.ExcelInfoId,
  920. UniqueCode: staticInfo.UniqueCode,
  921. BalanceType: staticInfo.BalanceType,
  922. RelExcelInfoId: staticInfo.RelExcelInfoId,
  923. VersionName: staticInfo.VersionName,
  924. }
  925. list = append(list, tmp)
  926. }
  927. ret := &response.BalanceTableVersionListResp{List: list}
  928. br.Data = ret
  929. br.Ret = 200
  930. br.Success = true
  931. br.Msg = "操作成功"
  932. }
  933. // ModifyBalanceExcelVersion
  934. // @Title 版本号重命名
  935. // @Description 版本号重命名
  936. // @Param request body request.AddBalanceStaticExcelInfoReq true "type json string"
  937. // @Success 200 {object} response.AddExcelInfoResp
  938. // @router /excel_info/balance/version/modify [post]
  939. func (c *ExcelInfoController) ModifyBalanceExcelVersion() {
  940. br := new(models.BaseResponse).Init()
  941. defer func() {
  942. c.Data["json"] = br
  943. c.ServeJSON()
  944. }()
  945. sysUser := c.SysUser
  946. if sysUser == nil {
  947. br.Msg = "请登录"
  948. br.ErrMsg = "请登录,SysUser Is Empty"
  949. br.Ret = 408
  950. return
  951. }
  952. var req request.AddBalanceStaticExcelInfoReq
  953. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  954. if err != nil {
  955. br.Msg = "参数解析异常!"
  956. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  957. return
  958. }
  959. if req.ExcelInfoId <= 0 {
  960. br.Msg = "请传入ETA表格标识!"
  961. return
  962. }
  963. req.VersionName = strings.Trim(req.VersionName, " ")
  964. if req.VersionName == "" {
  965. br.Msg = "请填写版本名称!"
  966. br.IsSendEmail = false
  967. return
  968. }
  969. excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
  970. if err != nil {
  971. br.Msg = "获取ETA表格失败"
  972. br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
  973. return
  974. }
  975. if excelInfo.Source != utils.BALANCE_TABLE {
  976. br.Msg = "请选择平衡表"
  977. return
  978. }
  979. if excelInfo.BalanceType != 1 && excelInfo.ParentId != 0 {
  980. br.Msg = "请选择静态表"
  981. return
  982. }
  983. // 检验分类下是否存在该版本号
  984. {
  985. var condition string
  986. var pars []interface{}
  987. condition += " AND rel_excel_info_id=? "
  988. pars = append(pars, req.ExcelInfoId)
  989. condition += " AND version_name=? "
  990. pars = append(pars, req.VersionName)
  991. count, tmpErr := excel.GetExcelInfoCountByCondition(condition, pars)
  992. if tmpErr != nil {
  993. br.Msg = "查询版本号失败"
  994. br.ErrMsg = tmpErr.Error()
  995. return
  996. }
  997. if count > 0 {
  998. br.Msg = "表格版本号已存在,请重新填写表格名称"
  999. br.IsSendEmail = false
  1000. return
  1001. }
  1002. }
  1003. excelInfo.ModifyTime = time.Now()
  1004. excelInfo.VersionName = req.VersionName
  1005. updateExcelInfoParams := []string{"ModifyTime", "VersionName"}
  1006. // todo 同步修改静态表中的图表和指标名称
  1007. // ETA表格信息变更
  1008. err = excelInfo.Update(updateExcelInfoParams)
  1009. if err != nil {
  1010. br.Msg = "操作失败"
  1011. br.ErrMsg = "操作失败,Err:" + err.Error()
  1012. return
  1013. }
  1014. br.Ret = 200
  1015. br.Success = true
  1016. br.Msg = "操作成功"
  1017. br.IsAddLog = true
  1018. }