package com.engine.salary.service.impl;
import com.alibaba.fastjson.JSON;
import com.engine.salary.constant.SzyhApiConstant;
import com.engine.salary.entity.datacollection.bo.DataCollectionBO;
import com.engine.salary.entity.taxagent.po.TaxAgentTaxReturnPO;
import com.engine.salary.entity.taxagent.response.SzyhResponseHead;
import com.engine.salary.entity.taxdeclaration.po.TaxDeclarationApiConfigPO;
import com.engine.salary.entity.taxpayment.dto.TaxWithheldVoucherResultDTO;
import com.engine.salary.entity.taxpayment.param.TaxPaymentQueryParam;
import com.engine.salary.entity.taxpayment.response.BaseResponse;
import com.engine.salary.entity.taxpayment.response.WithheldVoucherResponse;
import com.engine.salary.enums.salarysob.DeclareReportTypeEnum;
import com.engine.salary.enums.taxdeclaration.TaxPaymentServiceTypeEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
/**
* 完税证明
*
Copyright: Copyright (c) 2023
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
@Slf4j
public class TaxPaymentWithheldVoucherServiceImpl extends AbstractTaxPaymentService {
@Override
public Integer getServiceType() {
return TaxPaymentServiceTypeEnum.WITHHELD_VOUCHER.getValue();
}
protected T getRequestIdResponse(TaxAgentTaxReturnPO returnPO, String taxAgentName, TaxPaymentQueryParam param, TaxDeclarationApiConfigPO apiConfig, Class clazz) {
String url = apiConfig.getHost() + SzyhApiConstant.GET_WITHHELD_VOUCHER;
Map requestParam = DataCollectionBO.getApiBaseQueryParams(returnPO, taxAgentName, SalaryDateUtil.getFormatYYYYMM(param.getTaxYearMonth()));
return postRequest(apiConfig, url, requestParam, clazz);
}
@Override
public TaxWithheldVoucherResultDTO getWithheldVoucher(TaxPaymentQueryParam param) {
TempWrapper tempWrapper = checkBeforeGetRequestIdResponse(param);
WithheldVoucherResponse queryResponse = getRequestIdResponse(tempWrapper.getTaxReturnPO(), tempWrapper.getTaxAgentPO().getName(),
param, tempWrapper.getApiConfigPO(), WithheldVoucherResponse.class);
// 校验请求结果
String responseCode = Optional.ofNullable(queryResponse).map(WithheldVoucherResponse::getHead).map(SzyhResponseHead::getCode).orElse(null);
WithheldVoucherResponse.Feedback feedback = Optional.ofNullable(queryResponse).map(WithheldVoucherResponse::getBody).map(body -> {
if (DeclareReportTypeEnum.COMPREHENSIVE_INCOME.getValue().equals(param.getReportType())) {
return body.getZhsd();
} else if (DeclareReportTypeEnum.CLASSIFIED_INCOME.getValue().equals(param.getReportType())) {
return body.getFlsd();
} else if (DeclareReportTypeEnum.NONRESIDENT_INCOME.getValue().equals(param.getReportType())) {
return body.getFjmsd();
} else {
return body.getXsgsd();
}
}).orElse(null);
if (!SzyhApiConstant.SUCCESS_CODE.equals(responseCode) || Objects.isNull(feedback)) {
log.info("getWithheldVoucher code error:{}", JSON.toJSONString(queryResponse));
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(95828, "税局接口异常,请稍后重试"));
}
Integer status = SalaryEntityUtil.getIntValue(feedback.getJkzt(), -1);
if (status.equals(1)) {
log.info("getWithheldVoucher status error:{}", JSON.toJSONString(queryResponse));
throw new SalaryRunTimeException(feedback.getJksbyy());
}
List vouchers = new ArrayList<>();
for (int i = 1; i <= feedback.getWszmlb().size(); i++) {
vouchers.add(new TaxWithheldVoucherResultDTO.Voucher(SalaryI18nUtil.getI18nLabel(184013, "完税证明") + i, feedback.getWszmlb().get(i - 1)));
}
return new TaxWithheldVoucherResultDTO().setVouchers(vouchers);
}
}