package com.sckw.transport.controller;

import com.alibaba.fastjson.JSONObject;
import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.model.enums.OrderRuleEnum;
import com.sckw.core.model.enums.RedisOrderGenerateEnum;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.model.vo.BaseList;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.OrderGenerateSeqNoUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.config.easyexcel.RequestHolder;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.transport.model.dto.AddOrderDTO;
import com.sckw.transport.model.dto.OrderDTO;
import com.sckw.transport.model.dto.OrderFinishDTO;
import com.sckw.transport.model.dto.StopOrderTakingDTO;
import com.sckw.transport.model.param.AcceptCarriageOrderQuery;
import com.sckw.transport.model.param.ConsignOrderQuery;
import com.sckw.transport.model.param.DriverParam;
import com.sckw.transport.model.param.LogisticsOrderStatisticsParam;
import com.sckw.transport.model.vo.execlVo.ConsignOrderExcelVo;
import com.sckw.transport.service.ConsignOrderService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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
 * @description 托运订单controller
 * @date 2023-06-26 16:06:04
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/kwtConsignOrder")
public class ConsignOrderController {

    @Autowired
    ConsignOrderService consignOrderService;

    /**
     * 托运订单首页条件查询
     *
     * @param query 查询参数
     * @return
     */
    @RequestMapping(value = "/selectConsignOrderList", method = RequestMethod.POST)
    public HttpResult selectConsignOrderList(@Valid @RequestBody AcceptCarriageOrderQuery query) {
        try {
            PageRes result = consignOrderService.selectConsignOrderList(query, "1");
            return HttpResult.ok(result);
        } catch (Exception e) {
            log.error("托运订单首页条件查询 查询失败:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 托运订单-top统计
     *
     * @param query 查询参数
     * @return
     */
    @RequestMapping(value = "/statisticsConsign", method = RequestMethod.POST)
    public HttpResult statisticsConsign(@Valid @RequestBody AcceptCarriageOrderQuery query) {
        try {
            HttpResult result = consignOrderService.statisticsConsign(query, "1");
            return result;
        } catch (Exception e) {
            log.error("托运订单-top统计 查询失败:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 托运订单-导出
     *
     * @param query 查询参数
     * @return
     */
    @RequestMapping(value = "/consignExport", method = RequestMethod.POST)
    public void exportConsign(@Validated @RequestBody ConsignOrderQuery query) {
        HttpServletResponse response = RequestHolder.getResponse();
        List<ConsignOrderExcelVo> list = consignOrderService.exportConsign(query, "1");
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, ConsignOrderExcelVo.class, list);
    }


    /**
     * 托运订单/承运订单-获取车辆运单
     *
     * @param orderDto
     * @return
     */
    @RequestMapping(value = "/getCarWaybillByOrder", method = RequestMethod.POST)
    public HttpResult getCarWaybillByOrder(@Validated @RequestBody OrderDTO orderDto) {
        log.info("托运订单/承运订单-获取车辆运单 query {}", JSONObject.toJSONString(orderDto));
        try {
            return consignOrderService.getCarWaybillByOrder(orderDto);
        } catch (Exception e) {
            log.error("托运订单获取车辆运单失败:订单ids {},error {} ", JSONObject.toJSONString(orderDto), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 托运订单-设置停止接单-物流订单id查询-针对循环单
     *
     * @param baseList 页面请求数据
     * @return
     */
    @RequestMapping(value = "/consignStopDocumentDetailByOrderIds", method = RequestMethod.POST)
    public HttpResult consignStopDocumentDetailByOrderIds(@Valid @RequestBody BaseList baseList) {
        log.info("托运订单-设置停止接单-物流订单id查询 query {}", JSONObject.toJSONString(baseList));
        try {
            return consignOrderService.consignStopDocumentDetailByOrderIds(baseList);
        } catch (Exception e) {
            log.error("托运订单-设置停止接单-物流订单id查询:query {},error {} ", JSONObject.toJSONString(baseList), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单-设置停止接单-条件查询-针对循环单
     *
     * @param driverParam 页面请求数据
     * @return
     */
    @RequestMapping(value = "/consignStopDocumentDetail", method = RequestMethod.POST)
    public HttpResult consignStopDocumentDetail(@Validated @RequestBody DriverParam driverParam) {
        log.info("托运订单-设置停止接单-条件查询 query {}", JSONObject.toJSONString(driverParam));
        try {
            return consignOrderService.consignStopDocumentDetail(driverParam);
        } catch (Exception e) {
            log.error("托运订单-设置停止接单-条件查询:query {},error {} ", JSONObject.toJSONString(driverParam), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 托运订单-设置停止接单-提交-针对循环单
     *
     * @param stopOrderTakingDTO 请求参数
     * @return
     */
    @RequestMapping(value = "/consignStopDocumentCommit", method = RequestMethod.POST)
    public HttpResult consignStopDocumentCommit(@Validated @RequestBody StopOrderTakingDTO stopOrderTakingDTO) {
        log.info("托运订单-设置停止接单-提交 commit {}", JSONObject.toJSONString(stopOrderTakingDTO));
        try {
            return consignOrderService.consignStopDocumentCommit(stopOrderTakingDTO);
        } catch (Exception e) {
            log.error("托运订单-设置停止接单-提交 commit {},error {} ", JSONObject.toJSONString(stopOrderTakingDTO), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


//
//    /**
//     * 托运订单-设置停止接单-查询
//     *
//     * @return
//     */
//    @RequestMapping(value = "/stopDocumentDetail", method = RequestMethod.POST)
//    public HttpResult stopDocumentDetail(@Validated @RequestBody DriverParam driverParam) {
//        log.info("托运订单-设置停止接单-查询 请求参数:{}", JSONObject.toJSONString(driverParam));
//        try {
//            return consignOrderService.stopDocumentDetail(driverParam);
//        } catch (Exception e) {
//            log.error("托运订单-设置停止接单-查询 error:{}", e.getMessage(), e);
//            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
//        }
//    }
//
//    /**
//     * 托运订单-设置停止接单-提交
//     *
//     * @param stopOrderTakingDTO
//     * @return
//     */
//    @RequestMapping(value = "/stopDocumentCommit", method = RequestMethod.POST)
//    public HttpResult stopDocumentCommit(@Validated @RequestBody StopOrderTakingDTO stopOrderTakingDTO) {
//        log.info("托运订单-设置停止接单-提交 请求参数:{}", JSONObject.toJSONString(stopOrderTakingDTO));
//        try {
//            return consignOrderService.stopDocumentCommit(stopOrderTakingDTO);
//        } catch (Exception e) {
//            log.error("托运订单-设置停止接单-提交 error:{}", e.getMessage(), e);
//            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
//        }
//    }

    /**
     * 托运订单/承运订单-获取车辆运单-统计
     *
     * @param orderDto
     * @return
     */
    @RequestMapping(value = "/getCarWaybillByOrderCount", method = RequestMethod.POST)
    public HttpResult getCarWaybillByOrderCount(@Validated @RequestBody OrderDTO orderDto) {
        log.info("托运订单/承运订单-获取车辆运单-统计 query {}", JSONObject.toJSONString(orderDto));
        try {
            return consignOrderService.getCarWaybillByOrderCount(orderDto);
        } catch (Exception e) {
            log.error("托运订单/承运订单-获取车辆运单-统计: query {},error {} ", JSONObject.toJSONString(orderDto), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 托运订单查看驳回原因
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getRejectReason", method = RequestMethod.GET)
    public HttpResult getRejectReasonById(@NotBlank @RequestParam("id") String id,
                                          @NotNull @RequestParam("status") Integer status) {
        return consignOrderService.getRejectReasonById(id, status);
    }

    /**
     * 托运订单-获取车辆信息
     *
     * @param orderDto
     * @return
     */
    @RequestMapping(value = "/getCarListByOrder", method = RequestMethod.POST)
    public HttpResult getCarListByOrder(@Validated @RequestBody OrderDTO orderDto) {
        log.info("托运订单/承运订单-获取车辆信息 query {}", JSONObject.toJSONString(orderDto));
        try {
            return consignOrderService.getCarListByOrder(orderDto, "1");
        } catch (Exception e) {
            log.error("托运订单获取车辆运单失败:query {},error {} ", JSONObject.toJSONString(orderDto), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单-完结订单-页面数据查询
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/consignOrderFinishDetail", method = RequestMethod.GET)
    public HttpResult consignOrderFinishDetail(@RequestParam("orderId") @NotNull(message = "数据id不能为空") String orderId) {
        log.info("托运订单-完结订单-页面数据查询 传递参数信息:{}", orderId);
        try {
            return consignOrderService.consignOrderFinishDetail(orderId);
        } catch (Exception e) {
            log.error("托运订单-完结订单-页面数据查询 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单-完结订单
     *
     * @param orderFinishDTO
     * @return
     */
    @RequestMapping(value = "/commitConsignOrderFinish", method = RequestMethod.POST)
    public HttpResult commitConsignOrderFinish(@Validated @RequestBody OrderFinishDTO orderFinishDTO) {
        log.error("托运订单-完结订单:订单 {}", JSONObject.toJSONString(orderFinishDTO));
        try {
            return consignOrderService.commitConsignOrderFinish(orderFinishDTO);
        } catch (Exception e) {
            log.error("托运订单-完结订单失败:订单id {},error {} ", orderFinishDTO.getId(), e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单-新建订单
     *
     * @param addOrderDTO 页面传递参数
     * @return
     */
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST)
    public HttpResult addOrder(@Valid @RequestBody AddOrderDTO addOrderDTO) {
        try {
            return consignOrderService.addOrder(addOrderDTO);
        } catch (Exception e) {
            OrderGenerateSeqNoUtils.getSeqNoMinusOne(RedisOrderGenerateEnum.LOGISTICS_ORDER, OrderRuleEnum.LOGISTICS_ORDER);
            log.error("托运订单-新建订单 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 托运订单/承运订单-获取装卸货地址信息
     */
    @RequestMapping(name = "托运订单/承运订单-获取装卸货地址信息", value = "/getAddressList", method = RequestMethod.GET)
    public HttpResult getAddressList() {
        try {
            return consignOrderService.getLogisticsOrderAddressList();
        } catch (Exception e) {
            log.error("托运订单/承运订单-获取装卸货地址信息 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 工作台-托运业务统计
     *
     * @return
     */
    @RequestMapping(value = "/workbenchStatistics", method = RequestMethod.GET)
    public HttpResult workbenchStatistics(@RequestParam("dateType") @NotBlank(message = "类型不能为空") String dateType) {
        log.info("工作台-托运业务统计 {}", dateType);
        try {
            return consignOrderService.workbenchStatistics(dateType);
        } catch (Exception e) {
            log.error("工工作台-托运业务统计 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 工作台-物流运输数据统计-托运业务
     *
     * @return
     */
    @RequestMapping(value = "/workbenchLogisticsOrderStatistics", method = RequestMethod.POST)
    public HttpResult workbenchLogisticsOrderStatistics(@RequestBody LogisticsOrderStatisticsParam logisticsOrderStatistics) {
        log.info("工作台-物流运输数据统计-托运业务 {}", JSONObject.toJSONString(logisticsOrderStatistics));
        try {
            return consignOrderService.workbenchLogisticsOrderStatistics(logisticsOrderStatistics);
        } catch (Exception e) {
            log.error("工工作台-托运业务统计 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


}
