package com.pj.api.open.service;

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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

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

    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) {
        int inOrOut = checkCarNumberBO.getInOrOut();
        String carNo = checkCarNumberBO.getCarNumber();
        TbBusiness tbBusiness = tbBusinessService.findCarTheNewRecord(carNo,inOrOut);
        if (tbBusiness == null) {
            return ResultJson.error("无业务登记记录");
        }
        Date now=new Date();
        String channel=checkCarNumberBO.getChannelName();
        if (inOrOut==1){
            tbBusiness.setRealInTime(now).setInChannel(channel);
            if (StrUtil.isNotEmpty(tbBusiness.getChinaCarInChannel())||tbBusiness.getChinaCarInTime()!=null){
                tbBusiness.setAdminConfirmIn(1);
            }
            tbBusinessService.updateById(tbBusiness);
            BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(),"订单号【"+tbBusiness.getNo()+"】，车辆【"+tbBusiness.getChinaCarNo()+"】已入场。"+ DateUtil.now()+"。");
            BusinessMessageManager.set(tbBusiness.getCustomerId(),"您的订单号【"+tbBusiness.getNo()+"】，车辆【"+tbBusiness.getChinaCarNo()+"】已入场。"+ DateUtil.now()+"。");
            return ResultJson.success();
        }else {
            if (tbBusiness.getAdminConfirmPay() ==0&&tbBusiness.getCustomerType()==1) {
                return ResultJson.error("账单未确认支付");
            }
            if (tbBusiness.getConfirmInput()==0&&tbBusiness.getCustomerType()==2){
                return ResultJson.error("账单未确认");
            }
            tbBusiness.setOutDayTime(now).setOutChannel(channel);
            if (StrUtil.isNotEmpty(tbBusiness.getChinaCarOutChannel())){
                tbBusiness.setAdminConfirmOut(1);
            }
            tbBusinessService.calculationPartMoney("越南车", tbBusiness);
            tbBusinessService.updateById(tbBusiness);
            BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(),"订单号【"+tbBusiness.getNo()+"】，车辆【"+tbBusiness.getChinaCarNo()+"】已完成业务离场。"+ DateUtil.now()+"。");
            BusinessMessageManager.set(tbBusiness.getCustomerId(),"您的订单号【"+tbBusiness.getNo()+"】，车辆"+tbBusiness.getChinaCarNo()+"已完成业务离场。"+ DateUtil.now()+"。");
            return ResultJson.success();
        }
    }

    private ResultJson handlerChinaCar(CheckCarNumberBO checkCarNumberBO) {
        String carNo = checkCarNumberBO.getCarNumber();
        int inOrOut=checkCarNumberBO.getInOrOut();
        TbBusiness cb = tbBusinessService.findByChinaCarNo(carNo,inOrOut);
        if (cb==null){
            return ResultJson.error("未查询到相应业务");
        }
        Date now = new Date();
        String channel = checkCarNumberBO.getChannelName();
        if (checkCarNumberBO.getInOrOut() == 1) {
            cb.setChinaCarInTime(now).setChinaCarInChannel(channel);
            if (StrUtil.isNotEmpty(cb.getInChannel())||cb.getRealInTime()!=null){
                cb.setAdminConfirmIn(1);
            }
            BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(),"订单号【"+cb.getNo()+"】，车辆【"+cb.getChinaCarNo()+"】已入场。"+DateUtil.now()+"。");
            BusinessMessageManager.set(cb.getCustomerId(),"您的订单号【"+cb.getNo()+"】，车辆【"+cb.getChinaCarNo()+"】已入场。"+DateUtil.now()+"。");
            tbBusinessService.updateById(cb);
            return ResultJson.success();
        }else  {
            //现结
            if (cb.getCustomerType()==1&&cb.getAdminConfirmPay()==0){
                return ResultJson.error("账单未确认支付");
            }
            if (cb.getConfirmInput()==0&&cb.getCustomerType()==2){
                return ResultJson.error("账单未确认");
            }
            cb.setChinaCarOutTime(now).setChinaCarOutChannel(channel);
            if (StrUtil.isNotEmpty(cb.getOutChannel())){
                cb.setAdminConfirmOut(1);
            }
            tbBusinessService.calculationPartMoney("中国车", cb);
            tbBusinessService.updateById(cb);
            BusinessMessageManager.set(cb.getCustomerId(),"您的订单号【"+cb.getNo()+"】，车辆【"+cb.getChinaCarNo()+"】已完成业务离场");
            BusinessMessageManager.set(UserTypeEnum.PLATFORM_ADMIN.getCustomerId(),"业务订单号【"+cb.getNo()+"】，车辆【"+cb.getChinaCarNo()+"】已完成业务离场");
            return ResultJson.success();
        }
    }

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