package com.sckw.transport.controller;

import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.transport.model.dto.OrderDTO;
import com.sckw.transport.model.dto.OrderFinishDTO;
import com.sckw.transport.service.ConsignOrderService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author lfdc
 * @description 托运订单controller
 * @date 2023-06-26 16:06:04
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/kwtConsignOrder")
public class ConsignOrderController {

    @Autowired
    ConsignOrderService consignOrderService;


    /**
     * 托运订单/承运订单-获取车辆运单
     *
     * @param orderDto
     * @return
     */
    @RequestMapping(value = "/getCarWaybillByOrder", method = RequestMethod.POST)
    public HttpResult getCarWaybillByOrder(@Validated @RequestBody OrderDTO orderDto) {
        try {
            return consignOrderService.getCarWaybillByOrder(orderDto);
        } catch (Exception e) {
            log.error("托运订单获取车辆运单失败:订单id {},error {} ", orderDto.getId(), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单查看驳回原因
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getRejectReason", method = RequestMethod.GET)
    public HttpResult getRejectReasonById(@Validated @NotNull @RequestParam("id") String id) {
        return consignOrderService.getRejectReasonById(id);
    }

    /**
     * 托运订单获取车辆信息
     *
     * @param orderDto
     * @return
     */
    @RequestMapping(value = "/getCarListByOrder", method = RequestMethod.POST)
    public HttpResult getCarListByOrder(@Validated @RequestBody OrderDTO orderDto) {
        try {
            return consignOrderService.getCarListByOrder(orderDto);
        } catch (Exception e) {
            log.error("托运订单获取车辆运单失败:订单id {},error {} ", orderDto.getId(), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单-完结订单
     *
     * @param orderFinishDTO
     * @return
     */
    @RequestMapping(value = "/commitConsignOrderFinish", method = RequestMethod.POST)
    public HttpResult commitConsignOrderFinish(@Validated @RequestBody OrderFinishDTO orderFinishDTO) {
        try {
            return consignOrderService.commitConsignOrderFinish(orderFinishDTO);
        } catch (Exception e) {
            log.error("托运订单-完结订单失败:订单id {},error {} ", orderFinishDTO.getId(), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

}
