BillingServiceImpl.java 28.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
package com.zteits.bill.biz.billing;

import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.clouds.common.cache.sys.SysCodeValueCacheUtil;
import com.clouds.common.constants.BillingFreeTimeTypeEnum;
import com.clouds.common.constants.BillingLogEnum;
import com.clouds.common.constants.CarTypeEnum;
import com.clouds.common.constants.CodeValKindEnum;
import com.clouds.common.constants.CodeValueEnum;
import com.clouds.common.constants.WhitelistEnum;
import com.clouds.common.entity.DateSpiltBean;
import com.clouds.common.utils.ResultUtils;
import com.clouds.common.utils.datespilt.DateSpiltUtil;
import com.xiaoleilu.hutool.date.DateUnit;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.date.TimeInterval;
import com.zteits.bill.biz.billing.inner.BillingInnerService;
import com.zteits.bill.biz.billrule.inner.BillingRuleInnerService;
import com.zteits.bill.dao.billlog.BillingLogDao;
import com.zteits.bill.dao.whitelist.WhitelistDao;
import com.zteits.bill.domain.WhitelistIsFreeDO;
import com.zteits.bill.domain.sdomain.ParkFeeTempDO;
import com.zteits.bill.util.BillingInfoUtil;
import com.zteits.clouds.api.apibase.bean.BizResult;
import com.zteits.clouds.api.apibase.constants.ErrorType;
import com.zteits.clouds.api.apibase.constants.ParkOrderTypeEnum;
import com.zteits.clouds.api.apibase.exception.BizException;
import com.zteits.clouds.api.dto.bill.billing.dto.BillingResultDTO;
import com.zteits.clouds.api.dto.bill.billing.param.BillingReq;
import com.zteits.clouds.api.dto.bill.billinglog.dto.BillingLogDTO;
import com.zteits.clouds.api.dto.bill.billinglog.dto.BillingLogDetailDTO;
import com.zteits.clouds.api.dto.bill.billinglog.dto.BillingLogOfDayDTO;
import com.zteits.clouds.api.dto.bill.billingrule.dto.BillingRuleCollectDTO;
import com.zteits.clouds.api.dto.bill.billingrule.dto.BillingRuleDTO;
import com.zteits.clouds.api.dto.bill.cloudbilling.dto.FreeSectionDTO;
import com.zteits.clouds.api.dto.bill.cloudbilling.param.CloudBillingReq;
import com.zteits.clouds.api.dto.bill.dto.TVipAreaCardCustInfoDTO;
import com.zteits.clouds.api.dto.bill.param.TVipAreaCardCustInfoQueryRequest;
import com.zteits.clouds.api.dto.bill.vipcard.VipCardCustInfoDTO;
import com.zteits.clouds.api.dto.bill.vipcard.VipCardCustSubPlateInfoDTO;
import com.zteits.clouds.api.dto.bill.vipcard.param.QueryIsBuyVipCardCustReq;
import com.zteits.clouds.api.dto.bill.vipcard.param.VipCardInfoReq;
import com.zteits.clouds.api.dto.bill.whitelist.CarNumIsFreeDTO;
import com.zteits.clouds.api.dto.bill.whitelist.param.QueryCarNumIsWhitelistRequest;
import com.zteits.clouds.api.service.bill.billing.BillingService;
import com.zteits.clouds.api.service.bill.cloudbilling.CloudBillingService;
import com.zteits.clouds.api.service.bill.viparea.TVipAreaCardCustInfoService;
import com.zteits.clouds.api.service.bill.vipcard.VipCardCustInfoService;
import com.zteits.clouds.api.service.bill.whitelist.WhitelistService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
 * Copyright: Copyright (c) 2017  zteits
 *
 * @ClassName: com.zteits.billing.biz.billing
 * @Description:
 * @version: v1.0.0
 * @author: atao
 * @date: 2017/11/1   上午11:15 Modification History: Date         Author          Version      Description
 * ---------------------------------------------------------* 2017/11/1      atao          v1.0.0          创建
 */
@Component
public class BillingServiceImpl implements BillingService {

    private static final Logger log = LoggerFactory.getLogger(BillingServiceImpl.class);
    @Autowired
    private BillingInnerService billingInnerService;
    @Autowired
    private BillingLogDao billingLogDao;
    @Autowired
    private BillingRuleInnerService billingRuleInnerService;
    @Autowired
    private VipCardCustInfoService vipCardCustInfoService;
    @Autowired
    private WhitelistService whitelistService;
    @Autowired
    private WhitelistDao whitelistDao;
    @Autowired
    private TVipAreaCardCustInfoService tVipAreaCardCustInfoService;

