package com.xxfc.platform.vehicle.rest;

import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.xxfc.platform.vehicle.biz.VehicleActiveService;
import com.xxfc.platform.vehicle.biz.VehicleBiz;
import com.xxfc.platform.vehicle.common.RestResponse;
import com.xxfc.platform.vehicle.constant.ResCode.ResCode;
import com.xxfc.platform.vehicle.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("active")
public class VehicleActiveController {

    @Autowired
    VehicleActiveService vehicleActiveService;
    @Autowired
    VehicleBiz vehicleBiz;

    /**
     * 发车
     *
     * @return
     */
    @PostMapping("/departure")
    public RestResponse departure(@RequestBody VehicleDepartureVo departureVo) {
        if (departureVo == null || departureVo.getVehicleId() == null
                || departureVo.getMileage() == null || departureVo.getDepartureBranchCompanyId() == null
                || departureVo.getExpectArrivalBranchCompanyId() == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }

        vehicleActiveService.departure(departureVo);
        return RestResponse.suc();
    }

    /**
     * 发车
     *
     * @return
     */
    @PostMapping("small/departure")
    @IgnoreUserToken
    public RestResponse departureBySmall(@RequestBody VehicleDepartureVo departureVo) {
        if (departureVo == null || departureVo.getVehicleId() == null
                || departureVo.getMileage() == null || departureVo.getDepartureBranchCompanyId() == null
                || departureVo.getExpectArrivalBranchCompanyId() == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        vehicleActiveService.departure(departureVo);
        return RestResponse.suc();
    }

    /**
     * 还车
     *
     * @param arrivalVo
     * @return
     */
    @PostMapping("small/arrival")
    public RestResponse arrivalBySmall(@RequestBody VehicleArrivalVo arrivalVo) {
        if (arrivalVo == null || arrivalVo.getVehicleId() == null
                || arrivalVo.getMileage() == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        vehicleActiveService.arrival(arrivalVo);
        return RestResponse.suc();
    }

    /**
     * 还车
     *
     * @param arrivalVo
     * @return
     */
    @PostMapping("arrival")
    public RestResponse arrival(@RequestBody VehicleArrivalVo arrivalVo) {
        if (arrivalVo == null || arrivalVo.getVehicleId() == null
                || arrivalVo.getMileage() == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        vehicleActiveService.arrival(arrivalVo);
        return RestResponse.suc();
    }


    @GetMapping("/getOne")
    public ObjectRestResponse selectByRecordId(Long bookRecordId) {
        return vehicleActiveService.selectDepartureLogByBookRecordId(bookRecordId);
    }



    /**
     * 保养
     *
     * @param vehicleUpkeepVo
     * @return
     */
    @PostMapping("upkeep")
    public RestResponse upkeep(@RequestBody VehicleUpkeepVo vehicleUpkeepVo) {
        if (vehicleUpkeepVo.getVehicleId() == null || vehicleUpkeepVo.getOperator() == null || vehicleUpkeepVo.getAmount() == null ||
                vehicleUpkeepVo.getApprovers() == null || vehicleUpkeepVo.getBranchCompanyId() == null || vehicleUpkeepVo.getMileage() == null ||
                vehicleUpkeepVo.getUpkeepItems() == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        if (vehicleUpkeepVo.getDate() == null) {
            vehicleUpkeepVo.setDate(new Date());
        }
        vehicleActiveService.upkeep(vehicleUpkeepVo);
        return RestResponse.suc();
    }

    /**
     * 保养结束
     *
     * @param vehicleUpkeepEndVo
     * @return
     */
    @PostMapping("upkeepEnd")
    public RestResponse upkeepEnd(@RequestBody VehicleUpkeepEndVo vehicleUpkeepEndVo) {
        if (vehicleUpkeepEndVo.getVehicleId() == null) {
            return RestResponse.codeAndMessage(ResCode.INVALID_REST_REQ_PARAM.getCode(),
                    ResCode.INVALID_REST_REQ_PARAM.getDesc());
        }
        vehicleActiveService.upkeepEnd(vehicleUpkeepEndVo);
        return RestResponse.suc();
    }

    /**
     * 维修
     *
     * @return
     */
    public RestResponse repair(@RequestBody VehicleRepairVo vehicleRepairVo) {

        return RestResponse.suc();
    }

    /**
     * 维修结束
     *
     * @return
     */
    public RestResponse repairEnd() {
        return RestResponse.suc();
    }

}
