package com.qtzl.alterSales.manager.service; import cn.com.taiji.common.manager.net.http.ServiceHandleException; import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSON; import com.google.common.collect.Lists; import com.qtzl.alterSales.dao.entity.primary.AflApPayOrder; import com.qtzl.alterSales.dao.entity.primary.FssPaccountPay; import com.qtzl.alterSales.dao.entity.primary.WechatBillPayApply; import com.qtzl.alterSales.dao.entity.primary.WechatBillPaySuccess; import com.qtzl.alterSales.dao.entity.second.AflPaccountRefundCount; import com.qtzl.alterSales.dao.repo.jpa.primary.AflApPayOrderRepo; import com.qtzl.alterSales.dao.repo.jpa.primary.AflSupplementaryPayOrderRepo; import com.qtzl.alterSales.dao.repo.jpa.primary.FssPaccountPayRepo; import com.qtzl.alterSales.dao.repo.jpa.primary.FssPaccountRefundRepo; import com.qtzl.alterSales.dao.repo.jpa.primary.WechatBillPayApplyRepo; import com.qtzl.alterSales.dao.repo.jpa.primary.WechatBillPaySuccessRepo; import com.qtzl.alterSales.dao.repo.jpa.second.AflPaccountRefundCountRepo; import com.qtzl.alterSales.manager.enums.VehiclePlateColorEnum; import com.qtzl.alterSales.manager.model.protocol.UcServiceError; import com.qtzl.alterSales.manager.model.protocol.sales.FssPaccountPayResultFindByPayIdResponse; import com.qtzl.alterSales.manager.vo.AflPaccountRefundCountFindVo; import com.qtzl.alterSales.manager.vo.VehicleAgreementNumBillsVo; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; /*** *

* 选装-原始流水 service *

* @author hou yi * {@code @date} 2023/10/12 11:43 **/ @Service public class FssPaccountPayServiceImpl implements FssPaccountPayService { private static final Logger log = LoggerFactory.getLogger(FssPaccountPayServiceImpl.class); @Resource private FssPaccountPayRepo paccountPayRepo; @Resource private AflPaccountRefundCountRepo aflPaccountRefundCountRepo; @Resource private FssPaccountRefundRepo fssPaccountRefundRepo; @Resource private AflSupplementaryPayOrderRepo supplementaryPayOrderRepo; @Resource private WechatBillPayApplyRepo wechatBillPayApplyRepo; @Resource private WechatBillPaySuccessRepo successRepo; @Resource private AflApPayOrderRepo apPayOrderRepo; @Override public List getAgreementBills(String plateNumber, Integer plateColor) throws ServiceHandleException { if (StringUtils.isEmpty(plateNumber) || null == plateColor) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("请指定车牌号、车牌颜色"); } List agreementBills = paccountPayRepo.findByVehicleId(plateNumber + "_" + plateColor); if (CollectionUtils.isEmpty(agreementBills)) { return Lists.newArrayList(); } /* // 过滤掉已生成补缴单的流水 final List payIds = agreementBills.stream().map(FssPaccountPay::getPayId).toList(); final List orders = supplementaryPayOrderRepo.findAll((root, query, cb) -> query .where( root.get("payId").in(payIds) ).getRestriction()); if (!CollectionUtils.isEmpty(orders)) { final List orderPayIds = orders.stream().map(AflSupplementaryPayOrder::getPayId).toList(); agreementBills = agreementBills.stream().filter(vo -> !orderPayIds.contains(vo.getPayId())).toList(); }*/ return toVo(agreementBills); } @Override public List findByPayId(List payIds) { if (CollectionUtils.isEmpty(payIds)) { return Lists.newArrayList(); } return toVo(paccountPayRepo.findByPayIdIn(payIds)); } @Override public void count(int day) throws ServiceHandleException { // 获取当前日期前一天的日期 LocalDate localDate = LocalDate.now().minusDays(day);//1代表提前多少天 String yesterday = localDate.toString(); System.out.println("当前导入记录日期=" + yesterday); Integer i = aflPaccountRefundCountRepo.findByToDay(yesterday.replaceAll("-", "")); if (i != null && i > 0) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("当前n+" + day + "天记录已存在"); } List> countTotal = paccountPayRepo.findCountTotal(yesterday + " 00:00:00", yesterday + " " + "23:59:59"); List list = new ArrayList(); for (Map stringObjectMap : countTotal) { AflPaccountRefundCountFindVo aflPaccountRefundCountVo = JSON.parseObject(JSON.toJSONString(stringObjectMap), AflPaccountRefundCountFindVo.class); list.add(aflPaccountRefundCountVo); } if (list == null || list.size() < 1) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("当前n+" + day + "天记录无数据"); } log.info("本次处理数据:{}", JSON.toJSON(list)); AflPaccountRefundCount bydAflPaccountRefundCount = null; for (AflPaccountRefundCountFindVo aflPaccountRefundCountVo : list) { AflPaccountRefundCount aflPaccountRefundCount = new AflPaccountRefundCount(); Long refundMoney = fssPaccountRefundRepo.findByCountRefundMoney(aflPaccountRefundCountVo.getAgentId(), aflPaccountRefundCountVo.getAccountDate()); BeanUtils.copyProperties(aflPaccountRefundCountVo, aflPaccountRefundCount); aflPaccountRefundCount.setRefundMoney(refundMoney); if (!StringUtils.isEmpty(aflPaccountRefundCountVo.getAccountDate())) { String accountDate = aflPaccountRefundCountVo.getAccountDate().replaceAll("-", ""); Integer newAccountDate = Integer.parseInt(accountDate); aflPaccountRefundCount.setAccountDate(newAccountDate); } if ("52010188999".equals(aflPaccountRefundCount.getAgentId()) && aflPaccountRefundCount.getAccountDate() != null) { bydAflPaccountRefundCount = aflPaccountRefundCount; AflPaccountRefundCount bydNewAflPaccountRefundCount = aflPaccountRefundCountRepo.finByDateAndAgentId( "52010188930", aflPaccountRefundCount.getAccountDate()); if (bydNewAflPaccountRefundCount != null) { if (bydAflPaccountRefundCount.getMoneyTotal() != null) { bydNewAflPaccountRefundCount.setMoneyTotal(bydNewAflPaccountRefundCount.getMoneyTotal() + bydAflPaccountRefundCount.getMoneyTotal()); } if (bydAflPaccountRefundCount.getRefundMoney() != null) { bydNewAflPaccountRefundCount.setRefundMoney(bydNewAflPaccountRefundCount.getRefundMoney() + bydAflPaccountRefundCount.getRefundMoney()); } if (bydAflPaccountRefundCount.getCollectMoney() != null) { bydNewAflPaccountRefundCount.setCollectMoney(bydNewAflPaccountRefundCount.getCollectMoney() + bydAflPaccountRefundCount.getCollectMoney()); } if (bydAflPaccountRefundCount.getNumberTotal() != null) { bydNewAflPaccountRefundCount.setNumberTotal(bydNewAflPaccountRefundCount.getNumberTotal() + bydAflPaccountRefundCount.getNumberTotal()); } aflPaccountRefundCountRepo.save(bydNewAflPaccountRefundCount); } continue; } if ("52010188930".equals(aflPaccountRefundCount.getAgentId()) && bydAflPaccountRefundCount != null) { if (bydAflPaccountRefundCount.getMoneyTotal() != null) { aflPaccountRefundCount.setMoneyTotal(aflPaccountRefundCount.getMoneyTotal() + bydAflPaccountRefundCount.getMoneyTotal()); } if (bydAflPaccountRefundCount.getRefundMoney() != null) { aflPaccountRefundCount.setRefundMoney(aflPaccountRefundCount.getRefundMoney() + bydAflPaccountRefundCount.getRefundMoney()); } if (bydAflPaccountRefundCount.getCollectMoney() != null) { aflPaccountRefundCount.setCollectMoney(aflPaccountRefundCount.getCollectMoney() + bydAflPaccountRefundCount.getCollectMoney()); } if (bydAflPaccountRefundCount.getNumberTotal() != null) { aflPaccountRefundCount.setCollectMoney(aflPaccountRefundCount.getCollectMoney() + bydAflPaccountRefundCount.getCollectMoney()); } } aflPaccountRefundCount.setId(UUID.randomUUID().toString().replace("-", "")); aflPaccountRefundCount.setUpdateTime(LocalDateTime.now()); aflPaccountRefundCount.setInsertTime(LocalDateTime.now()); aflPaccountRefundCountRepo.save(aflPaccountRefundCount); } } /** * 更新统计 * @param day * @throws ServiceHandleException */ @Override public void newCount(int day) throws ServiceHandleException { // 获取当前日期前一天的日期 //1代表提前多少天 LocalDate localDate = LocalDate.now().minusDays(day); String yesterday = localDate.toString(); log.info("当前导入记录日期="+yesterday); List> countTotal = paccountPayRepo.findCountTotal(yesterday+" 00:00:00",yesterday+" 23:59:59"); List list = new ArrayList(); for (Map stringObjectMap : countTotal) { AflPaccountRefundCountFindVo aflPaccountRefundCountVo = JSON.parseObject(JSON.toJSONString(stringObjectMap), AflPaccountRefundCountFindVo.class); list.add(aflPaccountRefundCountVo); } if (list==null||list.size()<1){ log.error("当前n+"+day+"天记录无数据"); return; // throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("当前n+"+day+"天记录无数据"); } log.info("本次处理数据:{}", JSON.toJSON(list)); AflPaccountRefundCount bydAflPaccountRefundCount = null; for (AflPaccountRefundCountFindVo aflPaccountRefundCountVo : list) { Integer newAccountDate1 =null; if (!StringUtils.isEmpty(aflPaccountRefundCountVo.getAccountDate())){ String accountDate=aflPaccountRefundCountVo.getAccountDate().replaceAll("-",""); newAccountDate1 = Integer.parseInt(accountDate); } AflPaccountRefundCount aflPaccountRefundCount= null; if("52010188999".equals(aflPaccountRefundCountVo.getAgentId())&&newAccountDate1!=null){ aflPaccountRefundCount= aflPaccountRefundCountRepo.finByDateAndAgentId("52010188930",newAccountDate1); }else { aflPaccountRefundCount= aflPaccountRefundCountRepo.finByDateAndAgentId(aflPaccountRefundCountVo.getAgentId(),newAccountDate1); } if (aflPaccountRefundCount==null){ aflPaccountRefundCount = new AflPaccountRefundCount(); } Long refundMoney = fssPaccountRefundRepo.findByCountRefundMoney(aflPaccountRefundCountVo.getAgentId(),aflPaccountRefundCountVo.getAccountDate()); aflPaccountRefundCountVo.setId(aflPaccountRefundCount.getId()); BeanUtils.copyProperties(aflPaccountRefundCountVo,aflPaccountRefundCount); aflPaccountRefundCount.setRefundMoney(refundMoney); if (!StringUtils.isEmpty(aflPaccountRefundCountVo.getAccountDate())){ String accountDate=aflPaccountRefundCountVo.getAccountDate().replaceAll("-",""); Integer newAccountDate = Integer.parseInt(accountDate); aflPaccountRefundCount.setAccountDate(newAccountDate); } if ("52010188999".equals(aflPaccountRefundCount.getAgentId())&&aflPaccountRefundCount.getAccountDate()!=null){ bydAflPaccountRefundCount=aflPaccountRefundCount; AflPaccountRefundCount bydNewAflPaccountRefundCount= aflPaccountRefundCountRepo.finByDateAndAgentId("52010188930",aflPaccountRefundCount.getAccountDate()); if (bydNewAflPaccountRefundCount!=null){ if (bydAflPaccountRefundCount.getMoneyTotal()!=null){ bydNewAflPaccountRefundCount.setMoneyTotal(bydNewAflPaccountRefundCount.getMoneyTotal()+bydAflPaccountRefundCount.getMoneyTotal()); } if (bydAflPaccountRefundCount.getRefundMoney()!=null){ bydNewAflPaccountRefundCount.setRefundMoney(bydNewAflPaccountRefundCount.getRefundMoney()+bydAflPaccountRefundCount.getRefundMoney()); } if (bydAflPaccountRefundCount.getCollectMoney()!=null){ bydNewAflPaccountRefundCount.setCollectMoney(bydNewAflPaccountRefundCount.getCollectMoney()+bydAflPaccountRefundCount.getCollectMoney()); } if (bydAflPaccountRefundCount.getNumberTotal()!=null){ bydNewAflPaccountRefundCount.setNumberTotal(bydNewAflPaccountRefundCount.getNumberTotal()+bydAflPaccountRefundCount.getNumberTotal()); } aflPaccountRefundCountRepo.save(bydNewAflPaccountRefundCount); } continue; } if ("52010188930".equals(aflPaccountRefundCount.getAgentId())&&bydAflPaccountRefundCount!=null){ if (bydAflPaccountRefundCount.getMoneyTotal()!=null){ aflPaccountRefundCount.setMoneyTotal(aflPaccountRefundCount.getMoneyTotal()+bydAflPaccountRefundCount.getMoneyTotal()); } if (bydAflPaccountRefundCount.getRefundMoney()!=null){ aflPaccountRefundCount.setRefundMoney(aflPaccountRefundCount.getRefundMoney()+bydAflPaccountRefundCount.getRefundMoney()); } if (bydAflPaccountRefundCount.getCollectMoney()!=null){ aflPaccountRefundCount.setCollectMoney(aflPaccountRefundCount.getCollectMoney()+bydAflPaccountRefundCount.getCollectMoney()); } if (bydAflPaccountRefundCount.getNumberTotal()!=null){ aflPaccountRefundCount.setCollectMoney(aflPaccountRefundCount.getCollectMoney()+bydAflPaccountRefundCount.getCollectMoney()); } } if (StringUtils.isEmpty(aflPaccountRefundCount.getId())){ aflPaccountRefundCount.setId(UUID.randomUUID().toString().replace("-", "")); aflPaccountRefundCount.setInsertTime(LocalDateTime.now()); } aflPaccountRefundCount.setUpdateTime(LocalDateTime.now()); aflPaccountRefundCountRepo.save(aflPaccountRefundCount); } } private List toVo(List payList) { if (CollectionUtils.isEmpty(payList)) { return Lists.newArrayList(); } List billsVos = Lists.newArrayList(); VehicleAgreementNumBillsVo billsVo; for (FssPaccountPay bill : payList) { final LocalDateTime firstInsertTime = wechatBillPayApplyRepo.getFirstInsertTime(bill.getPayId()); if (null == firstInsertTime) { // 过滤掉没有// 过滤掉没有轮扣过的流水 continue; } billsVo = new VehicleAgreementNumBillsVo(bill.getPayId(), bill.getSceneTp(), bill.getReceiptAmt(), bill.getChargeTime()); // 处理车牌号、车牌颜色 if (!StringUtils.isEmpty(bill.getVehicleId()) && bill.getVehicleId().contains("_")) { final String[] vehicle = bill.getVehicleId().split("_"); billsVo.setPlateNumber(vehicle[0]); billsVo.setPlateNumberColor(VehiclePlateColorEnum.find(Integer.parseInt(vehicle[1])).getColor()); } // 处理出入口站名、停车场名称 if (!StringUtils.isEmpty(bill.getTitle()) && null != bill.getSceneTp()) { final String[] titleSplit = bill.getTitle().split("[,,]"); if (titleSplit.length != 5) { continue; } if (bill.getSceneTp().equals(2)) { // 停车场流水 billsVo.setParkingLotName(titleSplit[3]); } else { // 非停车场流水的, 按照通行流水处理 billsVo.setInboundName(titleSplit[2]); billsVo.setOutboundName(titleSplit[3]); } } billsVos.add(billsVo); } return billsVos; } @Override public FssPaccountPayResultFindByPayIdResponse findPaccountPayResultByPayId(String payId) throws ServiceHandleException { FssPaccountPay fssPaccountPay = paccountPayRepo.findByPayId(payId); if (fssPaccountPay == null) { throw UcServiceError.NOT_FOUND_DATA.toHandleException("未查询到数据"); } FssPaccountPayResultFindByPayIdResponse response = new FssPaccountPayResultFindByPayIdResponse(); BeanUtil.copyProperties(fssPaccountPay, response); response.setPayStatus(fssPaccountPay.getStatus()); // 请款中的数据,查看是否存在扣款失败原因 if (fssPaccountPay.getStatus().equals(2)) { final LocalDateTime firstInsertTime = wechatBillPayApplyRepo.getFirstInsertTime(payId); final WechatBillPayApply payApply = null == firstInsertTime ? null : wechatBillPayApplyRepo.findByPayIdAndInsertTime(payId, firstInsertTime); if (null != payApply) { response.setPayMsg(StringUtils.isEmpty(payApply.getErrCodeDes()) ? payApply.getResErrCodeDes() : payApply.getErrCodeDes()); response.setWxOrderId(payApply.getOutTradeNo()); } } //扣款成功,需返回微信支付订单号 if (fssPaccountPay.getStatus().equals(1) && StringUtils.isNotBlank(fssPaccountPay.getWxOrderId())) { WechatBillPaySuccess paySuccess = successRepo.findByOutTradeNo(fssPaccountPay.getWxOrderId()); if (null != paySuccess) { response.setTransactionId(paySuccess.getTransactionId()); } else { AflApPayOrder aflApPayOrder = apPayOrderRepo.findByOrderNo(fssPaccountPay.getWxOrderId()); if (null != aflApPayOrder) { response.setTransactionId(aflApPayOrder.getTransactionId()); } } } return response; } }