    @Autowired
    private CloudBillingService cloudBillingService;

    @Override
    public BizResult<BillingResultDTO> billing(BillingReq req) {

        validateReq(req);

        //调用云计费
        BizResult<BillingResultDTO> cloudBillResult = this.billWithCloud(req);
        if(null != cloudBillResult){
            return cloudBillResult;
        }

        TimeInterval timer = DateUtil.timer();
        log.info("{}===计费处理===start==", req.getBaseRequest().getRequestId());
        log.info("{}===计费处理=== req:{}", req.getBaseRequest().getRequestId(), JSONObject.toJSONString(req));
        timer.start();


        //真实车型
        Integer realCarType = req.getCarType();

        if (CarTypeEnum.OUT_LIMIT_LARGE_CAR.val().equals(req.getCarType())) {
            //超标大型车
            req.setCarType(CarTypeEnum.LARGE_CAR.val());
        }

        if (CarTypeEnum.OUT_LIMIT_SMALL_CAR.val().equals(
            req.getCarType())) {
            //超标小型车
            req.setCarType(CarTypeEnum.SMALL_CAR.val());
        }

        if (CarTypeEnum.TRUCK_SMALL_CAR.val().equals(req.getCarType())) {
            req.setCarType(CarTypeEnum.SMALL_CAR.val());
        }

        if (CarTypeEnum.TRUCK_LARGE_CAR.val().equals(req.getCarType())) {
            req.setCarType(CarTypeEnum.LARGE_CAR.val());
        }
        /**
         * 初始化计费日志
         */
        BillingLogDTO billingLogDTO = BillingInfoUtil.init(req);

        /**
         * 计算停车时长,单位为秒
         */
        Long parkDuration = DateUtil.between(req.getInTime(), req.getOutTime(), DateUnit.SECOND);

        BillingResultDTO resultDTO = new BillingResultDTO();
        resultDTO.setCarNo(req.getCarNo());
        resultDTO.setParkDuration(parkDuration);
        resultDTO.setBillingLogId(billingLogDTO.getLogId());

        /**
         * 查询计费规则集合对象,获取免费时长 及 免费时长减免类型
         */
        BillingRuleCollectDTO collectDTO = billingRuleInnerService.queryBillingRuleCollectByPlNo(req.getPlNo(),
            req.getInTime(), req.getCarType());
        if (collectDTO == null) {
            throw new BizException(ErrorType.BIZ_ERROR, "未查询到生效的计费规则集合");
        }

        /**
         * 获取免费时长
         */
        Long freeTime = collectDTO.getFreeTime();
        freeTime = (freeTime != null && freeTime > 0) ? freeTime : 0;

        billingLogDTO.setFreeTime(freeTime);
        billingLogDTO.setFreeTimeType(collectDTO.getFreeTimeType());

        /**
         * 如果停车时长小于免费时长, 费用为0
         */
        if (parkDuration <= freeTime) {

            billingLogDTO.setMaxFee(collectDTO.getMaxCharge());
            billingLogDTO.setRealFee(0L);
            billingLogDTO.setReceFee(0L);
            //短时停车免费
            resultDTO.setFreeType(ParkOrderTypeEnum.ShortTimeParkFree.getValue());
            resultDTO.setParkFee(0L);
            return new BizResult<>(resultDTO);

        }

        /**
         * 查询是否货车免费半小时白名单车辆
         */

        try {
            //获取白名单货车免费时长
            String trunkFreeTimeStr = SysCodeValueCacheUtil.queryCodeNameByCodeValue(
                CodeValKindEnum.TRUCK_CAR_WHITE_LIST,
                CodeValueEnum.TRUCK_CAR_WHITE_LIST_FREE_TIME);
            Integer trunkFreeTime = 0;
            if (StringUtils.isNotEmpty(trunkFreeTimeStr)) {
                trunkFreeTime = Integer.valueOf(trunkFreeTimeStr);
            }
            log.info("{}===计费处理: 车牌号从缓存中获取货车白名单免费时长{}", req.getBaseRequest().getRequestId(), trunkFreeTimeStr);
            if (trunkFreeTime > 0) {
                QueryCarNumIsWhitelistRequest request = new QueryCarNumIsWhitelistRequest();
                request.setCarNumber(req.getCarNo());
                request.setPlNo(req.getPlNo());
                request.setCurrentDate(req.getOutTime());
                request.setChargerType(WhitelistEnum.CHARGE_TYPE_4.getKey());
                WhitelistIsFreeDO whitelistIsFreeDO = whitelistDao.queryWhitelistByCarNumAndPlNo(request);
                boolean truckFlag = CarTypeEnum.TRUCK_SMALL_CAR.val().equals(realCarType) || CarTypeEnum.TRUCK_LARGE_CAR
                    .val().equals(realCarType) || (null != whitelistIsFreeDO && WhitelistEnum.CHARGE_TYPE_4.getKey()
                    .equals(
                        whitelistIsFreeDO.getChargeType()));
                if (truckFlag) {
                    log.info("{}===计费处理: 车牌号 {} 属于白名单货车,免费时长为:{}", req.getBaseRequest().getRequestId(), req.getCarNo(),
                        trunkFreeTime);
                    if (parkDuration <= trunkFreeTime) {
                        resultDTO.setParkFee(0L);
                        resultDTO.setFreeType(ParkOrderTypeEnum.WhiteListCarFree.getValue());
                        return new BizResult<>(resultDTO);
                    }

                }

            }

        } catch (Exception e) {
            log.error("{}===计费处理: 查询处理白名单货车免费时长异常", req.getBaseRequest().getRequestId(), e);
        }

        /**
         * 免费时长进行减免  并且 停车时长小于等于免费时长 费用为0

         if (!BillingFreeTimeTypeEnum.BILLING_FREE_TIME_NO_FREE.getVal().equals(collectDTO.getFreeTimeType())
         && parkDuration <= freeTime) {
         log.info("停车时长小于免费时长,费用为0");
         billingLogDTO.setMaxFee(collectDTO.getMaxCharge());
         billingLogDTO.setRealFee(0L);
         billingLogDTO.setReceFee(0L);
         resultDTO.setParkFee(0L);
         return new BizResult<>(resultDTO);

         } */

        /**
         * 停车时长减免 免费时长 按照进场时间减免
         */
        if (BillingFreeTimeTypeEnum.BILLING_FREE_TIME_FREE_IN_TIME.getVal().equals(collectDTO.getFreeTimeType())) {
            log.info("按照进场时间减免停车时长");
            BillingInfoUtil.newDayLog(req.getInTime());
            BillingLogOfDayDTO dayDTO = BillingInfoUtil.getLastBillingLogDay();
            dayDTO.setShowDay(false);
            dayDTO.setRealFeeOfDay(0L);
            dayDTO.setReceFeeOfDay(0L);
            dayDTO.setMaxFeeOfDay(-1L);
            dayDTO.setFreeReason("按照进场时间减免时长");
            BillingLogDetailDTO detailDTO = BillingInfoUtil.newDayLogDetail();
            detailDTO.setBillingTimeLimitStart(0);
            detailDTO.setBillingTimeLimitEnd(0);
            detailDTO.setDetailFee(0L);
            detailDTO.setFreeReason("减免免费时长");
            detailDTO.setStartTime(req.getInTime());

            req.setInTime(DateUtil.offsetSecond(req.getInTime(), freeTime.intValue()));

            detailDTO.setEndTime(req.getInTime());
        }

        /**
         * 停车时长减免 免费时长 按照出场时间减免
         */
        if (BillingFreeTimeTypeEnum.BILLING_FREE_TIME_FREE_OUT_TIME.getVal().equals(collectDTO.getFreeTimeType())) {
            log.info("按照出场时间减免停车时长");
            BillingInfoUtil.newDayLog(req.getInTime());
            BillingLogOfDayDTO dayDTO = BillingInfoUtil.getLastBillingLogDay();
            dayDTO.setShowDay(false);
            dayDTO.setRealFeeOfDay(0L);
            dayDTO.setReceFeeOfDay(0L);
            dayDTO.setMaxFeeOfDay(-1L);
            dayDTO.setFreeReason("按照出场时间减免时长");
            BillingLogDetailDTO detailDTO = BillingInfoUtil.newDayLogDetail();
            detailDTO.setBillingTimeLimitStart(0);
            detailDTO.setBillingTimeLimitEnd(0);
            detailDTO.setDetailFee(0L);
            detailDTO.setFreeReason("减免免费时长");
            detailDTO.setStartTime(req.getInTime());

            req.setOutTime(DateUtil.offsetSecond(req.getOutTime(), 0 - freeTime.intValue()));

            detailDTO.setEndTime(req.getInTime());

        }

        /**
         * 按时间切分后的日期进行计费
         */
        ParkFeeTempDO parkFeeBean = billingOfSpiltDay(req, collectDTO);

        /**
         * 放入计费费用 及 计费时长
         */
        resultDTO.setParkFee(parkFeeBean.getFee());
        resultDTO.setParkFeeDuration(parkFeeBean.getParkFeeDuration());

        log.info("{}===计费处理===end==", req.getBaseRequest().getRequestId());
        log.info("{}===计费处理=== res:{}", req.getBaseRequest().getRequestId(), JSONObject.toJSONString(resultDTO));
        log.info("{}===计费处理=== 耗费:{}毫秒", req.getBaseRequest().getRequestId(), timer.interval());
        log.info("{}===计费处理=== 计费log={}", req.getBaseRequest().getRequestId(), JSONObject.toJSONString(billingLogDTO));

        /**
         * 如果计费日志需要保存,进行日志保存
         */
        if (BillingLogEnum.BILLING_LOG_SAVE.getVal().equals(req.getIsShowLog())) {
            billingLogDao.saveBillingLog(billingLogDTO);
        }

        return new BizResult<>(resultDTO);
    }

