weaver-hrm-salary/src/com/engine/salary/service/impl/TaxDeclarationApiFlowStatis...

231 lines
13 KiB
Java

package com.engine.salary.service.impl;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.salary.common.BaseQueryParam;
import com.engine.salary.constant.SzyhApiConstant;
import com.engine.salary.entity.taxagent.po.TaxAgentPO;
import com.engine.salary.entity.taxagent.po.TaxAgentTaxReturnPO;
import com.engine.salary.entity.taxagent.response.SzyhResponseHead;
import com.engine.salary.entity.taxapiflow.dto.TaxDeclarationApiFlowStatisticDetailListDTO;
import com.engine.salary.entity.taxapiflow.dto.TaxDeclarationApiFlowStatisticListDTO;
import com.engine.salary.entity.taxapiflow.dto.TaxDeclarationApiFlowTotalDTO;
import com.engine.salary.entity.taxapiflow.param.TaxDeclarationApiFlowMonthQueryParam;
import com.engine.salary.entity.taxapiflow.response.QueryAccountBalanceResponse;
import com.engine.salary.entity.taxapiflow.response.QueryDetailsByTaxNumberResponse;
import com.engine.salary.entity.taxdeclaration.po.TaxDeclarationApiConfigPO;
import com.engine.salary.service.TaxAgentService;
import com.engine.salary.service.TaxAgentTaxReturnService;
import com.engine.salary.service.TaxDeclarationApiConfigService;
import com.engine.salary.service.TaxDeclarationApiFlowStatisticService;
import com.engine.salary.util.*;
import com.engine.salary.util.excel.ExcelUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import dm.jdbc.util.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.hrm.User;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author chengliming
* @date 2022-11-11 2:57 PM
**/
@Slf4j
public class TaxDeclarationApiFlowStatisticServiceImpl extends Service implements TaxDeclarationApiFlowStatisticService {
private TaxDeclarationApiConfigService getTaxDeclarationApiConfigService(User user) {
return ServiceUtil.getService(TaxDeclarationApiConfigServiceImpl.class, user);
}
private TaxAgentTaxReturnService getTaxAgentTaxReturnService(User user) {
return ServiceUtil.getService(TaxAgentTaxReturnServiceImpl.class, user);
}
private TaxAgentService getTaxAgentService(User user) {
return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
}
// private SalaryBatchService salaryBatchService;
@Override
public TaxDeclarationApiFlowTotalDTO getFlowStatistics() {
TaxDeclarationApiConfigPO apiConfigPO = getTaxDeclarationApiConfigService(user).getConfig(true);
QueryAccountBalanceResponse response = getQueryAccountBalanceResponse(apiConfigPO);
apiConfigPO.setTotality(Long.parseLong(response.getBody().getTotal()));
apiConfigPO.setRemain(Long.parseLong(response.getBody().getSurplus()));
apiConfigPO.setLastUpdateTime(new Date());
getTaxDeclarationApiConfigService(user).update(apiConfigPO);
return TaxDeclarationApiFlowTotalDTO.builder()
.total(apiConfigPO.getTotality())
.remain(apiConfigPO.getRemain())
.used(apiConfigPO.getTotality() - apiConfigPO.getRemain())
.lastUpdateTime(SalaryDateUtil.getFormatLocalDateTime(apiConfigPO.getLastUpdateTime()))
.build();
}
@Override
public PageInfo<TaxDeclarationApiFlowStatisticListDTO> pageFlowStatistics(BaseQueryParam queryParam) {
return SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), listFlowStatistic(),TaxDeclarationApiFlowStatisticListDTO.class);
}
@Override
public List<TaxDeclarationApiFlowStatisticListDTO> listFlowStatistic() {
TaxDeclarationApiConfigPO config = getTaxDeclarationApiConfigService(user).getConfig(true);
QueryAccountBalanceResponse response = getQueryAccountBalanceResponse(config);
List<QueryAccountBalanceResponse.Detail> details = Optional.of(response).map(e -> e.getBody().getTaxList()).orElse(new ArrayList<>());
if (details.isEmpty()) {
log.info("details is empty");
return new ArrayList<>();
}
Set<String> taxCodes = details.stream().map(QueryAccountBalanceResponse.Detail::getTaxNumber).collect(Collectors.toSet());
List<TaxAgentTaxReturnPO> returnPOList = getTaxAgentTaxReturnService(user).getByTaxCodes(taxCodes);
if (returnPOList.isEmpty()) {
log.info("TaxAgentTaxReturnPO is empty");
return new ArrayList<>();
}
Set<Long> taxAgentIds = returnPOList.stream().map(TaxAgentTaxReturnPO::getTaxAgentId).collect(Collectors.toSet());
List<TaxAgentPO> taxAgentPOList = getTaxAgentService(user).listByIdsIncludeDel(taxAgentIds);
if (taxAgentPOList.isEmpty()) {
log.info("TaxAgentPO is empty");
return new ArrayList<>();
}
Map<Long, String> taxAgentMap = SalaryEntityUtil.convert2Map(taxAgentPOList, TaxAgentPO::getId, TaxAgentPO::getName);
Map<String, List<TaxAgentTaxReturnPO>> taxCodeMap = returnPOList.stream().collect(Collectors.groupingBy(TaxAgentTaxReturnPO::getTaxCode));
List<TaxDeclarationApiFlowStatisticListDTO> flowStatisticDTOS = new ArrayList<>();
for (QueryAccountBalanceResponse.Detail detail : details) {
List<TaxAgentTaxReturnPO> list = taxCodeMap.getOrDefault(detail.getTaxNumber(), new ArrayList<>());
for (TaxAgentTaxReturnPO returnPO : list) {
TaxDeclarationApiFlowStatisticListDTO statisticDTO = new TaxDeclarationApiFlowStatisticListDTO();
statisticDTO.setId(IdGenerator.generate());
statisticDTO.setUsed(Integer.parseInt(detail.getUsed()));
statisticDTO.setTaxAgentId(returnPO.getTaxAgentId());
statisticDTO.setTaxAgentName(taxAgentMap.get(returnPO.getTaxAgentId()));
flowStatisticDTOS.add(statisticDTO);
}
}
flowStatisticDTOS.sort((o1, o2) -> o2.getTaxAgentId().compareTo(o1.getTaxAgentId()));
for (int i = 0; i < flowStatisticDTOS.size(); i++) {
flowStatisticDTOS.get(i).setIndexNum(i + 1);
}
return flowStatisticDTOS;
}
@Override
public PageInfo<TaxDeclarationApiFlowStatisticDetailListDTO> pageFlowStatisticsDetail(TaxDeclarationApiFlowMonthQueryParam queryParam) {
List<TaxDeclarationApiFlowStatisticDetailListDTO> dtoList = listFlowStatisticDetail(queryParam);
return SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), dtoList, TaxDeclarationApiFlowStatisticDetailListDTO.class);
}
@Override
public List<TaxDeclarationApiFlowStatisticDetailListDTO> listFlowStatisticDetail(TaxDeclarationApiFlowMonthQueryParam queryParam) {
TaxDeclarationApiConfigPO apiConfigPO = getTaxDeclarationApiConfigService(user).getConfig(true);
TaxAgentTaxReturnPO taxReturnPO = getTaxAgentTaxReturnService(user).getByTaxAgentId(queryParam.getTaxAgentId());
SalaryAssert.notNull(taxReturnPO, SalaryI18nUtil.getI18nLabel(184065, "个税扣缴义务人暂无报税信息"));
List<TaxAgentPO> taxAgentPOS = getTaxAgentService(user).listByIdsIncludeDel(Collections.singleton(taxReturnPO.getTaxAgentId()));
SalaryAssert.notEmpty(taxAgentPOS, SalaryI18nUtil.getI18nLabel(100545, "个税扣缴义务人不存在"));
QueryDetailsByTaxNumberResponse response = getQueryDetailsByTaxNumberResponse(apiConfigPO, taxReturnPO.getTaxCode());
List<QueryDetailsByTaxNumberResponse.Detail> details = Optional.of(response)
.map(QueryDetailsByTaxNumberResponse::getBody)
.map(QueryDetailsByTaxNumberResponse.Body::getDetail)
.orElse(new ArrayList<>());
if (details.isEmpty()) {
return new ArrayList<>();
}
details.sort((o1, o2) -> Integer.valueOf(o2.getMonth()).compareTo(Integer.valueOf(o1.getMonth())));
details = details.stream().filter(detail -> detail.getMonthValue() >= queryParam.getStartTaxMonth()
&& detail.getMonthValue() <= queryParam.getEndTaxMonth()).collect(Collectors.toList());
return details.stream().map(e -> TaxDeclarationApiFlowStatisticDetailListDTO.builder()
.id(IdGenerator.generate())
.taxAgentId(taxAgentPOS.get(0).getId())
.taxAgentName(taxAgentPOS.get(0).getName())
.taxMonth(LocalDate.parse(e.getMonth() + "01", DateTimeFormatter.ofPattern("yyyyMMdd")).format(SalaryDateUtil.MONTH_FORMATTER))
.used(Integer.valueOf(e.getUsed()))
.build()).collect(Collectors.toList());
}
@Override
public XSSFWorkbook exportFlowStatistics(List<TaxDeclarationApiFlowStatisticListDTO> dtoList) {
List<List<Object>> rows = new ArrayList<>();
// 表头
List<Object> headers = new ArrayList<>();
headers.add(SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人"));
headers.add(SalaryI18nUtil.getI18nLabel(159084, "已使用流量"));
rows.add(headers);
// 组装数据
for (TaxDeclarationApiFlowStatisticListDTO dto : dtoList) {
List<Object> row = new ArrayList<>();
row.add(dto.getTaxAgentName());
row.add(dto.getUsed());
rows.add(row);
}
// 生成表格
return ExcelUtil.genWorkbookV2(rows, "接口流量统计数据");
}
@Override
public XSSFWorkbook exportFlowStatisticsDetail(List<TaxDeclarationApiFlowStatisticDetailListDTO> dtoList) {
List<List<Object>> rows = new ArrayList<>();
// 表头
List<Object> headers = new ArrayList<>();
headers.add(SalaryI18nUtil.getI18nLabel(86184, "个税扣缴义务人"));
headers.add(SalaryI18nUtil.getI18nLabel(86176, "税款所属期"));
headers.add(SalaryI18nUtil.getI18nLabel(159087, "月使用流量数"));
// 组装数据
for (TaxDeclarationApiFlowStatisticDetailListDTO dto : dtoList) {
List<Object> row = new ArrayList<>();
row.add(dto.getTaxAgentName());
row.add(dto.getTaxMonth());
row.add(dto.getUsed());
rows.add(row);
}
// 生成表格
return ExcelUtil.genWorkbookV2(rows, "接口流量月统计数据");
}
public QueryAccountBalanceResponse getQueryAccountBalanceResponse(TaxDeclarationApiConfigPO apiConfig) {
String url = apiConfig.getHost() + SzyhApiConstant.QUERY_ACCOUNT_BALANCE;
Map<String, String> params = new HashMap<>(1);
Map<String, String> header = SingnatureData.initHeader(params, apiConfig.getAppKey(), apiConfig.getAppSecret());
String res = HttpUtil.getRequest(url, header, params);
log.info("getQueryAccountBalanceResponse response : {}", res);
QueryAccountBalanceResponse response = JsonUtil.parseObject(res, QueryAccountBalanceResponse.class);
QueryAccountBalanceResponse.Body body = Optional.ofNullable(response).map(QueryAccountBalanceResponse::getBody).orElse(null);
String code = Optional.ofNullable(response)
.map(QueryAccountBalanceResponse::getHead)
.map(SzyhResponseHead::getCode).orElse("error");
String msg = Optional.ofNullable(response)
.map(QueryAccountBalanceResponse::getHead)
.map(SzyhResponseHead::getMsg).orElse(SalaryI18nUtil.getI18nLabel(184014, "税局接口异常,请稍后再试"));
SalaryAssert.isTrue(SzyhApiConstant.SUCCESS_CODE.equals(code), msg);
SalaryAssert.notNull(body, SalaryI18nUtil.getI18nLabel(184014, "税局接口异常,请稍后再试"));
return response;
}
public QueryDetailsByTaxNumberResponse getQueryDetailsByTaxNumberResponse(TaxDeclarationApiConfigPO apiConfig, String taxCode) {
String url = apiConfig.getHost() + SzyhApiConstant.QUERY_DETAILS_BY_TAX_NUMBER;
Map<String, String> params = new HashMap<>(1);
params.put("taxNumber", taxCode);
Map<String, String> header = SingnatureData.initHeader(Collections.emptyMap(), apiConfig.getAppKey(), apiConfig.getAppSecret());
String res = HttpUtil.getRequest(url, header, params);
log.info("getQueryDetailsByTaxNumberResponse response : {}", res);
QueryDetailsByTaxNumberResponse response = JsonUtil.parseObject(res, QueryDetailsByTaxNumberResponse.class);
QueryDetailsByTaxNumberResponse.Body body = Optional.ofNullable(response).map(QueryDetailsByTaxNumberResponse::getBody).orElse(null);
String code = Optional.ofNullable(response)
.map(QueryDetailsByTaxNumberResponse::getHead)
.map(SzyhResponseHead::getCode).orElse("error");
String msg = Optional.ofNullable(response)
.map(QueryDetailsByTaxNumberResponse::getHead)
.map(SzyhResponseHead::getMsg).orElse(SalaryI18nUtil.getI18nLabel(184014, "税局接口异常,请稍后再试"));
SalaryAssert.isTrue(SzyhApiConstant.SUCCESS_CODE.equals(code), msg);
SalaryAssert.notNull(body, SalaryI18nUtil.getI18nLabel(184014, "税局接口异常,请稍后再试"));
return response;
}
}