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.SalarySobTaxReportRuleDTO;
import com.engine.salary.entity.salarysob.param.SalarySobTaxReportRuleSaveParam;
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.SalarySobTaxReportRulePO;
import com.engine.salary.entity.taxdeclaration.po.TaxReportColumnPO;
import com.engine.salary.enums.salarysob.IncomeCategoryEnum;
import com.engine.salary.enums.sicategory.DeleteTypeEnum;
import com.engine.salary.mapper.salarysob.SalarySobTaxReportRuleMapper;
import com.engine.salary.service.*;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.engine.salary.util.db.IdGenerator;
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 SalarySobTaxReportRuleServiceImpl extends Service implements SalarySobTaxReportRuleService {
private SalarySobTaxReportRuleMapper salarySobTaxReportRuleMapper() {
return MapperProxyFactory.getProxy(SalarySobTaxReportRuleMapper.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 listBySalarySobIds(Collection salarySobIds) {
if (CollectionUtils.isEmpty(salarySobIds)) {
return Collections.emptyList();
}
return salarySobTaxReportRuleMapper().listSome(SalarySobTaxReportRulePO.builder().salarySobIds(salarySobIds).build());
}
@Override
public List getSalarySobTaxReportRuleDTO(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 = listBySalarySobIds(Collections.singleton(salarySobId));
Map salarySobTaxReportRuleMap = SalaryEntityUtil.convert2Map(salarySobTaxReportRules,
e -> e.getIncomeCategory() + "-" + e.getReportColumnDataIndex(), SalarySobTaxReportRulePO::getSalaryItemId);
for (Integer incomeCategoryId : incomeCategoryIds) {
IncomeCategoryEnum incomeCategoryEnum = IncomeCategoryEnum.parseByValue(incomeCategoryId);
List taxReportColumns = getTaxReportColumnService(user).listByIncomeCategory(incomeCategoryEnum);
List taxReportRules = Lists.newArrayList();
for (TaxReportColumnPO taxReportColumn : taxReportColumns) {
SalarySobTaxReportRuleDTO.TaxReportRuleDTO taxReportRuleDTO = new SalarySobTaxReportRuleDTO.TaxReportRuleDTO();
taxReportRuleDTO.setId(taxReportColumn.getId());
taxReportRuleDTO.setReportColumnName(taxReportColumn.getReportColumnName());
taxReportRuleDTO.setReportColumnDataIndex(taxReportColumn.getReportColumnDataIndex());
taxReportRuleDTO.setSalaryItem(Collections.emptyList());
taxReportRuleDTO.setCanEdit(true);
Long salaryItemId = salarySobTaxReportRuleMap.get(incomeCategoryId + "-" + taxReportColumn.getReportColumnDataIndex());
SalaryItemPO salaryItem = idKeySalaryItemMap.get(salaryItemId);
if (Objects.nonNull(salaryItem)) {
Map dataMap = Maps.newHashMap();
dataMap.put("id", salaryItem.getId().toString());
dataMap.put("name", salaryItem.getName());
dataMap.put("icon", "Icon-N-Salary-item-management");
taxReportRuleDTO.setSalaryItem(Collections.singletonList(dataMap));
}
taxReportRules.add(taxReportRuleDTO);
}
SalarySobTaxReportRuleDTO salarySobTaxReportRuleDTO = new SalarySobTaxReportRuleDTO();
salarySobTaxReportRuleDTO.setIncomeCategoryId(incomeCategoryId.toString());
salarySobTaxReportRuleDTO.setIncomeCategoryName(incomeCategoryEnum == null ? "" : SalaryI18nUtil.getI18nLabel(incomeCategoryEnum.getLabelId(), incomeCategoryEnum.getDefaultLabel()));
salarySobTaxReportRuleDTO.setTaxReportRules(taxReportRules);
resultList.add(salarySobTaxReportRuleDTO);
}
return resultList;
}
@Override
public void saveByParam(SalarySobTaxReportRuleSaveParam saveParam) {
// 校验参数是否合法
// 构建po
Date now = new Date();
List salarySobTaxReportRules = Lists.newArrayList();
for (SalarySobTaxReportRuleSaveParam.TaxReportRuleIncomeCategoryParam incomeCategoryParam : saveParam.getIncomeCategoryParams()) {
for (SalarySobTaxReportRuleSaveParam.TaxReportRuleParam taxReportRuleParam : incomeCategoryParam.getTaxReportRuleParams()) {
SalarySobTaxReportRulePO salarySobTaxReportRule = new SalarySobTaxReportRulePO();
salarySobTaxReportRule.setId(IdGenerator.generate());
salarySobTaxReportRule.setSalarySobId(saveParam.getSalarySobId());
salarySobTaxReportRule.setIncomeCategory(incomeCategoryParam.getIncomeCategory());
salarySobTaxReportRule.setReportColumnDataIndex(taxReportRuleParam.getReportColumnDataIndex());
salarySobTaxReportRule.setSalaryItemId(taxReportRuleParam.getSalaryItemId());
salarySobTaxReportRule.setTenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY);
salarySobTaxReportRule.setCreator((long) user.getUID());
salarySobTaxReportRule.setDeleteType(DeleteTypeEnum.NOT_DELETED.getValue());
salarySobTaxReportRule.setCreateTime(now);
salarySobTaxReportRule.setUpdateTime(now);
salarySobTaxReportRules.add(salarySobTaxReportRule);
}
}
deleteBySalarySobIds(Collections.singleton(saveParam.getSalarySobId()));
if (CollectionUtils.isNotEmpty(salarySobTaxReportRules)) {
salarySobTaxReportRules.forEach(salarySobTaxReportRuleMapper()::insertIgnoreNull);
// salarySobTaxReportRuleMapper().batchInsert(salarySobTaxReportRules);
}
}
@Override
public void deleteBySalarySobIds(Collection salarySobIds) {
if (CollectionUtils.isEmpty(salarySobIds)) {
return;
}
salarySobTaxReportRuleMapper().deleteBySalarySobIds(salarySobIds);
}
@Override
public void saveBatch(List salarySobTaxReportRules) {
if (CollectionUtils.isEmpty(salarySobTaxReportRules)) {
return;
}
salarySobTaxReportRules.forEach(salarySobTaxReportRuleMapper()::insertIgnoreNull);
}
}