package com.prolog.rdc.tms.controller;

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

import com.prolog.rdc.tms.model.dto.*;
import com.prolog.rdc.tms.model.vo.*;
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.service.TrailerService;
import com.prolog.wms.base.util.ExcelUtils;

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

/**
 * 已托运控制层
 *
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author zhangsj
 * @since 2022/4/18 12:42
 */
@RestController
@Api(tags = "调度管理-已托运控制层")
@RequestMapping("trailer")
public class TrailerController {

	@Autowired
	private TrailerService service;

	@UserLog("已托运分页查询")
	@ApiOperation(value = "已托运分页查询", notes = "已托运分页查询")
	@PostMapping("/trailer_page")
	public RestMessage<Page<TrailerPageDTO>> trailerPageExecute(@RequestBody TrailerPageVO trailerPageVO) {
		try {
			Page<TrailerPageDTO> page = service.trailerPageData(trailerPageVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运高级查询")
	@ApiOperation(value = "已托运高级查询", notes = "已托运高级查询")
	@PostMapping("/advanced")
	public RestMessage<Page<TrailerPageDTO>> pageAdvancedExecute(@RequestBody TrailerAdsQueryVO advancedQueryVO) {
		try {
			Page<TrailerPageDTO> page = service.pageAdvanced(advancedQueryVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运审核")
	@ApiOperation(value = "已托运审核", notes = "已托运审核")
	@PostMapping("/trailer_audit")
	public RestMessage<Object> trailerAuditExecute(@RequestBody List<String> ids) {
		try {
			service.trailerAuditData(ids);
			return RestMessage.newInstance(true, "审核成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运运单撤销")
	@ApiOperation(value = "已托运运单撤销", notes = "已托运运单撤销")
	@PostMapping("/trailer_undo")
	public RestMessage<Object> trailerUndoExecute(@RequestBody List<String> ids) {
		try {
			service.trailerUndoData(ids);
			return RestMessage.newInstance(true, "撤销成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("托运单查看")
	@ApiOperation(value = "托运单查看", notes = "托运单查看")
	@PostMapping("/trailer_view")
	public RestMessage<TrailerViewDTO> viewTrailerExecute(@RequestParam("id") String id) {
		try {
			TrailerViewDTO dto = service.viewTrailerData(id);
			return RestMessage.newInstance(true, "查看成功！", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("托运单根据单号查看")
	@ApiOperation(value = "托运单根据单号查看", notes = "托运单根据单号查看")
	@PostMapping("/trailer_view_loadingbillid")
	public RestMessage<TrailerViewByIdDTO> viewTrailerByIdExecute(@RequestParam("id") String id) {
		try {
			TrailerViewByIdDTO dto = service.viewTrailerByIdData(id);
			return RestMessage.newInstance(true, "查看成功！", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("导出所有数据")
	@ApiOperation(value = "导出所有数据", notes = "导出所有数据")
	@PostMapping("/trailer_export_all")
	public RestMessage<Object> exportAllTrailerExecute(HttpServletResponse response, @RequestBody TrailerExportAllVO trailerExportAllVO) {
		try {
			List<ExportTrailerDTO> list = service.exportAllTrailerData(trailerExportAllVO);
			ExcelUtils.export(response, ExportTrailerDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
			return RestMessage.newInstance(true, "导出成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("导出当前页数据")
	@ApiOperation(value = "导出当前页数据", notes = "导出当前数据")
	@PostMapping("/trailer_export_page")
	public RestMessage<Object> exportPageTrailerExecute(HttpServletResponse response, @RequestBody TrailerExportPageVO trailerExportPageVO) {
		try {
			List<ExportTrailerDTO> list = service.exportPageTrailerData(trailerExportPageVO);
			ExcelUtils.export(response, ExportTrailerDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
			return RestMessage.newInstance(true, "导出成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("导出选中数据")
	@ApiOperation(value = "导出选中数据", notes = "导出选中数据")
	@PostMapping("/trailer_export_choose")
	public RestMessage<Object> exportChooseTrailerExecute(HttpServletResponse response, @RequestBody List<String> ids) {
		try {
			List<ExportTrailerDTO> list = service.exportChooseTrailerData(ids);
			ExcelUtils.export(response, ExportTrailerDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
			return RestMessage.newInstance(true, "导出成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("打印托车单")
	@ApiOperation(value = "打印托车单", notes = "打印托车单")
	@PostMapping("/trailer_print")
	public RestMessage<Map<String, Object>> printTrailerExecute(@RequestParam("id") String id) {
		try {
			Map<String, Object> map = service.printTrailerData(id);
			return RestMessage.newInstance(true, "打印成功！", map);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运配载运单查询")
	@ApiOperation(value = "已托运配载运单查询", notes = "已托运配载运单查询")
	@PostMapping("/trailer_page_detail")
	public RestMessage<Page<TrailerPageDetailDTO>> pageDetailTrailerExecute(@RequestBody TrailerPageDetailVO trailerPageDetailVO) {
		try {
			Page<TrailerPageDetailDTO> page = service.pageDetailTrailerData(trailerPageDetailVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运配载运单添加")
	@ApiOperation(value = "已托运配载运单添加", notes = "已托运配载运单添加")
	@PostMapping("/trailer_add_detail")
	public RestMessage<List<ShipbillHeaderPageDTO>> addDetailTrailerExecute(@RequestBody TrailerAddDetailVO trailerAddDetailVO) {
		try {
			List<ShipbillHeaderPageDTO> models = service.addDetailTrailerData(trailerAddDetailVO);
			return RestMessage.newInstance(true, "添加成功！", models);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运配载运单剔除")
	@ApiOperation(value = "已托运配载运单剔除", notes = "已托运配载运单剔除")
	@PostMapping("/trailer_delete_detail")
	public RestMessage<Object> deleteDetailTrailerExecute(@RequestBody TrailerDeleteDetailVO trailerDeleteDetailVO) {
		try {
			service.deleteDetailTrailerData(trailerDeleteDetailVO);
			return RestMessage.newInstance(true, "剔除成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运配载运单移动")
	@ApiOperation(value = "已托运配载运单移动", notes = "已托运配载运单移动")
	@PostMapping("/trailer_mobile_detail")
	public RestMessage<Object> mobileDetailTrailerExecute(@RequestBody TrailerMobileDetailVO trailerMobileDetailVO) {
		try {
			service.mobileDetailTrailerData(trailerMobileDetailVO);
			return RestMessage.newInstance(true, "移动成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运配载商品查询")
	@ApiOperation(value = "已托运配载商品查询", notes = "已托运配载商品查询")
	@PostMapping("/trailer_item_detail")
	public RestMessage<Page<TrailerItemPageDTO>> itemDetailTrailerExecute(@RequestBody TrailerItemDetailVO trailerItemDetailVO) {
		try {
			Page<TrailerItemPageDTO> page = service.itemDetailTrailerData(trailerItemDetailVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运配载包裹查询")
	@ApiOperation(value = "已托运配载包裹查询", notes = "已托运配载包裹查询")
	@PostMapping("/trailer_package_detail")
	public RestMessage<Page<TrailerPackagePageDTO>> packageDetailTrailerExecute(@RequestBody TrailerPackageDetailVO trailerPackageDetailVO) {
		try {
			Page<TrailerPackagePageDTO> page = service.packageDetailTrailerData(trailerPackageDetailVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运修改")
	@ApiOperation(value = "已托运修改", notes = "已托运修改")
	@PostMapping("/trailer_update")
	public RestMessage<Object> updateTrailerExecute(@RequestBody TrailerUpdateVO trailerUpdateVO) {
		try {
			service.updateTrailerData(trailerUpdateVO);
			return RestMessage.newInstance(true, "修改成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运修改—运单添加")
	@ApiOperation(value = "已托运修改—运单添加", notes = "已托运修改—运单添加")
	@PostMapping("/trailer_update_add_detail")
	public RestMessage<List<ShipbillHeaderPageDTO>> updateAddDetailTrailerExecute(@RequestBody TrailerUpdateAddDetailVO trailerUpdateAddDetailVO) {
		try {
			List<ShipbillHeaderPageDTO> models = service.updateAddDetailTrailerData(trailerUpdateAddDetailVO);
			return RestMessage.newInstance(true, "添加成功！", models);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运修改—运单剔除")
	@ApiOperation(value = "已托运修改—运单剔除", notes = "已托运修改—运单剔除")
	@PostMapping("/trailer_update_delete_detail")
	public RestMessage<Object> updateDeleteDetailTrailerExecute(@RequestBody TrailerUpdateDeleteDetailVO trailerUpdateDeleteDetailVO) {
		try {
			service.updateDeleteDetailTrailerData(trailerUpdateDeleteDetailVO);
			return RestMessage.newInstance(true, "剔除成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已托运修改—运单移动")
	@ApiOperation(value = "已托运修改—运单移动", notes = "已托运修改—运单移动")
	@PostMapping("/trailer_update_mobile_detail")
	public RestMessage<Object> updateMobileDetailTrailerExecute(@RequestBody TrailerUpdateMobileDetailVO trailerUpdateMobileDetailVO) {
		try {
			service.updateMobileDetailTrailerData(trailerUpdateMobileDetailVO);
			return RestMessage.newInstance(true, "移动成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("调度单审核信息查询")
	@ApiOperation(value = "调度单审核信息查询", notes = "调度单审核信息查询")
	@PostMapping("/trailer_get_approval_info")
	public RestMessage<DispatchCkApprovalSCDTO> getApprovalInfoExecute(@RequestBody DispatchApprovalInfoVO approvalInfoVO) {
		try {
			DispatchCkApprovalSCDTO dispatchCkApprovalSCDTO = service.getApprovalInfoData(approvalInfoVO);
			return RestMessage.newInstance(true, "查询成功", dispatchCkApprovalSCDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("调度单审批作废")
	@ApiOperation(value = "调度单审批作废", notes = "调度单审批作废")
	@PostMapping("/trailer_dispatch_approval_invalid")
	public RestMessage<Object> dispatchApprovalInvalidExecute(@RequestBody DispatchCkApprovalInvalidVO approvalInvalidVO) {
		try {
			service.dispatchApprovalInvalidData(approvalInvalidVO);
			return RestMessage.newInstance(true, "作废成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("调度单审批流撤回")
	@ApiOperation(value = "调度单审批流撤回", notes = "调度单审批流撤回")
	@PostMapping("/trailer_dispatch_approval_retract")
	public RestMessage<Object> dispatchApprovalRetractExecute(@RequestBody DispatchApprovalRetractVO approvalRetractVO) {
		try {
			service.dispatchApprovalRetractData(approvalRetractVO);
			return RestMessage.newInstance(true, "撤回成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("调度单审批流提交")
	@ApiOperation(value = "调度单审批流提交", notes = "调度单审批流提交")
	@PostMapping("/trailer_dispatch_approval_submit")
	public RestMessage<Object> dispatchApprovalSubmitExecute(@RequestBody DispatchApprovalSubmitVO approvalSubmitVO) {
		try {
			service.dispatchApprovalSubmitData(approvalSubmitVO);
			return RestMessage.newInstance(true, "提交成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("调度单审批流审核")
	@ApiOperation(value = "调度单审批流审核", notes = "调度单审批流审核")
	@PostMapping("/trailer_dispatch_approval")
	public RestMessage<Object> dispatchApprovalExecute(@RequestBody DispatchApprovalVO approvalVO) {
		try {
			service.dispatchApprovalData(approvalVO);
			return RestMessage.newInstance(true, "审核成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}
}
