package com.sckw.transport.controller.enterpriseApp;

import com.alibaba.fastjson.JSONObject;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.transport.model.param.AcceptCarriageOrderQuery;
import com.sckw.transport.model.param.DriverParam;
import com.sckw.transport.service.KwtConsignOrderService;
import com.sckw.transport.service.KwtWaybillOrderService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
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 企业端app托运controller
 * @date 2023-08-21 16:08:41
 */
@Slf4j
@RestController
@RequestMapping("/appKwtConsignOrder")
public class AppConsignController {

    @Autowired
    private KwtConsignOrderService consignOrderService;

    @Autowired
    private KwtWaybillOrderService waybillOrderService;

    /**
     * 企业app订单管理数据托运订单统计
     * @param query 请求参数
     * @return  统计响应结果
     */
    @RequestMapping(name = "企业app订单管理数据托运订单统计", path = "/statisticsConsign", method = RequestMethod.POST)
    public HttpResult statisticsConsign(@Valid  @RequestBody AcceptCarriageOrderQuery query) {
        log.info("企业app订单管理数据托运订单统计 query :{}", JSONObject.toJSONString(query));
        try {
            return HttpResult.ok(consignOrderService.statisticsConsignByApp(query));
        } catch (Exception e) {
            log.error("企业app订单管理数据托运订单统计失败  query:{}, error:{}", JSONObject.toJSONString(query), e.getMessage(), e);
            return HttpResult.error(e.getMessage());
        }
    }

    /**
     * 企业app托运订单详情统计-统计车辆运单
     * @param orderId   物流订单id
     * @return  车辆运单数据
     */
    @RequestMapping(name = "企业app托运订单详情统计", path = "/statisticsConsignByOrderId", method = RequestMethod.GET)
    public HttpResult statisticsConsignByOrderId(@NotBlank(message = "订单id不能为空") @RequestParam("orderId") String orderId) {
        log.info("企业app托运订单详情统计-统计车辆运单 query :{}",orderId);
        try {
            return HttpResult.ok(waybillOrderService.statisticsByOrderId(orderId,"1"));
        } catch (Exception e) {
            log.error("企业app托运订单详情统计失败-统计车辆运单  query:{}, error:{}", orderId, e.getMessage(), e);
            return HttpResult.error(e.getMessage());
        }
    }

    /**
     * 企业app托运订单循环车辆运单列表查询
     * @param driverParam   循环单查询请求数据
     * @return  车辆运单列表数据
     */
    @RequestMapping(name = "企业app托运订单循环车辆运单列表查询", path = "/consignStopDocumentDetail", method = RequestMethod.POST)
    public HttpResult consignStopDocumentDetail(@Validated @RequestBody DriverParam driverParam) {
        log.info("企业app托运订单循环车辆运单列表查询 query {}", JSONObject.toJSONString(driverParam));
        try {
            return consignOrderService.consignStopDocumentDetail(driverParam);
        } catch (Exception e) {
            log.error("企业app托运订单循环车辆运单列表查询:query {},error {} ", JSONObject.toJSONString(driverParam), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    @RequestMapping(name = "企业app托运订单订单进度", path = "/getOrderTrackByOrderId", method = RequestMethod.GET)
    public HttpResult getOrderTrackByOrderId(@NotBlank(message = "订单id不能为空") @RequestParam("orderId") String orderId) {
        log.info("企业app托运订单订单进度 query :{}", orderId);
        try {
            return consignOrderService.getOrderTrackByOrderId(orderId,"1");
        } catch (Exception e) {
            log.error("企业app托运订单订单进度失败  query:{}, error:{}", orderId, e.getMessage(), e);
            return HttpResult.error(e.getMessage());
        }
    }
}
