package com.qtzl.alterSales.manager.service;
import cn.com.taiji.common.manager.net.http.ServiceHandleException;
import com.google.common.collect.Lists;
import com.qtzl.alterSales.dao.entity.primary.AflSupplementaryPayOrder;
import com.qtzl.alterSales.dao.entity.primary.FssPaccountPay;
import com.qtzl.alterSales.dao.entity.primary.WechatBillPayApply;
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.WechatBillPayApplyRepo;
import com.qtzl.alterSales.manager.enums.VehiclePlateColorEnum;
import com.qtzl.alterSales.manager.model.protocol.UcServiceError;
import com.qtzl.alterSales.manager.vo.VehicleAgreementNumBillsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
/***
*
* 选装-原始流水 service
*
* @author hou yi
* {@code @date} 2023/10/12 11:43
**/
@Service
public class FssPaccountPayServiceImpl implements FssPaccountPayService {
@Resource
private FssPaccountPayRepo paccountPayRepo;
@Resource
private AflSupplementaryPayOrderRepo supplementaryPayOrderRepo;
@Resource
private WechatBillPayApplyRepo wechatBillPayApplyRepo;
@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));
}
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;
}
}