    /**
     * 按照白天夜间分割时间进行计费
     *
     * @param req        计费请求对象
     * @param collectDTO 计费规则集合
     * @return 费用
     */
    private ParkFeeTempDO billingOfSpiltDay(BillingReq req, BillingRuleCollectDTO collectDTO) {

        Long freeTime = collectDTO.getFreeTime();

        /**
         * 获取进出场分隔开的时间列表
         */
        List<DateSpiltBean> spiltBeans = DateSpiltUtil.spiltTimeOfOndDay(req.getInTime(), req.getOutTime(),
            collectDTO.getDayStartTime(), collectDTO.getDayEndTime());

        Long fee = 0L;
        Long parkFeeDuration = 0L;

        /**
         * 计费规则中本次最高收费
         */
        Integer maxCharge = 0;

        QueryCarNumIsWhitelistRequest request;
        request = new QueryCarNumIsWhitelistRequest();
        request.setCarNumber(req.getCarNo());
        request.setCarType(req.getCarType());
        request.setPlNo(req.getPlNo());
        request.setSysCode(req.getSysCode());
        BizResult<CarNumIsFreeDTO> bizResult;
        BizResult<Boolean> isBuyVipCardResult;
        /**
         * 按照切分的日期 计算每天的费用
         */
        for (DateSpiltBean bean : spiltBeans) {
            try {
                /**
                 * 查询会员卡免费信息
                 */

                request.setCurrentDate(bean.getStartDate());
                log.info("查询会员卡免费信息入参为{}" + JSONObject.toJSONString(request));

                bizResult = vipCardCustInfoService.queryCarNumIsBuyVipCard(request);

                log.info("查询会员卡免费信息结果为{}" + JSONObject.toJSONString(bizResult));
                CarNumIsFreeDTO dto = ResultUtils.getBizResultData(bizResult);

                /**
                 * 车辆 会员卡用户信息
                 */
                if (dto.getIsFree() == CarNumIsFreeDTO.FREE) {
                    /**
                     * 是会员卡返回0
                     */
                    fee += 0L;
                    continue;
                }

                QueryIsBuyVipCardCustReq queryIsBuyVipCardCustReq = new QueryIsBuyVipCardCustReq();
                queryIsBuyVipCardCustReq.setCarNumber(req.getCarNo());
                queryIsBuyVipCardCustReq.setPlNo(req.getPlNo());
                queryIsBuyVipCardCustReq.setDateTime(bean.getStartDate());
                log.info("查询区域(全域)会员卡免费信息入参为{}" + JSONObject.toJSONString(queryIsBuyVipCardCustReq));

                isBuyVipCardResult = tVipAreaCardCustInfoService.queryIsBuyVipCardCust(
                    queryIsBuyVipCardCustReq);

                log.info("查询区域(全域)会员卡免费信息结果为{}" + JSONObject.toJSONString(isBuyVipCardResult));
                Boolean isBuyFlag = ResultUtils.getBizResultData(isBuyVipCardResult);
                /**
                 * 车辆 会员卡用户信息
                 */
                if (isBuyFlag) {
                    /**
                     * 是会员卡返回0
                     */
                    fee += 0L;
                    continue;
                }

            } catch (Exception e) {
                log.error("调用查询会员卡免费结果信息捕获了异常", e);
                throw new BizException(ErrorType.BIZ_ERROR, "捕获异常:" + e.getMessage());
            }

            /**
             * 根据停车编码、车辆类型、进场日期获取计费规则
             */
            BillingRuleDTO billingRuleDTO = billingInnerService.queryRuleInfoByPlNOAndCarType(req.getPlNo(),
                req.getCarType(), bean.getStartDate());

            /**
             * 新增一天的日志
             */
            BillingLogOfDayDTO billingLogOfDayDTO = BillingInfoUtil.newDayLog(bean.getStartDate());
            billingLogOfDayDTO.setApplyDayType(billingLogOfDayDTO.getApplyDayType());
            billingLogOfDayDTO.setCrossTimeRuTleType(billingLogOfDayDTO.getCrossTimeRuTleType());
            billingLogOfDayDTO.setIsHoliday(billingLogOfDayDTO.getIsHoliday());
            billingLogOfDayDTO.setMaxFeeOfDay(billingLogOfDayDTO.getMaxFeeOfDay());
            billingLogOfDayDTO.setRuleNo(billingRuleDTO.getRuleNo());

            if (null == billingRuleDTO) {
                /**
                 * 根据日期查不到计费规则,则该时间段费用为0.
                 */
                billingLogOfDayDTO.setReceFeeOfDay(0L);
                billingLogOfDayDTO.setRealFeeOfDay(0L);
                billingLogOfDayDTO.setFreeReason("该段时间未查询到计费规则,默认停车费用为0元");
                continue;
            }
            /**
             * 获取所有规则中 单次停车收费最大值 用于与最终的费用结果进行对比
             */
            maxCharge = maxCharge >= billingRuleDTO.getMaxCharge() ? maxCharge : billingRuleDTO.getMaxCharge();

            /**
             * 默认夜间免费,判断白天时间 是否 小于免费时长 如果小于在免费
             */

            Date nightTimeOfDay = DateUtil.parse(
                DateUtil.formatDate(bean.getStartDate()) + " " + DateUtil.formatTime(collectDTO.getDayEndTime()));

            Long duration = DateUtil.between(bean.getStartDate(), nightTimeOfDay, DateUnit.SECOND);

            Long feeOfDay = 0L;
            /**
             * 默认夜间免费(赤峰 和 丹阳这么处理), 判断 进场时间 与白天夜间分界线 之间的停车时长是否在免费时长内,如果在的话就免费
             */
            boolean flag = duration <= freeTime && (Long.valueOf(10003).equals(collectDTO.getOrgId()) || Long.valueOf(
                10064).equals(collectDTO.getOrgId()));
            if (flag) {
                feeOfDay = 0L;
            } else {
                ParkFeeTempDO parkFeeTempDO = billingInnerService.billingFee(bean.getStartDate(), bean.getEndDate(),
                    billingRuleDTO);
                feeOfDay = parkFeeTempDO.getFee();
                parkFeeDuration += parkFeeTempDO.getParkFeeDuration();
            }

            /**
             *如果总费用为0,并且每天的总时长小于等于免费时长,则费用为0,不计费
             */
            if (fee.equals(0L) && DateUtil.between(bean.getStartDate(), bean.getEndDate(), DateUnit.SECOND)
                <= freeTime) {
                continue;
            }

            billingLogOfDayDTO.setReceFeeOfDay(feeOfDay);

            if (billingRuleDTO.getDayMaxCharge() > 0) {

                /**
                 * 与单日最大收费进行对比
                 */
                feeOfDay = feeOfDay <= billingRuleDTO.getDayMaxCharge() ? feeOfDay : billingRuleDTO.getDayMaxCharge();
                billingLogOfDayDTO.setRealFeeOfDay(feeOfDay);
            }

            if (feeOfDay != 0) {

                /**
                 * 查询白名单免费信息
                 */
                request.setCurrentDate(bean.getEndDate());
                bizResult = whitelistService.queryCarNumIsOnlyOneFeeEveryDay(request);
                CarNumIsFreeDTO dto = ResultUtils.getBizResultData(bizResult);

                if (dto.getIsFree() == CarNumIsFreeDTO.FREE) {
                    /**
                     * 白名单免费
                     */
                    feeOfDay = 0L;
                }
                if (dto.getIsFree() == CarNumIsFreeDTO.NO_FREE && dto.getMaxCharge() != null) {

                    /**
                     * 白名单按照固定费率收费
                     */
                    feeOfDay = dto.getMaxCharge();
                }
            }
            /**
             * 当日费用叠加
             */
            fee += feeOfDay;

        }

        BillingLogDTO billingLogDTO = BillingInfoUtil.getBillingLog();
        billingLogDTO.setReceFee(fee);

        if (maxCharge > 0) {
            /**
             * 与单次最高限价相比
             */
            fee = fee < maxCharge ? fee : maxCharge;

        }

        billingLogDTO.setMaxFee(Long.valueOf(maxCharge));
        billingLogDTO.setRealFee(fee);

        return new ParkFeeTempDO(fee, parkFeeDuration);

    }

