package com.sckw.transport.controller;

import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.model.ValiList;
import com.sckw.core.web.response.HttpResult;
import com.sckw.transport.model.dto.*;
import com.sckw.transport.service.DriverService;
import com.sckw.transport.service.KwtWaybillOrderService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author lfq
 * @desc 车辆运单
 * @date 2023/7/19 0019
 */
@Slf4j
@RestController
@RequestMapping("/kwtWaybillOrder")
public class KwtWaybillOrderController {

    @Autowired
    KwtWaybillOrderService waybillOrderService;

    @Autowired
    private DriverService driverService;

    /**
     * 司机端 根据当前登录 id获取派给当前id的车辆运单
     * @return
     */
    @RequestMapping(value = "/getWaybillDriverDetail", method = RequestMethod.GET)
    public HttpResult getWaybillDriverDetail(@RequestParam("id") Long id, @RequestParam("type") String type) {
        try {
            return driverService.getWaybillDriverDetail(id, type);
        } catch (Exception e) {
            log.error("派车给当前司机单据查询 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 司机app top统计
     * @param id 司机id
     * @return
     */
    @RequestMapping(value = "/statistics", method = RequestMethod.POST)
    public HttpResult statistics(@RequestParam("id") Long id) {
        try {
            return driverService.statistics(id);
        } catch (Exception e) {
            log.error("司机app top统计 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 承运订单-更换司机
     * @param dto
     * @return
     */
    @RequestMapping(value = "/updateDriver", method = RequestMethod.POST)
    public HttpResult updateDriver(@Validated @RequestBody UpdateDriverCarDTO dto) {
        try {
            return driverService.updateDriver(dto);
        } catch (Exception e) {
            log.error("承运订单更换司机 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 承运订单-更换车辆
     * @param dto
     * @return
     */
    @RequestMapping(value = "/updateCarCommit", method = RequestMethod.POST)
    public HttpResult updateCarCommit(@Validated @RequestBody UpdateDriverCarDTO dto) {
        try {
            return driverService.updateCarCommit(dto);
        } catch (Exception e) {
            log.error("承运订单更换司机 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * @param params {}
     * @desc 派车-趟次/循环
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/sendCar")
    public HttpResult sendCar(@RequestBody @Valid ValiList<SendCarDto1> params){
        if (CollectionUtils.isEmpty(params)) {
            return HttpResult.error("请选择委派车辆信息！");
        }
        return waybillOrderService.sendCar(params);
    }

    /**
     * @param ids 车辆运单主键ID，多个已逗号隔开
     * @desc 取消派车
     * @author zk
     * @date 2023/7/24
     **/
    @PostMapping("/cancelSendCar")
    public HttpResult cancelSendCar(@RequestBody List<Long> ids){
        return waybillOrderService.cancelSendCar(ids);
    }

    /**
     * @param params {}
     * @desc 确认接单
     * @author zk
     * @date 2023/7/24
     **/
    @PostMapping("/confirmSendCar")
    public HttpResult confirmSendCar(@RequestBody @Valid OperateSendCarDto params){
        return waybillOrderService.confirmSendCar(params);
    }

    /**
     * @param params {}
     * @desc 拒绝接单
     * @author zk
     * @date 2023/7/24
     **/
    @PostMapping("/refuseSendCar")
    public HttpResult refuseSendCar(@RequestBody @Valid OperateSendCarDto params){
        return waybillOrderService.refuseSendCar(params);
    }

    /**
     * @param params {}
     * @desc 确认出车
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/confirmDeparture")
    public HttpResult confirmDeparture(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderService.confirmDeparture(params);
    }

    /**
     * @param params {}
     * @desc 拒绝出车
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/refuseDeparture")
    public HttpResult refuseDeparture(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderService.refuseDeparture(params);
    }

    /**
     * @param params {}
     * @desc 到达装货地点
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/arriveLoadLocation")
    public HttpResult arriveLoadLocation(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderService.arriveLoadLocation(params);
    }

    /**
     * @param params {}
     * @desc 装货
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/loading")
    public HttpResult loading(@RequestBody @Valid WaybillOrderTicketDto params){
        return waybillOrderService.loading(params);
    }

    /**
     * @param params {}
     * @desc 到达卸货地点
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/arriveUnloadLocation")
    public HttpResult arriveUnloadLocation(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderService.arriveUnloadLocation(params);
    }

    /**
     * @param params {}
     * @desc 卸货
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/unloading")
    public HttpResult unloading(@RequestBody @Valid WaybillOrderTicketDto params){
        return waybillOrderService.unloading(params);
    }



    //取消派车（未接单前）
    //更换车辆（趟次派车(-出车前)/循环派车(接单后-出车前)）
    //更换司机（趟次派车(-卸货前)/循环派车(接单后-卸货前)）
    //确认接单-趟次/循环
    //拒绝接单-趟次/循环
    //确认/拒绝出车
    //到达装货地点 arriveLoadLocation
    //确认装货
    //到达卸货地点 arriveUnloadLocation
    //确认卸货
    //修改单证
    //单证审核

}
