package com.qtzl.alterSales.manager.service; import cn.com.taiji.common.manager.net.http.ServiceHandleException; import com.alibaba.fastjson.JSON; import com.qtzl.alterSales.dao.entity.primary.AflBindInfo; import com.qtzl.alterSales.dao.entity.primary.FssPaccountPay; import com.qtzl.alterSales.dao.entity.second.AflCenterVehicleInfo; import com.qtzl.alterSales.dao.entity.second.AflVehicleStatusHisInfo; import com.qtzl.alterSales.dao.entity.second.AflVehicleStatusInfo; import com.qtzl.alterSales.dao.repo.jpa.primary.AflBindInfoRepo; import com.qtzl.alterSales.dao.repo.jpa.primary.FssPaccountPayRepo; import com.qtzl.alterSales.dao.repo.jpa.second.AflCenterVehicleInfoRepo; import com.qtzl.alterSales.dao.repo.jpa.second.AflTokenRepo; import com.qtzl.alterSales.dao.repo.jpa.second.AflVehicleStatusHisInfoRepo; import com.qtzl.alterSales.dao.repo.jpa.second.AflVehicleStatusInfoRepo; import com.qtzl.alterSales.manager.enums.BlacklistOpType; import com.qtzl.alterSales.manager.model.protocol.UcServiceError; import com.qtzl.alterSales.manager.model.protocol.external.VehicleBlackListRequest; import com.qtzl.alterSales.manager.model.protocol.sales.VehicleStatusListChangeRequest; import com.qtzl.alterSales.manager.service.center.SendCenterService; 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.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; /** * @author zhangxin */ @Service public class AflVehicleStatusInfoServiceImpl implements AflVehicleStatusInfoService{ private static final Logger log = LoggerFactory.getLogger(AflVehicleStatusInfoServiceImpl.class); @Resource AflVehicleStatusInfoRepo aflVehicleStatusInfoRepo; @Resource AflVehicleStatusHisInfoRepo aflVehicleStatusHisInfoRepo; @Resource AflCenterVehicleInfoRepo aflCenterVehicleInfoRepo; @Resource AflBindInfoRepo aflBindInfoRepo; @Resource FssPaccountPayRepo fssPaccountPayRepo; @Resource private FssBlackListManager fssBlackListManager; @Resource private AflTokenRepo aflTokenRepo; @Resource SendCenterService sendCenterService; @Override public void VehicleStatusListChange(VehicleStatusListChangeRequest request) throws ServiceHandleException { log.info("操作状态名单-请求参数为{}", JSON.toJSON(request)); isParamCheck(request); AflVehicleStatusInfo aflVehicleStatusInfo = aflVehicleStatusInfoRepo.findByVehicleIdAndCardIdAndObuId(request.getVehicleId(),request.getCpuId(),request.getObuId()); Boolean flash = true; log.info("操作状态名单-查询状态名单记录为{}",JSON.toJSON(aflVehicleStatusInfo)); if (aflVehicleStatusInfo!=null){ flash=false; if (request.getType().equals(aflVehicleStatusInfo.getType())){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("当为状态已为"+ BlacklistOpType.findByCode(request.getType()).getValue()); } AflVehicleStatusHisInfo aflVehicleStatusHisInfo = new AflVehicleStatusHisInfo(); BeanUtils.copyProperties(aflVehicleStatusInfo,aflVehicleStatusHisInfo); aflVehicleStatusHisInfoRepo.save(aflVehicleStatusHisInfo); }else{ aflVehicleStatusInfo=new AflVehicleStatusInfo(); BeanUtils.copyProperties(request,aflVehicleStatusInfo); } aflVehicleStatusInfo.setCreateTime(LocalDateTime.now()); if (flash){ AflBindInfo aflBindInfo = aflBindInfoRepo.findByVehicleId(request.getVehicleId()); log.info("操作状态名单-查询车辆绑定记录为{}",JSON.toJSON(aflBindInfo)); if (aflBindInfo==null){ AflCenterVehicleInfo aflCenterVehicleInfo = aflCenterVehicleInfoRepo.findByVehicleId(request.getVehicleId()); log.info("操作状态名单-查询车辆信息为{}",JSON.toJSON(aflCenterVehicleInfo)); if (aflCenterVehicleInfo==null){ throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("未查询到该车辆信息"); } aflVehicleStatusInfo.setPlateNum(aflCenterVehicleInfo.getPlateNum()); aflVehicleStatusInfo.setPlateColor(aflCenterVehicleInfo.getPlateColor()); }else { aflVehicleStatusInfo.setAgreementNum(aflBindInfo.getAgreementNum()); aflVehicleStatusInfo.setPlateNum(aflBindInfo.getPlateNum()); aflVehicleStatusInfo.setPlateColor(aflBindInfo.getPlateColor()); } } log.info("操作状态名单-查询状态名单记录为{}",JSON.toJSON(aflVehicleStatusInfo)); if (BlacklistOpType.OUT.getCode().equals(request.getType())){ List fssPaccountPays = null; if (StringUtils.isEmpty(aflVehicleStatusInfo.getAgreementNum())){ fssPaccountPays = fssPaccountPayRepo.findByAgreementNum(aflVehicleStatusInfo.getAgreementNum()); }else { fssPaccountPays = fssPaccountPayRepo.findByVehicleId(aflVehicleStatusInfo.getPlateNum()+"_"+aflVehicleStatusInfo.getPlateColor()); } log.info("操作状态名单-查询流水记录为{}",JSON.toJSON(fssPaccountPays)); if (!CollectionUtils.isEmpty(fssPaccountPays)) { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("该协议号或者车辆下存在未扣款流水,不允许反白"); } } log.info("操作状态名单-查询状态名单记录为{}",JSON.toJSON(aflVehicleStatusInfo)); VehicleBlackListRequest vehicleBlackListRequest = new VehicleBlackListRequest(); BeanUtils.copyProperties(request,vehicleBlackListRequest); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); vehicleBlackListRequest.setCreateTime(aflVehicleStatusInfo.getCreateTime().format(formatter)); boolean implement; try { implement = fssBlackListManager.isSue(vehicleBlackListRequest, sendCenterService.getToken(request.getUniqueKey())); } catch (ServiceHandleException e) { log.error("请求部中心状态名单接口失败,请求部中心状态名单参数:{}", JSON.toJSON(vehicleBlackListRequest)); e.printStackTrace(); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException(e.getMessage()); } if (implement){ aflVehicleStatusInfo.setUpdateTime(LocalDateTime.now()); aflVehicleStatusInfoRepo.save(aflVehicleStatusInfo); }else { throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("操作状态名单失败"); } } private void isParamCheck(VehicleStatusListChangeRequest request) throws ServiceHandleException { if (request==null){ log.error("下黑或返白时,需指定请求参数..."); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("下黑或返白时,需指定请求参数..."); } if (request.getType()==null){ log.error("下黑或返白时,需指定状态类型..."); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("下黑或返白时,需指定状态类型..."); } if (request.getReason()==null){ log.error("下黑或返白时,需指定状态原因..."); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("下黑或返白时,需指定状态原因..."); } if (StringUtils.isEmpty(request.getVehicleId())){ log.error("下黑或返白时,需指定车辆编号..."); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("下黑或返白时,需指定车辆编号..."); } if (StringUtils.isEmpty(request.getCpuId())){ log.error("下黑或返白时,需指定卡号..."); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("下黑或返白时,需指定卡号..."); } if (StringUtils.isEmpty(request.getObuId())){ log.error("下黑或返白时,需指定OBU号..."); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("下黑或返白时,需指定OBU号..."); } if (StringUtils.isEmpty(request.getAgentId())){ log.error("下黑或返白时,需指定下黑渠道..."); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("下黑或返白时,需指定下黑渠道..."); } if (StringUtils.isEmpty(request.getUniqueKey())){ log.error("下黑或返白时,需指定uniqueKey..."); throw UcServiceError.BUSINESS_VALIDATE_ERR.toHandleException("下黑或返白时,需指定uniqueKey..."); } } }