package com.sckw.transport.controller.operationManagement;

import com.alibaba.fastjson.JSONObject;
import com.sckw.core.exception.CustomPromptException;
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.ManagementWaybillOrderQuery;
import com.sckw.transport.model.vo.execlVo.ManagementWaybillOrderExcelVO;
import com.sckw.transport.service.ManagementWaybillOrderService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
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  JC
 * @description 运营管理端-运单controller
 * @date 2023/09/12 09:20:21
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/kwtManagementWaybillOrder")
public class ManagementWaybillOrderController {

    @Autowired
    ManagementWaybillOrderService managementWaybillOrderService;


    /**
     * 运营管理端-运单-首页条件查询
     *
     * @param query 查询参数
     * @return
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public HttpResult selectWaybillOrder(@Valid @RequestBody ManagementWaybillOrderQuery query) {
        try {
            return managementWaybillOrderService.selectWaybillOrder(query);
        } 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 statisticsWaybillOrder(@Valid @RequestBody ManagementWaybillOrderQuery query) {
        log.error("运营管理端-运单-top统计 查询 :{}", JSONObject.toJSONString(query));
        try {
            return managementWaybillOrderService.statisticsWaybillOrder(query);
        } catch (Exception e) {
            log.error("运营管理端-运单-top统计 失败 :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

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


    /**
     * 运营端-运单-获取订单详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getOrderDetail", method = RequestMethod.GET)
    public HttpResult getOrderDetail(@RequestParam("id") Long id) {
        try {
            return managementWaybillOrderService.getOrderDetail(id);
        } catch (Exception e) {
            log.error("运营端-运单-获取订单详情失败:error :单据id:{}, errorMessage:{}", id, e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

}
