package com.sckw.transport.controller;

import com.alibaba.fastjson.JSONObject;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.transport.model.dto.AddOrderDTO;
import com.sckw.transport.model.dto.OrderDTO;
import com.sckw.transport.model.dto.OrderFinishDTO;
import com.sckw.transport.service.ConsignOrderService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
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) {
        log.info("托运订单/承运订单-获取车辆运单 query {}", JSONObject.toJSONString(orderDto));
        try {
            return consignOrderService.getCarWaybillByOrder(orderDto);
        } catch (Exception e) {
            log.error("托运订单获取车辆运单失败:订单ids {},error {} ", JSONObject.toJSONString(orderDto), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单/承运订单-获取车辆运单-统计
     *
     * @param orderDto
     * @return
     */
    @RequestMapping(value = "/getCarWaybillByOrderCount", method = RequestMethod.POST)
    public HttpResult getCarWaybillByOrderCount(@Validated @RequestBody OrderDTO orderDto) {
        log.info("托运订单/承运订单-获取车辆运单-统计 query {}", JSONObject.toJSONString(orderDto));
        try {
            return consignOrderService.getCarWaybillByOrderCount(orderDto);
        } catch (Exception e) {
            log.error("托运订单/承运订单-获取车辆运单-统计: query {},error {} ", JSONObject.toJSONString(orderDto), 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 @NotBlank @RequestParam("id") String id) {
        return consignOrderService.getRejectReasonById(id);
    }

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

    /**
     * 托运订单-完结订单-页面数据查询
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/consignOrderFinishDetail", method = RequestMethod.GET)
    public HttpResult consignOrderFinishDetail(@RequestParam("orderId") @NotNull(message = "数据id不能为空") String orderId) {
        log.info("托运订单-完结订单-页面数据查询 传递参数信息:{}", orderId);
        try {
            return consignOrderService.consignOrderFinishDetail(orderId);
        } catch (Exception e) {
            log.error("托运订单-完结订单-页面数据查询 error :{}", 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());
        }
    }

    /**
     * 托运订单-新建订单
     *
     * @param addOrderDTO 页面传递参数
     * @return
     */
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST)
    public HttpResult addOrder(@Valid @RequestBody AddOrderDTO addOrderDTO) {
        try {
            return consignOrderService.addOrder(addOrderDTO);
        } catch (Exception e) {
            log.error("托运订单-新建订单 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单/承运订单-获取装卸货地址信息
     */
    @RequestMapping(name = "托运订单/承运订单-获取装卸货地址信息", value = "/getAddressList", method = RequestMethod.GET)
    public HttpResult getAddressList() {
        try {
            return consignOrderService.getLogisticsOrderAddressList();
        } catch (Exception e) {
            log.error("托运订单/承运订单-获取装卸货地址信息 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

}
