balance_table.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. package excel
  2. import (
  3. "archive/zip"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_mobile/models"
  7. "eta/eta_mobile/models/data_manage"
  8. "eta/eta_mobile/models/data_manage/excel"
  9. "eta/eta_mobile/models/data_manage/excel/request"
  10. "eta/eta_mobile/models/data_manage/excel/response"
  11. "eta/eta_mobile/services/data"
  12. "eta/eta_mobile/services/data/data_manage_permission"
  13. excelService "eta/eta_mobile/services/data/excel"
  14. excel2 "eta/eta_mobile/services/excel"
  15. "eta/eta_mobile/utils"
  16. "fmt"
  17. "github.com/tealeg/xlsx"
  18. "io/ioutil"
  19. "os"
  20. "time"
  21. )
  22. // DeleteBalanceChart
  23. // @Title 删除平衡表图表
  24. // @Description 删除平衡表图表
  25. // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
  26. // @Success 200 Ret=200 删除成功
  27. // @router /excel_info/balance/chart_del [post]
  28. func (c *ExcelInfoController) DeleteBalanceChart() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. c.Data["json"] = br
  32. c.ServeJSON()
  33. }()
  34. sysUser := c.SysUser
  35. if sysUser == nil {
  36. br.Msg = "请登录"
  37. br.ErrMsg = "请登录,SysUser Is Empty"
  38. br.Ret = 408
  39. return
  40. }
  41. var req data_manage.DeleteChartClassifyReq
  42. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  43. if err != nil {
  44. br.Msg = "参数解析异常!"
  45. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  46. return
  47. }
  48. if req.ChartInfoId <= 0 {
  49. br.Msg = "参数错误"
  50. br.IsSendEmail = false
  51. return
  52. }
  53. //删除图表
  54. if req.ChartInfoId > 0 {
  55. chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
  56. if err != nil {
  57. if err.Error() == utils.ErrNoRow() {
  58. br.Msg = "图表已删除,请刷新页面"
  59. br.ErrMsg = "指标不存在,Err:" + err.Error()
  60. return
  61. } else {
  62. br.Msg = "删除失败"
  63. br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
  64. return
  65. }
  66. }
  67. if chartInfo == nil {
  68. br.Msg = "图表已删除,请刷新页面"
  69. return
  70. }
  71. // 操作权限校验, 增加协作人判断
  72. {
  73. // 获取表格信息
  74. excelInfo, e := excel.GetExcelInfoByChartInfoId(req.ChartInfoId)
  75. if e != nil {
  76. br.Msg = "获取ETA表格失败"
  77. return
  78. }
  79. checkExcelInfo := excelInfo
  80. if excelInfo.Source == utils.BALANCE_TABLE {
  81. checkExcelInfoId := excelInfo.ExcelInfoId
  82. if excelInfo.BalanceType == 1 {
  83. checkExcelInfoId = excelInfo.RelExcelInfoId
  84. } else {
  85. if excelInfo.ParentId > 0 {
  86. checkExcelInfoId = excelInfo.ParentId
  87. }
  88. }
  89. if checkExcelInfoId != excelInfo.ExcelInfoId {
  90. checkExcelInfo, e = excel.GetExcelInfoById(checkExcelInfoId)
  91. if e != nil {
  92. br.Msg = "获取平衡表格信息失败"
  93. br.ErrMsg = "获取平衡表格信息失败,Err:" + e.Error()
  94. return
  95. }
  96. }
  97. }
  98. // 数据权限
  99. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
  100. if e != nil {
  101. br.Msg = "获取ETA表格权限失败"
  102. br.ErrMsg = "获取表格权限信息失败,Err" + e.Error()
  103. return
  104. }
  105. button := excelService.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
  106. if !button.OpButton {
  107. br.Msg = "无操作权限"
  108. br.IsSendEmail = false
  109. return
  110. }
  111. }
  112. // 获取引用该图表的MyCharts, 用于ES删除
  113. var myCond string
  114. var myPars []interface{}
  115. myCond += ` AND a.chart_info_id = ? `
  116. myPars = append(myPars, req.ChartInfoId)
  117. myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
  118. if e != nil {
  119. br.Msg = "删除失败"
  120. br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
  121. return
  122. }
  123. myIds := make([]int, 0)
  124. for _, m := range myCharts {
  125. myIds = append(myIds, m.MyChartId)
  126. }
  127. //删除图表及关联指标
  128. e = excel.DeleteBalanceExcelChartInfoAndData(req.ChartInfoId)
  129. if e != nil {
  130. br.Msg = "删除失败"
  131. br.ErrMsg = "删除失败,Err:" + e.Error()
  132. return
  133. }
  134. //删除ES
  135. {
  136. go data.EsDeleteChartInfo(req.ChartInfoId)
  137. // 删除MY ETA 图表 es数据
  138. //go data.EsDeleteMyChartInfoByChartInfoId(req.ChartInfoId)
  139. go data.EsDeleteMyChartInfoByMyChartIds(myIds)
  140. }
  141. //新增操作日志
  142. {
  143. chartLog := new(data_manage.ChartInfoLog)
  144. chartLog.ChartName = chartInfo.ChartName
  145. chartLog.ChartInfoId = req.ChartInfoId
  146. chartLog.ChartClassifyId = chartInfo.ChartClassifyId
  147. chartLog.SysUserId = sysUser.AdminId
  148. chartLog.SysUserRealName = sysUser.RealName
  149. chartLog.UniqueCode = chartInfo.UniqueCode
  150. chartLog.CreateTime = time.Now()
  151. chartLog.Content = string(c.Ctx.Input.RequestBody)
  152. chartLog.Status = "删除图表"
  153. chartLog.Method = c.Ctx.Input.URI()
  154. go data_manage.AddChartInfoLog(chartLog)
  155. }
  156. }
  157. br.Ret = 200
  158. br.Msg = "删除成功"
  159. br.Success = true
  160. br.IsAddLog = true
  161. }
  162. func refreshBalanceTable(excelDetail response.ExcelInfoDetail, lang string) (err error) {
  163. edbInfoIds := make([]int, 0)
  164. edbInfoIdExist := make(map[int]bool)
  165. if excelDetail.ParentId > 0 {
  166. newResult := excelDetail.TableData.(request.MixedTableReq)
  167. newData := newResult.Data
  168. if len(newData) > 0 {
  169. for _, t := range newData {
  170. for _, v := range t {
  171. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  172. edbInfoIdExist[v.EdbInfoId] = true
  173. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  174. }
  175. }
  176. }
  177. }
  178. // 清除缓存
  179. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  180. if utils.Re == nil {
  181. _ = utils.Rc.Delete(key)
  182. }
  183. } else {
  184. //查询父表和子表
  185. cCondition := " AND parent_id = ? AND balance_type=0"
  186. var cPars []interface{}
  187. cPars = append(cPars, excelDetail.ExcelInfoId)
  188. childList, e := excel.GetExcelInfoListByCondition(cCondition, cPars)
  189. if e != nil {
  190. err = fmt.Errorf("获取子表失败 %s", err.Error())
  191. return
  192. }
  193. // 遍历
  194. for _, child := range childList {
  195. var result request.MixedTableReq
  196. err = json.Unmarshal([]byte(child.Content), &result)
  197. if err != nil {
  198. err = errors.New("表格json转结构体失败,Err:" + err.Error())
  199. return
  200. }
  201. var newData [][]request.MixedTableCellDataReq
  202. newData, err, _ = excelService.GetMixedTableCellData(result, lang)
  203. if err != nil {
  204. return
  205. }
  206. if len(newData) > 0 {
  207. for _, t := range newData {
  208. for _, v := range t {
  209. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  210. edbInfoIdExist[v.EdbInfoId] = true
  211. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  212. }
  213. }
  214. }
  215. }
  216. // 清除缓存
  217. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + child.UniqueCode
  218. if utils.Re == nil {
  219. _ = utils.Rc.Delete(key)
  220. }
  221. }
  222. }
  223. if len(edbInfoIds) > 0 {
  224. err, _ = data.EdbInfoRefreshAllFromBaseV3(edbInfoIds, false, true, false)
  225. if err != nil {
  226. err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error())
  227. return
  228. }
  229. }
  230. return
  231. }
  232. // GetChildTable
  233. // @Title 获取子表
  234. // @Description 获取子表
  235. // @Param request body request.MixedTableCellDataReq true "type json string"
  236. // @router /excel_info/child_table [get]
  237. func (c *ExcelInfoController) GetChildTable() {
  238. br := new(models.BaseResponse).Init()
  239. defer func() {
  240. c.Data["json"] = br
  241. c.ServeJSON()
  242. }()
  243. sysUser := c.SysUser
  244. if sysUser == nil {
  245. br.Msg = "请登录"
  246. br.ErrMsg = "请登录,SysUser Is Empty"
  247. br.Ret = 408
  248. return
  249. }
  250. parentId, _ := c.GetInt("ParentId")
  251. if parentId <= 0 {
  252. br.Msg = "请选择父表"
  253. return
  254. }
  255. list := make([]*excel.ExcelInfo, 0)
  256. // 查询所有子表
  257. childList, err := excel.GetChildExcelInfoByParentId(parentId)
  258. if err != nil {
  259. br.Msg = "查询子表失败"
  260. return
  261. }
  262. if len(childList) > 0 {
  263. list = childList
  264. }
  265. resp := &response.BalanceChildTableResp{List: list}
  266. br.Ret = 200
  267. br.Success = true
  268. br.Msg = "查询成功"
  269. br.Data = resp
  270. }
  271. // BalanceVersionList
  272. // @Title 查询平衡表版本号列表
  273. // @Description 查询平衡表版本号列表
  274. // @Param request body request.EditExcelInfoReq true "type json string"
  275. // @Success 200 {object} response.AddExcelInfoResp
  276. // @router /excel_info/balance/version [get]
  277. func (c *ExcelInfoController) BalanceVersionList() {
  278. br := new(models.BaseResponse).Init()
  279. defer func() {
  280. c.Data["json"] = br
  281. c.ServeJSON()
  282. }()
  283. sysUser := c.SysUser
  284. if sysUser == nil {
  285. br.Msg = "请登录"
  286. br.ErrMsg = "请登录,SysUser Is Empty"
  287. br.Ret = 408
  288. return
  289. }
  290. excelInfoId, _ := c.GetInt("ExcelInfoId")
  291. if excelInfoId <= 0 {
  292. br.Msg = "请选择表格"
  293. br.ErrMsg = "ExcelInfoId未传"
  294. br.IsSendEmail = false
  295. return
  296. }
  297. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  298. if err != nil {
  299. br.Msg = "获取ETA表格失败"
  300. br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
  301. return
  302. }
  303. if excelInfo.Source != utils.BALANCE_TABLE {
  304. br.Msg = "请选择平衡表"
  305. return
  306. }
  307. if excelInfo.BalanceType != 0 && excelInfo.ParentId != 0 {
  308. br.Msg = "请选择动态平衡表"
  309. return
  310. }
  311. list := make([]*response.BalanceTableVersionListItem, 0)
  312. list = append(list, &response.BalanceTableVersionListItem{
  313. ExcelInfoId: excelInfo.ExcelInfoId,
  314. UniqueCode: excelInfo.UniqueCode,
  315. BalanceType: excelInfo.BalanceType,
  316. RelExcelInfoId: excelInfo.RelExcelInfoId,
  317. VersionName: "动态表", //todo 有个默认的版本名称
  318. })
  319. //查询动态表所有的子表,并复制为静态表
  320. condition := " AND rel_excel_info_id=? AND parent_id = 0 AND balance_type = 1 "
  321. var pars []interface{}
  322. pars = append(pars, excelInfoId)
  323. staticList, err := excel.GetNoContentExcelInfoListByConditionNoPage(condition, pars)
  324. if err != nil {
  325. br.Msg = "获取子表失败"
  326. br.ErrMsg = fmt.Sprintf("获取子表失败 %s", err.Error())
  327. return
  328. }
  329. for _, staticInfo := range staticList {
  330. tmp := &response.BalanceTableVersionListItem{
  331. ExcelInfoId: staticInfo.ExcelInfoId,
  332. UniqueCode: staticInfo.UniqueCode,
  333. BalanceType: staticInfo.BalanceType,
  334. RelExcelInfoId: staticInfo.RelExcelInfoId,
  335. VersionName: staticInfo.VersionName,
  336. }
  337. list = append(list, tmp)
  338. }
  339. ret := &response.BalanceTableVersionListResp{List: list}
  340. br.Data = ret
  341. br.Ret = 200
  342. br.Success = true
  343. br.Msg = "操作成功"
  344. }
  345. // GetBalanceChartList
  346. // @Title 获取平衡表表关联的图表
  347. // @Description 获取平衡表表关联的图表
  348. // @Param request body request.MixedTableCellDataReq true "type json string"
  349. // @router /excel_info/balance/chart_list [get]
  350. func (c *ExcelInfoController) GetBalanceChartList() {
  351. br := new(models.BaseResponse).Init()
  352. defer func() {
  353. c.Data["json"] = br
  354. c.ServeJSON()
  355. }()
  356. sysUser := c.SysUser
  357. if sysUser == nil {
  358. br.Msg = "请登录"
  359. br.ErrMsg = "请登录,SysUser Is Empty"
  360. br.Ret = 408
  361. return
  362. }
  363. excelInfoId, _ := c.GetInt("ExcelInfoId")
  364. if excelInfoId <= 0 {
  365. br.Msg = "请选择平衡表"
  366. return
  367. }
  368. // 查询所有子表
  369. excelInfo, err := excel.GetExcelInfoById(excelInfoId)
  370. if err != nil {
  371. if err.Error() == utils.ErrNoRow() {
  372. br.Msg = "表格不存在"
  373. return
  374. }
  375. br.Msg = "查询子表失败"
  376. br.ErrMsg = "查询子表失败,Err:" + err.Error()
  377. return
  378. }
  379. list := make([]*data_manage.BalanceChartInfoDetailResp, 0)
  380. chartInfoList, mappingListMap, dataListMap, err, errMsg := excelService.GetBalanceExcelChartList(excelInfo, "")
  381. if err != nil {
  382. if errMsg != "" {
  383. br.Msg = errMsg
  384. br.ErrMsg = err.Error()
  385. return
  386. } else {
  387. br.Msg = "查询图表失败"
  388. br.ErrMsg = "查询图表失败,Err:" + err.Error()
  389. }
  390. return
  391. }
  392. for _, chartInfo := range chartInfoList {
  393. mappingList, ok := mappingListMap[chartInfo.ChartInfoId]
  394. if !ok {
  395. br.Msg = "未找到图表关联的指标信息"
  396. return
  397. }
  398. var chartInfoResp *data_manage.ChartInfoDetailResp
  399. chartInfoResp, err, errMsg = data.GetBalanceExcelChartDetail(chartInfo, mappingList, sysUser, dataListMap)
  400. if err != nil {
  401. br.Msg = "查询图表详情失败"
  402. br.ErrMsg = "查询图表详情失败,Err:" + err.Error()
  403. return
  404. }
  405. chartEdbList := make([]*data_manage.ExcelChartEdbView, 0)
  406. for _, v := range mappingList {
  407. tmp := &data_manage.ExcelChartEdbView{
  408. ExcelChartEdbId: v.ExcelChartEdbId,
  409. DateSequenceStr: v.DateSequence,
  410. DataSequenceStr: v.DataSequence,
  411. FromTag: v.FromTag,
  412. }
  413. chartEdbList = append(chartEdbList, tmp)
  414. }
  415. balanceChartInfoResp := &data_manage.BalanceChartInfoDetailResp{
  416. ChartInfoDetailResp: chartInfoResp,
  417. ExcelEdbList: chartEdbList,
  418. }
  419. list = append(list, balanceChartInfoResp)
  420. }
  421. ret := &data_manage.BalanceTableChartListResp{List: list}
  422. br.Ret = 200
  423. br.Success = true
  424. br.Msg = "查询成功"
  425. br.Data = ret
  426. }
  427. func downloadBalanceTable(excelInfo *excel.ExcelInfo, lang string) (savePath, zipName string, uploadDir string, err error, errMsg string) {
  428. dateDir := time.Now().Format("20060102")
  429. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  430. uploadDir = "static/xls/" + dateDir + "/" + randStr
  431. err = os.MkdirAll(uploadDir, utils.DIR_MOD)
  432. if err != nil {
  433. return
  434. }
  435. fileList := make([]string, 0)
  436. if excelInfo.ParentId != 0 && excelInfo.BalanceType != 0 {
  437. errMsg = "平衡表类型错误"
  438. err = fmt.Errorf("平衡表类型错误 ")
  439. return
  440. }
  441. //复制静态表
  442. staticCondition := " AND parent_id = 0 AND balance_type = 1 AND rel_excel_info_id=? "
  443. var staticPars []interface{}
  444. staticPars = append(staticPars, excelInfo.ExcelInfoId)
  445. excelList, err := excel.GetExcelInfoListByCondition(staticCondition, staticPars)
  446. if err != nil {
  447. errMsg = "获取表格失败"
  448. err = fmt.Errorf("获取表格失败 %s", err.Error())
  449. return
  450. }
  451. excelList = append(excelList, excelInfo)
  452. for _, staticExcelInfo := range excelList {
  453. cCondition := " AND parent_id = ?"
  454. var cPars []interface{}
  455. cPars = append(cPars, staticExcelInfo.ExcelInfoId)
  456. childList, e := excel.GetExcelInfoListByCondition(cCondition, cPars)
  457. if e != nil {
  458. errMsg = "获取子表失败"
  459. err = fmt.Errorf("获取子表失败 %s", err.Error())
  460. return
  461. }
  462. xlsxFile := xlsx.NewFile()
  463. fileName := staticExcelInfo.ExcelName + ".xlsx"
  464. fpath := uploadDir + "/" + fileName
  465. for _, childExcelInfo := range childList {
  466. var result request.MixedTableReq
  467. err = json.Unmarshal([]byte(childExcelInfo.Content), &result)
  468. if err != nil {
  469. errMsg = "获取失败"
  470. err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
  471. return
  472. }
  473. newResult, er, msg := excelService.GetMixedTableCellData(result, lang)
  474. if er != nil {
  475. err = er
  476. errMsg = msg
  477. return
  478. }
  479. tableData, er := excel2.GetTableDataByMixedTableData(newResult, false)
  480. if er != nil {
  481. errMsg = "获取失败"
  482. err = fmt.Errorf("转换成table失败,Err:" + err.Error())
  483. return
  484. }
  485. tableData, err = excel2.HandleRuleToTableCell(childExcelInfo.ExcelInfoId, tableData)
  486. if err != nil {
  487. errMsg = "获取失败"
  488. err = fmt.Errorf("处理条件格式管理规则失败,Err:%w", err)
  489. return
  490. }
  491. // 将单个sheet的数据写入到excel
  492. err = tableData.WriteExcelSheetData(xlsxFile, childExcelInfo.ExcelName)
  493. if err != nil {
  494. return
  495. }
  496. }
  497. //处理excel文件
  498. //return
  499. err = xlsxFile.Save(fpath)
  500. if err != nil {
  501. return
  502. }
  503. fileList = append(fileList, fileName)
  504. }
  505. // 创建zip
  506. zipName = excelInfo.ExcelName + ".zip"
  507. savePath = uploadDir + "/" + zipName
  508. fmt.Println(savePath)
  509. zipFile, err := os.Create(savePath)
  510. if err != nil {
  511. return
  512. }
  513. zipWriter := zip.NewWriter(zipFile)
  514. // 生成zip过程中报错关闭
  515. defer func() {
  516. if err != nil {
  517. zipWriter.Close()
  518. zipFile.Close()
  519. }
  520. //os.Remove(savePath)
  521. }()
  522. //写入zip
  523. for i := 0; i < len(fileList); i++ {
  524. ioWriter, e := zipWriter.Create(fileList[i])
  525. if e != nil {
  526. err = fmt.Errorf("创建zip失败,Err:" + e.Error())
  527. if os.IsPermission(e) {
  528. fmt.Println("权限不足: ", e)
  529. return
  530. }
  531. return
  532. }
  533. fullPath := uploadDir + "/" + fileList[i]
  534. content, e := ioutil.ReadFile(fullPath)
  535. if e != nil {
  536. err = fmt.Errorf("读取文件失败,Err:" + e.Error())
  537. return
  538. }
  539. ioWriter.Write(content)
  540. }
  541. // 生成zip后关闭,否则下载文件会损坏
  542. zipWriter.Close()
  543. zipFile.Close()
  544. //this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  545. return
  546. }