    private void validateReq(BillingReq req) {
        if (StringUtils.isBlank(req.getCarNo())) {
            throw new BizException(ErrorType.PARAMM_NULL, "车牌号");
        }
        if (StringUtils.isBlank(req.getPlNo())) {
            throw new BizException(ErrorType.PARAMM_NULL, "停车场编号");
        }

        if (null == req.getInTime()) {
            throw new BizException(ErrorType.PARAMM_NULL, "进场时间");
        }
        if (null == req.getOutTime()) {
            throw new BizException(ErrorType.PARAMM_NULL, "出场时间");
        }
        if (null == req.getCarType()) {
            throw new BizException(ErrorType.PARAMM_NULL, "车辆类型");
        }

    }

    private BizResult<BillingResultDTO> billWithCloud(BillingReq req) {
        BizResult<BillingResultDTO> resultDTO = null;
        try {
            String cloudeBillPlNos = SysCodeValueCacheUtil.queryCodeNameByCodeValue(
                CodeValKindEnum.PL_BILLL_4_CLOUD_RULE,
                CodeValueEnum.PL_BILLL_4_CLOUD_RULE_PLNO);

            if (StringUtils.isBlank(cloudeBillPlNos)) {
                return null;
            }

            List<String> plNos = Arrays.asList(cloudeBillPlNos.split(";"));
            if (CollectionUtils.isEmpty(plNos) || !plNos.contains(req.getPlNo())) {
                return null;
            }
            //获取免费时段
            List<FreeSectionDTO> freeSectionDTOList = new ArrayList<>();

            VipCardInfoReq vipCardInfoReq = new VipCardInfoReq();
            vipCardInfoReq.setCarNumber(req.getCarNo());
            vipCardInfoReq.setPlNo(req.getPlNo());
            vipCardInfoReq.setSysCode("sysCode");

            // 获取该车所有的免费时段-会员车、区域卡车、子母车中的固定车
            BizResult<List<VipCardCustInfoDTO>> vipBizResponse = vipCardCustInfoService.queryAllCardCustInfoByParkcodeAndCarnumber(vipCardInfoReq);
            BizResult<List<TVipAreaCardCustInfoDTO>> vipAreaBizResponse = tVipAreaCardCustInfoService.queryAllAreaVipCardByParkcodeAndCarnumber(vipCardInfoReq);

            // 会员车
            List<VipCardCustInfoDTO> vipCardCustInfoDTOList = vipBizResponse.getData() == null ? new ArrayList<>() : vipBizResponse.getData();
            log.info("{}的会员卡信息: {}", req.getCarNo(), JSON.toJSONString(vipCardCustInfoDTOList));
            vipCardCustInfoDTOList.forEach(dto -> {

                int hourOfValidStartTime = dto.getValidStartTime().getHours();
                int minOfValidStartTime = dto.getValidStartTime().getMinutes();
                int secOfValidStartTime = dto.getValidStartTime().getSeconds();
                int hourOfValidEndTime = dto.getValidEndTime().getHours();
                int minOfValidEndTime = dto.getValidEndTime().getMinutes();
                int secOfValidEndTime = dto.getValidEndTime().getSeconds();

                // 如果月卡每天生效时间是00:00 到 23:59 代表全天月卡生效
                if(hourOfValidStartTime == 0 && minOfValidStartTime == 0 && hourOfValidEndTime == 23 && minOfValidEndTime == 59) {
                    FreeSectionDTO freeSectionDTO = new FreeSectionDTO();
                    freeSectionDTO.setStartTime(dto.getEffDate());
                    freeSectionDTO.setEndTime(dto.getExpDate());
                    freeSectionDTOList.add(freeSectionDTO);
                }else{
                    // 否则月卡每天生效时间段进行判断 提取每天没费时间加入至计费的免费时段
                    List<FreeSectionDTO> list = this.genVaildDate(dto.getEffDate(), dto.getExpDate(), dto.getValidStartTime(), dto.getValidEndTime());
                    log.info("{}的会员卡免费时段: {}", req.getCarNo(), JSON.toJSONString(vipCardCustInfoDTOList));
                    freeSectionDTOList.addAll(list);
                }
            });
            // 区域卡车
            List<TVipAreaCardCustInfoDTO> tVipAreaCardCustInfoDTOList = vipAreaBizResponse.getData() == null ? new ArrayList<>() : vipAreaBizResponse.getData();
            tVipAreaCardCustInfoDTOList.forEach(dto -> {
                FreeSectionDTO freeSectionDTO = new FreeSectionDTO();
                freeSectionDTO.setStartTime(dto.getEffDate());
                freeSectionDTO.setEndTime(dto.getExpDate());
                freeSectionDTOList.add(freeSectionDTO);
            });


            CloudBillingReq cloudBillingReq = new CloudBillingReq();
            cloudBillingReq.setCarNo(req.getCarNo());
            cloudBillingReq.setAreaId(0);
            cloudBillingReq.setCarType(req.getCarType());
            cloudBillingReq.setFreeSectionDTOList(freeSectionDTOList);
            cloudBillingReq.setInTime(req.getInTime());
            cloudBillingReq.setOutTime(req.getOutTime());
            cloudBillingReq.setPlNo(req.getPlNo());
            log.info("调用云计费 req={}", JSONObject.toJSONString(cloudBillingReq));
            resultDTO = cloudBillingService.billing(cloudBillingReq);
            log.info("调用云计费 res={}", JSONObject.toJSONString(resultDTO));

        } catch (Exception e) {
            log.error("查询是否云计费异常",e);
        }

        return resultDTO;
    }

