package com.engine.salary.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.component.PageInfo;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.deductionamount.dto.DeductionAmountDTO;
import com.engine.salary.entity.deductionamount.param.*;
import com.engine.salary.entity.deductionamount.po.DeductionAmountPO;
import com.engine.salary.entity.employeedeclare.po.EmployeeDeclarePO;
import com.engine.salary.enums.SalaryOnOffEnum;
import com.engine.salary.enums.employeedeclare.CardTypeEnum;
import com.engine.salary.mapper.deductionamount.DeductionAmountMapper;
import com.engine.salary.mapper.employeedeclare.EmployeeDeclareMapper;
import com.engine.salary.remote.tax.client.DeductionAmountClient;
import com.engine.salary.remote.tax.request.deductionAmount.ConfirmPreDeductRequest;
import com.engine.salary.service.*;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.SalaryPageUtil;
import com.engine.salary.util.valid.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import weaver.hrm.User;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* 年收入不足6万元的纳税人暂不预扣预缴税款扣除名单
*
Copyright: Copyright (c) 2023
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
@Slf4j
public class DeductionAmountServiceImpl extends Service implements DeductionAmountService {
private DeductionAmountMapper getDeductionAmountMapper() {
return MapperProxyFactory.getProxy(DeductionAmountMapper.class);
}
private EmployeeDeclareMapper getEmployeeDeclareMapper() {
return MapperProxyFactory.getProxy(EmployeeDeclareMapper.class);
}
private TaxAgentService getTaxAgentService(User user) {
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
}
private SalaryEmployeeService getSalaryEmployeeService(User user) {
return ServiceUtil.getService(SalaryEmployeeServiceImpl.class, user);
}
private SalaryArchiveService getSalaryArchiveService(User user) {
return ServiceUtil.getService(SalaryArchiveServiceImpl.class, user);
}
private TaxDeclarationApiConfigService getTaxDeclarationApiConfigService(User user) {
return ServiceUtil.getService(TaxDeclarationApiConfigServiceImpl.class, user);
}
private TaxAgentTaxReturnService getTaxAgentTaxReturnService(User user) {
return ServiceUtil.getService(TaxAgentTaxReturnServiceImpl.class, user);
}
private EmployeeDeclareRecordService getEmployeeDeclareRecordService(User user) {
return ServiceUtil.getService(EmployeeDeclareRecordServiceImpl.class, user);
}
private TaxDeclarationApiBillingService getTaxDeclarationApiBillingService(User user) {
return ServiceUtil.getService(TaxDeclarationApiBillingServiceImpl.class, user);
}
@Override
public PageInfo list(DeductionAmountListParam param) {
ValidUtil.doValidator(param);
List deductionAmountPOS = getDeductionAmountMapper().listSome(DeductionAmountPO.builder().year(param.getYear()).taxAgentId(param.getTaxAgentId()).build());
List list = deductionAmountPOS.stream().map(deductionAmountPO -> {
return DeductionAmountDTO.builder()
.id(deductionAmountPO.getId())
.taxAgentId(deductionAmountPO.getEmployeeId())
.year(deductionAmountPO.getYear())
.employeeId(deductionAmountPO.getEmployeeId())
.employeeName(deductionAmountPO.getEmployeeName())
.employeeType(deductionAmountPO.getEmployeeType())
.jobNum(deductionAmountPO.getJobNum())
.nationality(deductionAmountPO.getNationality())
.cardNum(deductionAmountPO.getCardNum())
.cardType(deductionAmountPO.getCardType())
.declareStatus(deductionAmountPO.getDeclareStatus())
.deductFlag(deductionAmountPO.getDeductFlag())
.successfullyDeclared(deductionAmountPO.getSuccessfullyDeclared())
.creator(deductionAmountPO.getCreator())
.createTime(deductionAmountPO.getCreateTime())
.updateTime(deductionAmountPO.getUpdateTime())
.build();
}).collect(Collectors.toList());
return SalaryPageUtil.buildPage(param.getCurrent(), param.getPageSize(), list, DeductionAmountDTO.class);
}
@Override
public void add(DeductionAmountAddParam param) {
ValidUtil.doValidator(param);
//已存在
List deductionAmountPOS = getDeductionAmountMapper().listSome(DeductionAmountPO.builder().taxAgentId(param.getTaxAgentId()).year(param.getYear()).build());
List oldIds = SalaryEntityUtil.properties(deductionAmountPOS, DeductionAmountPO::getEmployeeId, Collectors.toList());
//本次新增
List employeeDeclareIds = param.getEmployeeDeclareIds();
List employeeDeclarePOS = getEmployeeDeclareMapper().listSome(EmployeeDeclarePO.builder().ids(employeeDeclareIds).build());
Date now = new Date();
employeeDeclarePOS.stream()
.filter(employeeDeclarePO -> !oldIds.contains(employeeDeclarePO.getEmployeeId()))
.map(employeeDeclarePO -> DeductionAmountPO.builder()
.id(employeeDeclarePO.getEmployeeId())
.taxAgentId(employeeDeclarePO.getEmployeeId())
.year(param.getYear())
.employeeId(employeeDeclarePO.getEmployeeId())
.employeeName(employeeDeclarePO.getEmployeeName())
.employeeType(employeeDeclarePO.getEmployeeType())
.jobNum(employeeDeclarePO.getJobNum())
.cardNum(employeeDeclarePO.getCardNum())
.cardType(employeeDeclarePO.getCardType())
.nationality(employeeDeclarePO.getNationality())
.declareStatus(0)
.deductFlag(1)
.successfullyDeclared(0)
.creator((long) user.getUID())
.createTime(now)
.updateTime(now)
.tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
.deleteType(NumberUtils.INTEGER_ZERO)
.build())
.forEach(deductionAmountPO -> getDeductionAmountMapper().insertIgnoreNull(deductionAmountPO));
}
@Override
public void delete(DeductionAmountDeleteParam param) {
if (CollUtil.isEmpty(param.getIds())) {
return;
}
param.getIds().forEach(id -> {
getDeductionAmountMapper().delete(DeductionAmountPO.builder().id(id).build());
});
}
@Override
public Object query(DeductionAmountOnlineQueryParam param) {
Long taxAgentId = param.getTaxAgentId();
DeductionAmountClient deductionAmountClient = new DeductionAmountClient(taxAgentId);
return deductionAmountClient.query(param.getYear());
}
@Override
public Object confirm(DeductionAmountConfirmParam param) {
ValidUtil.doValidator(param);
Long taxAgentId = param.getTaxAgentId();
String year = param.getYear();
List deductionAmountPOS = getDeductionAmountMapper().listSome(DeductionAmountPO.builder().taxAgentId(taxAgentId).year(year).build());
List kczglb = deductionAmountPOS.stream()
.map(deductionAmountPO -> {
ConfirmPreDeductRequest.kczg kczg = new ConfirmPreDeductRequest.kczg();
kczg.setXm(deductionAmountPO.getEmployeeName());
kczg.setZzhm(deductionAmountPO.getCardNum());
kczg.setZzlx(CardTypeEnum.getByValue(deductionAmountPO.getCardType()).getDefaultLabel());
kczg.setGj(deductionAmountPO.getNationality());
kczg.setKcbs(SalaryOnOffEnum.parseByValue(deductionAmountPO.getDeductFlag()).getDefaultLabel());
return kczg;
}).collect(Collectors.toList());
DeductionAmountClient deductionAmountClient = new DeductionAmountClient(taxAgentId);
return deductionAmountClient.confirmPreDeduct(year, kczglb);
}
}