package com.sckw.transport.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.sckw.core.annotation.RepeatSubmit;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.BaseResult;
import com.sckw.core.web.response.HttpResult;
import com.sckw.core.web.response.result.PageDataResult;
import com.sckw.transport.api.model.param.OrderPara;
import com.sckw.transport.model.LogisticsOrderPara;
import com.sckw.transport.model.dto.LogisticOrderSelectReq;
import com.sckw.transport.model.dto.LogisticsConsignmentDto;
import com.sckw.transport.model.dto.LogisticsOrderFinishDto;
import com.sckw.transport.model.dto.LogisticsOrderSubcontractDto;
import com.sckw.transport.model.param.AddLogisticsOrderParam;
import com.sckw.transport.model.param.CreateLogisticsOrderParam;
import com.sckw.transport.model.param.LogisticsOrderResp;
import com.sckw.transport.model.param.QueryLogisticsOrderReq;
import com.sckw.transport.model.vo.*;
import com.sckw.transport.service.KwtLogisticsConsignmentService;
import com.sckw.transport.service.KwtTransportCommonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfdc
 * @desc 物流订单Controller-v4.2
 * @date 2024-03-11 11:06:06
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/kwtLogisticsOrder")
@Tag(name = "物流订单相关接口")
public class KwtLogisticsOrderController {

    @Autowired
    KwtLogisticsConsignmentService logisticsConsignmentService;

    @Autowired
    KwtTransportCommonService transportCommonService;


