package cn.com.goldenwater.dcproj.service.impl; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.common.WtprjConvert; import cn.com.goldenwater.dcproj.constValue.BisInspEnum; import cn.com.goldenwater.dcproj.constValue.SplitValue; import cn.com.goldenwater.dcproj.dao.BisInspWtprjRegisterDao; import cn.com.goldenwater.dcproj.dto.BisInspWtprjRegisterDto; import cn.com.goldenwater.dcproj.dto.ResponsibilityDto; import cn.com.goldenwater.dcproj.model.*; import cn.com.goldenwater.dcproj.param.BisInspWtprjRegisterParam; import cn.com.goldenwater.dcproj.param.TypeParam; import cn.com.goldenwater.dcproj.service.*; import cn.com.goldenwater.dcproj.utils.AdLevelUtil; import cn.com.goldenwater.dcproj.utils.Builder; import cn.com.goldenwater.dcproj.utils.DateUtils; import cn.com.goldenwater.dcproj.utils.InspPblmUtils; import cn.com.goldenwater.id.util.UuidUtil; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.List; import java.util.*; import java.util.stream.Collectors; /** * @author lhc * @date 2021-4-14 */ @Service @Transactional public class BisInspWtprjRegisterServiceImpl extends AbstractCrudService implements BisInspWtprjRegisterService, BisInspObjStrategy { @Autowired private BisInspWtprjRegisterDao bisInspWtprjRegisterDao; @Autowired private AttWtprjBaseService attWtprjBaseService; @Autowired private BisInspWtprjSectionService bisInspWtprjSectionService; @Autowired private BisInspWtprjUnitService bisInspWtprjUnitService; public BisInspWtprjRegisterServiceImpl(BisInspWtprjRegisterDao bisInspWtprjRegisterDao) { super(bisInspWtprjRegisterDao); this.bisInspWtprjRegisterDao = bisInspWtprjRegisterDao; } @Override public int insert(BisInspWtprjRegister bisInspWtprjRegister) { bisInspWtprjRegister.setId(UuidUtil.uuid()); bisInspWtprjRegister.setIntm(new Date()); bisInspWtprjRegister.setUptm(new Date()); bisInspWtprjRegister.setDataStat("0"); return this.bisInspWtprjRegisterDao.insert(bisInspWtprjRegister); } @Override public int update(BisInspWtprjRegister bisInspWtprjRegister) { bisInspWtprjRegister.setUptm(new Date()); int ret = this.bisInspWtprjRegisterDao.update(bisInspWtprjRegister); // 更新基础表信息 updateAttBase(bisInspWtprjRegister); return ret; } private void updateAttBase(BisInspWtprjRegister rgstr) { BisInspWtprjRegister r = get(rgstr.getId()); AttWtprjBase base = attWtprjBaseService.getObjId(r.getObjId()); base.setNm(rgstr.getNm()); base.setAdCode(rgstr.getAdCode()); base.setAdName(rgstr.getAdName()); base.setLoc(rgstr.getLoc()); base.setPrjCtgy(rgstr.getPrjCtgy()); base.setPrjType(rgstr.getPrjType()); base.setPrjRisk(rgstr.getPrjRisk()); base.setCenterX(rgstr.getCenterX()); base.setCenterY(rgstr.getCenterY()); base.setGdX(rgstr.getGdX()); base.setGdY(rgstr.getGdY()); base.setNote(rgstr.getNote()); attWtprjBaseService.update(base); } @Override public int delete(String id) { return this.bisInspWtprjRegisterDao.delete(id); } // AttEmpwtprjBase @Override public void insertObj(BisInspAllObj bisInspAllObj, String persId) { BisInspWtprjRegister b = new BisInspWtprjRegister(); AttWtprjBase base = attWtprjBaseService.get(bisInspAllObj.getCode()); BeanUtils.copyProperties(base, b); b.setObjId(bisInspAllObj.getObjId()); b.setId(UuidUtil.uuid()); b.setIntm(new Date()); b.setUptm(new Date()); b.setDataStat("0"); b.setState("0"); /* 添加 登记表 */ bisInspWtprjRegisterDao.insert(b); } @Override public String getType() { return BisInspEnum.WTPRJ.getValue(); } @Override public void cleanObjData(String objId) { BisInspWtprjRegisterParam rgstrParam = new BisInspWtprjRegisterParam(); rgstrParam.setObjId(objId); List list = this.bisInspWtprjRegisterDao.findList(rgstrParam); if (list.size() > 0) { String id = list.get(0).getId(); // 删除登记表 this.bisInspWtprjRegisterDao.delete(id); } } @Override public Object findObjListByType(TypeParam typeParam) { return this.bisInspWtprjRegisterDao.findPageList(typeParam); } @Override public PageInfo findObjPageByType(TypeParam typeParam, HttpServletResponse response) { if (StringUtils.isNotBlank(typeParam.getAdCode())) { typeParam.setAdCode(String.valueOf(AdLevelUtil.SubAd(typeParam.getAdCode()).get(SplitValue.SUBAD))); } PageHelper.startPage(typeParam); List list = this.bisInspWtprjRegisterDao.findPageList(typeParam); return new PageInfo<>(list); } @Override public List wtprjTree(TypeParam typeParam) { List list = bisInspWtprjRegisterDao.findPageList(typeParam); List nodes = new ArrayList<>(); for (BisInspWtprjRegisterDto reg : list) { BisInspAllNode node = Builder.of(BisInspAllNode::new) .with(BisInspAllNode::setId, reg.getId()) .with(BisInspAllNode::setPid, "0") .with(BisInspAllNode::setPnm, reg.getNm()) .with(BisInspAllNode::setObjId, reg.getObjId()) .with(BisInspAllNode::setCode, reg.getCode()) .with(BisInspAllNode::setState, reg.getState()) .with(BisInspAllNode::setLevel, "1") .with(BisInspAllNode::setNodeId, reg.getNodeId()) .with(BisInspAllNode::setChildren, new ArrayList<>()) .build(); nodes.add(node); // 添加登记表下的 标段 addSection(node); } return nodes; } void addSection(BisInspAllNode node) { List sectionList = bisInspWtprjSectionService.listByRegId(node.getId()); for (BisInspWtprjSection sec : sectionList) { BisInspAllNode secNode = new BisInspAllNode(); secNode.setId(sec.getId()); secNode.setObjId(sec.getObjId()); secNode.setPnm(sec.getNm()); secNode.setPid(node.getId()); secNode.setLevel("2"); secNode.setChildren(new ArrayList<>()); // 添加标段下的公司 addUnit(secNode); node.getChildren().add(secNode); } } /** * 添加标段下的公司 * * @param section */ void addUnit(BisInspAllNode section) { List unitList = bisInspWtprjUnitService.listBySecId(section.getId()); for (BisInspWtprjUnit unit : unitList) { BisInspAllNode unitNode = new BisInspAllNode(); unitNode.setId(unit.getId()); unitNode.setPnm(unit.getNm()); unitNode.setObjId(unit.getObjId()); unitNode.setPid(section.getId()); unitNode.setLevel("3"); unitNode.setState(unit.getMendType()); section.getChildren().add(unitNode); } } @Override public void exportResponsibility(String id, HttpServletResponse response) { try { String fileName = "水利项目安全生产责任追究建议清单.pdf"; // 修改模板内容导出新模板 response.setContentType("application/x-msdownload"); String fileNameURL = URLEncoder.encode(fileName, "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";filename*=utf-8''" + fileNameURL); OutputStream out = response.getOutputStream(); testRowSpan(id, out); out.flush(); out.close(); } catch (IOException | DocumentException e) { e.printStackTrace(); } } private void testRowSpan(String id, OutputStream out) throws IOException, DocumentException { BaseFont bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); // 正文的字体 Font headFont = new Font(bfCN, 14f, Font.BOLD); Font topFont = new Font(bfCN, 28f, Font.BOLD); Font textFont = new Font(bfCN, 14f, Font.NORMAL); Document document = new Document(); PdfWriter.getInstance(document, out); document.open(); // 获取数据 List responsibilityDtoList = getResponsibility(id); //建立一个4列的表格 PdfPTable table = new PdfPTable(new float[]{5f, 10f, 10f, 40f, 5f, 5f, 25f}); table.setTotalWidth(PageSize.A4.getWidth() - 20f); table.setLockedWidth(true); String nm = ""; String duchaDate = ""; if (responsibilityDtoList != null && responsibilityDtoList.size() > 0) { nm = responsibilityDtoList.get(0).getNm(); duchaDate = DateUtils.Date2Str(responsibilityDtoList.get(0).getIntm(), "yyyy-MM-dd"); } //第1列 Paragraph paragraph = new Paragraph("水利项目安全生产责任追究建议清单", topFont); paragraph.setAlignment(1); PdfPCell cel0 = new PdfPCell(); cel0.setBorder(0); cel0.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); cel0.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cel0.setPaddingTop(-2f);//把字垂直居中 cel0.setPaddingBottom(8f);//把字垂直居中 cel0.addElement(paragraph); cel0.setColspan(7); table.addCell(cel0); //第1列 Paragraph paragraph01 = new Paragraph("工程名称:" + nm, textFont); paragraph01.setAlignment(1); PdfPCell cell = new PdfPCell(); cell.setBorder(0); cell.addElement(paragraph01); cell.setColspan(4); table.addCell(cell); //第1列 Paragraph paragraph02 = new Paragraph("巡查时间:" + duchaDate, textFont); paragraph02.setAlignment(1); PdfPCell cell2 = new PdfPCell(); cell2.setBorder(0); cell2.addElement(paragraph02); cell2.setColspan(3); table.addCell(cell2); //加表格头部 String[] titleArr = {"序号", "标段", "责任单位", "存在的主要问题", "问题分类", "问题个数", "责任追究建议"}; addTitle(table, titleArr, headFont); //加表格内容 if (responsibilityDtoList != null && responsibilityDtoList.size() > 0) { addContent(table, responsibilityDtoList, textFont); } document.add(table); document.close(); } private List getResponsibility(String id) { List responsibilityList = bisInspWtprjRegisterDao.getResponsibility(id); if (responsibilityList == null || responsibilityList.size() == 0) { return null; } int index = 1; String secNmIndex = responsibilityList.get(0).getSecNm(); for (ResponsibilityDto responsibilityDto : responsibilityList) { if (!secNmIndex.equals(responsibilityDto.getSecNm())) { secNmIndex = responsibilityDto.getSecNm(); index++; } responsibilityDto.setSeq(index); } return responsibilityList; } private void addContent(PdfPTable table, List list, Font textFont) { List seqList = list.stream().map(ResponsibilityDto::getSeq).distinct().sorted().collect(Collectors.toList()); //表格数据内容 for (Integer index : seqList) { // 各标段 List secList = list.stream().filter(res -> index.equals(res.getSeq())).collect(Collectors.toList()); setSecCell(table, secList, textFont); } } private void setSecCell(PdfPTable table, List secList, Font textFont) { //第1列 Paragraph paragraph01 = new Paragraph(String.valueOf(secList.get(0).getSeq()), textFont); paragraph01.setAlignment(1); PdfPCell cell = new PdfPCell(); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 把字垂直居中 cell.setPaddingTop(-2f); // 把字垂直居中 cell.setPaddingBottom(8f); cell.addElement(paragraph01); cell.setRowspan(secList.size()); table.addCell(cell); //合并列 Paragraph paragraph02 = new Paragraph(secList.get(0).getSecNm(), textFont); paragraph02.setAlignment(1); PdfPCell cell1 = new PdfPCell(); cell1.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵 cell1.setPaddingTop(-2f);//把字垂直居中 cell1.setPaddingBottom(8f);//把字垂直居中 cell1.addElement(paragraph02); cell1.setRowspan(secList.size()); table.addCell(cell1); List unitNmList = secList.stream().map(ResponsibilityDto::getUnitNm).distinct().collect(Collectors.toList()); for (String unitNm : unitNmList) { List unitList = secList.stream().filter(unit -> unitNm.equals(unit.getUnitNm())).collect(Collectors.toList()); setUnitCell(table, unitList, textFont); } } private void setUnitCell(PdfPTable table, List secList, Font textFont) { int index = 0; //计算'合并列'的合并相关集合 List indexs; //每个'合并列'的初始下标集合 List spanStartIndex; //每个'合并列'的合并数量集合 List spanNumList; //取出'合并列'的所有数据计算 List ListIn = new ArrayList<>(); long yibanCount = secList.stream().filter(res -> "0".equals(res.getInspPblmCate())).count(); long jiaozhongCount = secList.stream().filter(res -> "1".equals(res.getInspPblmCate())).count(); long yanzhongCount = secList.stream().filter(res -> "2".equals(res.getInspPblmCate())).count(); for (ResponsibilityDto res : secList) { if ("0".equals(res.getInspPblmCate())) { res.setPblmCount(String.valueOf(yibanCount)); } if ("1".equals(res.getInspPblmCate())) { res.setPblmCount(String.valueOf(jiaozhongCount)); } if ("2".equals(res.getInspPblmCate())) { res.setPblmCount(String.valueOf(yanzhongCount)); } ListIn.add(res.getInspPblmCate()); } spanNumList = getSpanNumList(ListIn); spanStartIndex = getStartIndexList(ListIn, spanNumList); indexs = getIndexs(secList, spanNumList, spanStartIndex); for (ResponsibilityDto res : secList) { if (index == 0) { //第1列 Paragraph paragraph01 = new Paragraph(res.getUnitNm(), textFont); paragraph01.setAlignment(1); PdfPCell cell = new PdfPCell(); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setPaddingTop(-2f);//把字垂直居中 cell.setPaddingBottom(8f);//把字垂直居中 cell.addElement(paragraph01); cell.setRowspan(secList.size()); table.addCell(cell); } //合并列 Paragraph paragraph02 = new Paragraph(StringUtils.isBlank(res.getInspPblmDesc()) ? "无" : res.getInspPblmDesc(), textFont); paragraph02.setAlignment(1); PdfPCell cell1 = new PdfPCell(); cell1.addElement(paragraph02); table.addCell(cell1); //合并列 Paragraph paragraph03 = new Paragraph(InspPblmUtils.plbmCateDesc(res.getInspPblmCate()), textFont); paragraph03.setAlignment(1); PdfPCell cell3 = new PdfPCell(); cell3.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵 cell3.setPaddingTop(-2f);//把字垂直居中 cell3.setPaddingBottom(8f);//把字垂直居中 cell3.addElement(paragraph03); setRowsSpan(indexs, spanStartIndex, spanNumList, table, cell3, index); //合并列 Paragraph paragraph04 = new Paragraph(res.getPblmCount(), textFont); paragraph04.setAlignment(1); PdfPCell cell4 = new PdfPCell(); cell4.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); cell4.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//然并卵 cell4.setPaddingTop(-2f);//把字垂直居中 cell4.setPaddingBottom(8f);//把字垂直居中 cell4.addElement(paragraph04); setRowsSpan(indexs, spanStartIndex, spanNumList, table, cell4, index); if (index == 0) { //第1列 Paragraph paragraph01 = new Paragraph(WtprjConvert.mendType1(res.getMendType()), textFont); paragraph01.setAlignment(1); PdfPCell cell = new PdfPCell(); cell.addElement(paragraph01); cell.setRowspan(secList.size()); table.addCell(cell); } index++; } } /** * 设置恰当位置的setRowSpan属性 * * @param indexs 不加单元格的索引集合 * @param spanStartIndex 要加的单元格开始索引集合 * @param spanNumList 要加的单元格合并的值集合 * @param table table * @param cell cell * @param i 索引 */ private void setRowsSpan(List indexs, List spanStartIndex, List spanNumList, PdfPTable table, PdfPCell cell, int i) { if (indexs != null) { boolean isAllNotEqual = true; for (Integer index : indexs) { if (i == index) { isAllNotEqual = false; break; } } //没有不加的下标 if (isAllNotEqual) { // 判断在哪里设置span值 if (spanStartIndex != null) { boolean isSpan = false; int copyJ = 0; for (int j = 0; j < spanStartIndex.size(); j++) { if (i == spanStartIndex.get(j)) { isSpan = true; copyJ = j; break; } } if (isSpan) { int spanNum = spanNumList.get(copyJ); cell.setRowspan(spanNum); } } table.addCell(cell); } } else { table.addCell(cell); } } private void addTitle(PdfPTable table, String[] titleArr, Font headFont) { for (String aTitleArr : titleArr) { Paragraph p = new Paragraph(aTitleArr, headFont); PdfPCell cell = new PdfPCell(); p.setAlignment(1); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 然并卵 cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 把字垂直居中 cell.setPaddingTop(-2f); // 把字垂直居中 cell.setPaddingBottom(8f); cell.addElement(p); table.addCell(cell); } } /** * 获取某列要合并的单元格数量 * * @param list 内容必须符合'A,A,B,C,D,D,D,E' 而不是 'A,A,B,C,A,D,D,D,E' ,即所有相同的值必须在一起不能分散 * @return */ private List getSpanNumList(List list) { LinkedHashMap map = new LinkedHashMap<>(); if (list.size() > 0) { for (int i = 0; i < list.size(); i = i + 1) { map.put(list.get(i), i); } } //修改对应key值的value Set s = map.keySet();//获取KEY集合 List strings = new ArrayList<>(s); int spanNum = 0; for (String string : strings) { int tmpSpanNum = map.get(string) - spanNum + 1; map.put(string, tmpSpanNum); spanNum += tmpSpanNum; } List res = new ArrayList<>(); for (String string : strings) { res.add(map.get(string)); } return res; } /** * 获取某列要合并的单元格初始下标 * * @param list 内容必须符合'A,A,B,C,D,D,D,E' 而不是 'A,A,B,C,A,D,D,D,E' ,即所有相同的值必须在一起不能分散 * @param resSpanNumList * @return */ private List getStartIndexList(List list, List resSpanNumList) { LinkedHashMap map = new LinkedHashMap<>(); if (list.size() > 0) { for (int i = 0; i < list.size(); i = i + 1) { map.put(list.get(i), i); } } Set keys = map.keySet();//获取KEY集合 List stringKeys = new ArrayList<>(keys); List res = new ArrayList<>(); if (stringKeys.size() == resSpanNumList.size()) { for (int i = 0; i < stringKeys.size(); i++) { res.add(map.get(stringKeys.get(i)) - resSpanNumList.get(i) + 1); } } return res; } /** * 不加某字段的索引集合 * * @param list * @param resSpanNumList * @param resStartIndexList * @return */ private List getIndexs(List list, List resSpanNumList, List resStartIndexList) { List indexs = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { for (int j = 0; j < resStartIndexList.size(); j++) { if (i == resStartIndexList.get(j)) { for (int k = i + 1; k < i + resSpanNumList.get(j); k++) { indexs.add(k); } } } } return indexs; } private String getResDesc(String unitId) { List> res = bisInspWtprjUnitService.getResponsibility(unitId); StringBuilder stb = new StringBuilder(); for (Map map : res) { if (MapUtils.getIntValue(map, "count") != 0) { stb.append(InspPblmUtils.plbmCateDesc(MapUtils.getString(map, "level"))) .append("问题:") .append(MapUtils.getIntValue(map, "count")) .append("个,\n") .append(StringUtils.isBlank(MapUtils.getString(map, "chose")) ? "" : ("可选责任追究方式:" + MapUtils.getString(map, "chose") + "\n")) .append("最高责任追究方式:") .append(MapUtils.getString(map, "max", "")) .append("\n"); } } return stb.toString(); } }