package com.prolog.rdc.tms.controller;

import java.util.List;
import java.util.Map;

import com.prolog.rdc.tms.model.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

import com.prolog.common.util.DateUtils;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.rdc.tms.model.vo.WaitDeliveredAdvancedQueryByShipbillVO;
import com.prolog.rdc.tms.model.vo.WaitDeliveredDeliveredConfirmVO;
import com.prolog.rdc.tms.model.vo.WaitDeliveredExportAllVO;
import com.prolog.rdc.tms.model.vo.WaitDeliveredExportPageVO;
import com.prolog.rdc.tms.model.vo.WaitDeliveredItemDetailVO;
import com.prolog.rdc.tms.model.vo.WaitDeliveredPackageDetailVO;
import com.prolog.rdc.tms.model.vo.WaitDeliveredQueryByShipbillVO;
import com.prolog.rdc.tms.model.vo.WaitDeliveredQueryTypeByReceiverVO;
import com.prolog.rdc.tms.model.vo.WaitDeliveredReceiverGetShipbillVO;
import com.prolog.rdc.tms.service.WaitDeliveredService;
import com.prolog.rdc.tms.util.PublicMethods;
import com.prolog.wms.base.util.ExcelUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 运输管理-待送达控制层
 *
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author zhangsj
 * @since 2022/6/15 9:46
 */
@RestController
@Api(tags = "运输管理-待送达控制层")
@RequestMapping("/waitDelivered")
public class WaitDeliveredController {

    @Autowired
    private WaitDeliveredService service;

