package com.prolog.rdc.tms.controller;

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.dto.*;
import com.prolog.rdc.tms.model.vo.*;
import com.prolog.rdc.tms.service.WaitLoadingAndUnloadingService;
import com.prolog.rdc.tms.util.PublicMethods;
import com.prolog.wms.base.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 运输管理-待装卸控制层
 *
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author zhangsj
 * @since 2022/6/6 10:34
 */
@RestController
@Api(tags = "运输管理-待装卸控制层")
@RequestMapping("/waitLoadingAndUnloading")
public class WaitLoadingAndUnloadingController {

	@Autowired
	private WaitLoadingAndUnloadingService service;

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

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

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

	@UserLog("预约单模式分页查询")
	@ApiOperation(value = "预约单模式分页查询", notes = "预约单模式分页查询")
	@PostMapping("/queryAppointmentTypePage")
	public RestMessage<Page<QueryTypeByAppointmentDTO>> queryAppointmentTypePage(@RequestBody @Valid QueryTypeByAppointmentVO queryTypeByAppointmentVO) {
		Page<QueryTypeByAppointmentDTO> page = service.queryAppointmentTypePage(queryTypeByAppointmentVO);
		return RestMessage.newInstance(true, "查询成功！", page);
	}

	@UserLog("收货方模式-配载运单查询")
	@ApiOperation(value = "收货方模式-配载运单查询", notes = "收货方模式-配载运单查询")
	@PostMapping("/receiver_get_shipbill")
	public RestMessage<Page<ReceiverGetShipbillDTO>> receiverGetShipbillExecute(@RequestBody ReceiverGetShipbillVO receiverGetShipbillVO) {
		try {
			Page<ReceiverGetShipbillDTO> page = service.receiverGetShipbillData(receiverGetShipbillVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}

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

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

	@UserLog("开始装卸")
	@ApiOperation(value = "开始装卸", notes = "开始装卸")
	@PostMapping("/started_work")
	public RestMessage<Object> startedWorkExecute(@RequestBody List<String> shipbillIds) {
		try {
			service.startedWorkData(shipbillIds);
			return RestMessage.newInstance(true, "操作成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}

	@UserLog("装卸完成")
	@ApiOperation(value = "装卸完成", notes = "装卸完成")
	@PostMapping("/complete_work")
	public RestMessage<Object> completeWorkExecute(@RequestBody List<String> shipbillIds) {
		try {
			service.completeWorkData(shipbillIds);
			return RestMessage.newInstance(true, "操作成功！", null);
		} catch (Exception e) {
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}

	@UserLog("导出所有数据")
	@ApiOperation(value = "导出所有数据", notes = "导出所有数据")
	@PostMapping("/export_all_shipbill")
	public RestMessage<Object> exportAllShipbillExecute(@RequestBody DzxExportAllVO dzxExportAllVO, HttpServletResponse response) {
		try {
			List<DzxExportShipbillDTO> list = service.exportAllShipbillData(dzxExportAllVO);
			ExcelUtils.export(response, DzxExportShipbillDTO.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 DzxExportPageVO dzxExportPageVO, HttpServletResponse response) {
		try {
			List<DzxExportShipbillDTO> list = service.exportPageShipbill(dzxExportPageVO);
			ExcelUtils.export(response, DzxExportShipbillDTO.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<DzxExportShipbillDTO> list = service.exportSelectLineShipbillData(shipbillIds);
			ExcelUtils.export(response, DzxExportShipbillDTO.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) {
			e.printStackTrace();
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}

	@UserLog("装车扫描")
	@ApiOperation(value = "装车扫描", notes = "装车扫描")
	@PostMapping("/loadingScan")
	public RestMessage<LoadingScanDTO> loadingScanExecute(@RequestBody @Valid LoadingScanVO loadingScanVO) {
		try {
			LoadingScanDTO dto = service.loadingScanData(loadingScanVO);
			return RestMessage.newInstance(true, "操作成功", dto);
		} catch (Exception e) {
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}

	@UserLog("运单剔除")
	@ApiOperation(value = "运单剔除", notes = "运单剔除")
	@PostMapping("/shipBillOut")
	public RestMessage<Object> shipBillOut(@RequestBody WaitPickUpConfirmVO vo) {
		service.shipBillOut(vo);
		return RestMessage.newInstance(true, "操作成功！", null);
	}

	@UserLog("装车确认")
	@ApiOperation(value = "装车确认", notes = "装车确认")
	@PostMapping("/loadingConfirm")
	public RestMessage<LoadingScanConfirmDTO> loadingConfirmExecute(@RequestBody WaitPickUpConfirmVO waitPickUpConfirmVO) {
		try {
			LoadingScanConfirmDTO dto = service.loadingConfirmData(waitPickUpConfirmVO);
			return RestMessage.newInstance(true, "操作成功！", dto);
		} catch (Exception e) {
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}

	@UserLog("根据装车单查询运单")
	@ApiOperation(value = "根据装车单查询运单", notes = "根据装车单查询运单")
	@PostMapping("/queryShipbillByLoadingBill")
	public RestMessage<LoadingScanDTO> queryShipbillByLoadingBillExecute(@RequestParam("loadingId") String loadingId) {
		try {
			LoadingScanDTO dto = service.queryShipbillByLoadingBillData(loadingId);
			return RestMessage.newInstance(true, "查询成功", dto);
		} catch (Exception e) {
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}

	@UserLog("根据装车单查询运单明细")
	@ApiOperation(value = "根据装车单查询运单明细", notes = "根据装车单查询运单明细")
	@PostMapping("/queryShipbillDetailByLoadingBill")
	public RestMessage<LoadingScanDTO> queryShipbillDetailByLoadingBillExecute(@RequestParam("loadingId") String loadingId) {
		try {
			LoadingScanDTO dto = service.queryShipbillDetailByLoadingBillData(loadingId);
			return RestMessage.newInstance(true, "查询成功", dto);
		} catch (Exception e) {
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}

	@UserLog("获取派车单扫描信息")
	@ApiOperation(value = "获取派车单扫描信息", notes = "获取派车单扫描信息")
	@PostMapping("/queryLoadingBillScanInfo")
	public RestMessage<LoadingScanDTO> queryLoadingBillScanInfo(QueryTypeByShipbillVO vo) {
		try {
			LoadingScanDTO dto = service.queryLoadingBillScanInfo(vo);
			return RestMessage.newInstance(true, "操作成功", dto);
		} catch (Exception e) {
			return RestMessage.newInstance(false, PublicMethods.getExceptionMessage(e), null);
		}
	}
}
