package com.xxfc.platform.order.rest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreClientToken;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.order.biz.BaseOrderBiz;
import com.xxfc.platform.order.biz.OrderUserLicenseBiz;
import com.xxfc.platform.order.biz.OrderVehicalCrosstownBiz;
import com.xxfc.platform.order.entity.BaseOrder;
import com.xxfc.platform.order.entity.OrderUserLicense;
import com.xxfc.platform.order.entity.OrderVehicaleCrosstown;
import com.xxfc.platform.order.pojo.order.OrderVehicleCrosstownDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/orderVehicle")
@Api(value = "交还车记录", tags = {"交还车记录"})
@Slf4j
public class OrderVehicleCrosstownController {

    @Autowired
    OrderVehicalCrosstownBiz orderVehicalCrosstownBiz;

    @Autowired
    OrderUserLicenseBiz orderUserLicenseBiz;

    @Autowired
    BaseOrderBiz baseOrderBiz;

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加交还车记录")
    @Transactional
    public ObjectRestResponse<OrderVehicleCrosstownDto> add(@RequestBody 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,"订单不存在！");
            }
            List<OrderVehicaleCrosstown> 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());
                    }
                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);
                }
                return ObjectRestResponse.succ(orderVehicleCrosstownDto);
            }
            return ObjectRestResponse.createDefaultFail();
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查询记录信息")
    public ObjectRestResponse<OrderVehicaleCrosstown> get(@PathVariable Integer id) {
        return ObjectRestResponse.succ(orderVehicalCrosstownBiz.get(id));
    }

    @GetMapping(value = "/getByOrderId")
    @ApiOperation(value = "根据订单Id获取所有记录信息")
    public ObjectRestResponse<List<OrderVehicaleCrosstown>> getByOrderId(OrderVehicleCrosstownDto orderVehicleCrosstownDto) {
        if (orderVehicleCrosstownDto == null) {
            return ObjectRestResponse.createFailedResult(500, "参数orderId为空");
        }
        return ObjectRestResponse.succ(orderVehicalCrosstownBiz.selectByOrderId(orderVehicleCrosstownDto));
    }


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

}
