package com.sckw.transport.controller.operate;

import com.alibaba.fastjson.JSONObject;
import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.model.page.PageRes;
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.param.LogisticsOrderQuery;
import com.sckw.transport.model.param.ManagementContractParam;
import com.sckw.transport.model.param.ManagementLogisticsOrderQuery;
import com.sckw.transport.model.param.OrderQuery;
import com.sckw.transport.model.vo.execlVo.ManagementLogisticsOrderExcelVo;
import com.sckw.transport.service.operateService.KwtManagementLogisticsOrderService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lfdc
 * @description 运营管理端-物流订单controller
 * @date 2023-09-11 09:06:07
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/kwtManagementLogisticsOrder")
public class ManagementLogisticsOrderController {

    @Autowired
    KwtManagementLogisticsOrderService managementLogisticsOrderService;


    /**
     * 运营管理端-物流订单-首页条件查询
     *
     * @param query 查询参数
     * @return  物流订单数据查询结果
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public HttpResult selectLogisticsOrder(@Valid @RequestBody ManagementLogisticsOrderQuery query) {
        log.error("运营管理端-物流订单-首页条件查询 :{}", JSONObject.toJSONString(query));
        try {
            PageRes result = managementLogisticsOrderService.selectLogisticsOrder(query);
            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 = "/statistics", method = RequestMethod.POST)
    public HttpResult statisticsLogisticsOrder(@Valid @RequestBody ManagementLogisticsOrderQuery query) {
        log.info("运营管理端-物流订单-top统计 查询 :{}", JSONObject.toJSONString(query));
        try {
            return managementLogisticsOrderService.statisticsLogisticsOrder(query);
        } catch (Exception e) {
            log.error("运营管理端-物流订单-top统计 失败 :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 运营管理端-物流订单-导出
     * @param query 查询参数
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public void exportLogisticsOrder(@Validated @RequestBody ManagementLogisticsOrderQuery query) {
        log.error("运营管理端-物流订单-导出 query :{}", JSONObject.toJSONString(query));
        HttpServletResponse response = RequestHolder.getResponse();
        List<ManagementLogisticsOrderExcelVo> list = managementLogisticsOrderService.exportLogisticsOrder(query);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
//      Translator.translate(list);
        ExcelUtil.downData(response, ManagementLogisticsOrderExcelVo.class, list);
    }


    /**
     * 运营端-物流订单-获取订单详情
     * @param orderId   物流订单id
     * @param orderNo   物流订单编号
     * @return  订单详情数据
     */
    @RequestMapping(value = "/getOrderDetail", method = RequestMethod.GET)
    public HttpResult getOrderDetail(@RequestParam("orderId") @NotBlank(message = "订单id不能为空") String orderId,
                                     @RequestParam("orderNo") @NotBlank(message = "订单编号不能为空") String orderNo) {
        log.info("运营端-物流订单-获取订单详情参数:单据id:{},单据编号：{}", orderId, orderNo);
        try {
            return managementLogisticsOrderService.getOrderDetail(orderId, orderNo);
        } catch (Exception e) {
            log.error("运营端-物流订单-获取订单详情失败:error :单据id:{},单据编号：{} errorMessage:{}", orderId, orderNo, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 运营端-物流订单-获取运单信息
     *
     * @param orderDto  物流订单查询请求参数
     * @return  车辆运单信息
     */
    @RequestMapping(value = "/getCarWaybillByOrder", method = RequestMethod.POST)
    public HttpResult getCarWaybillByOrder(@Valid @RequestBody LogisticsOrderQuery orderDto) {
        log.info("运营端-物流订单-获取车辆运单 query {}", JSONObject.toJSONString(orderDto));
        try {
            return managementLogisticsOrderService.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 contractParam 请求参数[合同ids]
     * @return  物流订单数据
     */
    @RequestMapping(value = "/getLogisticsContract", method = RequestMethod.POST)
    public HttpResult getLogisticsContract(@Valid @RequestBody ManagementContractParam contractParam) {
        log.info("基础档案-物流合同档案-根据合同id获取托运订单列表 query:{}", JSONObject.toJSONString(contractParam));
        try {
            return managementLogisticsOrderService.getLogisticsContract(contractParam);
        } catch (Exception e) {
            log.error("基础档案-物流合同档案-根据合同id获取托运订单列表 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 运营端-贸易订单-获取物流订单
     *
     * @param query 订单查询
     * @return  物流订单数据
     */
    @RequestMapping(value = "/selectLogisticsOrder", method = RequestMethod.POST)
    public HttpResult selectLogisticsOrderByTradeOrder(@Valid @RequestBody OrderQuery query) {
        log.info("运营端-贸易订单-获取物流订单 传递参数信息:{}", JSONObject.toJSONString(query));
        try {
            return managementLogisticsOrderService.selectLogisticsOrderByTradeOrder(query.getIds(), query.getPage(), query.getPageSize());
        } catch (Exception e) {
            log.error("运营端-贸易订单-获取物流订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

}
