package com.engine.salary.service.impl; import com.alibaba.fastjson.JSON; import com.alipay.oceanbase.jdbc.StringUtils; import com.engine.common.util.ServiceUtil; import com.engine.core.impl.Service; import com.engine.salary.constant.SalaryDefaultTenantConstant; import com.engine.salary.encrypt.siaccount.InsuranceAccountDetailPOEncrypt; import com.engine.salary.entity.hrm.dto.HrmInfoDTO; import com.engine.salary.entity.hrm.param.HrmQueryParam; import com.engine.salary.entity.siaccount.dto.InsuranceCompensationDTO; import com.engine.salary.entity.siaccount.param.CompensationParam; import com.engine.salary.entity.siaccount.po.InsuranceAccountDetailPO; import com.engine.salary.entity.siaccount.po.InsuranceCompensationPO; import com.engine.salary.entity.taxagent.dto.TaxAgentEmployeeDTO; import com.engine.salary.enums.sicategory.DeleteTypeEnum; import com.engine.salary.mapper.datacollection.EmployMapper; import com.engine.salary.mapper.siaccount.InsuranceAccountDetailMapper; import com.engine.salary.mapper.siaccount.InsuranceCompensationMapper; import com.engine.salary.service.SIAccountService; import com.engine.salary.service.SICategoryService; import com.engine.salary.service.SICompensationService; import com.engine.salary.util.SalaryAssert; import com.engine.salary.util.SalaryEntityUtil; import com.engine.salary.util.SalaryI18nUtil; import com.engine.salary.util.db.MapperProxyFactory; import com.engine.salary.util.page.PageInfo; import com.engine.salary.util.page.SalaryPageUtil; import com.google.common.collect.Lists; import dm.jdbc.util.IdGenerator; import weaver.hrm.User; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; /** * @Author: sy * @Description: 福利台账-调差实现类 * @Date: 2022/11/23 **/ public class SICompensationServiceImpl extends Service implements SICompensationService { private InsuranceAccountDetailMapper getInsuranceAccountDetailMapper() { return MapperProxyFactory.getProxy(InsuranceAccountDetailMapper.class); } private EmployMapper getEmployMapper() { return MapperProxyFactory.getProxy(EmployMapper.class); } public SICategoryService getSICategoryService(User user) { return ServiceUtil.getService(SICategoryServiceImpl.class, user); } private SIAccountService getSIAccountService(User user) { return ServiceUtil.getService(SIAccountServiceImpl.class, user); } private InsuranceCompensationMapper getInsuranceCompensationMapper() { return MapperProxyFactory.getProxy(InsuranceCompensationMapper.class); } /** * 可调差人员 */ @Override public PageInfo getEmployeeListToCompensation(HrmQueryParam param) { // 当前登录人员 Long currentEmployeeId = (long) user.getUID(); //查询账单月份+个税扣缴义务人下的社保福利正常缴纳人员列表 List normalDataList = getInsuranceAccountDetailMapper().queryNormalListByBillMonth(param.getBillMonth(), param.getPaymentOrganization()); List empIds = normalDataList.stream().map(InsuranceAccountDetailPO::getEmployeeId).collect(Collectors.toList()); List resultData = new ArrayList<>(); if (empIds.size() > 0) { List> partition = Lists.partition(empIds, 1000); partition.forEach(p -> { param.setIds(p); resultData.addAll(getEmployMapper().listHrmInfoByIdAndName(param)); }); } // 分页 PageInfo page = new PageInfo<>(); if (resultData.size() == 0) { return page; } page.setTotal(resultData.size()); page.setList(SalaryPageUtil.subList(param.getPageNum(), param.getPageSize(), resultData)); page.setPageSize(param.getPageSize()); page.setPageNum(param.getPageNum()); return page; } /** * 调差福利项 * @param id InsuranceAccountDetailPO.id */ @Override public List> compensationCategoryType(Long id) { SalaryAssert.notNull(id, SalaryI18nUtil.getI18nLabel(120999, "调差对象必选")); InsuranceAccountDetailPO insuranceAccountDetailPO = getInsuranceAccountDetailMapper().getById(id); if (insuranceAccountDetailPO == null) { return Lists.newArrayList(); } InsuranceAccountDetailPOEncrypt.decryptItem(insuranceAccountDetailPO); List> result = new ArrayList<>(); String socialComJson = insuranceAccountDetailPO.getSocialComJson(); if (StringUtils.isNotBlank(socialComJson)) { Map categoryIdNameMap = getSICategoryService(user).categoryIdNameMap(); Map socialJson = JSON.parseObject(socialComJson, new HashMap().getClass()); for (Map.Entry entry : socialJson.entrySet()) { Map temp = new HashMap<>(); String insuranceId = entry.getKey(); if (StringUtils.isNotBlank(categoryIdNameMap.get(insuranceId))) { temp.put("id", insuranceId); temp.put("content", categoryIdNameMap.get(insuranceId)); result.add(temp); } } } return result; } /** * 获取当前调差福利类型-公司方支出总计 */ @Override public Map>> compensationComTotal(Map> param) { Map>> result = new HashMap<>(); param.forEach((paymentAgencyId, compensationList) -> { List> paymentList = new ArrayList<>(); compensationList.forEach(compensation -> { Map temp = new HashMap<>(); temp.put("rowId", compensation.getRowId()); if (StringUtils.isBlank(compensation.getCategoryType()) || compensation.getTarget() == null) { temp.put("error", SalaryI18nUtil.getI18nLabel(84026, "参数错误")); temp.put("totalNum", "0"); } else { InsuranceAccountDetailPO insuranceAccountDetailPO = getInsuranceAccountDetailMapper().getById(compensation.getTarget()); if (insuranceAccountDetailPO == null) { temp.put("error", SalaryI18nUtil.getI18nLabel(121038, "当前月在该缴纳组织下没有核算记录")); } InsuranceAccountDetailPOEncrypt.decryptItem(insuranceAccountDetailPO); BigDecimal total = new BigDecimal("0"); List categoryTypeList = Arrays.asList(compensation.getCategoryType().split(",")); if (StringUtils.isNotBlank(insuranceAccountDetailPO.getSocialComJson())) { Map socialJson = JSON.parseObject(insuranceAccountDetailPO.getSocialComJson(), new HashMap().getClass()); for (Map.Entry entry : socialJson.entrySet()) { String insuranceId = entry.getKey(); String num = entry.getValue(); if (categoryTypeList.contains(insuranceId)) { total = total.add(new BigDecimal(num)); } } } temp.put("totalNum", total.toPlainString()); } paymentList.add(temp); }); result.put(paymentAgencyId, paymentList); }); return result; } @Override public Map compensationAccount(List list) { long currentEmployeeId = user.getUID(); Map result = new HashMap<>(); for (InsuranceCompensationDTO param : list) { if (StringUtils.isBlank(param.getAdjustmentTotal()) || "0".equals(param.getAdjustmentTotal())) { continue; } InsuranceAccountDetailPO insuranceAccountDetailPO = getInsuranceAccountDetailMapper().getById(param.getTarget()); SalaryAssert.notNull(insuranceAccountDetailPO, SalaryI18nUtil.getI18nLabel(138849, "调差对象不存在")); InsuranceAccountDetailPOEncrypt.decryptItem(insuranceAccountDetailPO); if (StringUtils.isNotBlank(insuranceAccountDetailPO.getSocialComJson())) { Map socialJson = JSON.parseObject(insuranceAccountDetailPO.getSocialComJson(), new HashMap().getClass()); for (Map.Entry entry : socialJson.entrySet()) { String insuranceId = entry.getKey(); String num = entry.getValue(); if (Objects.equals(String.valueOf(param.getAdjustTo()), insuranceId)) { BigDecimal adjustmentTo = new BigDecimal(param.getAdjustmentTotal()); // 调差单位缴纳明细 BigDecimal insuranceNum = new BigDecimal(num); insuranceNum = insuranceNum.add(adjustmentTo); socialJson.replace(insuranceId, insuranceNum.toPlainString()); insuranceAccountDetailPO.setSocialComJson(JSON.toJSONString(socialJson)); // 调差单位合计 BigDecimal comSum = new BigDecimal(insuranceAccountDetailPO.getComSum()); comSum = comSum.add(adjustmentTo); insuranceAccountDetailPO.setComSum(comSum.toPlainString()); // 调差社保单位合计 BigDecimal socialComSum = new BigDecimal(insuranceAccountDetailPO.getSocialComSum()); socialComSum = socialComSum.add(adjustmentTo); insuranceAccountDetailPO.setSocialComSum(socialComSum.toPlainString()); // 调差社保合计 BigDecimal socialSum = new BigDecimal(insuranceAccountDetailPO.getSocialSum()); socialSum = socialSum.add(adjustmentTo); insuranceAccountDetailPO.setSocialSum(socialSum.toPlainString()); // 调差合计 BigDecimal totalSum = new BigDecimal(insuranceAccountDetailPO.getTotal()); totalSum = totalSum.add(adjustmentTo); insuranceAccountDetailPO.setTotal(totalSum.toPlainString()); //更新社保调差后的明细 getInsuranceAccountDetailMapper().updateById(insuranceAccountDetailPO); //新建调差记录 InsuranceCompensationPO insuranceCompensationPO = new InsuranceCompensationPO(); insuranceCompensationPO.setId(IdGenerator.generate()); insuranceCompensationPO.setAdjustmentTotal(param.getAdjustmentTotal()); insuranceCompensationPO.setAdjustTo(param.getAdjustTo()); insuranceCompensationPO.setBillMonth(param.getBillMonth()); insuranceCompensationPO.setCompanyTotal(param.getCompanyTotal()); insuranceCompensationPO.setCreator(currentEmployeeId); insuranceCompensationPO.setCategoryType(param.getCategoryType()); insuranceCompensationPO.setCreateTime(new Date()); insuranceCompensationPO.setDeleteType(DeleteTypeEnum.NOT_DELETED.getValue()); insuranceCompensationPO.setCountryTotal(param.getCountryTotal()); insuranceCompensationPO.setEmployeeId(insuranceAccountDetailPO.getEmployeeId()); insuranceCompensationPO.setPaymentOrganization(insuranceAccountDetailPO.getPaymentOrganization()); insuranceCompensationPO.setTenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY); insuranceCompensationPO.setWelfareType(Integer.valueOf(param.getWelfareType())); insuranceCompensationPO.setUpdateTime(new Date()); getInsuranceCompensationMapper().insert(insuranceCompensationPO); result.put(param.getOriginId(), insuranceCompensationPO.getId().toString()); } } } } //刷新bill_detail统计数据 getSIAccountService(user).refreshBillBatch(list.get(0).getPaymentOrganization(), list.get(0).getBillMonth()); return result; } }