    /**
     * 采购订单/销售订单-物流托运-V4.2
     *
     * @param logisticsConsignmentDto 页面请求参数
     * @return 响应结果
     */
    @Validated
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @RequestMapping(value = "/consignment", method = RequestMethod.POST)
    public HttpResult logisticsOrderConsignment(@Valid @RequestBody LogisticsConsignmentDto logisticsConsignmentDto) {
        log.info("采购订单/销售订单-物流托运传递参数信息:{}", JSONObject.toJSONString(logisticsConsignmentDto));
        try {
            return logisticsConsignmentService.logisticsOrderConsignment(logisticsConsignmentDto);
        } catch (Exception e) {
            log.error("采购订单/销售订单-物流托运生成托运订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     *
     */
    @Validated
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Operation(summary = "原矿运输下单")
    public HttpResult create(@Valid @RequestBody CreateLogisticsOrderParam addLogisticsOrderParam) {
        log.info("转运订单传递参数信息:{}", JSONObject.toJSONString(addLogisticsOrderParam));
        try {
            return logisticsConsignmentService.createLogisticsOrder(addLogisticsOrderParam);
        } catch (Exception e) {
            log.error("转运订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }
    /**
     * 承运订单/托运订单-自建物流订单 V4.2
     *
     * @param addLogisticsOrderParam 页面请求参数
     * @return 响应结果
     */
    @Validated
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public HttpResult addLogisticsOrder(@Valid @RequestBody AddLogisticsOrderParam addLogisticsOrderParam) {
        log.info("承运订单/托运订单-自建物流订单传递参数信息:{}", JSONObject.toJSONString(addLogisticsOrderParam));
        try {
            return logisticsConsignmentService.addLogisticsOrder(addLogisticsOrderParam);
        } catch (Exception e) {
            log.error("承运订单/托运订单-自建物流订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 物流订单-分包托运-V4.2
     *
     * @param logisticsOrderSubcontractDto 页面请求参数
     * @return 响应结果
     */
    @Validated
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @RequestMapping(value = "/subcontract", method = RequestMethod.POST)
    public HttpResult logisticsOrderSubcontract(@Valid @RequestBody LogisticsOrderSubcontractDto logisticsOrderSubcontractDto) {
        log.info("物流订单-分包托运传递参数信息:{}", JSONObject.toJSONString(logisticsOrderSubcontractDto));
        try {
            return logisticsConsignmentService.logisticsOrderSubcontract(logisticsOrderSubcontractDto);
        } catch (Exception e) {
            log.error("物流订单-分包托运 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 物流订单-完结订单-V4.2
     *
     * @param logisticsOrderSubcontractDto 页面请求参数
     * @return 响应结果
     */
    @Validated
    @RepeatSubmit(interval = 1000, message = "前方拥堵，请稍后尝试")
    @RequestMapping(value = "/logisticsOrderFinish", method = RequestMethod.POST)
    public HttpResult logisticsOrderFinish(@Valid @RequestBody LogisticsOrderFinishDto logisticsOrderSubcontractDto) {
        log.info("物流订单-完结订单传递参数信息:{}", JSONObject.toJSONString(logisticsOrderSubcontractDto));
        try {
            return transportCommonService.logisticsOrderFinish(logisticsOrderSubcontractDto);
        } catch (Exception e) {
            log.error("物流订单-完结订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 分页查询物流订单
     */
    @PostMapping(value = "/queryLogisticsOrderByPage")
    @Operation(summary = "分页查询物流订单", description = "分页查询物流订单")
    public BaseResult<PageDataResult<LogisticsOrderResp>> queryLogisticsOrderByPage(@RequestBody QueryLogisticsOrderReq req) {
        return BaseResult.success(logisticsConsignmentService.queryLogisticsOrderByPage(req));
    }

    /**
     * 获取物流订单详情
     */
    @PostMapping(value = "/getLogisticsOrderDetail")
    @Operation(summary = "获取物流订单详情", description = "获取物流订单详情")
    public BaseResult<LogisticsOrderDetailResp> getLogisticsOrderDetail(@RequestBody @Valid LogisticsOrderReq req) {
        return BaseResult.success(logisticsConsignmentService.getLogisticsOrderDetail(req));
    }

    /**
     * 根据物流订单查询已派运力
     */
    @Operation(summary = "根据物流订单查询已派运力", description = "根据物流订单查询已派运力")
    @PostMapping(value = "/getLogisticsOrderDispatch")
    public BaseResult<PageDataResult<LogisticsOrderDispatchResp>> getLogisticsOrderDispatch(@RequestBody @Valid LogisticsOrderReq req) {
        return BaseResult.success(logisticsConsignmentService.getLogisticsOrderDispatch(req));
    }

    /**
     * 分页查询已派运力
     */
    @PostMapping("/pageCapacity")
    @Operation(summary = "分页查询已派运力", description = "分页查询已派运力")
    public BaseResult<PageDataResult<CapacityVo>> pageCapacity(@RequestBody @Valid CapacityReq req) {
        return BaseResult.success(logisticsConsignmentService.pageCapacity(req));
    }


    /**
     * 关闭启用已派运力
     */

    @PostMapping("/closeCapacity")
    @Operation(summary = "关闭启用已派运力", description = "关闭启用已派运力")
    public BaseResult<Boolean> closeCapacity(@RequestBody @Valid DeleteCapacityReq req) {
        return BaseResult.success(logisticsConsignmentService.closeCapacity(req));
    }

    /**
     * 派车
     */
    @PostMapping("/dispatchCar")
    @Operation(summary = "派车", description = "派车")
    public BaseResult<DispatchCarResp> dispatchCar(@RequestBody @Valid DispatchCarReq req) {
        return BaseResult.success(logisticsConsignmentService.dispatchCar(req));
    }

    /**
     * 完结订单
     */
    @PostMapping("/logisticOrderFinish")
    @Operation(summary = "完结订单", description = "完结订单")
    public BaseResult<Boolean> logisticOrderFinish(@RequestBody @Valid OrderFinishReq req) {
        return BaseResult.success(logisticsConsignmentService.logisticOrderFinish(req));
    }

    /**
     * 状态统计
     */
    @PostMapping("/orderStatusStatistics")
    @Operation(summary = "状态统计", description = "状态统计")
    public BaseResult<OrderStatusStatisticsResp> orderStatusStatistics(@RequestBody QueryLogisticsOrderReq req) {
        return BaseResult.success(logisticsConsignmentService.orderStatusStatistics(req));
    }

    /**
     * 查询物流订单托运/承运企业（unitType：1托运、2承运）
     */
    @GetMapping("/orderUnits")
    @Operation(summary = "查询物流订单托运/承运企业")
    public BaseResult<List<LogisticsOrderUnitResp>> orderUnits() {
        return BaseResult.success(transportCommonService.queryOrderUnits());
    }


    @GetMapping(value = "/base")
    @Operation(summary = "分页查询物流订单概要信息", description = "分页查询物流订单概要信息")
    public HttpResult base(@ParameterObject OrderPara orderPara) {
        LogisticsOrderPara tradeOrderPara = new LogisticsOrderPara();
        tradeOrderPara.setStartTime(LocalDateTimeUtil.parse(orderPara.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        tradeOrderPara.setEndTime(LocalDateTimeUtil.parse(orderPara.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        tradeOrderPara.setUserId(orderPara.getUserId());
        return HttpResult.ok(logisticsConsignmentService.queryBaseList(tradeOrderPara));
    }


    /**
     * 运单号下拉列表（支持输入匹配）
     */
    @PostMapping("/waybillOrderSelect")
    @Operation(summary = "物流订单号下拉列表", description = "支持根据运单号输入匹配，返回运单号、车牌号、司机信息等")
    public BaseResult<List<LogisticOrderSelectOptionVo>> getLogisticOrderSelectList(@RequestBody LogisticOrderSelectReq req) {

        return BaseResult.success(logisticsConsignmentService.getLogisticOrderSelectList(req));
    }
}
