package com.prolog.rdc.tms.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 org.springframework.web.multipart.MultipartFile;

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.product.core.easyexcel.util.ExcelUtils;
import com.prolog.rdc.tms.model.vo.xlsimport.ShipbillImportVO;
import com.prolog.rdc.tms.service.ShipbillService;

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

/**
 * 订单管理控制层
 *
 * @author zhangsj
 * @since 2022-03-30 15:04:35
 */
@RestController
@Api(tags = "订单管理-订单管理控制层")
@RequestMapping("/shipbill")
public class ShipbillController {

	@Autowired
	private ShipbillService service;

	@UserLog("运单汇总查询")
	@ApiOperation(value = "运单汇总查询", notes = "运单汇总查询")
	@PostMapping("/page")
	public RestMessage<Page<ShipbillHeaderPageDTO>> pageShipbillHeaderExecute(@RequestBody ShipbillHeaderPageVO shipbillHeaderPageVO) {
		try {
			Page<ShipbillHeaderPageDTO> list = service.pageShipbillHeaderData(shipbillHeaderPageVO);
			return RestMessage.newInstance(true, "查询成功！", list);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

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

	@UserLog("运单新增保存")
	@ApiOperation(value = "运单新增保存", notes = "运单新增保存")
	@PostMapping("/add")
	public RestMessage<String> addSaveShipbillHeaderExecute(@RequestBody ShipbillHeaderSaveVO shipbillHeaderSaveVO) {
		try {
			String shipbillid = service.addSaveShipbillHeaderData(shipbillHeaderSaveVO);
			return RestMessage.newInstance(true, "保存成功！", shipbillid);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单新增保存提交")
	@ApiOperation(value = "运单新增保存提交", notes = "运单新增保存提交")
	@PostMapping("/add_submit")
	public RestMessage<String> addSubmitShipbillHeaderExecute(@RequestBody ShipbillHeaderSubmitVO shipbillHeaderSubmitVO) {
		try {
			String shipbillid = service.addSubmitShipbillHeaderData(shipbillHeaderSubmitVO);
			return RestMessage.newInstance(true, "提交成功！", shipbillid);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单批量提交")
	@ApiOperation(value = "运单批量提交", notes = "运单批量提交")
	@PostMapping("/submit")
	public RestMessage<String> submitShipbillExecute(@RequestBody List<String> ids) {
		try {
			service.submitShipbillData(ids);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@PostMapping("/page_detail")
	@UserLog("运单明细查询")
	@ApiOperation(value = "运单明细查询", notes = "运单明细查询")
	public RestMessage<Page<ShipbillDetailPageDTO>> pageShipbillDetailExecute(@RequestBody ShipbillDetailPageVO shipbillDetailPageVO) {
		try {
			Page<ShipbillDetailPageDTO> page = service.pageShipbillDetailData(shipbillDetailPageVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单查看")
	@ApiOperation(value = "运单查看", notes = "运单查看")
	@PostMapping("/view")
	public RestMessage<ShipbillViewDTO> viewShipbillExecute(@RequestBody ShipbillViewVO shipbillViewVO) {
		try {
			ShipbillViewDTO dto = service.viewShipbillData(shipbillViewVO);
			return RestMessage.newInstance(true, "查询成功！", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("点击运单查看")
	@ApiOperation(value = "点击运单查看", notes = "点击运单查看")
	@PostMapping("/view_id")
	public RestMessage<ShipbillViewlByIdDTO> viewShipbillByIdExecute(@RequestBody ShipbillViewIdVO shipbillViewIdVO) {
		try {
			ShipbillViewlByIdDTO dto = service.viewShipbilllByIdData(shipbillViewIdVO);
			return RestMessage.newInstance(true, "查询成功！", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单批量作废")
	@ApiOperation(value = "运单批量作废", notes = "运单批量作废")
	@PostMapping("/invalid")
	public RestMessage<String> invalidShipbillExecute(@RequestBody List<String> ids) {
		try {
			service.invalidShipbillData(ids);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单修改保存")
	@ApiOperation(value = "运单修改保存", notes = "运单修改保存")
	@PostMapping("/modify")
	public RestMessage<String> modifyShipbillHeaderExecute(@RequestBody ShipbillModifyVO shipbillModifyVO) {
		try {
			service.modifyShipbillHeaderData(shipbillModifyVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单修改保存提交")
	@ApiOperation(value = "运单修改保存提交", notes = "运单修改保存提交")
	@PostMapping("/modify_submit")
	public RestMessage<String> modifySubmitShipbillHeaderExecute(@RequestBody ShipbillModifySubmitVO shipbillModifySubmitVO) {
		try {
			service.modifySubmitShipbillHeaderData(shipbillModifySubmitVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单复制保存")
	@ApiOperation(value = "运单复制保存", notes = "运单复制保存")
	@PostMapping("/copy")
	public RestMessage<String> copyShipbillExecute(@RequestBody ShipbillCopyVO shipbillCopyVO) {
		try {
			String shipbillid = service.copyShipbillData(shipbillCopyVO);
			return RestMessage.newInstance(true, "保存成功", shipbillid);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单复制保存提交")
	@ApiOperation(value = "运单复制保存提交", notes = "运单复制保存提交")
	@PostMapping("/copy_submit")
	public RestMessage<String> copySubmitShipbillExecute(@RequestBody ShipbillCopySubmitVO shipbillCopySubmitVO) {
		try {
			String shipbillid = service.copySubmitShipbillData(shipbillCopySubmitVO);
			return RestMessage.newInstance(true, "提交成功", shipbillid);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单地址解析查询")
	@ApiOperation(value = "运单地址解析查询", notes = "运单地址解析查询")
	@PostMapping("/address_resolution/page")
	public RestMessage<Page<ShipbillAddressResolutionPageDTO>> pageAddressResolutionExecute(@RequestBody ShipbillAddressResolutionPageVO shipbillAddressResolutionPageVO) {
		try {
			Page<ShipbillAddressResolutionPageDTO> shipbillAddressResolutionPageDTOs = service.pageAddressResolutionData(shipbillAddressResolutionPageVO);
			return RestMessage.newInstance(true, "查询成功", shipbillAddressResolutionPageDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单地址解析")
	@ApiOperation(value = "运单地址解析", notes = "运单地址解析")
	@PostMapping("/parse_address")
	public RestMessage<String> addressResolutionExecute(@RequestBody ShipbillParseAddressVO shipbillParseAddressVO) {
		try {
			service.addressResolutionData(shipbillParseAddressVO);
			return RestMessage.newInstance(true, "提交成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单打印")
	@ApiOperation(value = "运单打印", notes = "运单打印")
	@PostMapping("/shipbillPrint")
	public RestMessage<Map<String, Object>> shipbillPrintExecute(@RequestBody ShipbillPrintVO shipbillPrintVO) {
		try {
			Map<String, Object> map = service.shipbillPrintData(shipbillPrintVO);
			return RestMessage.newInstance(true, "打印成功", map);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("导出所有数据")
	@ApiOperation(value = "导出所有数据", notes = "导出所有数据")
	@PostMapping("/shipbill_export_all")
	public RestMessage<Object> exportAllShipbillExecute(HttpServletResponse response, @RequestBody ShipbillExportAllVO shipbillExportAllVO) {
		try {
			List<ExportShipbillDTO> list = service.exportAllShipbillData(shipbillExportAllVO);
			ExcelUtils.export(response, ExportShipbillDTO.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("/shipbill_export_page")
	public RestMessage<Object> exportPageShipbillExecute(HttpServletResponse response, @RequestBody ShipbillExportPageVO shipbillExportPageVO) {
		try {
			List<ExportShipbillDTO> list = service.exportPageShipbillData(shipbillExportPageVO);
			ExcelUtils.export(response, ExportShipbillDTO.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("/shipbill_export_choose")
	public RestMessage<Object> exportChooseShipbillExecute(HttpServletResponse response, @RequestBody List<String> ids) {
		try {
			List<ExportShipbillDTO> list = service.exportChooseShipbillData(ids);
			ExcelUtils.export(response, ExportShipbillDTO.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("/download_template")
	public RestMessage<Object> downloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, ShipbillImportVO.class, "运单模板", null, null, null, true);
		return null;
	}

	@UserLog("运单导入")
	@ApiOperation(value = "运单导入", notes = "运单导入")
	@PostMapping("/import")
	public RestMessage<Object> importShipbillExecute(@RequestParam("file") MultipartFile file, @RequestParam("templateId") String templateId, HttpServletRequest httpServletRequest) {
		try {
			String importBatchNumber = service.importShipbillData(file, templateId);
			return RestMessage.newInstance(true, "导入中，导入批次【" + importBatchNumber + "】,您可以去导入日志下载导入结果", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单审核信息查询")
	@ApiOperation(value = "运单审核信息查询", notes = "运单审核信息查询")
	@PostMapping("/get_approval_info")
	public RestMessage<ShipbillApprovalInfoDTO> getApprovalInfoExecute(@RequestBody ShipbillApprovalInfoVO shipbillApprovalInfoVO) {
		try {
			ShipbillApprovalInfoDTO shipbillApprovalInfoDTO = service.getApprovalInfoData(shipbillApprovalInfoVO);
			return RestMessage.newInstance(true, "查询成功", shipbillApprovalInfoDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单审批作废")
	@ApiOperation(value = "运单审批作废", notes = "运单审批作废")
	@PostMapping("/shipbill_approval_invalid")
	public RestMessage<Object> shipbillApprovalInvalidExecute(@RequestBody ShipbillApprovalInvalidVO shipbillApprovalInvalidVO) {
		try {
			service.shipbillApprovalInvalidData(shipbillApprovalInvalidVO);
			return RestMessage.newInstance(true, "作废成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单审批撤回")
	@ApiOperation(value = "运单审批撤回", notes = "运单审批撤回")
	@PostMapping("/shipbill_approval_retract")
	public RestMessage<Object> shipbillApprovalRetractExecute(@RequestBody ShipbillApprovalRetractVO shipbillApprovalRetractVO) {
		try {
			service.shipbillApprovalRetractData(shipbillApprovalRetractVO);
			return RestMessage.newInstance(true, "撤回成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单审批提交")
	@ApiOperation(value = "运单审批提交", notes = "运单审批提交")
	@PostMapping("/shipbill_approval_submit")
	public RestMessage<Object> shipbillApprovalSubmitExecute(@RequestBody ShipbillApprovalSubmitVO shipbillApprovalSubmitVO) {
		try {
			service.shipbillApprovalSubmitData(shipbillApprovalSubmitVO);
			return RestMessage.newInstance(true, "提交成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单审核")
	@ApiOperation(value = "运单审核", notes = "运单审核")
	@PostMapping("/shipbill_approval")
	public RestMessage<Object> shipbillApprovalExecute(@RequestBody ShipbillApprovalVO shipbillApprovalVO) {
		try {
			service.shipbillApprovalData(shipbillApprovalVO);
			return RestMessage.newInstance(true, "审核成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单接单")
	@ApiOperation(value = "运单接单", notes = "运单接单")
	@PostMapping("/receive")
	public RestMessage<String> receiveShipbillDataExecute(@RequestBody List<String> ids) {
		try {
			service.receiveShipbillData(ids);
			return RestMessage.newInstance(true, "接单操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单拒绝")
	@ApiOperation(value = "运单拒绝", notes = "运单拒绝")
	@PostMapping("/reject")
	public RestMessage<String> rejectShipbillDataExecute(@RequestBody List<String> ids) {
		try {
			service.rejectShipbillData(ids);
			return RestMessage.newInstance(true, "拒绝操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单资料接口转TMS")
	@ApiOperation(value = "运单资料接口转TMS", notes = "运单资料接口转TMS")
	@PostMapping("/save/interface")
	public RestMessage<List<IntfErrorLogDTO>> saveShipbillInterfaceExecute(@RequestBody List<IntfShipbillHeaderVO> intfShipbillHeaderVOs) {
		try {
			List<IntfErrorLogDTO> intfErrorLogDTOs = service.saveShipbillInterfaceData(intfShipbillHeaderVOs);
			return RestMessage.newInstance(true, "保存成功", intfErrorLogDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单资包裹料接口转TMS")
	@ApiOperation(value = "运单资包裹料接口转TMS", notes = "运单资包裹料接口转TMS")
	@PostMapping("/save/package/interface")
	public RestMessage<List<IntfErrorLogDTO>> saveShipbillInterfacePackageExecute(@RequestBody List<IntfShipbillPackageVO> intfShipbillPackageVOs) {
		try {
			List<IntfErrorLogDTO> intfErrorLogDTOs = service.saveShipbillInterfacePackageData(intfShipbillPackageVOs);
			return RestMessage.newInstance(true, "保存成功", intfErrorLogDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单日志")
	@ApiOperation(value = "运单日志", notes = "运单日志")
	@PostMapping("/log")
	public RestMessage<Object> addLog(@RequestBody ShipbillOperateLogSaveNewVO shipbillHeaderVOs) {
		try {
			service.addLog(shipbillHeaderVOs);
			return RestMessage.newInstance(true, "处理成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("通过运单ID查询运单号、装车单号等信息")
	@ApiOperation(value = "通过运单ID查询运单号、装车单号等信息", notes = "通过运单ID查询运单号、装车单号等信息")
	@PostMapping("/getInfoByShipbillid")
	public RestMessage<ShipbillAndLoadingbillInfoDTO> getInfoByShipbillid(@RequestParam(value = "shipbillId") String shipbillId) throws Exception {
		ShipbillAndLoadingbillInfoDTO data = service.getInfoByShipbillid(shipbillId);
		return RestMessage.newInstance(true, "查询成功！", data);
	}

	@UserLog("通过运单ID查询所有父级运单信息")
	@ApiOperation(value = "通过运单ID查询所有父级运单信息", notes = "通过运单ID查询所有父级运单信息")
	@PostMapping("/getAllShipBillById")
	public RestMessage<List<ShipAndLoadingBillDTO>> getAllShipBillById(@RequestParam(value = "shipBillId") String shipBillId) {
		List<ShipAndLoadingBillDTO> list = service.getAllShipBillById(shipBillId);
		return RestMessage.newInstance(true, "查询成功！", list);
	}

	@UserLog("运单匹配业务规则")
	@ApiOperation(value = "运单匹配业务规则", notes = "运单匹配业务规则")
	@PostMapping("/match")
	public RestMessage<ShipbillBusinessRuleDTO> matchShipbillBusinessRuleExecute(@RequestBody ShipbillHeaderSaveVO shipbillHeaderSaveVO) {
		try {
			ShipbillBusinessRuleDTO businessRule = service.matchShipbillBusinessRule(shipbillHeaderSaveVO);
			return RestMessage.newInstance(true, "匹配业务规则成功！", businessRule);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("运单温度轨迹")
	@ApiOperation(value = "运单温度轨迹", notes = "运单温度轨迹")
	@PostMapping("/getShipBillTemperature")
	public RestMessage<ShipBillTemperatureDTO> getShipBillTemperature(@RequestParam(value = "shipBillId") String shipBillId) {
		ShipBillTemperatureDTO dto = service.getShipBillTemperature(shipBillId);
		return RestMessage.newInstance(true, "查询成功！", dto);
	}

	@UserLog("运单修改定位")
	@ApiOperation(value = "运单修改定位", notes = "运单修改定位")
	@PostMapping("/modify_location")
	public RestMessage<String> modifyShipbillHeaderLocationExecute(@RequestBody List<ShipbillLocationVO> shipbillLocationVOS) {
		try {
			service.modifyShipbillHeaderLocationData(shipbillLocationVOS);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("业务规则匹配-是否显示温度")
	@ApiOperation(value = "业务规则匹配-是否显示温度", notes = "业务规则匹配-是否显示温度")
	@PostMapping("/showTemperature")
	public RestMessage<StrategyBusinessTemperatureDTO> showTemperature(@RequestBody List<String> shipBillIdList) {
		return RestMessage.newInstance(true, "查询成功", service.showTemperature(shipBillIdList));
	}

	@UserLog("TMS运单轨迹追踪接口")
	@ApiOperation(value = "TMS运单轨迹追踪接口", notes = "TMS运单轨迹追踪接口")
	@PostMapping("/getTracking")
	public RestMessage<List<ShipbillTrackingDTO>> getShipBillTrackingExecute(@RequestBody ShipbillTrackVO vo) {
		try {
			List<ShipbillTrackingDTO> trackingDTOS = service.getShipBillTracking(vo);
			return RestMessage.newInstance(true, "获取运单轨迹数据成功", trackingDTOS);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, "获取运单轨迹数据失败：" + e.getMessage(), null);
		}
	}

}
