balance_table.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package excel
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mobile/models"
  6. "eta/eta_mobile/models/data_manage"
  7. "eta/eta_mobile/models/data_manage/excel"
  8. "eta/eta_mobile/models/data_manage/excel/request"
  9. "eta/eta_mobile/models/data_manage/excel/response"
  10. "eta/eta_mobile/services/data"
  11. "eta/eta_mobile/services/data/data_manage_permission"
  12. excelService "eta/eta_mobile/services/data/excel"
  13. "eta/eta_mobile/utils"
  14. "fmt"
  15. "time"
  16. )
  17. // DeleteBalanceChart
  18. // @Title 删除平衡表图表
  19. // @Description 删除平衡表图表
  20. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  21. // @Success 200 Ret=200 删除成功
  22. // @router /excel_info/balance/chart_del [post]
  23. func (c *ExcelInfoController) DeleteBalanceChart() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. c.Data["json"] = br
  27. c.ServeJSON()
  28. }()
  29. sysUser := c.SysUser
  30. if sysUser == nil {
  31. br.Msg = "请登录"
  32. br.ErrMsg = "请登录,SysUser Is Empty"
  33. br.Ret = 408
  34. return
  35. }
  36. var req data_manage.DeleteChartClassifyReq
  37. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  38. if err != nil {
  39. br.Msg = "参数解析异常!"
  40. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  41. return
  42. }
  43. if req.ChartInfoId <= 0 {
  44. br.Msg = "参数错误"
  45. br.IsSendEmail = false
  46. return
  47. }
  48. //删除图表
  49. if req.ChartInfoId > 0 {
  50. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  51. if err != nil {
  52. if err.Error() == utils.ErrNoRow() {
  53. br.Msg = "图表已删除,请刷新页面"
  54. br.ErrMsg = "指标不存在,Err:" + err.Error()
  55. return
  56. } else {
  57. br.Msg = "删除失败"
  58. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  59. return
  60. }
  61. }
  62. if chartInfo == nil {
  63. br.Msg = "图表已删除,请刷新页面"
  64. return
  65. }
  66. // 操作权限校验, 增加协作人判断
  67. {
  68. // 获取表格信息
  69. excelInfo, e := excel.GetExcelInfoByChartInfoId(req.ChartInfoId)
  70. if e != nil {
  71. br.Msg = "获取ETA表格失败"
  72. return
  73. }
  74. checkExcelInfo := excelInfo
  75. if excelInfo.Source == utils.BALANCE_TABLE {
  76. checkExcelInfoId := excelInfo.ExcelInfoId
  77. if excelInfo.BalanceType == 1 {
  78. checkExcelInfoId = excelInfo.RelExcelInfoId
  79. } else {
  80. if excelInfo.ParentId > 0 {
  81. checkExcelInfoId = excelInfo.ParentId
  82. }
  83. }
  84. if checkExcelInfoId != excelInfo.ExcelInfoId {
  85. checkExcelInfo, e = excel.GetExcelInfoById(checkExcelInfoId)
  86. if e != nil {
  87. br.Msg = "获取平衡表格信息失败"
  88. br.ErrMsg = "获取平衡表格信息失败,Err:" + e.Error()
  89. return
  90. }
  91. }
  92. }
  93. // 数据权限
  94. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
  95. if e != nil {
  96. br.Msg = "获取ETA表格权限失败"
  97. br.ErrMsg = "获取表格权限信息失败,Err" + e.Error()
  98. return
  99. }
  100. button := excelService.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
  101. if !button.OpButton {
  102. br.Msg = "无操作权限"
  103. br.IsSendEmail = false
  104. return
  105. }
  106. }
  107. // 获取引用该图表的MyCharts, 用于ES删除
  108. var myCond string
  109. var myPars []interface{}
  110. myCond += ` AND a.chart_info_id = ? `
  111. myPars = append(myPars, req.ChartInfoId)
  112. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  113. if e != nil {
  114. br.Msg = "删除失败"
  115. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  116. return
  117. }
  118. myIds := make([]int, 0)
  119. for _, m := range myCharts {
  120. myIds = append(myIds, m.MyChartId)
  121. }
  122. //删除图表及关联指标
  123. e = excel.DeleteBalanceExcelChartInfoAndData(req.ChartInfoId)
  124. if e != nil {
  125. br.Msg = "删除失败"
  126. br.ErrMsg = "删除失败,Err:" + e.Error()
  127. return
  128. }
  129. //删除ES
  130. {
  131. go data.EsDeleteChartInfo(req.ChartInfoId)
  132. // 删除MY ETA 图表 es数据
  133. //go data.EsDeleteMyChartInfoByChartInfoId(req.ChartInfoId)
  134. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  135. }
  136. //新增操作日志
  137. {
  138. chartLog := new(data_manage.ChartInfoLog)
  139. chartLog.ChartName = chartInfo.ChartName
  140. chartLog.ChartInfoId = req.ChartInfoId
  141. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  142. chartLog.SysUserId = sysUser.AdminId
  143. chartLog.SysUserRealName = sysUser.RealName
  144. chartLog.UniqueCode = chartInfo.UniqueCode
  145. chartLog.CreateTime = time.Now()
  146. chartLog.Content = string(c.Ctx.Input.RequestBody)
  147. chartLog.Status = "删除图表"
  148. chartLog.Method = c.Ctx.Input.URI()
  149. go data_manage.AddChartInfoLog(chartLog)
  150. }
  151. }
  152. br.Ret = 200
  153. br.Msg = "删除成功"
  154. br.Success = true
  155. br.IsAddLog = true
  156. }
  157. func refreshBalanceTable(excelDetail response.ExcelInfoDetail, lang string) (err error) {
  158. edbInfoIds := make([]int, 0)
  159. edbInfoIdExist := make(map[int]bool)
  160. if excelDetail.ParentId > 0 {
  161. newResult := excelDetail.TableData.(request.MixedTableReq)
  162. newData := newResult.Data
  163. if len(newData) > 0 {
  164. for _, t := range newData {
  165. for _, v := range t {
  166. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  167. edbInfoIdExist[v.EdbInfoId] = true
  168. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  169. }
  170. }
  171. }
  172. }
  173. // 清除缓存
  174. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  175. if utils.Re == nil {
  176. _ = utils.Rc.Delete(key)
  177. }
  178. } else {
  179. //查询父表和子表
  180. cCondition := " AND parent_id = ? AND balance_type=0"
  181. var cPars []interface{}
  182. cPars = append(cPars, excelDetail.ExcelInfoId)
  183. childList, e := excel.GetExcelInfoListByCondition(cCondition, cPars)
  184. if e != nil {
  185. err = fmt.Errorf("获取子表失败 %s", err.Error())
  186. return
  187. }
  188. // 遍历
  189. for _, child := range childList {
  190. var result request.MixedTableReq
  191. err = json.Unmarshal([]byte(child.Content), &result)
  192. if err != nil {
  193. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  194. return
  195. }
  196. var newData [][]request.MixedTableCellDataReq
  197. newData, err, _ = excelService.GetMixedTableCellData(result, lang)
  198. if err != nil {
  199. return
  200. }
  201. if len(newData) > 0 {
  202. for _, t := range newData {
  203. for _, v := range t {
  204. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  205. edbInfoIdExist[v.EdbInfoId] = true
  206. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  207. }
  208. }
  209. }
  210. }
  211. // 清除缓存
  212. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + child.UniqueCode
  213. if utils.Re == nil {
  214. _ = utils.Rc.Delete(key)
  215. }
  216. }
  217. }
  218. if len(edbInfoIds) > 0 {
  219. err, _ = data.EdbInfoRefreshAllFromBaseV3(edbInfoIds, false, true, false)
  220. if err != nil {
  221. err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error())
  222. return
  223. }
  224. }
  225. return
  226. }