package com.xxfc.platform.order.biz;

import com.github.wxiaoqi.security.admin.feign.UserFeign;
import com.github.wxiaoqi.security.admin.feign.dto.AppUserDTO;
import com.github.wxiaoqi.security.admin.feign.dto.UserDTO;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.activity.user.UserInfoBiz;
import com.xxfc.platform.order.contant.enumerate.CrosstownTypeEnum;
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.mq.OrderMQDTO;
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 com.xxfc.platform.vehicle.pojo.VehicleArrivalVo;
import com.xxfc.platform.vehicle.pojo.VehicleDepartureVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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

    @Autowired
    OrderVehicalCrosstownBiz orderVehicalCrosstownBiz;

    @Autowired
    OrderUserLicenseBiz orderUserLicenseBiz;

    @Autowired
    UserInfoBiz userInfoBiz;
    @Autowired
    VehicleFeign vehicleFeign;
    @Autowired
    UserFeign userFeign;
    @Autowired
    private OrderRentVehicleBiz orderRentVehicleBiz;

    @Autowired
    BaseOrderBiz baseOrderBiz;

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

    public List<OrderVehicleCrosstownDto> selectByOrderId(OrderVehicleCrosstownDto orderVehicleCrosstownDto) {
        List<OrderVehicleCrosstownDto> list = mapper.selectByOrderId(orderVehicleCrosstownDto);
        for(OrderVehicleCrosstownDto value : list) {
            if (value.getOrperaterId() != null) {
                ObjectRestResponse<AppUserDTO> objectRestResponse = userFeign.userDetailById(value.getOrperaterId());
                if(objectRestResponse.getData() != null) {
                    value.setUsername(objectRestResponse.getData().getNickname());
                }
            }
            if(value.getUserLicenseId() != null) {
                List<OrderUserLicense> orderUserLicenses = orderUserLicenseBiz.selectByIds(value.getUserLicenseId()).getData();
                if(orderUserLicenses != null && orderUserLicenses.size() > 0) {
                    value.setLicenseIdCard(orderUserLicenses.get(0).getLicenseIdCard());
                    value.setLicenseImg(orderUserLicenses.get(0).getLicenseImg());
                    value.setLicenseName(orderUserLicenses.get(0).getLicenseName());
                    value.setLicensePhone(orderUserLicenses.get(0).getLicensePhone());
                }
            }

        }
        return list;
    }

    @Transactional
    public ObjectRestResponse<OrderVehicleCrosstownDto> add(OrderVehicleCrosstownDto orderVehicleCrosstownDto) {
        log.info("添加的参数：orderVehicaleCrosstown = {}", orderVehicleCrosstownDto);
        UserDTO userDTO = userInfoBiz.getAdminUserInfo();
        if (userDTO == null) {
            return ObjectRestResponse.createFailedResult(508, "token is null or invalid");
        }
        if (orderVehicleCrosstownDto == null || orderVehicleCrosstownDto.getOrderId() == null ) {
            return ObjectRestResponse.createFailedResult(500, "参数为空！");
        }
        orderVehicleCrosstownDto.setOrperaterId(userDTO.getId());
        BaseOrder baseOrder = baseOrderBiz.selectById(orderVehicleCrosstownDto.getOrderId());
        if(baseOrder == null) {
            return ObjectRestResponse.createFailedResult(500,"订单不存在！");
        }
        OrderRentVehicleDetail orderRentVehicleDetail = orderRentVehicleBiz.selectById(baseOrder.getDetailId());
        //出车
        if(orderVehicleCrosstownDto.getType() == CrosstownTypeEnum.DEPARTURE.getCode()) { //交车
            VehicleDepartureVo vehicleDepartureVo = new VehicleDepartureVo();
            vehicleDepartureVo.setVehicleId(orderRentVehicleDetail.getVehicleId());
            vehicleDepartureVo.setDepartureBranchCompanyId(orderRentVehicleDetail.getStartCompanyId());
            vehicleDepartureVo.setExpectArrivalBranchCompanyId(orderRentVehicleDetail.getEndCompanyId());
            vehicleDepartureVo.setMileage(orderVehicleCrosstownDto.getMileage());
            vehicleDepartureVo.setUse("用户租车");
            vehicleDepartureVo.setBookRecordId(Integer.parseInt(orderRentVehicleDetail.getBookRecordId() + ""));
            vehicleDepartureVo.setUser(userDTO.getUsername());
            vehicleDepartureVo.setUserTel(userDTO.getTelPhone());
            vehicleDepartureVo.setCheckMan(orderVehicleCrosstownDto.getLicenseName());
            vehicleDepartureVo.setCheckManTel(orderVehicleCrosstownDto.getLicensePhone());
            vehicleFeign.departureBySmall(vehicleDepartureVo);
        } else if (orderVehicleCrosstownDto.getType() == CrosstownTypeEnum.ARRIVE.getCode()) { //还车
            VehicleArrivalVo vehicleArrivalVo = new VehicleArrivalVo();
            vehicleArrivalVo.setVehicleId(orderRentVehicleDetail.getVehicleId());
            vehicleArrivalVo.setArrivalBranchCompanyId(userDTO.getCompanyId());
            vehicleArrivalVo.setMileage(orderVehicleCrosstownDto.getMileage());
            vehicleArrivalVo.setRecycleMan(userDTO.getName());
            vehicleArrivalVo.setRecycleManTel(userDTO.getTelPhone());
            vehicleFeign.arrivalBySmall(vehicleArrivalVo);
        }
        //出车成功后修改订单状态
        List<OrderVehicleCrosstownDto> oldValue = orderVehicalCrosstownBiz.selectByOrderId(orderVehicleCrosstownDto);
        if (oldValue .size() == 1) {
            return ObjectRestResponse.succ(oldValue.get(0));
        } else if(oldValue .size() <= 0) {
            getOrderLicense(orderVehicleCrosstownDto);
            orderVehicalCrosstownBiz.insertSelective(orderVehicleCrosstownDto);
            handleOrderStatus(baseOrder, orderRentVehicleDetail);
            RestResponse<Vehicle> restResponse = vehicleFeign.findById(orderRentVehicleDetail.getVehicleId());
            if(restResponse.getData() != null) {
                orderVehicleCrosstownDto.setVehicleNumberPlat(restResponse.getData().getNumberPlate());
            }
            return ObjectRestResponse.succ(orderVehicleCrosstownDto);
        }
        return ObjectRestResponse.createDefaultFail();
    }

    private void handleOrderStatus(BaseOrder baseOrder, OrderRentVehicleDetail orderRentVehicleDetail) {
        //交车完成 设置订单状态为出行中
        if (baseOrder.getStatus() == 4) {
            //判断是否是今日交车
            baseOrder.setStatus(5);
            baseOrderBiz.updateSelectiveById(baseOrder);
        } else if (baseOrder.getStatus() == 5) {
            baseOrder.setStatus(6);
            baseOrderBiz.updateSelectiveById(baseOrder);
            baseOrderBiz.sendOrderMq(orderRentVehicleDetail, null, null, baseOrder, OrderMQDTO.ORDER_FINISH);
        }
    }

    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 + "");
        }
    }
}
