package com.pj.api.open.service;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.pj.api.open.ResultJson;
import com.pj.api.open.bo.CheckCarNumberBO;
import com.pj.current.config.PartConfig;
import com.pj.project.tb_business.TbBusiness;
import com.pj.project.tb_business.TbBusinessService;
import com.pj.project.tb_car.TbCar;
import com.pj.project.tb_car.TbCarService;
import com.pj.project.tb_costomer.TbCostomer;
import com.pj.project.tb_costomer.TbCostomerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

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

@Service
@Transactional
public class OpenService {
    @Resource
    private TbBusinessService tbBusinessService;
    @Resource
    private TbCostomerService tbCostomerService;
    @Resource
    private TbCarService tbCarService;
    private final List<String> CAR_LIST = StrUtil.splitTrim("浙,粤,京,津,冀,晋,蒙,辽,黑,沪,吉,苏,皖,赣,鲁,豫,鄂,湘,桂,琼,渝,川,贵,云,藏, 陕, 甘, 青, 宁", ",");


    public ResultJson checkCarNumber(CheckCarNumberBO checkCarNumberBO) {
        String carNo = checkCarNumberBO.getCarNumber();
        String start = carNo.substring(0, 1);
        if (CAR_LIST.contains(start)) {
            return handlerChinaCar(checkCarNumberBO);
        } else {
            return handlerOutCar(checkCarNumberBO);
        }

    }

    private ResultJson handlerOutCar(CheckCarNumberBO checkCarNumberBO) {
        String carNo = checkCarNumberBO.getCarNumber();
        TbBusiness tbBusiness = tbBusinessService.findCarTheNewRecord(carNo);
        if (tbBusiness == null) {
            return ResultJson.error("无业务登记记录");
        }
        Date outTime=tbBusiness.getOutDayTime();
        Date now=new Date();
        if (outTime!=null&&Math.abs(outTime.getHours()-now.getHours())>10) {
            return ResultJson.error("无业务登记记录");
        }
        if (tbBusiness.getAdminConfirmPay() ==0) {
            return ResultJson.error("账单未支付");
        }
        int inOrOut = checkCarNumberBO.getInOrOut();
        TbCar car = tbCarService.findByCardNo(carNo);
        if (inOrOut == 1) {
            if (tbBusiness.getRealInDay() == null) {
                //入场
                tbBusiness.setRealInTime(now)
                        .setInChannel(checkCarNumberBO.getChannelName());
                car.setLastInTime(now);
                tbBusinessService.updateById(tbBusiness);
                return ResultJson.success();
            } else {
                return ResultJson.error("车辆已入场");
            }
        } else {
            if (tbBusiness.getOutDayTime() == null) {
                TbCostomer tbCostomer = tbCostomerService.getById(tbBusiness.getCustomerId());
                car.setLastOutTime(now);
                tbCarService.updateById(car);
                tbBusiness.setOutChannel(checkCarNumberBO.getChannelName());
                handlerOut(tbBusiness);
                if (tbCostomer.getPayType() == 2) {
                    return ResultJson.success();
                } else {
                    if (tbBusiness.getPayStatus() != 3) {
                        return ResultJson.error("车辆未缴费");
                    }
                }
                return ResultJson.success();
            } else {
                return ResultJson.error("车辆已离场");
            }
        }

    }

    private ResultJson handlerChinaCar(CheckCarNumberBO checkCarNumberBO) {
        String carNo = checkCarNumberBO.getCarNumber();
        TbBusiness cb = tbBusinessService.findByChinaCarNo(carNo);
        Date now = new Date();
        String channel = checkCarNumberBO.getChannelName();
        if (checkCarNumberBO.getInOrOut() == 1 && cb.getRealInTime() == null) {
            cb.setChinaCarInTime(now).setChinaCarInChannel(channel);
            tbBusinessService.updateById(cb);
            return ResultJson.success();
        }
        if (checkCarNumberBO.getInOrOut() == 2) {
            cb.setChinaCarOutTime(now).setChinaCarOutChannel(channel);
            tbBusinessService.calculationPartMoney("中国车", cb);
            return ResultJson.success();
        }
        return null;
    }

    private void handlerOut(TbBusiness tbBusiness) {
        tbBusiness.setOutDayTime(new Date());
        tbBusinessService.calculationPartMoney("越南车", tbBusiness);
        tbBusinessService.updateById(tbBusiness);
    }
}
