package com.sckw.transport.controller.enterpriseApp;

import com.sckw.core.exception.SystemException;
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.model.dto.WaybillListAppDTO;
import com.sckw.transport.model.param.*;
import com.sckw.transport.model.vo.OrderTotalTakeVo;
import com.sckw.transport.model.vo.StatisticsWaybillResp;
import com.sckw.transport.service.KwtLogisticsConsignmentService;
import com.sckw.transport.service.KwtWaybillManagementService;
import com.sckw.transport.service.KwtWaybillManagementV1Service;
import com.sckw.transport.service.app.WaybillOrderService;
import io.swagger.v3.oas.annotations.Operation;
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.*;

/**
 * @author jc
 * @description 企业端app运单controller
 * @date 2023-08-21 16:08:41
 */
@Slf4j
@RestController
@RequestMapping("/appKwtWayBill")
public class AppWayBillController {

    @Autowired
    KwtWaybillManagementService waybillManagementService;
    @Autowired
    KwtWaybillManagementV1Service waybillManagementV1Service;
    @Autowired
    KwtLogisticsConsignmentService logisticsConsignmentService;
    @Autowired
    WaybillOrderService waybillOrderService;

    /**
     * APP 运单统计分类 - [运输中|已完成] - 数据库
     * @param keyword   查询关键字
     * @return  统计响应结果
     */
    @Valid
    @RequestMapping(name = "APP运单统计分类", value = "/waybillStatistica", method = RequestMethod.GET)
    public HttpResult waybillStatistic(@RequestParam("keyword") String keyword,
                                       @RequestParam("startDate") String startDate,
                                       @RequestParam("endDate") String endDate) {
        try {
            return waybillManagementService.waybillDataStatisticApp(keyword, startDate, endDate);
        } catch (Exception e) {
            log.error("APP运单统计分类:订单id {},error {} ", keyword, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * APP 运单统计分类 - [运输中|已完成] - mongo查询
     * @param keyword   查询关键字
     * @return  统计响应结果
     */
    @Valid
    @RequestMapping(name = "APP运单统计分类", value = "/waybillStatistic", method = RequestMethod.GET)
    public HttpResult waybillStatisticCount(@RequestParam("keyword") String keyword,
                                       @RequestParam("startDate") String startDate,
                                       @RequestParam("endDate") String endDate) {
        try {
            return waybillManagementService.waybillStatisticCountApp(keyword, startDate, endDate);
        } catch (Exception e) {
            log.error("APP运单统计分类:订单id {},error {} ", keyword, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * APP 运单列表 - [运输中|已完成]
     * @param waybillOrderDTO   运单查询请求dto
     * @return  运单查询结果
     */
    @Valid
    @RequestMapping(name = "APP运单列表", value = "/waybillData", method = RequestMethod.POST)
    public HttpResult waybillData(@Validated @RequestBody WaybillListAppDTO waybillOrderDTO) {
        try {
            return waybillManagementService.waybillDataApp(waybillOrderDTO);
        } catch (Exception e) {
            log.error("APP运单列表:error-msg: {} ,e: {}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * APP 运单列表吨量统计 - [运输中|已完成]
     * @param waybillOrderDTO   统计运单数据查询
     * @return  运单重量响应结果
     */
    @Valid
    @RequestMapping(name = "APP运单吨量统计", value = "/waybillAmountStatistic2", method = RequestMethod.POST)
    public HttpResult waybillAmountStatistic(@Validated @RequestBody WaybillListAppDTO waybillOrderDTO) {
        try {
            return waybillManagementService.waybillAmountStatistic(waybillOrderDTO);
        } catch (Exception e) {
            log.error("APP运单列表:订单id {},error {} ", waybillOrderDTO, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * APP 运单列表吨量统计 - [运输中|已完成] - mongo
     * @param waybillOrderDTO   统计运单数据查询
     * @return  运单重量响应结果
     */
    @Valid
    @RequestMapping(name = "APP运单吨量统计", value = "/waybillAmountStatistic", method = RequestMethod.POST)
    public HttpResult waybillAmountStatisticCount(@Validated @RequestBody WaybillListAppDTO waybillOrderDTO) {
        try {
            return waybillManagementService.waybillAmountStatisticCount(waybillOrderDTO);
        } catch (Exception e) {
            log.error("APP运单列表:订单id {},error {} ", waybillOrderDTO, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * APP 物流订单-运单列表
     * @param waybillOrderDTO   物流订单查询
     * @return  车辆运单响应结果
     */
    @Valid
    @RequestMapping(name = "物流订单-运单列表", value = "/logisticOrderWaybillData", method = RequestMethod.POST)
    public HttpResult logisticOrderWaybillData(@Validated @RequestBody WaybillListAppDTO waybillOrderDTO) {
        try {
            return waybillManagementService.logisticOrderWaybillData(waybillOrderDTO);
        } catch (Exception e) {
            log.error("物流订单-运单列表:订单id {},error {} ", waybillOrderDTO, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }





    /**
     * APP 物流订单详情-根据物流订单ID查询运单统计数据
     * @param waybillOrderDTO   物流订单ID
     * @return  车辆运单响应结果
     */
    @Valid
    @RequestMapping(name = "物流订单详情-运单统计", value = "/logisticOrderWaybillStatistic", method = RequestMethod.POST)
    public HttpResult logisticOrderWaybillStatistic(@Validated @RequestBody WaybillListAppDTO waybillOrderDTO) {
        try {
            return waybillManagementV1Service.logisticOrderWaybillStatistic(waybillOrderDTO);
        } catch (Exception e) {
            log.error("物流订单详情-运单统计:订单id {},error {} ", waybillOrderDTO, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * APP 物流订单详情-根据物流订单ID查询运单列表
     * @param waybillOrderDTO   物流订单查询
     * @return  车辆运单响应结果
     */
    @Valid
    @RequestMapping(name = "物流订单详情-运单列表", value = "/logisticOrderWaybill", method = RequestMethod.POST)
    public HttpResult logisticOrderWaybill(@Validated @RequestBody WaybillListAppDTO waybillOrderDTO) {
        try {
            return waybillManagementV1Service.logisticOrderWaybill(waybillOrderDTO);
        } catch (Exception e) {
            log.error("物流订单详情-运单列表:订单id {},error {} ", waybillOrderDTO, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * @desc 分页查询-看板
     * @param params {page：页数、pageSize：每页条数、。。。}
     * @author zk
     * @date 2024/4/3
     **/
    @PostMapping("/select")
    public HttpResult findNdexPage(@Validated @RequestBody WaybillListAppDTO params) throws SystemException {
        return HttpResult.ok(waybillManagementV1Service.findNdexPage(params));
    }

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

    /**
     * 查询司机关联的完成运单和累计运输量
     *
     * @param param
     * @return
     */
    @Operation(summary = "查询完成运单和累计运输量", description = "查询司机关联的完成运单和累计运输量")
    @PostMapping("/queryTotalTake")
    public BaseResult<OrderTotalTakeVo> queryTotalTake(@RequestBody @Valid OrderCirculateTotalTakeParam param){
        OrderTotalTakeVo totalTakeVo = waybillOrderService.queryTotalTake(param);
        return BaseResult.success(totalTakeVo);
    }


    /**
     * 分页查询物流订单
     *
     * @param param
     * @return
     */
    @Operation(summary = "分页查询物流订单", description = "查询司机关联车辆的物流订单")
    @PostMapping("/queryTruckLogisticsOrder")
    public BaseResult<PageDataResult<LogisticsOrderResp>> queryTruckLogisticsOrder(@RequestBody @Valid OrderCirculateQueryParam param){
        PageDataResult<LogisticsOrderResp> LogisticsOrderList = waybillOrderService.queryTruckLogisticsOrder(param);
        return BaseResult.success(LogisticsOrderList);
    }
    
    /**
     * 分页查询运单
     *
     * @param param
     * @return
     */
    @Operation(summary = "分页查询运单", description = "查询司机关联车辆接单的运单")
    @PostMapping("/queryWaybillOrder")
    public BaseResult<PageDataResult<WaybillOrderStatusResp>> queryWaybillOrder(@RequestBody @Valid WaybillOrderQueryParam param){
        PageDataResult<WaybillOrderStatusResp> LogisticsOrderList = waybillOrderService.queryWaybillOrder(param);
        return BaseResult.success(LogisticsOrderList);
    }

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

    /**
     * 物流订单接单
     *
     * @param param
     * @return
     */
    @Operation(summary = "接单", description = "物流订单接单")
    @PostMapping("/orderTaking")
    public BaseResult<OrderTakingResp> orderTaking(@RequestBody @Valid OrderCirculateTakingQueryParam param){
        OrderTakingResp orderTakingResp = waybillOrderService.orderTaking(param);
        return BaseResult.success(orderTakingResp);
    }

    /**
     * 取消订单接单
     *
     * @param param
     * @return
     */
    @Operation(summary = "取消接单", description = "取消订单接单")
    @PostMapping("/cancelWaybillOrder")
    public BaseResult cancelWaybillOrder(@RequestBody @Valid WaybillOrderCancelParam param){
        waybillOrderService.cancelWaybillOrder(param);
        return BaseResult.success();
    }

    /**
     * 过磅称重1 —— 手动推推送数据
     *
     * @param param
     * @return
     */
    @Operation(summary = "到达装货地点", description = "到达装货地点-手动推推送数据")
    @PostMapping("/comeInto")
    public BaseResult comeInto(@RequestBody @Valid WaybillOrderCmeIntoWeighParam param){
        waybillOrderService.comeInto(param);
        return BaseResult.success();
    }

//    /**
//     * 已装货 -手动推推送数据
//     *
//     * @param param
//     * @return
//     */
//    @Operation(summary = "已装货", description = "已装货 -手动推推送数据")
//    @PostMapping("/loading")
//    public BaseResult loading(@RequestBody @Valid WaybillOrderLoadingParam param){
//        waybillOrderService.loading(param);
//        return BaseResult.success();
//    }

//    /**
//     * 离场过磅（计算毛重和净重）-手动推推送数据
//     *
//     * @param param
//     * @return
//     */
//    @Operation(summary = "离场过磅", description = "离场过磅（计算毛重和净重）-手动推推送数据")
//    @PostMapping("/leave")
//    public BaseResult leave(@RequestBody @Valid WaybillOrderLeaveMockParam param){
//        waybillOrderService.leave(param);
//        return BaseResult.success();
//    }

    /**
     * 空载离场
     *
     * @param param
     * @return
     */
    @Operation(summary = "空载离场", description = "空载离场")
    @PostMapping("/emptyLoadLeave")
    public BaseResult emptyLoadLeave(@RequestBody @Valid WaybillOrderEmptyLoadLeaveParam param){
        waybillOrderService.emptyLoadLeave(param);
        return BaseResult.success();
    }

    /**
     * 补货
     *
     * @param param
     * @return
     */
    @Operation(summary = "补货", description = "补货")
    @PostMapping("/replenish")
    public BaseResult replenish(@RequestBody @Valid WaybillOrderReplenishParam param){
        waybillOrderService.replenish(param);
        return BaseResult.success();
    }

    /**
     * 离场
     *
     * @param param
     * @return
     */
    @Operation(summary = "离场", description = "离场")
    @PostMapping("/leavedWaybillOrder")
    public BaseResult leavedWaybillOrder(@RequestBody @Valid WaybillOrderLeaveParam param){
        waybillOrderService.leavedWaybillOrder(param);
        return BaseResult.success();
    }

    /**
     * 抬杆放行 mock
     *
     * @param param
     * @return
     */
    @Operation(summary = "mock抬杆", description = "mock抬杆")
    @PostMapping("/liftRodRelease")
    public BaseResult liftRodRelease(@RequestBody @Valid WaybillOrderLiftRodReleaseParam param){
        waybillOrderService.liftRodRelease(param);
        return BaseResult.success();
    }

    /**
     * 打印榜单
     *
     * @param
     * @return
     */
    @Operation(summary = "打印榜单", description = "打印榜单")
    @PostMapping("/printList")
    public BaseResult printList(@RequestBody @Valid WaybillOrderLiftRodReleaseParam param){
        waybillOrderService.printList(param);
        return BaseResult.success("打印中！");
    }

    /**
     * 卸货称重过磅2 —— 手动推推送数据
     *
     * @param param
     * @return
     */
    @Operation(summary = "卸货过磅", description = "卸货过磅")
    @PostMapping("/unloadingWeigh")
    public BaseResult unloadingWeigh(@RequestBody @Valid WaybillOrderUnloadingWeighParam param){
        waybillOrderService.unloadingWeigh(param);
        return BaseResult.success();
    }

    /**
     * 卸货离场
     *
     * @param param
     * @return
     */
    @Operation(summary = "卸货离场", description = "卸货离场")
    @PostMapping("/unloadingLeaved")
    public BaseResult unloadingLeaved(@RequestBody @Valid WaybillOrderUnLoadingLeaveParam param){
        waybillOrderService.unloadingLeaved(param);
        return BaseResult.success();
    }

    /**
     * 卸货
     *
     * @param param
     * @return
     */
    @Operation(summary = "卸货", description = "卸货")
    @PostMapping("/unloadingWaybillOrder")
    public BaseResult unloadingWaybillOrder(@RequestBody @Valid WaybillOrderUnloadParam param){
        waybillOrderService.unloadingWaybillOrder(param);
        return BaseResult.success();
    }


    /**
     * 推送过磅图片
     *
     * @param param
     * @return
     */
    @Operation(summary = "推送过磅图片", description = "推送过磅图片")
    @PostMapping("/weighImage")
    public BaseResult weighImage(@RequestBody @Valid WaybillOrderWeighImageParam param){
        waybillOrderService.weighImage(param);
        return BaseResult.success();
    }


}
