package com.engine.salary.service.impl;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.dto.SalarySobTaxRuleDTO;
import com.engine.salary.entity.salarysob.param.SalarySobTaxRuleSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobBackItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.entity.salarysob.po.SalarySobTaxRulePO;
import com.engine.salary.enums.salarysob.IncomeCategoryEnum;
import com.engine.salary.enums.sicategory.DeleteTypeEnum;
import com.engine.salary.mapper.salarysob.SalarySobTaxRuleMapper;
import com.engine.salary.service.*;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.IdGenerator;
import com.engine.salary.util.db.MapperProxyFactory;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;
import weaver.hrm.User;
import java.util.*;
/**
* 薪资账套的个税申报表规则
*
Copyright: Copyright (c) 2023
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
public class SalarySobTaxRuleServiceImpl extends Service implements SalarySobTaxRuleService {
private SalarySobTaxRuleMapper salarySobTaxRuleMapper() {
return MapperProxyFactory.getProxy(SalarySobTaxRuleMapper.class);
}
private SalarySobService getSalarySobService(User user) {
return ServiceUtil.getService(SalarySobServiceImpl.class, user);
}
private TaxReportColumnService getTaxReportColumnService(User user) {
return ServiceUtil.getService(TaxReportColumnServiceImpl.class, user);
}
private SalaryItemService getSalaryItemService(User user) {
return ServiceUtil.getService(SalaryItemServiceImpl.class, user);
}
private SalarySobItemService getSalarySobItemService(User user) {
return ServiceUtil.getService(SalarySobItemServiceImpl.class, user);
}
private SalarySobBackItemService getSalarySobBackItemService(User user) {
return ServiceUtil.getService(SalarySobBackItemServiceImpl.class, user);
}
@Override
public List listBySalarySobId(Long salarySobId) {
if (salarySobId == null) {
return Collections.emptyList();
}
return salarySobTaxRuleMapper().listSome(SalarySobTaxRulePO.builder().salarySobId(salarySobId).build());
}
@Override
public List getSalarySobTaxRuleDTO(Long salarySobId) {
List resultList = Lists.newArrayList();
// 查询薪资账套
SalarySobPO salarySob = getSalarySobService(user).getById(salarySobId);
Integer incomeCategory = salarySob.getIncomeCategory();
List incomeCategoryIds = Collections.singletonList(incomeCategory);
// 薪资账套的薪资项目
List salarySobItems = getSalarySobItemService(user).listBySalarySobId(salarySobId);
// 薪资账套的回算薪资项目
List salarySobBackItems = getSalarySobBackItemService(user).listBySalarySobId(salarySobId);
// 薪资项目
Set salaryItemIds = Sets.newHashSet();
salaryItemIds.addAll(SalaryEntityUtil.properties(salarySobItems, SalarySobItemPO::getSalaryItemId));
salaryItemIds.addAll(SalaryEntityUtil.properties(salarySobBackItems, SalarySobBackItemPO::getSalaryItemId));
List salaryItems = getSalaryItemService(user).listByIds(salaryItemIds);
Map idKeySalaryItemMap = SalaryEntityUtil.convert2Map(salaryItems, SalaryItemPO::getId);
// 薪资账套中已经存在的对应关系
List salarySobTaxReportRules = listBySalarySobId(salarySobId);
Map salarySobTaxReportRuleMap = SalaryEntityUtil.convert2Map(salarySobTaxReportRules,
e -> e.getIncomeCategory() + "-" + e.getTaxIndex(), SalarySobTaxRulePO::getSalaryItemId);
for (Integer incomeCategoryId : incomeCategoryIds) {
IncomeCategoryEnum incomeCategoryEnum = IncomeCategoryEnum.parseByValue(incomeCategoryId);
List taxRules = new ArrayList<>();
if (incomeCategoryEnum == IncomeCategoryEnum.WAGES_AND_SALARIES || incomeCategoryEnum == IncomeCategoryEnum.NON_RESIDENT_INCOME_WAGES_AND_SALARIES) {
//只有正常工资薪金,才显示算税明细规则
taxRules = incomeCategoryEnum.getReportType().getTaxRules();
} else {
taxRules.add(SalarySobTaxRuleDTO.TaxRuleDTO.builder().name("税率").taxIndex("sl").build());
taxRules.add(SalarySobTaxRuleDTO.TaxRuleDTO.builder().name("速算扣除数").taxIndex("sskcs").build());
taxRules.add(SalarySobTaxRuleDTO.TaxRuleDTO.builder().name("本月(次)应补税额").taxIndex("ybtse").build());
}
for (SalarySobTaxRuleDTO.TaxRuleDTO taxRule : taxRules) {
Long salaryItemId = salarySobTaxReportRuleMap.get(incomeCategoryId + "-" + taxRule.getTaxIndex());
SalaryItemPO salaryItem = idKeySalaryItemMap.get(salaryItemId);
if (Objects.nonNull(salaryItem)) {
taxRule.setSalaryItemId(salaryItem.getId());
taxRule.setSalaryItemName(salaryItem.getName());
}
}
SalarySobTaxRuleDTO salarySobTaxReportRuleDTO = new SalarySobTaxRuleDTO();
salarySobTaxReportRuleDTO.setIncomeCategoryId(incomeCategoryId.toString());
salarySobTaxReportRuleDTO.setIncomeCategoryName(SalaryI18nUtil.getI18nLabel(incomeCategoryEnum.getLabelId(), incomeCategoryEnum.getDefaultLabel()));
salarySobTaxReportRuleDTO.setTaxRules(taxRules);
resultList.add(salarySobTaxReportRuleDTO);
}
return resultList;
}
@Override
public void saveByParam(SalarySobTaxRuleSaveParam saveParam) {
// 校验参数是否合法
// 构建po
Date now = new Date();
List salarySobTaxReportRules = Lists.newArrayList();
for (SalarySobTaxRuleSaveParam.TaxReportRuleIncomeCategoryParam incomeCategoryParam : saveParam.getIncomeCategoryParams()) {
for (SalarySobTaxRuleSaveParam.TaxRuleParam taxReportRuleParam : incomeCategoryParam.getTaxRuleParams()) {
SalarySobTaxRulePO salarySobTaxRule = new SalarySobTaxRulePO();
salarySobTaxRule.setId(IdGenerator.generate());
salarySobTaxRule.setSalarySobId(saveParam.getSalarySobId());
salarySobTaxRule.setIncomeCategory(incomeCategoryParam.getIncomeCategory());
salarySobTaxRule.setTaxIndex(taxReportRuleParam.getTaxIndex());
salarySobTaxRule.setSalaryItemId(taxReportRuleParam.getSalaryItemId());
salarySobTaxRule.setTenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY);
salarySobTaxRule.setCreator((long) user.getUID());
salarySobTaxRule.setDeleteType(DeleteTypeEnum.NOT_DELETED.getValue());
salarySobTaxRule.setCreateTime(now);
salarySobTaxRule.setUpdateTime(now);
salarySobTaxReportRules.add(salarySobTaxRule);
}
}
deleteBySalarySobIds(Collections.singleton(saveParam.getSalarySobId()));
if (CollectionUtils.isNotEmpty(salarySobTaxReportRules)) {
salarySobTaxReportRules.forEach(salarySobTaxRuleMapper()::insertIgnoreNull);
}
}
@Override
public void deleteBySalarySobIds(Collection salarySobIds) {
if (CollectionUtils.isEmpty(salarySobIds)) {
return;
}
salarySobTaxRuleMapper().deleteBySalarySobIds(salarySobIds);
}
@Override
public void saveBatch(List salarySobTaxReportRules) {
if (CollectionUtils.isEmpty(salarySobTaxReportRules)) {
return;
}
// salarySobTaxReportRules.forEach(salarySobTaxReportRuleMapper()::insertIgnoreNull);
}
}