package com.xxfc.platform.order.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.order.entity.BaseOrder;
import com.xxfc.platform.order.entity.OrderRentVehicleDetail;
import com.xxfc.platform.order.entity.OrderUserLicense;
import com.xxfc.platform.order.entity.OrderVehicleCrosstown;
import com.xxfc.platform.order.mapper.OrderVehicaleCrosstownMapper;
import com.xxfc.platform.order.pojo.order.OrderVehicleCrosstownDto;
import com.xxfc.platform.vehicle.common.RestResponse;
import com.xxfc.platform.vehicle.entity.Vehicle;
import com.xxfc.platform.vehicle.feign.VehicleFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderVehicalCrosstownBiz extends BaseBiz<OrderVehicaleCrosstownMapper, OrderVehicleCrosstown> {

    @Autowired
    OrderVehicalCrosstownBiz orderVehicalCrosstownBiz;

    @Autowired
    OrderUserLicenseBiz orderUserLicenseBiz;

    @Autowired
    VehicleFeign vehicleFeign;

    @Autowired
    private OrderRentVehicleBiz orderRentVehicleBiz;

    @Autowired
    BaseOrderBiz baseOrderBiz;

    public OrderVehicleCrosstown get(Integer id){
        return selectById(id);
    }

    public List<OrderVehicleCrosstown> selectByOrderId(OrderVehicleCrosstownDto orderVehicleCrosstownDto) {
        return mapper.selectByOrderId(orderVehicleCrosstownDto);
    }

    public ObjectRestResponse<OrderVehicleCrosstownDto> add(OrderVehicleCrosstownDto orderVehicleCrosstownDto) {
        log.info("添加的参数：orderVehicaleCrosstown = {}", orderVehicleCrosstownDto);
        if (orderVehicleCrosstownDto == null || orderVehicleCrosstownDto.getOrderId() == null ) {
            return ObjectRestResponse.createFailedResult(500, "参数为空！");
        }
        BaseOrder baseOrder = baseOrderBiz.selectById(orderVehicleCrosstownDto.getOrderId());
        if(baseOrder == null) {
            return ObjectRestResponse.createFailedResult(500,"订单不存在！");
        }
        OrderRentVehicleDetail orderRentVehicleDetail = orderRentVehicleBiz.selectById(baseOrder.getDetailId());
        List<OrderVehicleCrosstown> oldValue = orderVehicalCrosstownBiz.selectByOrderId(orderVehicleCrosstownDto);
        if (oldValue .size() == 1) {
            //已有直接返回
            //交车完成 设置订单状态为出行中
            if(baseOrder.getStatus() == 4) {//
                //判断交车时间是否是今天
                   baseOrder.setStatus(5);
                   baseOrderBiz.updateSelectiveById(baseOrder);
            } else if(baseOrder.getStatus() == 5) {
                baseOrder.setStatus(6);
                baseOrderBiz.updateSelectiveById(baseOrder);
            }
            OrderVehicleCrosstownDto vehicleCrosstownDto = new OrderVehicleCrosstownDto();
            BeanUtil.copyProperties(oldValue.get(0), vehicleCrosstownDto, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            List<OrderUserLicense> orderUserLicenses = orderUserLicenseBiz.selectByIds(oldValue.get(0).getUserLicenseId()).getData();
            if(orderUserLicenses != null && orderUserLicenses.size() > 0) {
                orderVehicleCrosstownDto.setLicenseIdCard(orderUserLicenses.get(0).getLicenseIdCard());
                orderVehicleCrosstownDto.setLicenseImg(orderUserLicenses.get(0).getLicenseImg());
                orderVehicleCrosstownDto.setLicenseName(orderUserLicenses.get(0).getLicenseName());
                orderVehicleCrosstownDto.setLicensePhone(orderUserLicenses.get(0).getLicensePhone());
            }
            RestResponse<Vehicle> restResponse = vehicleFeign.findById(orderRentVehicleDetail.getVehicleId());
            if(restResponse.getData() != null) {
                vehicleCrosstownDto.setVehicleNumberPlat(restResponse.getData().getNumberPlate());
            }
            return ObjectRestResponse.succ(vehicleCrosstownDto);
        } else if(oldValue .size() <= 0) {
            getOrderLicense(orderVehicleCrosstownDto);
            orderVehicalCrosstownBiz.insertSelective(orderVehicleCrosstownDto);
            //交车完成 设置订单状态为出行中
            if(baseOrder.getStatus() == 4) {
                //判断是否是今日交车
                    baseOrder.setStatus(5);
                    baseOrderBiz.updateSelectiveById(baseOrder);
            } else if(baseOrder.getStatus() == 5) {
                baseOrder.setStatus(6);
                baseOrderBiz.updateSelectiveById(baseOrder);
            }
            RestResponse<Vehicle> restResponse = vehicleFeign.findById(orderRentVehicleDetail.getVehicleId());
            if(restResponse.getData() != null) {
                orderVehicleCrosstownDto.setVehicleNumberPlat(restResponse.getData().getNumberPlate());
            }
            return ObjectRestResponse.succ(orderVehicleCrosstownDto);
        }
        return ObjectRestResponse.createDefaultFail();
    }

    public void getOrderLicense(OrderVehicleCrosstownDto orderVehicleCrosstownDto) {
        OrderUserLicense orderUserLicense = new OrderUserLicense();
        orderUserLicense.setLicenseIdCard(orderVehicleCrosstownDto.getLicenseIdCard());
        orderUserLicense.setLicenseImg(orderVehicleCrosstownDto.getLicenseImg());
        orderUserLicense.setLicenseName(orderVehicleCrosstownDto.getLicenseName());
        orderUserLicense.setLicensePhone(orderVehicleCrosstownDto.getLicensePhone());
        if(orderUserLicense != null) {
            Integer id = orderUserLicenseBiz.add(orderUserLicense).getData().getId();
            orderVehicleCrosstownDto.setUserLicenseId(id + "");
        }
    }
}
