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.HttpResult;
import com.sckw.transport.model.dto.WaybillListAppDTO;
import com.sckw.transport.model.dto.WaybillOrderQueryDto;
import com.sckw.transport.service.KwtWaybillManagementService;
import com.sckw.transport.service.KwtWaybillManagementV1Service;
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;

    /**
     * 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);
    }

}