    @UserLog("运单模式分页查询")
    @ApiOperation(value = "运单模式分页查询", notes = "运单模式分页查询")
    @PostMapping("/query_shipbill")
    public RestMessage<Page<WaitDeliveredQueryByShipbillDTO>> queryTypeByShipbillPageExecute(@RequestBody WaitDeliveredQueryByShipbillVO waitDeliveredQueryByShipbillVO){
        try {
            Page<WaitDeliveredQueryByShipbillDTO> page = service.queryTypeByShipbillPageData(waitDeliveredQueryByShipbillVO);
            return RestMessage.newInstance(true, "查询成功！", page);
        }catch (Exception e){
            return RestMessage.newInstance(true, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("运单模式高级查询")
    @ApiOperation(value = "运单模式高级查询", notes = "运单模式高级查询")
    @PostMapping("/advanced_query_shipbill")
    public RestMessage<Page<WaitDeliveredQueryByShipbillDTO>> advancedQueryTypeByShipbillPageExecute(@RequestBody WaitDeliveredAdvancedQueryByShipbillVO waitDeliveredAdvancedQueryByShipbillVO){
        try {
            Page<WaitDeliveredQueryByShipbillDTO> page = service.advancedQueryTypeByShipbillPageData(waitDeliveredAdvancedQueryByShipbillVO);
            return RestMessage.newInstance(true, "查询成功！", page);
        }catch (Exception e){
            return RestMessage.newInstance(true, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("收货方模式分页查询")
    @ApiOperation(value = "收货方模式分页查询", notes = "收货方模式分页查询")
    @PostMapping("/query_receiver")
    public RestMessage<Page<WaitDeliveredQueryTypeByReceiverDTO>> queryTypeByReceiverPageExecute(@RequestBody WaitDeliveredQueryTypeByReceiverVO waitDeliveredQueryTypeByReceiverVO){
        try {
            Page<WaitDeliveredQueryTypeByReceiverDTO> page = service.queryTypeByReceiverPageData(waitDeliveredQueryTypeByReceiverVO);
            return RestMessage.newInstance(true, "查询成功！", page);
        }catch (Exception e){
            return RestMessage.newInstance(true, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("配载运单查询")
    @ApiOperation(value = "配载运单查询", notes = "配载运单查询")
    @PostMapping("/receiver_get_shipbill")
    public RestMessage<Page<WaitDeliveredReceiverGetShipbillDTO>> receiverGetShipbillExecute(@RequestBody WaitDeliveredReceiverGetShipbillVO waitDeliveredReceiverGetShipbillVO){
        try {
            Page<WaitDeliveredReceiverGetShipbillDTO> page = service.receiverGetShipbillData(waitDeliveredReceiverGetShipbillVO);
            return RestMessage.newInstance(true, "查询成功！", page);
        }catch (Exception e){
            return RestMessage.newInstance(true, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("配载商品查询")
    @ApiOperation(value = "配载商品查询", notes = "配载商品查询")
    @PostMapping("/item_detail")
    public RestMessage<Page<WaitDeliveredItemDetailDTO>> itemDetailExecute(@RequestBody WaitDeliveredItemDetailVO waitDeliveredItemDetailVO){
        try {
            Page<WaitDeliveredItemDetailDTO> page = service.itemDetailData(waitDeliveredItemDetailVO);
            return RestMessage.newInstance(true, "查询成功！", page);
        }catch (Exception e){
            return RestMessage.newInstance(true, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("配载包裹查询")
    @ApiOperation(value = "配载包裹查询", notes = "配载包裹查询")
    @PostMapping("/package_detail")
    public RestMessage<Page<WaitDeliveredPackageDetailDTO>> packageDetailExecute(@RequestBody WaitDeliveredPackageDetailVO waitDeliveredPackageDetailVO){
        try {
            Page<WaitDeliveredPackageDetailDTO> page = service.packageDetailData(waitDeliveredPackageDetailVO);
            return RestMessage.newInstance(true, "查询成功！", page);
        }catch (Exception e){
            return RestMessage.newInstance(true, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("导出所有数据")
    @ApiOperation(value = "导出所有数据", notes = "导出所有数据")
    @PostMapping("/export_all_shipbill")
    public RestMessage<Object> exportAllShipbillExecute(@RequestBody WaitDeliveredExportAllVO waitDeliveredExportAllVO, HttpServletResponse response) {
        try {
            List<WaitDeliveredExportShipbillDTO> list = service.exportAllShipbillData(waitDeliveredExportAllVO);
            ExcelUtils.export(response, WaitDeliveredExportShipbillDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("导出当前页数据")
    @ApiOperation(value = "导出当前页数据", notes = "导出当前页数据")
    @PostMapping("/export_page_shipbill")
    public RestMessage<Object> exportPageShipbillExecute(@RequestBody WaitDeliveredExportPageVO waitDeliveredExportPageVO, HttpServletResponse response) {
        try {
            List<WaitDeliveredExportShipbillDTO> list = service.exportPageShipbill(waitDeliveredExportPageVO);
            ExcelUtils.export(response, WaitDeliveredExportShipbillDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("导出选择行数据")
    @ApiOperation(value = "导出选择行数据", notes = "导出选择行数据")
    @PostMapping("/export_select_line_shipbill")
    public RestMessage<Object> exportSelectLineShipbillExecute(@RequestBody List<String> shipbillIds, HttpServletResponse response) {
        try {
            List<WaitDeliveredExportShipbillDTO> list = service.exportSelectLineShipbillData(shipbillIds);
            ExcelUtils.export(response, WaitDeliveredExportShipbillDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("待送达打印")
    @ApiOperation(value = "待送达打印", notes = "待送达打印")
    @PostMapping("/print")
    public RestMessage<Object> printShipbillExecute(@RequestParam("shipbillId") String shipbillId){
        try {
            Map<String,Object> map = service.printShipbillData(shipbillId);
            return RestMessage.newInstance(true, "打印成功！", map);
        } catch (Exception e) {
            return RestMessage.newInstance(true, PublicMethods.getExceptionMessage(e), null);
        }
    }

    @UserLog("送达确认")
    @ApiOperation(value = "待送达-送达确认", notes = "待送达-送达确认")
    @PostMapping("/delivered_confirm")
    public RestMessage<Object> deliveredConfirmExecute(@RequestBody WaitDeliveredDeliveredConfirmVO waitDeliveredDeliveredConfirmVO){
        service.deliveredConfirmData(waitDeliveredDeliveredConfirmVO);
        return RestMessage.newInstance(true, "操作成功！", null);
    }

    @UserLog("待送达-运单容器查询")
    @ApiOperation(value = "待送达-运单容器查询", notes = "待送达-运单容器查询")
    @PostMapping("/delivered_container_query")
    public RestMessage<List<TmsShipbillContainerDTO>> deliveredContainerQueryExecute(@RequestBody List<String> shipbillIds){
        List<TmsShipbillContainerDTO> dtos = service.deliveredContainerQuery(shipbillIds);
        return RestMessage.newInstance(true, "操作成功！", dtos);
    }

}
