package com.pj.api.open.service;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.pj.api.open.ResultJson;
import com.pj.api.open.bo.CheckCarNumberBO;
import com.pj.constants.UserTypeEnum;
import com.pj.project.tb_business.BusinessMessageManager;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessService;

import com.pj.project.tb_business_car.TbBusinessCar;
import com.pj.project.tb_business_car.TbBusinessCarService;
import com.pj.utils.cache.RedisUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OpenService {
    @Resource
    @Lazy
    private TbBusinessService tbBusinessService;
    @Resource
    @Lazy
    private TbBusinessCarService tbBusinessCarService;

    private final List<String> CAR_LIST = StrUtil.splitTrim("浙,粤,京,津,冀,晋,蒙,辽,黑,沪,吉,苏,皖,赣,鲁,豫,鄂,湘,桂,琼,渝,川,贵,云,藏, 陕, 甘, 青, 宁", ",");


    public ResultJson checkCarNumber(CheckCarNumberBO checkCarNumberBO) {
        String carNo = checkCarNumberBO.getCarNumber();
        int inOrOut = checkCarNumberBO.getInOrOut();
        if (inOrOut == 1) {
            handlerIn(carNo, checkCarNumberBO.getChannelName());
            return ResultJson.success();
        }
        return handlerOut(carNo, checkCarNumberBO.getChannelName());

    }

    private void handlerIn(String carNo, String channel) {
        carNo = carNo.toUpperCase();
        TbBusinessCar tbBusinessCar = tbBusinessCarService.findNotInCar(carNo);
        Date now = new Date();
        if (tbBusinessCar == null) {
            tbBusinessCar = new TbBusinessCar();
            tbBusinessCar.setCarNo(carNo).setPay(0).setCarSize(3D).setInChannel(channel).setRealInTime(now);
            tbBusinessCarService.save(tbBusinessCar);
            return;
        }
        tbBusinessCar.setRealInTime(now).setInChannel(channel);
        tbBusinessCarService.saveOrUpdate(tbBusinessCar);
        if (!CAR_LIST.contains(carNo)) {
            TbBusiness tbBusiness = tbBusinessService.getById(tbBusinessCar.getBusinessId());
            tbBusiness.setRealInTime(now).setInChannel(channel);
            tbBusinessService.updateById(tbBusiness);
        }
    }

    private ResultJson handlerOut(String carNo, String channel) {
        carNo=carNo.toUpperCase();
        TbBusinessCar tbBusinessCar = tbBusinessCarService.findNotOutCar(carNo);
        if (tbBusinessCar == null) {
            return ResultJson.error("未查询到入场记录");
        }
        if (tbBusinessCar.getIsLock() == 1) {
            return ResultJson.error("车辆已锁定");
        }
        Date now = new Date();
        String businessId = tbBusinessCar.getBusinessId();
        if (StrUtil.isEmpty(businessId)) {
            return ResultJson.success();
        }
        if (!CAR_LIST.contains(StrUtil.sub(carNo,0,1))){
            TbBusiness business = tbBusinessService.getById(businessId);
            if (business.getPayStatus() != 3 && business.getCustomerType() != 2) {
                return ResultJson.error("请缴纳业务费用");
            }
        }
        BigDecimal price = tbBusinessService.calculationPartMoney(tbBusinessCar.getRealInTime(), now);
        double dif = price.subtract(tbBusinessCar.getMoney()).doubleValue();
        if (dif > 0) {
            tbBusinessCar.setPay(0);
            tbBusinessCarService.updateById(tbBusinessCar);
            RedisUtil.set(channel,carNo);
            return ResultJson.error("请缴停车费" + dif + "元");
        }
        tbBusinessCar.setRealOutTime(now).setOutChannel(channel);
        tbBusinessCarService.updateById(tbBusinessCar);
        return ResultJson.success();
    }



}
