package com.engine.salary.biz; import com.engine.salary.constant.SalaryDefaultTenantConstant; import com.engine.salary.entity.sicategory.po.ICategoryPO; import com.engine.salary.entity.sischeme.bo.InsuranceSchemeBO; import com.engine.salary.entity.sischeme.dto.InsuranceSchemeDTO; import com.engine.salary.entity.sischeme.dto.InsuranceSchemeDetailDTO; import com.engine.salary.entity.sischeme.param.InsuranceSchemeReqParam; import com.engine.salary.entity.sischeme.po.InsuranceSchemeDetailPO; import com.engine.salary.entity.sischeme.po.InsuranceSchemePO; import com.engine.salary.entity.sischeme.vo.InsuranceSchemeFormVO; import com.engine.salary.enums.sicategory.*; import com.engine.salary.exception.SalaryRunTimeException; import com.engine.salary.mapper.sicategory.ICategoryMapper; import com.engine.salary.mapper.sischeme.InsuranceSchemeDetailMapper; import com.engine.salary.mapper.sischeme.InsuranceSchemeMapper; import com.engine.salary.util.SalaryAssert; import com.engine.salary.util.SalaryEnumUtil; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.SqlSession; import org.springframework.beans.BeanUtils; import weaver.conn.mybatis.MyBatisFactory; import java.math.BigDecimal; import java.util.*; import java.util.regex.Pattern; import java.util.stream.Collectors; /** * @Author weaver_cl * @Description: TODO * @Date 2022/3/7 * @Version V1.0 **/ public class SISchemeBiz { /** * 获取社保方案 * @param id * @param welfareTypeEnum * @return */ public InsuranceSchemeFormVO getForm(Long id, WelfareTypeEnum welfareTypeEnum) { InsuranceSchemeDTO insuranceSchemeDTO = getSchemeFormDTO(welfareTypeEnum,id); List insuranceSchemeDetailDTOList = getSchemeDetailFormDTO(welfareTypeEnum,id); return InsuranceSchemeFormVO.builder().schemeBatch(insuranceSchemeDTO).schemeDetailList(insuranceSchemeDetailDTOList).build(); } /** * 获取方案明细表集合 新建|详情 * * @param welfareTypeEnum 福利类型 * @param id 方案主键id * @return form */ public List getSchemeDetailFormDTO(WelfareTypeEnum welfareTypeEnum, Long id) { List insuranceCategoryPOS = listByWelfareType(welfareTypeEnum.getValue()); List insuranceSchemeDetailDTOList = new ArrayList<>(); insuranceCategoryPOS.forEach(item -> { PaymentScopeEnum[] paymentScopeEnums = SalaryEnumUtil.stringToEnums(item.getPaymentScope(), ","); Arrays.stream(paymentScopeEnums).forEach(e -> { InsuranceSchemeDetailDTO insuranceSchemeDetailDTO = InsuranceSchemeDetailDTO.builder().build(); InsuranceSchemeDetailPO insuranceSchemeDetailPO = getByPPI(id,e.getValue(),item.getId()); if (insuranceSchemeDetailPO == null) { insuranceSchemeDetailDTO = InsuranceSchemeDetailDTO.builder() .id((long) (Math.random() * 10000)) .insuranceId(item.getId()) .insuranceName(item.getInsuranceName()) .paymentScope(e.getDefaultLabel()) .rententionRule(String.valueOf(RententionRuleEnum.ROUND.getValue())) .build(); if (Objects.equals(item.getDataType(), DataTypeEnum.SYSTEM.getValue())) { insuranceSchemeDetailDTO.setIsPayment(true); } else { insuranceSchemeDetailDTO.setIsPayment(false); } } else { //BeanUtils.copyProperties(insuranceSchemeDetailPO, insuranceSchemeDetailDTO); insuranceSchemeDetailDTO.setEffectiveTime(insuranceSchemeDetailPO.getEffectiveTime()); insuranceSchemeDetailDTO.setExpirationTime(insuranceSchemeDetailPO.getExpirationTime()); insuranceSchemeDetailDTO.setId(insuranceSchemeDetailPO.getId()); insuranceSchemeDetailDTO.setInsuranceId(insuranceSchemeDetailPO.getInsuranceId()); insuranceSchemeDetailDTO.setPaymentScopeValue(insuranceSchemeDetailPO.getPaymentScope()); insuranceSchemeDetailDTO.setPrimaryId(insuranceSchemeDetailPO.getPrimaryId()); insuranceSchemeDetailDTO.setValidNum(insuranceSchemeDetailPO.getValidNum()); if (insuranceSchemeDetailPO.getIsPayment() != null) { insuranceSchemeDetailDTO.setIsPayment(Objects.equals(insuranceSchemeDetailPO.getIsPayment(), IsPaymentEnum.YES.getValue())); } if (StringUtils.isNotBlank(insuranceSchemeDetailPO.getUpperLimit())) { BigDecimal bigDecimal = new BigDecimal(insuranceSchemeDetailPO.getUpperLimit()); insuranceSchemeDetailDTO.setUpperLimit(numberCheck(bigDecimal.toPlainString()) ? null : bigDecimal); } if (StringUtils.isNotBlank(insuranceSchemeDetailPO.getLowerLimit())) { BigDecimal bigDecimal = new BigDecimal(insuranceSchemeDetailPO.getLowerLimit()); insuranceSchemeDetailDTO.setLowerLimit(numberCheck(bigDecimal.toPlainString()) ? null : bigDecimal); } if (StringUtils.isNotBlank(insuranceSchemeDetailPO.getPaymentProportion())) { BigDecimal bigDecimal = new BigDecimal(insuranceSchemeDetailPO.getPaymentProportion()); insuranceSchemeDetailDTO.setPaymentProportion(numberCheck(bigDecimal.toPlainString()) ? null : bigDecimal); } if (StringUtils.isNotBlank(insuranceSchemeDetailPO.getFixedCost())) { BigDecimal bigDecimal = new BigDecimal(insuranceSchemeDetailPO.getFixedCost()); insuranceSchemeDetailDTO.setFixedCost(numberCheck(bigDecimal.toPlainString()) ? null : bigDecimal); } insuranceSchemeDetailDTO.setInsuranceName(item.getInsuranceName()); insuranceSchemeDetailDTO.setRententionRule(String.valueOf(insuranceSchemeDetailPO.getRententionRule())); insuranceSchemeDetailDTO.setPaymentScope(e.getDefaultLabel()); } insuranceSchemeDetailDTO.setPaymentScopeValue(e.getValue()); insuranceSchemeDetailDTOList.add(insuranceSchemeDetailDTO); }); }); return insuranceSchemeDetailDTOList; } /** * 根据福利类型获取 * @param welfareType * @return */ public List listByWelfareType(Integer welfareType) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { ICategoryMapper iCategoryMapper = sqlSession.getMapper(ICategoryMapper.class); List insuranceCategoryPOS = iCategoryMapper.listByWelfareType(welfareType,null); return insuranceCategoryPOS; } finally { sqlSession.close(); } } public boolean numberCheck(String number) { return Pattern.compile("^0\\.[0]*").matcher(number).matches(); } private InsuranceSchemeDetailPO getByPPI(Long primaryId, Integer paymentScope, Long insuranceId) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { InsuranceSchemeDetailMapper insuranceSchemeDetailMapper = sqlSession.getMapper(InsuranceSchemeDetailMapper.class); InsuranceSchemeDetailPO insuranceSchemeDetailPO = insuranceSchemeDetailMapper.getByPPI(primaryId, paymentScope, insuranceId); return insuranceSchemeDetailPO; }finally { sqlSession.close(); } } /** * 获取方案主表信息 新建|详情 * * @param welfareTypeEnum 福利类型 * @param id 方案主键id * @return form */ private InsuranceSchemeDTO getSchemeFormDTO(WelfareTypeEnum welfareTypeEnum, Long id) { InsuranceSchemeDTO insuranceSchemeDTO = InsuranceSchemeDTO.builder().paymentType(PaymentTypeEnum.SCHEME_TOWN).welfareType(welfareTypeEnum).build(); if (id != null) { InsuranceSchemePO insuranceSchemePO = getById(id); SalaryAssert.notNull(insuranceSchemePO,"福利方案不存在"); //BeanUtils.copyProperties(insuranceSchemePO, insuranceSchemeDTO); insuranceSchemeDTO.setId(insuranceSchemePO.getId()); insuranceSchemeDTO.setPaymentArea(insuranceSchemePO.getPaymentArea()); insuranceSchemeDTO.setRemarks(insuranceSchemePO.getRemarks()); insuranceSchemeDTO.setSchemeName(insuranceSchemePO.getSchemeName()); insuranceSchemeDTO.setPaymentType(SalaryEnumUtil.enumMatchByValue(insuranceSchemePO.getPaymentType(), PaymentTypeEnum.values(), PaymentTypeEnum.class)); insuranceSchemeDTO.setWelfareType(welfareTypeEnum); } return insuranceSchemeDTO; } /** *社保方案基础信息主表 * @param id * @return */ public InsuranceSchemePO getById(Long id) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { InsuranceSchemeMapper insuranceSchemeMapper = sqlSession.getMapper(InsuranceSchemeMapper.class); InsuranceSchemePO insuranceSchemePO = insuranceSchemeMapper.getById(id); return insuranceSchemePO; } finally { sqlSession.close(); } } /** * 获取所有方案 * @return */ public List listAll() { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { InsuranceSchemeMapper insuranceSchemeMapper = sqlSession.getMapper(InsuranceSchemeMapper.class); List insuranceSchemePOList = insuranceSchemeMapper.listAll(); return insuranceSchemePOList; } finally { sqlSession.close(); } } /** * 社保方案基础信息明细表 * @param primaryId * @return */ public List listByPrimaryId(Long primaryId) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { InsuranceSchemeDetailMapper insuranceSchemeDetailMapper = sqlSession.getMapper(InsuranceSchemeDetailMapper.class); return insuranceSchemeDetailMapper.queryListBySchemeId(primaryId); } finally { sqlSession.close(); } } /** * 新增 * @param saveParam * @param employeeId */ public void save(InsuranceSchemeReqParam saveParam, long employeeId) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try{ //保存福利项目主表 InsuranceSchemeMapper insuranceSchemeMapper = sqlSession.getMapper(InsuranceSchemeMapper.class); InsuranceSchemePO insuranceSchemePO = InsuranceSchemeBO.convert2BatchPO(saveParam.getInsuranceScheme(), employeeId); insuranceSchemeMapper.insert(insuranceSchemePO); //日志 //保存福利项目明细表 InsuranceSchemeDetailMapper insuranceSchemeDetailMapper = sqlSession.getMapper(InsuranceSchemeDetailMapper.class); List insuranceSchemeDetailPOS = InsuranceSchemeBO.convertToInsuranceSchemeDetailPoList(saveParam.getInsuranceSchemeDetailList(),employeeId,insuranceSchemePO.getId()); insuranceSchemeDetailPOS.forEach(insuranceSchemeDetailMapper::insert); sqlSession.commit(); } finally { sqlSession.close(); } } /** * 更新 * @param updateParam * @param employeeId */ public void update(InsuranceSchemeReqParam updateParam, long employeeId) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { InsuranceSchemeMapper insuranceSchemeMapper = sqlSession.getMapper(InsuranceSchemeMapper.class); InsuranceSchemeDetailMapper insuranceSchemeDetailMapper = sqlSession.getMapper(InsuranceSchemeDetailMapper.class); //查询是否存在福利方案 InsuranceSchemePO insuranceSchemePO = getById(updateParam.getInsuranceScheme().getId()); if (Objects.isNull(insuranceSchemePO)) { throw new SalaryRunTimeException("福利方案不存在"); } //福利方案名称重复(待写) List insuranceSchemePOList = insuranceSchemeMapper.listByName(updateParam.getInsuranceScheme().getSchemeName()); if (CollectionUtils.isNotEmpty(insuranceSchemePOList)) { throw new SalaryRunTimeException("福利方案名称重复"); } //更新福利方案主表 InsuranceSchemePO insuranceSchemePO1 = InsuranceSchemeBO.buildInsuranceSchemePO(insuranceSchemePO, updateParam.getInsuranceScheme()); insuranceSchemeMapper.update(insuranceSchemePO1); //更新福利方案明细表 先删后插 insuranceSchemeDetailMapper.batchDeleteByPrimaryIds(Collections.singleton(updateParam.getInsuranceScheme().getId())); //更新明细表 List insuranceSchemeDetailPOS = InsuranceSchemeBO.convertToInsuranceSchemeDetailPoList(updateParam.getInsuranceSchemeDetailList(), employeeId, insuranceSchemePO.getId()); insuranceSchemeDetailPOS.forEach(insuranceSchemeDetailMapper::insert); //记录操作日志 sqlSession.commit(); }finally { sqlSession.close(); } } /** * 复制方案 * @param id * @param schemeName * @param employeeId */ public void copy(Long id, String schemeName, long employeeId) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { InsuranceSchemeMapper insuranceSchemeMapper = sqlSession.getMapper(InsuranceSchemeMapper.class); InsuranceSchemeDetailMapper insuranceSchemeDetailMapper = sqlSession.getMapper(InsuranceSchemeDetailMapper.class); if (Objects.isNull(id)) { throw new SalaryRunTimeException("方案id为空"); } if(Objects.isNull(schemeName)) { throw new SalaryRunTimeException("复制方案名为空"); } InsuranceSchemePO insuranceSchemePO = getById(id); if (Objects.isNull(insuranceSchemePO)) { throw new SalaryRunTimeException("方案不存在"); } if (insuranceSchemePO.getSchemeName().equals(schemeName)) { throw new SalaryRunTimeException("方案名称重复"); } InsuranceSchemePO batchPO = InsuranceSchemePO.builder() //.id(IdGenerator.generate()) .creator(employeeId) .createTime(new Date()) .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY) .schemeName(schemeName) .paymentArea(insuranceSchemePO.getPaymentArea()) .updateTime(new Date()) .deleteType(DeleteTypeEnum.NOT_DELETED.getValue()) .paymentType(insuranceSchemePO.getPaymentType()) .remarks(insuranceSchemePO.getRemarks()) .welfareType(insuranceSchemePO.getWelfareType()) .isUse(insuranceSchemePO.getIsUse()) .build(); insuranceSchemeMapper.insert(batchPO); List detailList = insuranceSchemeDetailMapper.queryListBySchemeId(batchPO.getId()); if (CollectionUtils.isNotEmpty(detailList)) { List detailPOS = detailList.stream().map(item -> InsuranceSchemeDetailPO.builder() //.id(IdGenerator.generate()) .creator(employeeId) .createTime(new Date()) .deleteType(DeleteTypeEnum.NOT_DELETED.getValue()) .effectiveTime(item.getEffectiveTime()) .expirationTime(item.getExpirationTime()) .fixedCost(item.getFixedCost()) .insuranceId(item.getInsuranceId()) .isPayment(item.getIsPayment()) .lowerLimit(item.getLowerLimit()) .paymentScope(item.getPaymentScope()) .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY) .paymentProportion(item.getPaymentProportion()) .updateTime(new Date()) .primaryId(batchPO.getId()) .rententionRule(item.getRententionRule()) .upperLimit(item.getUpperLimit()) .validNum(item.getValidNum()) .build() ).collect(Collectors.toList()); detailPOS.forEach(insuranceSchemeDetailMapper::insert); } sqlSession.commit(); }finally { sqlSession.close(); } } /** * 根据险种id和是否缴费查询社保方案明细表 * @param insuranceId 险种id * @param isPayment 是否缴费 * @return list */ public List queryListByInsuranceIdIsPayment(Long insuranceId, Integer isPayment) { SqlSession sqlSession = MyBatisFactory.sqlSessionFactory.openSession(); try { InsuranceSchemeDetailMapper insuranceSchemeDetailMapper = sqlSession.getMapper(InsuranceSchemeDetailMapper.class); List insuranceSchemeDetailPOList = insuranceSchemeDetailMapper.queryListByInsuranceIdIsPayment(insuranceId,isPayment); return insuranceSchemeDetailPOList; }finally { sqlSession.close(); } } }