package com.engine.salary.cmd.datacollection; import com.engine.common.biz.AbstractCommonCommand; import com.engine.common.entity.BizLogContext; import com.engine.core.interceptor.CommandContext; import com.engine.salary.biz.AddUpSituationBiz; import com.engine.salary.biz.EmployBiz; import com.engine.salary.biz.TaxAgentBiz; import com.engine.salary.entity.datacollection.AddUpSituation; import com.engine.salary.entity.datacollection.DataCollectionEmployee; import com.engine.salary.entity.datacollection.dto.AddUpSituationDTO; import com.engine.salary.entity.datacollection.param.AddUpSituationImportParam; import com.engine.salary.entity.taxrate.TaxAgent; import com.engine.salary.exception.SalaryRunTimeException; import com.engine.salary.util.excel.ExcelParseHelper; import com.google.common.collect.Maps; import lombok.SneakyThrows; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.poi.util.IOUtils; import weaver.file.ImageFileManager; import weaver.general.Util; import weaver.hrm.User; import javax.servlet.http.HttpServletRequest; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; import static com.engine.salary.constant.SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY; public class AddUpSituationImportCmd extends AbstractCommonCommand> { protected HttpServletRequest request; public AddUpSituationImportCmd(Map params, User user) { this.user = user; this.params = params; } @Override public BizLogContext getLogContext() { return null; } @SneakyThrows @Override public Map execute(CommandContext commandContext) { Map apidatas = new HashMap(); EmployBiz employBiz = new EmployBiz(); AddUpSituationBiz biz = new AddUpSituationBiz(); //检验参数 checkImportParam(); //导入参数 AddUpSituationImportParam importParam = (AddUpSituationImportParam) params.get("importParam"); //excel文件id String imageId = Util.null2String(importParam.getImageId()); //税款所属期 String taxYearMonthStr = Util.null2String(importParam.getTaxYearMonth()); InputStream fileInputStream = null; // try { fileInputStream = ImageFileManager.getInputStreamById(Integer.valueOf(imageId)); // fileInputStream = new FileInputStream("C:\\Users\\钱涛\\Desktop\\salary\\addUpSituation\\importTemplate.xlsx"); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } try { List excelDates = ExcelParseHelper.parse(fileInputStream, AddUpSituationDTO.class, 0, 1, 21, "template.xlsx"); int total = excelDates.size(); int index = 0; int successCount = 0; int errorCount = 0; //人员信息 List employees = employBiz.listEmployee(); List taxAgents = new TaxAgentBiz().listAll(); //税款所属期 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date taxYearMonth = simpleDateFormat.parse(taxYearMonthStr + "-01"); Date now = new Date(); // 错误excel内容 List errorData = new ArrayList<>(); //合规数据 List eligibleData = new ArrayList<>(); for (int i = 0; i < excelDates.size(); i++) { //excel中的数据 AddUpSituationDTO dto = excelDates.get(i); //待插入数据库对象 AddUpSituation po = AddUpSituation.builder().tenantKey(DEFAULT_TENANT_KEY) .createTime(now) .updateTime(now) .creator((long) user.getUID()) .year(Integer.valueOf(taxYearMonthStr.split("-")[0])) .taxYearMonth(taxYearMonth) .build(); //异常点数量 int errorSum = 0; //行号 String rowIndex = String.format("第%s行", i + 2); //相同的姓名 String userName = dto.getUsername(); String deparmentName = dto.getDepartmentName(); List employeeSameIds = employees.stream().filter(e -> (StringUtils.isBlank(userName) || Objects.equals(e.getUsername(), userName)) && (StringUtils.isBlank(deparmentName) || Objects.equals(e.getDeparmentName(), deparmentName))).map(DataCollectionEmployee::getEmployeeId) .collect(Collectors.toList()); if (StringUtils.isBlank(userName)) { //姓名 不能为空 //错误消息对象 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "姓名不能为空"); errorData.add(errorMessageMap); errorSum += 1; } else if (CollectionUtils.isEmpty(employeeSameIds) || employeeSameIds.size() > 1) { Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "员工信息不能为空且不可重复(姓名与部门同时确认唯一)"); errorData.add(errorMessageMap); errorSum += 1; } else { Long employeeId = CollectionUtils.isNotEmpty(employeeSameIds) && employeeSameIds.size() == 1 ? employeeSameIds.get(0) : null; if (employeeId != null && employeeId > 0) { po.setEmployeeId(employeeId); } else { //姓名错误,系统内不存在该姓名 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "姓名错误,系统内不存在该姓名"); errorData.add(errorMessageMap); errorSum += 1; } } String taxAgentName = dto.getTaxAgentName(); if (StringUtils.isBlank(taxAgentName)) { //个税扣缴义务人不能为空 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "个税扣缴义务人不能为空"); errorData.add(errorMessageMap); errorSum += 1; } else { Optional optionalTemp = taxAgents.stream().filter(m -> m.getName().equals(taxAgentName)).findFirst(); if (optionalTemp.isPresent()) { po.setTaxAgentId(optionalTemp.get().getId()); } else { //个税扣缴义务人不存在 Map errorMessageMap = Maps.newHashMap(); errorMessageMap.put("message", rowIndex + "个税扣缴义务人不存在"); errorData.add(errorMessageMap); errorSum += 1; } } //累计收入额 String addUpIncome = dto.getAddUpIncome(); po.setAddUpIncome(Util.null2String(addUpIncome)); //累计减除费用 String addUpSubtraction = dto.getAddUpSubtraction(); po.setAddUpSubtraction(addUpSubtraction); //累计社保个人合计 String addUpSocialSecurityTotal = dto.getAddUpSocialSecurityTotal(); po.setAddUpSocialSecurityTotal(addUpSocialSecurityTotal); //累计公积金个人合计 String addUpAccumulationFundTotal = dto.getAddUpAccumulationFundTotal(); po.setAddUpAccumulationFundTotal(addUpAccumulationFundTotal); //累计子女教育 String addUpChildEducation = dto.getAddUpChildEducation(); po.setAddUpChildEducation(Util.null2String(addUpChildEducation)); //累计继续教育 String addUpContinuingEducation = dto.getAddUpContinuingEducation(); po.setAddUpContinuingEducation(Util.null2String(addUpContinuingEducation)); //累计住房贷款利息 String addUpHousingLoanInterest = dto.getAddUpHousingLoanInterest(); po.setAddUpHousingLoanInterest(Util.null2String(addUpHousingLoanInterest)); //累计住房租金 String addUpHousingRent = dto.getAddUpHousingRent(); po.setAddUpHousingRent(Util.null2String(addUpHousingRent)); //累计赡养老人 String addUpSupportElderly = dto.getAddUpSupportElderly(); po.setAddUpSupportElderly(Util.null2String(addUpSupportElderly)); //累计企业(职业)年金及其他福利 String addUpEnterpriseAndOther = dto.getAddUpEnterpriseAndOther(); po.setAddUpEnterpriseAndOther(addUpEnterpriseAndOther); //累计其他扣除 String addUpOtherDeduction = dto.getAddUpOtherDeduction(); po.setAddUpOtherDeduction(addUpOtherDeduction); //累计免税收入 String addUpTaxExemptIncome = dto.getAddUpTaxExemptIncome(); po.setAddUpTaxExemptIncome(addUpTaxExemptIncome); //累计准予扣除的捐赠额 String addUpAllowedDonation = dto.getAddUpAllowedDonation(); po.setAddUpAllowedDonation(addUpAllowedDonation); //累计已预扣预缴税额 String addUpAdvanceTax = dto.getAddUpAdvanceTax(); po.setAddUpAdvanceTax(addUpAdvanceTax); if (errorSum == 0) { successCount += 1; // 合格数据 eligibleData.add(po); } else { errorCount += 1; // 添加错误数据 } } //入库 biz.handleImportData(eligibleData); apidatas.put("successCount", successCount); apidatas.put("errorCount", errorCount); apidatas.put("errorData", errorData); } finally { IOUtils.closeQuietly(fileInputStream); } return apidatas; } private void checkImportParam() { AddUpSituationImportParam importParam = (AddUpSituationImportParam) params.get("importParam"); //excel文件id String imageId = Util.null2String(importParam.getImageId()); //税款所属期 String declareMonthStr = Util.null2String(importParam.getTaxYearMonth()); if (StringUtils.isBlank(imageId)) { throw new SalaryRunTimeException("文件不存在"); } if (StringUtils.isBlank(declareMonthStr)) { throw new SalaryRunTimeException("税款所属期为空"); } } }