package com.sckw.transport.controller;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.exception.SystemException;
import com.sckw.core.model.constant.Global;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.core.web.model.ValiList;
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.excel.utils.ExcelUtil;
import com.sckw.payment.api.model.dto.common.R;
import com.sckw.transport.api.model.param.AddLogisticOrderParam;
import com.sckw.transport.dubbo.TransportServiceImpl;
import com.sckw.transport.model.dto.*;
import com.sckw.transport.model.dto.WaybillOrderSelectReq;
import com.sckw.transport.model.param.TradeOrderTransportQueryReq;
import com.sckw.transport.model.param.WaybillTransportQueryReq;
import com.sckw.transport.model.param.WaybillOrderNodeReq;
import com.sckw.transport.model.param.WaybillOrderReq;
import com.sckw.transport.model.param.WaybillOrderResp;
import com.sckw.transport.model.vo.*;
import com.sckw.transport.service.KwtWaybillOrderService;
import com.sckw.transport.service.KwtWaybillOrderV1Service;
import com.sckw.transport.service.TradeOrderTransportInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lfq
 * @desc 车辆运单
 * @date 2023/7/19 0019
 */
@Slf4j
@RestController
@RequestMapping("/kwtWaybillOrder")
@Tag(name = "运单相关接口")
public class KwtWaybillOrderController {

    @Autowired
    KwtWaybillOrderService waybillOrderService;
    @Autowired
    KwtWaybillOrderV1Service waybillOrderV1Service;
    @Autowired
    TransportServiceImpl transportService;
    @Autowired
    TradeOrderTransportInfoService tradeOrderTransportInfoService;

