package cn.com.goldenwater.dcproj.service.impl.ducha; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.dao.BisInspPlanDpDao; import cn.com.goldenwater.dcproj.dao.BisInspPlanYearDao; import cn.com.goldenwater.dcproj.dao.BisInspPlandpRlDao; import cn.com.goldenwater.dcproj.dao.DictTypeDao; import cn.com.goldenwater.dcproj.model.AttAdBase; import cn.com.goldenwater.dcproj.model.BisInspPlanDp; import cn.com.goldenwater.dcproj.model.BisInspPlanYear; import cn.com.goldenwater.dcproj.model.BisInspPlandpRl; import cn.com.goldenwater.dcproj.model.DictType; import cn.com.goldenwater.dcproj.param.BisInspPlanDpParam; import cn.com.goldenwater.dcproj.param.BisInspPlanYearParam; import cn.com.goldenwater.dcproj.param.BisInspPlandpRlParam; import cn.com.goldenwater.dcproj.param.DictTypeParam; import cn.com.goldenwater.dcproj.service.AttAdBaseService; import cn.com.goldenwater.dcproj.service.BisInspPlanDpService; import cn.com.goldenwater.dcproj.service.BisInspPlanYearService; import cn.com.goldenwater.dcproj.service.OlBisInspOrgService; import cn.com.goldenwater.dcproj.utils.AdLevelUtil; import cn.com.goldenwater.dcproj.utils.Constant; import cn.com.goldenwater.dcproj.utils.StringUtils; import cn.com.goldenwater.id.util.UuidUtil; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.apache.commons.collections.CollectionUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; /** * @author lhc * @date 2022-3-3 */ @Service @Transactional public class BisInspPlanYearServiceImpl extends AbstractCrudService implements BisInspPlanYearService { @Autowired private BisInspPlanYearDao bisInspPlanYearDao; @Value("${export.templatePath}") private String templatePath; @Autowired private DictTypeDao dictTypeDao; @Autowired private BisInspPlandpRlDao bisInspPlandpRlDao; @Autowired private BisInspPlanDpDao bisInspPlanDpDao; @Autowired private OlBisInspOrgService olBisInspOrgService; @Autowired private BisInspPlanDpService bisInspPlanDpService; @Autowired private AttAdBaseService attAdBaseService; private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM"); public BisInspPlanYearServiceImpl(BisInspPlanYearDao bisInspPlanYearDao) { super(bisInspPlanYearDao); this.bisInspPlanYearDao = bisInspPlanYearDao; } @Override public int insert(BisInspPlanYear bisInspPlanYear) { String uuid = UuidUtil.uuid(); // 生成uuid bisInspPlanYear.setId(uuid); bisInspPlanYear.setIntm(new Date()); bisInspPlanYear.setUptm(new Date()); bisInspPlanYear.setDataStat("0"); bisInspPlanYear.setState("0"); return this.bisInspPlanYearDao.insert(bisInspPlanYear); } @Override public int update(BisInspPlanYear bisInspPlanYear) { bisInspPlanYear.setUptm(new Date()); return this.bisInspPlanYearDao.update(bisInspPlanYear); } @Override public int delete(String id) { return this.bisInspPlanYearDao.delete(id); } @Override public PageInfo findPageList(BisInspPlanYearParam bisInspPlanYearParam) { PageHelper.startPage(bisInspPlanYearParam); List list = bisInspPlanYearDao.findList(bisInspPlanYearParam); String adCodeSub = StringUtils.substring(bisInspPlanYearParam.getAdCode(), Constant.INT_ZERO, Constant.INT_TWO); //山东 if (StringUtils.equalsAny(adCodeSub, "37")) { List dpList = bisInspPlanDpDao.findList(new BisInspPlanDpParam(adCodeSub)); list.parallelStream().forEach(l -> { if (StringUtils.isNotBlank(l.getAdCode())) { Optional.ofNullable(attAdBaseService.getByAdcode(l.getAdCode())).ifPresent(ad -> { l.setAdFullName(ad.getAdFullName()); l.setAdGrad(ad.getAdGrad()); }); } //参加处室单位 Optional.ofNullable(bisInspPlandpRlDao.findByPlanDpIdList(new BisInspPlandpRlParam(l.getId(), Constant.STRING_ZERO))).ifPresent(c -> { l.setJoinDep(dpList.stream().filter(dp -> c.contains(dp.getId())).map(BisInspPlanDp::getDpName).collect(Collectors.joining(","))); l.setPlanDpRlIds(c); }); //牵头处室 if (StringUtils.isNotBlank(l.getLeadDepId())) { l.setLeadDep(dpList.stream().filter(dp -> StringUtils.equals(dp.getId(), l.getLeadDepId())).map(BisInspPlanDp::getDpName).collect(Collectors.joining(","))); } }); } return new PageInfo<>(list); } @Override public Integer bingObjType(BisInspPlanYear bisInspPlanYear) { bisInspPlanYear.setUptm(new Date()); //绑定督查类型 //通用督查添加 字典添加子对象类型 if (bisInspPlanYear.getObjType().equals("111")) { DictTypeParam dicParam = new DictTypeParam(); dicParam.setOwnerOfc(bisInspPlanYear.getOrgId()); dicParam.setOwnerSystem("gnrl"); dicParam.setDictTypeDesc(bisInspPlanYear.getChkName()); List dictTypes = dictTypeDao.findList(dicParam); if (dictTypes.size() == 0) { //插入通用类型 DictType dictType = new DictType(); dictType.setId(UuidUtil.uuid()); dictType.setOwnerSystem("gnrl"); dictType.setFlagValid("1"); dictType.setOwnerOfc(bisInspPlanYear.getOrgId()); dictType.setDictTypeDesc(bisInspPlanYear.getChkName()); dictType.setDictTypeCd(getMaxDicTypeCd("gnrl")); dictTypeDao.insert(dictType); } } return this.bisInspPlanYearDao.update(bisInspPlanYear); } public String getMaxDicTypeCd(String ownerSystem) { DictTypeParam dicParam = new DictTypeParam(); dicParam.setOwnerSystem("gnrl"); List dictTypes = dictTypeDao.findList(dicParam); if (dictTypes.size() == 0) { return "700"; } else { String maxDicTypeCd = dictTypes.get(0).getDictTypeCd(); maxDicTypeCd = String.valueOf(Integer.parseInt(maxDicTypeCd) + 1); return maxDicTypeCd; } } @Override public void export(HttpServletResponse response, BisInspPlanYearParam bisInspPlanYearParam) { String templateFilePath = templatePath + File.separator + "planCheckSd.xls"; File file = new File(templateFilePath); if (file.exists()) { try { InputStream inputStream = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(inputStream); exportFile(workbook, bisInspPlanYearParam); writeToResponse(response, workbook, "山东年度检查计划.xls"); } catch (Exception e) { e.printStackTrace(); } } } private void exportFile(Workbook xssfWorkbook, BisInspPlanYearParam bisInspPlanYearParam) { List list = bisInspPlanYearDao.findList(bisInspPlanYearParam); String adCodeSub = StringUtils.substring(bisInspPlanYearParam.getAdCode(), Constant.INT_ZERO, Constant.INT_TWO); //山东 if (StringUtils.equalsAny(adCodeSub, "37")) { List dpList = bisInspPlanDpDao.findList(new BisInspPlanDpParam(adCodeSub)); list.parallelStream().forEach(l -> { if (StringUtils.isNotBlank(l.getAdCode())) { Optional.ofNullable(attAdBaseService.getByAdcode(l.getAdCode())).ifPresent(ad -> { l.setAdFullName(ad.getAdFullName()); l.setAdGrad(ad.getAdGrad()); }); } //参加处室单位 Optional.ofNullable(bisInspPlandpRlDao.findByPlanDpIdList(new BisInspPlandpRlParam(l.getId(), Constant.STRING_ZERO))).ifPresent(c -> { l.setJoinDep(dpList.stream().filter(dp -> c.contains(dp.getId())).map(BisInspPlanDp::getDpName).collect(Collectors.joining(","))); l.setPlanDpRlIds(c); }); //牵头处室 if (StringUtils.isNotBlank(l.getLeadDepId())) { l.setLeadDep(dpList.stream().filter(dp -> StringUtils.equals(dp.getId(), l.getLeadDepId())).map(BisInspPlanDp::getDpName).collect(Collectors.joining(","))); } }); } Sheet sheet = xssfWorkbook.getSheetAt(0); String title = "年度检查计划"; if (bisInspPlanYearParam.getChkYear() != null) { title = bisInspPlanYearParam.getChkYear() + title; } CellStyle styleContent = xssfWorkbook.createCellStyle(); styleContent.setAlignment(HorizontalAlignment.CENTER); styleContent.setVerticalAlignment(VerticalAlignment.CENTER); setCellValue(sheet, 0, 1, title); if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { Row row = sheet.createRow(3 + i); int cols = 15; for (int j = 0; j <= cols; j++) { Cell cell = row.createCell(j); cell.setCellStyle(styleContent); } } for (int i = 0; i < list.size(); i++) { BisInspPlanYear bisInspPlanYear = list.get(i); setAllCell(sheet, i, bisInspPlanYear); } } } private void setAllCell(Sheet sheet, int i, BisInspPlanYear bisInspPlanYear) { setCellValue(sheet, 3 + i, 0, String.valueOf(bisInspPlanYear.getOrdr())); setCellValue(sheet, 3 + i, 1, String.valueOf(bisInspPlanYear.getChkYear())); AttAdBase byAdcode = attAdBaseService.getByAdcode(bisInspPlanYear.getAdCode()); setCellValue(sheet, 3 + i, 2, byAdcode.getAdName()); setCellValue(sheet, 3 + i, 3, byAdcode.getLabelFromAdGrad()); setCellValue(sheet, 3 + i, 4, bisInspPlanYear.getChkName()); setCellValue(sheet, 3 + i, 5, bisInspPlanYear.getChkType()); setCellValue(sheet, 3 + i, 6, bisInspPlanYear.getChkContent()); setCellValue(sheet, 3 + i, 7, simpleDateFormat.format(bisInspPlanYear.getPlanSttm())); setCellValue(sheet, 3 + i, 8, simpleDateFormat.format(bisInspPlanYear.getPlanEntm())); setCellValue(sheet, 3 + i, 9, bisInspPlanYear.getLeadDep()); setCellValue(sheet, 3 + i, 10, bisInspPlanYear.getJoinDep()); setCellValue(sheet, 3 + i, 11, bisInspPlanYear.getChkForm()); setCellValue(sheet, 3 + i, 12, bisInspPlanYear.getChkScope()); setCellValue(sheet, 3 + i, 13, bisInspPlanYear.getChkRly()); setCellValue(sheet, 3 + i, 14, "1".equals(bisInspPlanYear.getState())?"是":"否"); setCellValue(sheet, 3 + i, 15, bisInspPlanYear.getNote()); } private void setCellValue(Sheet sheet, int rownum, int cellnum, String value) { Cell cell = sheet.getRow(rownum).getCell(cellnum); cell.setCellValue(value); } private void writeToResponse(HttpServletResponse response, Workbook workbook, String name) throws IOException { // 修改模板内容导出新模板 response.setContentType("application/x-msdownload"); String fileNameURL = URLEncoder.encode(name, "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";filename*=utf-8''" + fileNameURL); OutputStream out = response.getOutputStream(); workbook.write(out); out.flush(); out.close(); } /** * 针对 年度计划的子表参加处室(单位) 更新数据 * @param bisInspPlanYear */ @Override public void updatePostProcessing(BisInspPlanYear bisInspPlanYear) { //清空 bisInspPlandpRlDao.deleteBy(new BisInspPlandpRlParam(bisInspPlanYear.getId(), Constant.STRING_ZERO)); List rlIds = bisInspPlanYear.getPlanDpRlIds(); //自定义 参加单位 添加 String planDpRlNames = bisInspPlanYear.getJoinDep(); //参加处室(单位) Map list = new HashMap<>(); if (CollectionUtils.isNotEmpty(rlIds)) { for (String planDpId : rlIds) { list.put(planDpId,new BisInspPlandpRl(bisInspPlanYear.getId(), planDpId, Constant.STRING_ZERO, bisInspPlanYear.getPersId())); } } if (StringUtils.isNotBlank(planDpRlNames)) { Set dpRlNames = Arrays.stream(StringUtils.split(planDpRlNames, ",")).filter(StringUtils::isNotBlank).collect(Collectors.toSet()); String province = AdLevelUtil.getAddvcd(olBisInspOrgService.getRlProvince(bisInspPlanYear.getOrgId())); List rlist = new ArrayList<>(); dpRlNames.forEach(dpRlName -> rlist.add(new BisInspPlanDp(dpRlName, Constant.STRING_TWO, bisInspPlanYear.getPersId(), province))); if(rlist.size()>0){ bisInspPlanDpService.batchInsert(rlist); } if (CollectionUtils.isNotEmpty(rlist)) { rlist.forEach(dp -> { list.put(dp.getId(),new BisInspPlandpRl(bisInspPlanYear.getId(), dp.getId(), Constant.STRING_ZERO, bisInspPlanYear.getPersId())); }); } } if (list.size() > 0 && null !=list.values() && list.values().size()>0) { bisInspPlandpRlDao.batchInsert(new ArrayList<>(list.values())); } } }