|
|
@@ -0,0 +1,571 @@
|
|
|
+package com.bstek.ureport.console.ureport.controller;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.bstek.ureport.build.ReportBuilder;
|
|
|
+import com.bstek.ureport.console.BaseServletAction;
|
|
|
+import com.bstek.ureport.console.cache.TempObjectCache;
|
|
|
+import com.bstek.ureport.console.designer.ReportDefinitionWrapper;
|
|
|
+import com.bstek.ureport.console.ureport.entity.ReportEntity;
|
|
|
+import com.bstek.ureport.console.ureport.entity.SystemEntity;
|
|
|
+import com.bstek.ureport.console.ureport.entity.UserEntity;
|
|
|
+import com.bstek.ureport.console.ureport.model.*;
|
|
|
+import com.bstek.ureport.console.ureport.service.CodeNumService;
|
|
|
+import com.bstek.ureport.console.ureport.service.ReportService;
|
|
|
+import com.bstek.ureport.console.ureport.service.SystemService;
|
|
|
+import com.bstek.ureport.console.ureport.service.UserService;
|
|
|
+import com.bstek.ureport.console.ureport.util.DownUtil;
|
|
|
+import com.bstek.ureport.console.ureport.util.UreportPreviewUtil;
|
|
|
+import com.bstek.ureport.console.ureport.util.UreportUtil;
|
|
|
+import com.bstek.ureport.console.util.*;
|
|
|
+import com.bstek.ureport.definition.ReportDefinition;
|
|
|
+import com.bstek.ureport.definition.datasource.DatasourceDefinition;
|
|
|
+import com.bstek.ureport.definition.datasource.JdbcDatasourceDefinition;
|
|
|
+import com.bstek.ureport.export.ReportRender;
|
|
|
+import com.bstek.ureport.export.html.HtmlReport;
|
|
|
+import com.bstek.ureport.model.Report;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+import jakarta.servlet.http.Part;
|
|
|
+import jnpf.constant.CodeConst;
|
|
|
+import jnpf.database.util.DynamicDataSourceUtil;
|
|
|
+import jnpf.util.JsonUtil;
|
|
|
+import jnpf.util.StringUtil;
|
|
|
+import jnpf.util.UserProvider;
|
|
|
+import jnpf.util.context.RequestContext;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.dao.QueryTimeoutException;
|
|
|
+import org.springframework.jdbc.support.JdbcUtils;
|
|
|
+
|
|
|
+import jakarta.servlet.ServletException;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import jakarta.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 核心控制层,大部分功能
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class DataReportController extends BaseServletAction {
|
|
|
+
|
|
|
+ public static final String MAIN_SYSTEM = "mainSystem";
|
|
|
+ @Autowired
|
|
|
+ private ReportService reportService;
|
|
|
+ @Autowired
|
|
|
+ private ReportRender reportRender;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private SystemService systemService;
|
|
|
+ @Autowired
|
|
|
+ private CodeNumService codeNumService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
+ String url = retriveMethod(req);
|
|
|
+ String method = req.getMethod();
|
|
|
+ if (method.equals("POST")) {
|
|
|
+ if (url == null) {
|
|
|
+ savaData(req, resp);
|
|
|
+ } else if (url.contains("Actions/Copy")) {
|
|
|
+ String id = url.split("/")[0];
|
|
|
+ copyReport(req, resp, id);
|
|
|
+ } else if (url.contains("Actions/Import")) {//导入
|
|
|
+ importDataReport(req, resp);
|
|
|
+ }
|
|
|
+ } else if (method.equals("PUT")) {
|
|
|
+ if (url.contains("Actions/State")) {
|
|
|
+ String id = url.split("/")[0];
|
|
|
+ stateDataReport(req, resp, id);
|
|
|
+ }else {
|
|
|
+ String id = url;//ids.substring(strStartIndex + 1);
|
|
|
+ if (StringUtil.isEmpty(id)) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ updateData(req, resp, id);
|
|
|
+ }
|
|
|
+ } else if (method.equals("DELETE")) {
|
|
|
+ if (StringUtil.isEmpty(url)) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ deleteData(req, resp, url);
|
|
|
+ } else if (method.equals("GET")) {
|
|
|
+ if (url == null) {
|
|
|
+ getList(req, resp);
|
|
|
+ } else if (url.equals("init")) {
|
|
|
+ init(req, resp);
|
|
|
+ } else if (url.equals("Selector")) {
|
|
|
+ Selector(req, resp);
|
|
|
+ } else if (url.equals("preview")) {
|
|
|
+ previewData(req, resp);
|
|
|
+ } else if (url.contains("Actions/Export")) {
|
|
|
+ //截取id
|
|
|
+ String id = url.split("/")[0];
|
|
|
+ exportDataReport(req, resp, id);
|
|
|
+ } else {
|
|
|
+ //打开报表
|
|
|
+ if (StringUtil.isEmpty(url)) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ getInfo(req, resp, url);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新状态
|
|
|
+ *
|
|
|
+ * @param req
|
|
|
+ * @param resp
|
|
|
+ * @param id
|
|
|
+ */
|
|
|
+ private void stateDataReport(HttpServletRequest req, HttpServletResponse resp, String id) throws IOException {
|
|
|
+ ReportEntity entity = reportService.GetInfo(id);
|
|
|
+ if(entity==null){
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("更新接口状态失败,数据不存在"));
|
|
|
+ }else {
|
|
|
+ entity.setEnabledMark("0".equals(String.valueOf(entity.getEnabledMark()))?1:0);
|
|
|
+ reportService.Update(id,entity);
|
|
|
+ writeObjectToJson(resp, ActionResult.success("更新接口状态成功"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出报表
|
|
|
+ *
|
|
|
+ * @param req
|
|
|
+ * @param resp
|
|
|
+ * @param id
|
|
|
+ */
|
|
|
+ private void exportDataReport(HttpServletRequest req, HttpServletResponse resp, String id) {
|
|
|
+ ReportEntity entity = reportService.GetInfo(id);
|
|
|
+ if (StringUtil.isEmpty(entity.getSystemId())){
|
|
|
+ SystemEntity code = systemService.getInfoByEnCode(RequestContext.getAppCode());
|
|
|
+ if (BeanUtil.isNotEmpty(code)){
|
|
|
+ entity.setSystemId(code.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String contentJson = entity.getContent();
|
|
|
+ try {
|
|
|
+ byte[] content = entity.getContent().getBytes(StandardCharsets.UTF_8);
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
|
|
|
+ ReportDefinition reportDefinition = UreportUtil.parseReport(inputStream, entity.getFullName(), true);
|
|
|
+ for (DatasourceDefinition definition : reportDefinition.getDatasources()) {
|
|
|
+ if (definition instanceof JdbcDatasourceDefinition) {
|
|
|
+ contentJson = contentJson.replaceAll("password=\"" + ((JdbcDatasourceDefinition) definition).getPassword() + "\"", "password=\"\"");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ entity.setContent(contentJson);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ String objectToString = JsonUtil.getObjectToString(entity);
|
|
|
+ DownUtil.downloadFile(objectToString, entity.getFullName() + ".json");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导入
|
|
|
+ *
|
|
|
+ * @param req
|
|
|
+ * @param resp
|
|
|
+ */
|
|
|
+ private void importDataReport(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
|
|
|
+ String fileContent = null;
|
|
|
+ SystemEntity code = systemService.getInfoByEnCode(RequestContext.getAppCode());
|
|
|
+ try {
|
|
|
+ String contentType = req.getContentType();
|
|
|
+ String type = "0";
|
|
|
+ if (contentType != null && contentType.contains("multipart/form-data")) {
|
|
|
+ Part file = req.getPart("file");
|
|
|
+ type = req.getParameter("type");
|
|
|
+ if (file != null) {
|
|
|
+ fileContent = FileUtil.getFileContent(file);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ReportEntity entity = JsonUtil.getJsonToBean(fileContent, ReportEntity.class);
|
|
|
+ if (entity.getContent() == null || entity.getContent().isEmpty()) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("导入失败,数据有误"));
|
|
|
+ }
|
|
|
+ StringJoiner joiner = new StringJoiner("、");
|
|
|
+ boolean idCheck=false;
|
|
|
+ if (StringUtil.isEmpty(entity.getSystemId())
|
|
|
+ ||!entity.getSystemId().equals(code.getId())){
|
|
|
+ entity.setId(jnpf.util.RandomUtil.uuId());
|
|
|
+ entity.setEnCode(codeNumService.getCodeFunction(() ->
|
|
|
+ codeNumService.getCodeOnce(CodeConst.BB), encode -> reportService.IsExistEncode(encode)));
|
|
|
+ entity.setSystemId(code.getId());
|
|
|
+ }else {
|
|
|
+ idCheck = true;
|
|
|
+ }
|
|
|
+ ReportEntity reportEntity = reportService.GetInfo(entity.getId());
|
|
|
+ if (idCheck&&reportEntity != null) {
|
|
|
+ joiner.add("ID");
|
|
|
+ }
|
|
|
+ if (reportService.IsExistByFullName(entity.getFullName(), null, code.getId())) {
|
|
|
+ joiner.add("名称");
|
|
|
+ }
|
|
|
+ if (reportService.IsExistEncode(entity.getEnCode())){
|
|
|
+ joiner.add("编码");
|
|
|
+ }
|
|
|
+ if (ObjectUtil.equal(type, "0") && joiner.length() > 0) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail(joiner.toString() + "重复"));
|
|
|
+ }
|
|
|
+ if (ObjectUtil.equal(type, "1") && joiner.length() > 0) {
|
|
|
+ String copyNum = UUID.randomUUID().toString().substring(0, 5);
|
|
|
+ entity.setFullName(entity.getFullName() + ".副本" + copyNum);
|
|
|
+ entity.setEnCode(entity.getEnCode() + copyNum);
|
|
|
+ entity.setId(null);
|
|
|
+ }
|
|
|
+ String token = req.getHeader("Authorization");
|
|
|
+ String userId = UserProvider.getLoginUserId();
|
|
|
+ entity.setCreatorTime(new Date());
|
|
|
+ entity.setCreatorUser(userId);
|
|
|
+ entity.setLastModifyTime(null);
|
|
|
+ entity.setLastModifyUser(null);
|
|
|
+ entity.setEnabledMark(0);
|
|
|
+ String appCode = RequestContext.getAppCode();
|
|
|
+ SystemEntity systemEntity = systemService.getInfoByEnCode(appCode);
|
|
|
+ entity.setSystemId(systemEntity!=null?systemEntity.getId():"");
|
|
|
+ reportService.Create(entity);
|
|
|
+ writeObjectToJson(resp, ActionResult.success("导入成功"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("导入失败,数据有误"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //初始化
|
|
|
+ public void init(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
+ ReportDefinition reportDef = reportRender.parseReport("classpath:template/template.ureport.xml");
|
|
|
+ writeObjectToJson(resp, ActionResult.success(new ReportDefinitionWrapper(reportDef)));
|
|
|
+ }
|
|
|
+
|
|
|
+ //列表
|
|
|
+ public void getList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
|
|
+ String currentPage = req.getParameter("currentPage");
|
|
|
+ String pageSize = req.getParameter("pageSize");
|
|
|
+ String enabledMark = req.getParameter("enabledMark");
|
|
|
+
|
|
|
+ PaginationReport paginationReport = new PaginationReport();
|
|
|
+ paginationReport.setKeyword(req.getParameter("keyword"));
|
|
|
+ if (ObjectUtil.isNotEmpty(currentPage)) {
|
|
|
+ paginationReport.setCurrentPage(Long.parseLong(currentPage));
|
|
|
+ }
|
|
|
+ paginationReport.setCategory(req.getParameter("category"));
|
|
|
+ if (ObjectUtil.isNotEmpty(pageSize)) {
|
|
|
+ paginationReport.setPageSize(Long.parseLong(pageSize));
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(enabledMark)) {
|
|
|
+ paginationReport.setEnabledMark(Integer.parseInt(enabledMark));
|
|
|
+ }
|
|
|
+
|
|
|
+ String appCode = RequestContext.getAppCode();
|
|
|
+ if (!MAIN_SYSTEM.equals(appCode)){
|
|
|
+ SystemEntity infoByEnCode = systemService.getInfoByEnCode(appCode);
|
|
|
+ paginationReport.setSystemId(infoByEnCode!=null?infoByEnCode.getId():appCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ReportEntity> data = reportService.GetList(paginationReport);
|
|
|
+ List<ReportListVO> list = JsonUtil.getJsonToList(data, ReportListVO.class);
|
|
|
+ for (ReportListVO vo : list) {
|
|
|
+ if (vo.getCreatorUser() != null && !vo.getCreatorUser().equals("")) {
|
|
|
+ UserEntity entity = userService.getInfo(vo.getCreatorUser());
|
|
|
+ if (entity != null) {
|
|
|
+ vo.setCreatorUser(entity.getRealName() + "/" + entity.getAccount());
|
|
|
+ } else {
|
|
|
+ vo.setCreatorUser("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ UserEntity entity1 = null;
|
|
|
+ if (vo.getLastModifyUser() != null && !vo.getLastModifyUser().equals("")) {
|
|
|
+ entity1 = userService.getInfo(vo.getLastModifyUser());
|
|
|
+ if (entity1 != null) {
|
|
|
+ vo.setLastModifyUser(entity1.getRealName() + "/" + entity1.getAccount());
|
|
|
+ } else {
|
|
|
+ vo.setLastModifyUser("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ PaginationVO pagination = JsonUtil.getJsonToBean(paginationReport, PaginationVO.class);
|
|
|
+ writeObjectToJson(resp, ActionResult.page(list, pagination));
|
|
|
+ }
|
|
|
+
|
|
|
+ //下拉
|
|
|
+ public void Selector(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
+ String appCode = RequestContext.getAppCode();
|
|
|
+ SystemEntity infoByEnCode = systemService.getInfoByEnCode(appCode);
|
|
|
+ String systemId = infoByEnCode!=null?infoByEnCode.getId():"";
|
|
|
+ List<ReportEntity> data = reportService.GetList().stream().filter(t -> Objects.equals(t.getEnabledMark(), 1) && Objects.equals(t.getSystemId(),systemId)).collect(Collectors.toList());
|
|
|
+ List<ReportSelectorVO> list = JsonUtil.getJsonToList(data, ReportSelectorVO.class);
|
|
|
+ ListVO vo = new ListVO();
|
|
|
+ vo.setList(list);
|
|
|
+ writeObjectToJson(resp, ActionResult.success(vo));
|
|
|
+ }
|
|
|
+
|
|
|
+ //预览报表
|
|
|
+ public void previewData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
+ String id = req.getParameter("id");
|
|
|
+ String page = req.getParameter("page");
|
|
|
+ String token = req.getHeader("Authorization");
|
|
|
+ String isSwitch = req.getParameter("isSwitch");
|
|
|
+ if ("preview".equals(id)) {
|
|
|
+ try {
|
|
|
+ //未保存文件在编辑器预览
|
|
|
+ ReportDefinition reportDefinition = (ReportDefinition) TempObjectCache.getObject(token);
|
|
|
+ Map<String, Object> parameters = buildParameters(req);
|
|
|
+ ReportBuilder reportBuilder = new ReportBuilder();
|
|
|
+ Connection connection = DynamicDataSourceUtil.getCurrentConnection();
|
|
|
+ Report report;
|
|
|
+ try {
|
|
|
+ if ("true".equals(isSwitch) && TempObjectCache.getObject(token + "_report") != null) {
|
|
|
+ report = (Report) TempObjectCache.getObject(token + "_report");
|
|
|
+ } else {
|
|
|
+ report = reportBuilder.buildReports(reportDefinition, parameters, connection);
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ JdbcUtils.closeConnection(connection);
|
|
|
+ }
|
|
|
+ UreportPreviewUtil previewUtil = new UreportPreviewUtil();
|
|
|
+ HtmlReport htmlReport = null;
|
|
|
+ //分页操作
|
|
|
+ if ("".equals(page) || null == page || "0".equals(page)) {
|
|
|
+ htmlReport = previewUtil.loadReport(report, false, 0);
|
|
|
+ } else {
|
|
|
+ htmlReport = previewUtil.loadReport(report, true, Integer.valueOf(page));
|
|
|
+ TempObjectCache.putObject(token + "_report", report);
|
|
|
+ }
|
|
|
+ htmlReport.setStyle(reportDefinition.getStyle());
|
|
|
+ htmlReport.setSearchFormData(reportDefinition.buildSearchFormData(report.getContext().getDatasetMap(), parameters));
|
|
|
+ writeObjectToJson(resp, ActionResult.success(htmlReport));
|
|
|
+ } catch (QueryTimeoutException qt) {
|
|
|
+ log.error(qt.getMessage());
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("查询数据库超时, 请减少查询的数据量"));
|
|
|
+ } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("缓存已超时"));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //通过id预览
|
|
|
+ ReportEntity entity = reportService.GetInfo(id);
|
|
|
+ if (Objects.equals(entity.getEnabledMark(), 0)) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("报表已被禁用"));
|
|
|
+ }
|
|
|
+ Map<String, Object> parameters = buildParameters(req);
|
|
|
+ UreportPreviewUtil previewUtil = new UreportPreviewUtil();
|
|
|
+ Connection connection = null;
|
|
|
+ try {
|
|
|
+ connection = DynamicDataSourceUtil.getCurrentConnection();
|
|
|
+ } catch (SQLException throwables) {
|
|
|
+ throwables.printStackTrace();
|
|
|
+ }
|
|
|
+ ReportPreviewVO vo;
|
|
|
+ if ("".equals(page) || null == page || "0".equals(page)) {
|
|
|
+ vo = previewUtil.preview(entity, false, 1, parameters, connection, false);
|
|
|
+ } else {
|
|
|
+ vo = previewUtil.preview(entity, true, Integer.valueOf(page), parameters, connection, "true".equals(isSwitch));
|
|
|
+ }
|
|
|
+ vo.setEnabledMark(entity.getEnabledMark());
|
|
|
+ try {
|
|
|
+ connection.close();
|
|
|
+ } catch (SQLException throwables) {
|
|
|
+ log.error("点击预览报错:" + throwables.getMessage());
|
|
|
+ }
|
|
|
+ writeObjectToJson(resp, ActionResult.success(vo));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //通过id打开到报表编辑器
|
|
|
+ public void getInfo(HttpServletRequest req, HttpServletResponse resp, String id) throws ServletException, IOException {
|
|
|
+ ReportEntity entity = reportService.GetInfo(id);
|
|
|
+ ReportDefinition reportDefinition = null;
|
|
|
+ if (entity == null) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在"));
|
|
|
+ }
|
|
|
+ byte[] content = entity.getContent().getBytes(StandardCharsets.UTF_8);
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
|
|
|
+ reportDefinition = UreportUtil.parseReport(inputStream, entity.getFullName(), true);
|
|
|
+ ReportDefinitionWrapper wrapper = new ReportDefinitionWrapper(reportDefinition);
|
|
|
+ ReportInfoModel model = JsonUtil.getJsonToBean(entity, ReportInfoModel.class);
|
|
|
+ writeObjectToJson(resp, ActionResult.successTOBase(wrapper, model));
|
|
|
+ }
|
|
|
+
|
|
|
+ //保存报表
|
|
|
+ public void savaData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
|
|
+ String payload = RequestUtil.getPayload(req);
|
|
|
+ SystemEntity code = systemService.getInfoByEnCode(RequestContext.getAppCode());
|
|
|
+ String token = req.getHeader("Authorization");
|
|
|
+ Map<String, Object> map = JsonUtil.stringToMap(payload);
|
|
|
+ if (map == null) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("不能添加空数据"));
|
|
|
+ } else {
|
|
|
+ ReportCrForm reportCrForm = JsonUtil.getJsonToBean(map, ReportCrForm.class);
|
|
|
+ reportCrForm.setContent(UreportUtil.decodeContent(reportCrForm.getContent()));
|
|
|
+ ReportEntity entity = JsonUtil.getJsonToBean(reportCrForm, ReportEntity.class);
|
|
|
+ if (reportService.IsExistByFullName(entity.getFullName(), entity.getId(),null==code?null:code.getId())) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("名称不能重复"));
|
|
|
+ }else if (reportService.IsExistEncode(entity.getEnCode())){
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("编码不能重复"));
|
|
|
+ }else {
|
|
|
+ //检查表格内容是否合规
|
|
|
+ byte[] content = entity.getContent().getBytes(StandardCharsets.UTF_8);
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
|
|
|
+ UreportUtil.parseReport(inputStream, entity.getFullName());
|
|
|
+
|
|
|
+ String userId = UserProvider.getLoginUserId();
|
|
|
+ entity.setCreatorUser(userId);
|
|
|
+ String appCode = RequestContext.getAppCode();
|
|
|
+ SystemEntity systemEntity = systemService.getInfoByEnCode(appCode);
|
|
|
+ entity.setSystemId(systemEntity!=null?systemEntity.getId():"");
|
|
|
+ reportService.Create(entity);
|
|
|
+ Object id = entity.getId();
|
|
|
+ writeObjectToJson(resp, ActionResult.success(id));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //修改
|
|
|
+ public void updateData(HttpServletRequest req, HttpServletResponse resp, String id) throws ServletException, IOException {
|
|
|
+ String token = req.getHeader("Authorization");
|
|
|
+ SystemEntity code = systemService.getInfoByEnCode(RequestContext.getAppCode());
|
|
|
+ String payload = RequestUtil.getPayload(req);
|
|
|
+ Map<String, Object> map = JsonUtil.stringToMap(payload);
|
|
|
+ if (id == null || id.isEmpty()) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在,修改失败"));
|
|
|
+ }else {
|
|
|
+ ReportUpForm reportUpForm = JsonUtil.getJsonToBean(map, ReportUpForm.class);
|
|
|
+ reportUpForm.setContent(UreportUtil.decodeContent(reportUpForm.getContent()));
|
|
|
+ ReportEntity entity = JsonUtil.getJsonToBean(reportUpForm, ReportEntity.class);
|
|
|
+ //entity.setContent(UreportUtil.decodeContent(entity.getContent()));
|
|
|
+ if (reportService.IsExistByFullName(entity.getFullName(), id,null==code?null:code.getId())) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("名称不能重复"));
|
|
|
+ }else if (reportService.IsExistEncode(entity.getEnCode(),id)){
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("编码不能重复"));
|
|
|
+ } else {
|
|
|
+ //检查表格内容是否合规
|
|
|
+ byte[] content = entity.getContent().getBytes(StandardCharsets.UTF_8);
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
|
|
|
+ UreportUtil.parseReport(inputStream, entity.getFullName());
|
|
|
+
|
|
|
+ String userId = UserProvider.getLoginUserId();
|
|
|
+ entity.setLastModifyUser(userId);
|
|
|
+ boolean flags = reportService.Update(id, entity);
|
|
|
+ if (flags) {
|
|
|
+ writeObjectToJson(resp, ActionResult.success("修改成功"));
|
|
|
+ } else {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在,修改失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //复制报表
|
|
|
+ public void copyReport(HttpServletRequest req, HttpServletResponse resp, String id) throws ServletException, IOException {
|
|
|
+ String token = req.getHeader("Authorization");
|
|
|
+ if (id == null || id.isEmpty()) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在,复制失败"));
|
|
|
+ } else {
|
|
|
+ ReportEntity entity = reportService.GetInfo(id);
|
|
|
+ String userId = UserProvider.getLoginUserId();
|
|
|
+ entity.setCreatorUser(userId);
|
|
|
+ boolean flags = reportService.Copy(entity);
|
|
|
+ if (flags) {
|
|
|
+ writeObjectToJson(resp, ActionResult.success("复制成功"));
|
|
|
+ } else {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在,复制失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //删除
|
|
|
+ public void deleteData(HttpServletRequest req, HttpServletResponse resp, String id) throws ServletException, IOException {
|
|
|
+ if (id == null || id.isEmpty()) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在,修改失败"));
|
|
|
+ } else {
|
|
|
+ ReportEntity entity = reportService.GetInfo(id);
|
|
|
+ boolean flags = reportService.Delete(entity);
|
|
|
+ if (flags) {
|
|
|
+ writeObjectToJson(resp, ActionResult.success("删除成功"));
|
|
|
+ } else {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("数据不存在,修改失败"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /*//通过id导出报表
|
|
|
+ public void exportData(HttpServletRequest req, HttpServletResponse resp, String id, String type) throws ServletException, IOException {
|
|
|
+ ReportEntity entity = reportService.GetInfo(id);
|
|
|
+ if (entity == null) {
|
|
|
+ writeObjectToJson(resp, ActionResult.fail("导出数据不能为空"));
|
|
|
+ }
|
|
|
+ String fileName = entity.getFullName();
|
|
|
+ resp.setCharacterEncoding("UTF-8");
|
|
|
+ resp.setHeader("content-Type", "application/x-download");
|
|
|
+ Connection connection = null;
|
|
|
+ try {
|
|
|
+ if (!dataSourceConfig.isMultiTenancy()) {
|
|
|
+ connection = dataSource.getConnection();
|
|
|
+ } else {
|
|
|
+ connection = JdbcUtil.getConn(dataSourceConfig.getUserName(), dataSourceConfig.getPassword(), dataSourceConfig.getUrl().replace("{dbName}", TenantHolder.getDatasourceName()));
|
|
|
+ }
|
|
|
+ } catch (Exception throwables) {
|
|
|
+ throwables.printStackTrace();
|
|
|
+ log.error("数据源错误:" + throwables.getMessage());
|
|
|
+ }
|
|
|
+ if (type.toLowerCase().equals("pdf")) {
|
|
|
+ resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".pdf", "UTF-8"));
|
|
|
+ OutputStream outputStream = resp.getOutputStream();
|
|
|
+ UreportPdfUtil pdfUtil = new UreportPdfUtil();
|
|
|
+ pdfUtil.buildPdfToConnection(entity, outputStream, connection);
|
|
|
+ outputStream.flush();
|
|
|
+ outputStream.close();
|
|
|
+ } else if (type.toLowerCase().equals("word")) {
|
|
|
+ resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".docx", "UTF-8"));
|
|
|
+ UreportWordUtil wordUtil = new UreportWordUtil();
|
|
|
+ XWPFDocument xwpfDocument = wordUtil.buildWord(entity, connection);
|
|
|
+ xwpfDocument.write(resp.getOutputStream());
|
|
|
+ xwpfDocument.close();
|
|
|
+ } else if (type.toLowerCase().equals("excel")) {
|
|
|
+ resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
|
|
|
+ UreportExcelUtil excelUtil = new UreportExcelUtil();
|
|
|
+ Workbook workbook = excelUtil.buildExcel(entity, false, false, connection);
|
|
|
+ workbook.write(resp.getOutputStream());
|
|
|
+ workbook.close();
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String url() {
|
|
|
+ return "/Data";
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void writeObjectToJson(HttpServletResponse resp, Object obj) throws IOException {
|
|
|
+ resp.setContentType("application/json");
|
|
|
+ resp.setCharacterEncoding("UTF-8");
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+// mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
|
+ mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
|
|
+ mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ OutputStream out = resp.getOutputStream();
|
|
|
+ try {
|
|
|
+ mapper.writeValue(out, obj);
|
|
|
+ } finally {
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|