    /**
     * 根据会员卡当天有效时间段,将用户会员卡有效期,拆成每天有效时段,
     * 并组成List
     * @param eff_date
     * @param exp_date
     * @param eff_time
     * @param exp_time
     * @return
     */
    private List<FreeSectionDTO> genVaildDate(Date eff_date, Date exp_date, Time eff_time, Time exp_time)
    {

        Long oneDay = 1000 * 60 * 60 * 24L;
        Long time = eff_date.getTime();
        Long endTime = exp_date.getTime();
        List freeSectionDTOList = new ArrayList<>();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        while (time <= endTime) {
            FreeSectionDTO freeSectionDTO = new FreeSectionDTO();
            Date d = new Date(time);
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(d);
            calendar.set(Calendar.HOUR_OF_DAY, eff_time.getHours());
            calendar.set(Calendar.MINUTE, eff_time.getMinutes());
            calendar.set(Calendar.SECOND, eff_time.getSeconds());
            Date eff_date_temp = calendar.getTime();
            freeSectionDTO.setStartTime(eff_date_temp);

            calendar.set(Calendar.HOUR_OF_DAY, exp_time.getHours());
            calendar.set(Calendar.MINUTE, exp_time.getMinutes());
            calendar.set(Calendar.SECOND, exp_time.getSeconds());
            Date exp_date_temp = calendar.getTime();
            // 有效时段跨天
            if(eff_date_temp.after(exp_date_temp)){
                exp_date_temp = cn.hutool.core.date.DateUtil.offsetDay(exp_date_temp, 1);
                Date real_exp_date = new Date(exp_date.getTime());
                real_exp_date.setHours(23);
                real_exp_date.setMinutes(59);
                real_exp_date.setSeconds(59);
                if(exp_date_temp.after(real_exp_date)){
                    exp_date_temp = new Date(exp_date.getTime());
                    exp_date_temp.setHours(23);
                    exp_date_temp.setMinutes(59);
                    exp_date_temp.setSeconds(59);
                }
            }
            freeSectionDTO.setEndTime(exp_date_temp);
            freeSectionDTOList.add(freeSectionDTO);

            log.info("开始时间:{},结束时间:{}", df.format(eff_date_temp), df.format(exp_date_temp));
            time += oneDay;
        }
        return freeSectionDTOList;
    }

}