    /**
     * @param params 请求参数
     * @desc 统计
     * @author zk
     * @date 2024/3/8
     **/
    @PostMapping("/statistics")
    public HttpResult statistics(@RequestBody WaybillOrderQueryDto params) throws SystemException {
        // 运单创建人或有关授权用户
        List<Long> userIds = LoginUserHolder.getAuthUserIdList();
        params.setUserIds(userIds);
        params.setEntId(LoginUserHolder.getEntId());
        return HttpResult.ok(waybillOrderV1Service.statistics(params));
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @desc 分页查询
     * @author zk
     * @date 2024/3/8
     **/
    @PostMapping("/select")
    public HttpResult findListPage(@RequestBody WaybillOrderQueryDto params) throws SystemException {
        // 运单创建人或有关授权用户
        List<Long> userIds = LoginUserHolder.getAuthUserIdList();
        params.setUserIds(userIds);
        params.setEntId(LoginUserHolder.getEntId());
        // 设置分页参数
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<WaybillOrderSelectVo> list = waybillOrderV1Service.findListPage(params);
        PageResult result = PageHelperUtil.getPageResult(new PageInfo<>(list));
        return HttpResult.ok(result);
    }

    /**
     * @param params 查询参数
     * @desc 司机导出
     * @author zk
     * @date 2023/07/11
     **/
    @PostMapping(value = "/export", produces = MediaType.APPLICATION_JSON_VALUE)
    public void export(@RequestBody WaybillOrderQueryDto params, HttpServletResponse response) {
        // 运单创建人或有关授权用户
        List<Long> userIds = LoginUserHolder.getAuthUserIdList();
        params.setUserIds(userIds);
        params.setEntId(LoginUserHolder.getEntId());
        /*查询数据*/
        List<WaybillOrderSelectVo> list = waybillOrderV1Service.findListPage(params);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认！");
        }
        //数据处理
        List<WaybillOrderExportVo> orders = new ArrayList<>();
        for (WaybillOrderSelectVo select:list) {
            WaybillOrderExportVo exportVo = new WaybillOrderExportVo();
            exportVo.setLoadName(String.join(Global.COMMA, select.getLoadName()));
            exportVo.setLoadAddress(String.join(Global.COMMA, select.getLoadAddress()));
            exportVo.setUnloadName(String.join(Global.COMMA, select.getUnloadName()));
            exportVo.setUnloadAddress(String.join(Global.COMMA, select.getUnloadAddress()));
            orders.add(exportVo);
        }
        ExcelUtil.downData(response, WaybillOrderExportVo.class, orders);
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @desc 分页查询
     * @author zk
     * @date 2024/3/8
     **/
    @PostMapping("/ndex")
    public HttpResult findNdexPage(@RequestBody WaybillOrderQueryDto params) throws SystemException {
        // 运单创建人或有关授权用户
        List<Long> userIds = LoginUserHolder.getAuthUserIdList();
        params.setUserIds(userIds);
        params.setEntId(LoginUserHolder.getEntId());
        return HttpResult.ok(waybillOrderV1Service.findNdexPage(params));
    }

    /**
     * @param wOrderId 运单ID
     * @desc 运单详情
     * @author zk
     * @date 2024/3/13
     **/
    @GetMapping("/waybillDetail")
    public HttpResult waybillDetail(@RequestParam("wOrderId") Long wOrderId){
        return waybillOrderV1Service.waybillDetail(wOrderId);
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @desc 分页查询(子订单)
     * @author zk
     * @date 2024/3/8
     **/
    @PostMapping("/subtaskSelect")
    public HttpResult subtaskSelect(@RequestBody WaybillSubtaskOrderQueryDto params) throws SystemException {
        return HttpResult.ok(waybillOrderV1Service.subtaskSelect(params));
    }

    /**
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @desc 分页查询(运力)
     * @author zk
     * @date 2024/3/8
     **/
    @PostMapping("/transportationCapacitySelect")
    public HttpResult transportationCapacity(@RequestBody WaybillSubtaskOrderQueryDto params) throws SystemException {
        return HttpResult.ok(waybillOrderV1Service.transportationCapacitySelect(params));
    }

    /**
     * @param wSubtaskId 运单子单ID
     * @desc 运单详情
     * @author zk
     * @date 2024/3/13
     **/
    @GetMapping("/waybillSubtaskDetail")
    public HttpResult waybillSubtaskDetail(@RequestParam("wSubtaskId") Long wSubtaskId){
        return waybillOrderV1Service.waybillSubtaskDetail(wSubtaskId);
    }

    /**
     * @desc 获取企业地址信息
     * @author zk
     * @date 2023/9/1
     **/
    @GetMapping("/findAddressList")
    public HttpResult findAddressList(){
        return HttpResult.ok(waybillOrderV1Service.findAddressList());
    }

    /**
     * @desc 统计司机任务数(待接单/出车/执行/完结)
     * @author zk
     * @date 2023/8/1
     **/
    @GetMapping("/findCountByDriverV1")
    public HttpResult findCountByDriver(HttpServletRequest request){
        Long driverId = LoginUserHolder.getUserId();
        Map<String, Object> count = waybillOrderV1Service.findStatisticsCountByDriver(driverId, request);
        return HttpResult.ok(count);
    }

    /**
     * @param params {driverId 司机ID, month月份}
     * @desc 统计司机任务数（任务数/委派量/装货量/卸货量/亏吨量/亏吨扣款）
     * @author zk
     * @date 2023/8/1
     **/
    @PostMapping("/findSumByDriver")
    public HttpResult findSumByDriver(@RequestBody Map<String, Object> params, HttpServletRequest request){
        try {
            params.put("driverId", LoginUserHolder.getUserId());
            Map<String, Object> count = waybillOrderService.findStatisticsSumByDriver(params, request);
            return HttpResult.ok(count);
        } catch (Exception e) {
            log.error("findSumByDriver error query:{},errorMessage:{}", JSONObject.toJSONString(params),e.getMessage(),e);
            return HttpResult.error(e.getMessage());
        }
    }

    /**
     * @param params {driverId 司机ID, busStatus  1待接单/2出车/3执行/4完结, month月份, startTime 开始实际, endTime 结束时间}
     * @desc 查询司机任务列表-待接单/出车/执行/完结
     * @author zk
     * @date 2023/8/1
     **/
    @PostMapping("/findWaybillOrderByDriverV1")
    public HttpResult findWaybillOrderByDriver(@RequestBody Map<String, Object> params, HttpServletRequest request){
        /*校验**/
        if (StringUtils.isBlank(params.get("busStatus"))) {
            return HttpResult.error("请选择业务状态！");
        }
        // 设置分页参数
        PageHelper.startPage(PageResult.getPage(params), PageResult.getPageSize(params));
        List<JSONObject> list = waybillOrderV1Service.findWaybillOrderByDriver(params, request);
        PageResult pageResult = PageHelperUtil.getPageResult(new PageInfo<>(list));
        return HttpResult.ok(pageResult);
    }

    /**
     * @param params 请求参数
     * @desc 变更司机
     * @author zk
     * @date 2023/8/2
     **/
    @PostMapping("/changeDriver")
    public HttpResult changeDriver(@RequestBody @Valid ChangeDriverDto params){
        return waybillOrderV1Service.changeDriver(params);
    }

    /**
     * @param params 请求参数
     * @desc 变更车辆
     * @author zk
     * @date 2023/8/2
     **/
    @PostMapping("/changeTruck")
    public HttpResult changeTruck(@RequestBody @Valid ChangeTruckDto params){
        return waybillOrderV1Service.changeTruck(params);
    }

    /**
     * @param wOrderId 运单ID
     * @desc 变更车辆/司机查询
     * @author zk
     * @date 2023/8/2
     **/
    @GetMapping("/changeWaybillOrder")
    public HttpResult changeWaybillOrder(@RequestParam("wOrderId") Long wOrderId){
        return waybillOrderV1Service.changeWaybillOrder(wOrderId);
    }

    /**
     * 派车-查询已派车量、待运量数据(4.2废弃)
     * @param orderId
     * @return
     */
    @RequestMapping(name = "查询已派车量、待运量数据",path = "/getLogisticsAmountByOrderId",method = RequestMethod.GET)
    public HttpResult getLogisticsAmountByOrderId(@NotBlank @RequestParam("orderId") String orderId){
        log.info("查询已排量、待运量数据 query :{}",orderId);
        try {
            return waybillOrderService.getLogisticsAmountByOrderId(orderId);
        } 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 params {}
     * @desc 派车-趟次/循环
     * @author zk
     * @date 2024/3/1
     **/
    @PostMapping("/sendCarV1")
    public HttpResult sendCarV1(@RequestBody @Valid SendCarDto2 params){
        return waybillOrderV1Service.sendCarV1(params);
    }


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

    /**
     * @param ids 车辆运单主键ID，多个已逗号隔开
     * @desc 取消派车
     * @author zk
     * @date 2024/3/7
     **/
    @PostMapping("/cancelSendCarV1")
    public HttpResult cancelSendCarV1(@RequestBody List<Long> ids){
        return waybillOrderV1Service.cancelSendCarV1(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 2024/3/7
     **/
    @PostMapping("/confirmSendCarV1")
    public HttpResult confirmSendCarV1(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderV1Service.confirmSendCarV1(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 2024/3/7
     **/
    @PostMapping("/refuseSendCarV1")
    public HttpResult refuseSendCarV1(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderV1Service.refuseSendCarV1(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 2024/3/7
     **/
    @PostMapping("/confirmDepartureV1")
    public HttpResult confirmDepartureV1(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderV1Service.confirmDepartureV1(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 2024/3/7
     **/
    @PostMapping("/refuseDepartureV1")
    public HttpResult refuseDepartureV1(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderV1Service.refuseDepartureV1(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 2024/3/7
     **/
    @PostMapping("/arriveLoadLocationV1")
    public HttpResult arriveLoadLocationV1(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderV1Service.arriveLoadLocationV1(params);
    }


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

    /**
     * @param params {}
     * @desc 装货
     * @author zk
     * @date 2024/3/7
     **/
    @PostMapping("/loadingV1")
    public HttpResult loadingV1(@RequestBody @Valid WaybillOrderTicketV4Dto params){
        return waybillOrderV1Service.loadingV1(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 2024/3/7
     **/
    @PostMapping("/arriveUnloadLocationV1")
    public HttpResult arriveUnloadLocationV1(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderV1Service.arriveUnloadLocationV1(params);
    }


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

    /**
     * @param params {}
     * @desc 卸货
     * @author zk
     * @date 2024/3/7
     **/
    @PostMapping("/unloadingV1")
    public HttpResult unloadingV1(@RequestBody @Valid WaybillOrderTicketV4Dto params){
        return waybillOrderV1Service.unloadingV1(params);
    }


    /**
     * @param params {}
     * @desc 修改单证
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/editTicket")
    public HttpResult editTicket(@RequestBody @Valid WaybillOrderTicketDto params){
        return waybillOrderService.editTicket(params);
    }

    /**
     * @param params {}
     * @desc 修改单证-装卸货一起修改
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/editTicketV1")
    public HttpResult editTicketV1(@RequestBody @Valid WaybillOrderTicketV1Dto params){
        return waybillOrderService.editTicket(params);
    }

    /**
     * @param params {}
     * @desc 修改单证-装卸货一起修改
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/editTicketV2")
    public HttpResult editTicketV1(@RequestBody @Valid WaybillOrderTicketV3Dto params){
        return waybillOrderService.editTicketV1(params);
    }

    /**
     * @param params {}
     * @desc 修改单证
     * @author zk
     * @date 2024/3/7
     **/
    @PostMapping("/editTicketV3")
    public HttpResult editTicketV3(@RequestBody @Valid List<WaybillOrderEditTicketDto> params){
        if (CollectionUtils.isEmpty(params)) {
            return HttpResult.error("榜单信息不存在，请确认！");
        }
        return waybillOrderV1Service.editTicketV2(params);
    }

    /**
     * @param wOrderId 运单ID
     * @desc 修改单证查询运单
     * @author zk
     * @date 2023/8/2
     **/
    @GetMapping("/ticketWaybillOrder")
    public HttpResult ticketWaybillOrder(@RequestParam("wOrderId") Long wOrderId){
        return waybillOrderV1Service.ticketWaybillOrder(wOrderId);
    }

    /**
     * @param wOrderId 运单ID
     * @desc 运单榜单信息
     * @author zk
     * @date 2023/8/2
     **/
    @GetMapping("/ticketApproval")
    public HttpResult ticketApproval(@RequestParam("wOrderId") Long wOrderId){
        return waybillOrderV1Service.ticketApproval(wOrderId);
    }

    /**
     * @param wAddressId 运单地址ID
     * @desc 运单榜单信息
     * @author zk
     * @date 2023/8/2
     **/
    @GetMapping("/ticketInfo")
    public HttpResult ticketInfo(@RequestParam("wAddressId") Long wAddressId){
        return waybillOrderV1Service.ticketInfo(wAddressId);
    }


    /**
     * @param params {}
     * @desc 单证审核
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/approvalTicket")
    public HttpResult approvalTicket(@RequestBody @Valid WaybillOrderApprovalDto params){
        return waybillOrderService.approvalTicket(params);
    }

    /**
     * @param params {}
     * @desc 单证审核-驳回
     * @author zk
     * @date 2024/3/7
     **/
    @PostMapping("/approvalReject")
    public HttpResult approvalReject(@RequestBody @Valid ValiList<WaybillOrderTicketApprovalRejectDto> params){
        return waybillOrderV1Service.approvalReject(params);
    }


    /**
     * @param params {}
     * @desc 单证审核（编辑单证）
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/approvalEditTicket")
    public HttpResult approvalEditTicket(@RequestBody @Valid WaybillOrderTicketApprovalV1Dto params){
        return waybillOrderService.approvalEditTicketV1(params);
    }

    /**
     * @param params {}
     * @desc 单证审核（编辑单证）
     * @author zk
     * @date 2024/3/7
     **/
    @PostMapping("/approvalEditTicketV1")
    public HttpResult approvalEditTicketV1(@RequestBody @Valid WaybillOrderTicketApprovalV2Dto params){
        return waybillOrderV1Service.approvalEditTicketV1(params);
    }

    /**
     * @param params {}
     * @desc 查询运单信息
     * @author zk
     * @date 2024/3/7
     **/
    @PostMapping("/queryBillOrderInfo")
    @Operation(summary = "查询运单信息")
    public BaseResult<WaybillOrderDetailVo> queryBillOrderInfo(@RequestBody WaybillOrderRequest params){
        WaybillOrderDetailVo orderDetailVo = waybillOrderV1Service.queryBillOrderInfo(params);
        return BaseResult.success(orderDetailVo);
    }

    /**
     * 分页查询运单信息
     */
    @PostMapping("/findBillOrderListPage")
    @Operation(summary = "分页查询运单信息")
    public BaseResult<PageDataResult<WaybillOrderResp>> findBillOrderListPage(@RequestBody WaybillOrderReq req) {
        return BaseResult.success(waybillOrderV1Service.findBillOrderListPage(req));
    }

    /**
     * 查询运单详情信息
     */
    @PostMapping("/queryBillOrderDetail")
    @Operation(summary = "查询运单详情信息")
    public BaseResult<WaybillOrderDetailResp> queryBillOrderDetail(@RequestBody WaybillOrderRequest req) {
        return BaseResult.success(waybillOrderV1Service.queryBillOrderDetail(req));
    }
    /**
     * 运单审核，完善单证接口
     */
    @PostMapping("/reviewWaybillOrder")
    @Operation(summary = "运单审核，完善单证接口")
    public BaseResult<Boolean> reviewWaybillOrder(@RequestBody @Valid ReviewWaybillOrderReq req) {
        return BaseResult.success(waybillOrderV1Service.reviewWaybillOrder(req));
    }

    /**
     * 运单状态统计
     */
    @PostMapping("/statisticsWaybillOrder")
    @Operation(summary = "运单状态统计")
    public BaseResult<StatisticsWaybillResp> statisticsWaybillOrder(@RequestBody WaybillOrderReq req) {
        return BaseResult.success(waybillOrderV1Service.statisticsWaybillOrder(req));
    }

    /**
     * 查询运单节点轨迹时间线
     *
     * @param req 请求参数
     * @return 节点轨迹列表
     * @author system
     * @date 2025/06/24
     */
    @PostMapping("/queryWaybillOrderNodeTimeline")
    @Operation(summary = "查询运单节点轨迹时间线")
    public BaseResult<WaybillOrderNodeVo> queryWaybillOrderNodeTimeline(@RequestBody @Valid WaybillOrderNodeReq req) {
        return BaseResult.success(waybillOrderV1Service.queryWaybillOrderNodeTimeline(req));
    }

    @PostMapping("/addtr")
    @Operation(summary = "查询运单节点轨迹时间线")
    public void addtr(@RequestBody AddLogisticOrderParam req) {
         transportService.addLogisticOrder(req);
    }

    /**
     * 根据运单号模糊查询运单ID和运单号列表
     *
     * @param req 查询请求参数
     * @return 运单列表
     * @author system
     * @date 2025-12-01
     */
    @PostMapping("/queryWaybillOrderList")
    @Operation(summary = "根据运单号模糊查询运单ID和运单号列表")
    public BaseResult<List<WaybillOrderSimpleVo>> queryWaybillOrderList(@RequestBody WaybillOrderQueryByNoReq req) {
        return BaseResult.success(waybillOrderV1Service.queryWaybillOrderListByWOrderNo(req.getWOrderNo()));
    }

    /**
     * 查询运单车辆ID和车牌号列表
     *
     * @return 车辆列表
     * @author system
     * @date 2025-12-02
     */
    @GetMapping("/queryTruckList")
    @Operation(summary = "查询运单车辆ID和车牌号列表")
    public BaseResult<List<WaybillOrderTruckVo>> queryTruckList() {
        return BaseResult.success(waybillOrderV1Service.queryTruckList());
    }


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

        return BaseResult.success(waybillOrderService.getWaybillOrderSelectList(req));
    }

    @GetMapping("/saleSum")
    @Operation(summary = "主要商品销售数据")
    public R<WaybillOrderSaleVo> saleSum() {
        return R.ok(waybillOrderV1Service.saleSum());
    }



    /**
     * 根据贸易订单号查询运输相关信息。
     *
     * @param req 查询请求
     * @return 运输相关信息
     */
    @PostMapping("/tradeOrder/transportInfo")
    @Operation(summary = "根据贸易订单号查询运输相关信息")
    public BaseResult<TradeOrderTransportInfoResp> queryTransportInfoByTradeOrderNo(@RequestBody @Valid TradeOrderTransportQueryReq req) {
        try {
            return BaseResult.success(tradeOrderTransportInfoService.queryByTradeOrderNo(req));
        } catch (IllegalArgumentException e) {
            log.warn("根据贸易订单号查询运输相关信息参数异常，tradeOrderNo:{}, errorMessage:{}",
                    req == null ? null : req.getTradeOrderNo(), e.getMessage());
            return BaseResult.failed(HttpStatus.PARAMETERS_MISSING_CODE, e.getMessage());
        } catch (Exception e) {
            log.error("根据贸易订单号查询运输相关信息失败，tradeOrderNo:{}, errorMessage:{}",
                    req == null ? null : req.getTradeOrderNo(), e.getMessage(), e);
            return BaseResult.failed(HttpStatus.GLOBAL_EXCEPTION_CODE, "查询运输相关信息失败");
        }
    }

    /**
     * 根据运单号查询运输相关信息。
     *
     * @param req 查询请求
     * @return 运输相关信息
     */
    @PostMapping("/waybill/transportInfo")
    @Operation(summary = "根据运单号查询运输相关信息")
    public BaseResult<TradeOrderTransportInfoResp> queryTransportInfoByWaybillNo(@RequestBody @Valid WaybillTransportQueryReq req) {
        try {
            return BaseResult.success(tradeOrderTransportInfoService.queryByWaybillNo(req));
        } catch (IllegalArgumentException e) {
            log.warn("根据运单号查询运输相关信息参数异常，waybillNo:{}, errorMessage:{}",
                    req == null ? null : req.getWaybillNo(), e.getMessage());
            return BaseResult.failed(HttpStatus.PARAMETERS_MISSING_CODE, e.getMessage());
        } catch (Exception e) {
            log.error("根据运单号查询运输相关信息失败，waybillNo:{}, errorMessage:{}",
                    req == null ? null : req.getWaybillNo(), e.getMessage(), e);
            return BaseResult.failed(HttpStatus.GLOBAL_EXCEPTION_CODE, "查询运输相关信息失败");
        }
    }
}
