package com.sckw.report.controller;

import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.utils.CollectionUtils;
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.report.service.KwAcceptCarriageService;
import com.sckw.report.service.KwConsignService;
import com.sckw.report.service.KwTransportService;
import com.sckw.report.service.param.*;
import com.sckw.report.service.vo.AcceptCarriageOrderExcelVo;
import com.sckw.report.service.vo.ConsignOrderExcelVo;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zk
 * @desc 运输服务
 * @date 2023/7/10 0010
 */
@Slf4j
@Validated
@AllArgsConstructor
@RestController
@RequestMapping("/kwTransport")
public class KwTransportController {

    @Autowired
    KwTransportService transportService;

    @Autowired
    KwConsignService consignService;

    @Autowired
    KwAcceptCarriageService acceptCarriageService;

    /**
     * 承运订单首页条件查询
     *
     * @param query 查询参数
     * @return
     */
    @RequestMapping(value = "/selectAcceptCarriageOrderList", method = RequestMethod.POST)
    public HttpResult acceptCarriageOrderList(@Validated @RequestBody AcceptCarriageOrderQuery query) {
        HttpResult httpResult = new HttpResult();
        try {
            httpResult = acceptCarriageService.acceptCarriageOrderList(query,"2");
        } catch (Exception e) {
            log.error("承运订单首页条件查询 查询失败:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
        return httpResult;
    }

    /**
     * 承运订单top统计
     *
     * @param query
     * @return
     */
    @RequestMapping(value = "/statisticsAcceptCarriage", method = RequestMethod.POST)
    public HttpResult statisticsAcceptCarriage(@Validated @RequestBody AcceptCarriageOrderQuery query) {
        try {
            return acceptCarriageService.statisticsAcceptCarriage(query,"2");
        } catch (Exception e) {
            log.error("承运订单top统计失败:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

//    /**
//     * 承运订单导出
//     *
//     * @param query 查询参数
//     * @return
//     */
//    @RequestMapping(value = "/acceptCarriageOrderExport", method = RequestMethod.POST)
//    public HttpResult exportAcceptCarriage(@Validated @RequestBody AcceptCarriageOrderQuery query) {
//        HttpServletResponse response = RequestHolder.getResponse();
//        List<AcceptCarriageOrderExcelVo> list = acceptCarriageService.exportAcceptCarriage(query,"2");
//        if (!org.springframework.util.CollectionUtils.isEmpty(list)) {
//            ExcelUtil.download(response, AcceptCarriageOrderExcelVo.class, list);
//            return null;
//        }
//        return HttpResult.ok("没有可导出的数据");
//    }

    /**
     * 托运订单导出
     *
     * @param query 查询参数
     * @return
     */
    @RequestMapping(value = "/consignOrderExport", method = RequestMethod.POST)
    public HttpResult consignOrderExport(@Validated @RequestBody AcceptCarriageOrderQuery query) {
        HttpServletResponse response = RequestHolder.getResponse();
        List<AcceptCarriageOrderExcelVo> list = acceptCarriageService.exportAcceptCarriage(query,"2");
        if (!org.springframework.util.CollectionUtils.isEmpty(list)) {
            ExcelUtil.download(response, AcceptCarriageOrderExcelVo.class, list);
            return null;
        }
        return HttpResult.ok("没有可导出的数据");
    }

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

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

    /**
     * 托运订单-导出
     *
     * @param query 查询参数
     * @return
     */
    @RequestMapping(value = "/exportConsign", method = RequestMethod.POST)
    public HttpResult exportConsign(@Validated @RequestBody ConsignOrderQuery query) {
        HttpServletResponse response = RequestHolder.getResponse();
        List<ConsignOrderExcelVo> list = consignService.exportConsign(query,"1");
        if (!org.springframework.util.CollectionUtils.isEmpty(list)) {
            ExcelUtil.download(response, ConsignOrderExcelVo.class, list);
            return null;
        }
        return HttpResult.ok("没有可导出的数据");
    }


    /**
     * 运单管理-运单列表
     * @param query
     * @return
     */
    @RequestMapping(value = "/getWaybillOrders", method = RequestMethod.POST)
    public HttpResult getWayBillOrders(@Validated @RequestBody WaybillOrderListSelect query){
        try {
            return transportService.getWaybillOrders(query);
        } catch (Exception e) {
            log.error("[WAYBILL]运单管理-运单列表失败:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 运单管理-运单列表 top 统计
     *
     * @param query
     * @return {@link HttpResult }
     * @author jc
     * @date 2023/07/21
     */
    @RequestMapping(value = "/getWayBillStatistics", method = RequestMethod.POST)
    public HttpResult wayBillStatistics(@Validated @RequestBody WaybillOrderListSelect query) {
        try {
            return transportService.getWayBillStatistics(query);
        } catch (Exception e) {
            log.error("运单管理-运单列表top统计失败:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 运单管理-运单列表 列表统计数据项
     * 【还需要修改】
     * @param query
     * @return {@link HttpResult }
     * @author jc
     * @date 2023/07/21
     */
    @RequestMapping(value = "/wayBillTotalCount", method = RequestMethod.POST)
    public HttpResult wayBillTotalCount(@Validated @RequestBody WaybillOrderListSelect query) {
        try {
            return transportService.wayBillTotalCount(query);
        } catch (Exception e) {
            log.error("运单管理-运单列表top统计失败:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 承运订单-获取分包托运列表
     *
     * @param lOrderId 单据id
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getSubcontractConsignment", method = RequestMethod.GET)
    public HttpResult getSubcontractConsignment(@RequestParam("lOrderId") @NotBlank(message = "单据id不能为空") String lOrderId,
                                                @RequestParam("page") @NotBlank(message = "分页不能为空") int page,
                                                @RequestParam("pageSize") @NotBlank(message = "分页条数不能为空") int pageSize) {
        try {
            return transportService.getSubcontractConsignment(lOrderId, page, pageSize);
        } catch (Exception e) {
            log.error("承运订单-获取分包托运列表 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 承运合同/托运合同-根据合同id获取托运订单列表
     * @param contractParam 请求参数
     * @return
     */
    @RequestMapping(value = "/getLogisticsOrderContract", method = RequestMethod.POST)
    public HttpResult getLogisticsOrderContract(@Validated @RequestBody ContractParam contractParam) {
        try {
            return transportService.getLogisticsOrderByContractId(contractParam);
        } catch (Exception e) {
            log.error("承运合同/托运合同-根据合同id获取托运订单列表 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 运单管理-运单列表 运单地址查询接口
     * @param tag
     * @return
     */
    @RequestMapping(value = "/wayBillAddresses", method = RequestMethod.GET)
    public HttpResult wayBillAddresses( @RequestParam("tag") @NotNull(message = "类型不能为空") int tag) {
        try {
            return transportService.getWayBillAddresses(tag);
        } catch (Exception e) {
            log.error("运单管理-运单地址查询接口 {}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * @desc: 运单列表导出
     * @author: jc
     * @date: 2023-08-09 20:16:22
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/wayBillOrderExport", produces = MediaType.APPLICATION_JSON_VALUE)
    public void wayBillOrderExport(@RequestBody WaybillOrderQuery query, HttpServletResponse response) {
        List<WaybillOrderListExport> list = transportService.wayBillOrderExport(query);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, WaybillOrderListExport.class, list);
    }
}
