package com.prolog.rdc.tms.controller;

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

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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 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.DispatchCkApprovalSCDTO;
import com.prolog.rdc.tms.model.dto.ExportSendCarDTO;
import com.prolog.rdc.tms.model.dto.LoadingBillMapDTO;
import com.prolog.rdc.tms.model.dto.LoadingSiteInfoDTO;
import com.prolog.rdc.tms.model.dto.SendCarItemPageDTO;
import com.prolog.rdc.tms.model.dto.SendCarMapDTO;
import com.prolog.rdc.tms.model.dto.SendCarPackagePageDTO;
import com.prolog.rdc.tms.model.dto.SendCarPageDTO;
import com.prolog.rdc.tms.model.dto.SendCarPageDetailDTO;
import com.prolog.rdc.tms.model.dto.SendCarShipbillPopDTODTO;
import com.prolog.rdc.tms.model.dto.SendCarViewByIdDTO;
import com.prolog.rdc.tms.model.dto.SendCarViewDTO;
import com.prolog.rdc.tms.model.dto.ShipbillHeaderPageDTO;
import com.prolog.rdc.tms.model.vo.DispatchApprovalInfoVO;
import com.prolog.rdc.tms.model.vo.DispatchApprovalRetractVO;
import com.prolog.rdc.tms.model.vo.DispatchApprovalSubmitVO;
import com.prolog.rdc.tms.model.vo.DispatchApprovalVO;
import com.prolog.rdc.tms.model.vo.DispatchCkApprovalInvalidVO;
import com.prolog.rdc.tms.model.vo.LoadingBillMapVO;
import com.prolog.rdc.tms.model.vo.LoadingSiteInfoQueryVO;
import com.prolog.rdc.tms.model.vo.SendCarAddDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarAdsQueryVO;
import com.prolog.rdc.tms.model.vo.SendCarCalculateMileageVO;
import com.prolog.rdc.tms.model.vo.SendCarDeleteDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarExportAllVO;
import com.prolog.rdc.tms.model.vo.SendCarExportPageVO;
import com.prolog.rdc.tms.model.vo.SendCarItemDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarMobileDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarPackageDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarPageDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarPageVO;
import com.prolog.rdc.tms.model.vo.SendCarShipbillPopVO;
import com.prolog.rdc.tms.model.vo.SendCarUpdateAddDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarUpdateDeleteDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarUpdateMobileDetailVO;
import com.prolog.rdc.tms.model.vo.SendCarUpdateVO;
import com.prolog.rdc.tms.service.SendCarService;
import com.prolog.wms.base.util.ExcelUtils;

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

/**
 * 已派车控制层
 *
 * @author zhangsj
 * @since 2022-04-13 14:35:06
 */
@RestController
@Api(tags = "调度管理-已派车控制层")
@RequestMapping("sendcar")
public class SendCarController {

	@Autowired
	private SendCarService service;

	@UserLog("已派车分页查询")
	@ApiOperation(value = "已派车分页查询", notes = "已派车分页查询")
	@PostMapping("/sendcar_page")
	public RestMessage<Page<SendCarPageDTO>> sendcarPageExecute(@RequestBody SendCarPageVO sendCarPageVO) {
		try {
			Page<SendCarPageDTO> page = service.sendcarPageData(sendCarPageVO);
			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<SendCarPageDTO>> pageAdvancedExecute(@RequestBody SendCarAdsQueryVO advancedQueryVO) {
		try {
			Page<SendCarPageDTO> 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("/sendcar_undo")
	public RestMessage<Object> sendcarUndoExecute(@RequestBody List<String> ids) {
		try {
			service.sendcarUndoData(ids);
			return RestMessage.newInstance(true, "撤销成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

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

	@UserLog("已派车地图查询")
	@ApiOperation(value = "已派车地图查询", notes = "已派车地图查询")
	@PostMapping("/sendcar_map_query")
	public RestMessage<List<SendCarMapDTO>> sendcarMapQueryExecute(@RequestBody List<String> ids) {
		try {
			List<SendCarMapDTO> list = service.sendcarMapQueryData(ids);
			return RestMessage.newInstance(true, "查询成功！", list);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车计算里程")
	@ApiOperation(value = "已派车计算里程", notes = "已派车计算里程")
	@PostMapping("/calculate_mileage")
	public RestMessage<Object> calculateMileageExecute(@RequestBody List<SendCarCalculateMileageVO> sendCarCalculateMileageVOS) {
		try {
			service.calculateMileageData(sendCarCalculateMileageVOS);
			return RestMessage.newInstance(true, "计算成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("导出所有数据")
	@ApiOperation(value = "导出所有数据", notes = "导出所有数据")
	@PostMapping("/sendcar_export_all")
	public RestMessage<Object> exportAllSendCarExecute(HttpServletResponse response, @RequestBody SendCarExportAllVO SendCarExportAllVO) {
		try {
			List<ExportSendCarDTO> list = service.exportAllSendCarData(SendCarExportAllVO);
			ExcelUtils.export(response, ExportSendCarDTO.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("/sendcar_export_page")
	public RestMessage<Object> exportPageSendCarExecute(HttpServletResponse response, @RequestBody SendCarExportPageVO SendCarExportPageVO) {
		try {
			List<ExportSendCarDTO> list = service.exportPageSendCarData(SendCarExportPageVO);
			ExcelUtils.export(response, ExportSendCarDTO.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("/sendcar_export_choose")
	public RestMessage<Object> exportChooseSendCarExecute(HttpServletResponse response, @RequestBody List<String> ids) {
		try {
			List<ExportSendCarDTO> list = service.exportChooseSendCarData(ids);
			ExcelUtils.export(response, ExportSendCarDTO.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("/sendcar_print")
	public RestMessage<Map<String, Object>> printSendCarExecute(@RequestParam("id") String id) {
		try {
			Map<String, Object> map = service.printSendCarData(id);
			return RestMessage.newInstance(true, "打印成功！", map);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("派车单查看")
	@ApiOperation(value = "派车单查看", notes = "派车单查看")
	@PostMapping("/sendcar_view")
	public RestMessage<SendCarViewDTO> viewSendCarExecute(@RequestParam("id") String id) {
		try {
			SendCarViewDTO dto = service.viewSendCarData(id);
			return RestMessage.newInstance(true, "查看成功！", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("派车单根据单号查看")
	@ApiOperation(value = "派车单根据单号查看", notes = "派车单根据单号查看")
	@PostMapping("/sendcar_view_loadingbillid")
	public RestMessage<SendCarViewByIdDTO> viewSendCarByIdExecute(@RequestParam("id") String id) {
		try {
			SendCarViewByIdDTO dto = service.viewSendCarByIdData(id);
			return RestMessage.newInstance(true, "查看成功！", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车配载运单查询")
	@ApiOperation(value = "已派车配载运单查询", notes = "已派车配载运单查询")
	@PostMapping("/sendcar_page_detail")
	public RestMessage<Page<SendCarPageDetailDTO>> pageDetailSendCarExecute(@RequestBody SendCarPageDetailVO sendCarPageDetailVO) {
		try {
			Page<SendCarPageDetailDTO> page = service.pageDetailSendCarData(sendCarPageDetailVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车配载运单添加")
	@ApiOperation(value = "已派车配载运单添加", notes = "已派车配载运单添加")
	@PostMapping("/sendcar_add_detail")
	public RestMessage<List<ShipbillHeaderPageDTO>> addDetailSendCarExecute(@RequestBody SendCarAddDetailVO sendCarAddDetailVO) {
		try {
			List<ShipbillHeaderPageDTO> shipbillHeaderPageDTOS = service.addDetailSendCarData(sendCarAddDetailVO);
			return RestMessage.newInstance(true, "添加成功！", shipbillHeaderPageDTOS);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车配载运单剔除")
	@ApiOperation(value = "已派车配载运单剔除", notes = "已派车配载运单剔除")
	@PostMapping("/sendcar_delete_detail")
	public RestMessage<Object> deleteDetailSendCarExecute(@RequestBody SendCarDeleteDetailVO sendCarDeleteDetailVO) {
		try {
			service.deleteDetailSendCarData(sendCarDeleteDetailVO);
			return RestMessage.newInstance(true, "剔除成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车配载运单移动")
	@ApiOperation(value = "已派车配载运单移动", notes = "已派车配载运单移动")
	@PostMapping("/sendcar_mobile_detail")
	public RestMessage<Object> mobileDetailSendCarExecute(@RequestBody SendCarMobileDetailVO sendCarMobileDetailVO) {
		try {
			service.mobileDetailSendCarData(sendCarMobileDetailVO);
			return RestMessage.newInstance(true, "移动成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车配载商品查询")
	@ApiOperation(value = "已派车配载商品查询", notes = "已派车配载商品查询")
	@PostMapping("/sendcar_item_detail")
	public RestMessage<Page<SendCarItemPageDTO>> itemDetailSendCarExecute(@RequestBody SendCarItemDetailVO sendCarItemDetailVO) {
		try {
			Page<SendCarItemPageDTO> page = service.itemDetailSendCarData(sendCarItemDetailVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车配载包裹查询")
	@ApiOperation(value = "已派车配载包裹查询", notes = "已派车配载包裹查询")
	@PostMapping("/sendcar_package_detail")
	public RestMessage<Page<SendCarPackagePageDTO>> packageDetailSendCarExecute(@RequestBody SendCarPackageDetailVO sendCarPackageDetailVO) {
		try {
			Page<SendCarPackagePageDTO> page = service.packageDetailSendCarData(sendCarPackageDetailVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车修改")
	@ApiOperation(value = "已派车修改", notes = "已派车修改")
	@PostMapping("/sendcar_update")
	public RestMessage<Object> updateSendCarExecute(@RequestBody SendCarUpdateVO sendCarUpdateVO) {
		try {
			service.updateSendCarData(sendCarUpdateVO);
			return RestMessage.newInstance(true, "修改成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车修改—运单添加")
	@ApiOperation(value = "已派车修改—运单添加", notes = "已派车修改—运单添加")
	@PostMapping("/sendcar_update_add_detail")
	public RestMessage<List<ShipbillHeaderPageDTO>> updateAddDetailSendCarExecute(@RequestBody SendCarUpdateAddDetailVO sendCarUpdateAddDetailVO) {
		try {
			List<ShipbillHeaderPageDTO> shipbillHeaderPageDTOS = service.updateAddDetailSendCarData(sendCarUpdateAddDetailVO);
			return RestMessage.newInstance(true, "添加成功！", shipbillHeaderPageDTOS);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车修改—运单剔除")
	@ApiOperation(value = "已派车修改—运单剔除", notes = "已派车修改—运单剔除")
	@PostMapping("/sendcar_update_delete_detail")
	public RestMessage<Object> updateDeleteDetailSendCarExecute(@RequestBody SendCarUpdateDeleteDetailVO sendCarUpdateDeleteDetailVO) {
		try {
			service.updateDeleteDetailSendCarData(sendCarUpdateDeleteDetailVO);
			return RestMessage.newInstance(true, "剔除成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车修改—运单移动")
	@ApiOperation(value = "已派车修改—运单移动", notes = "已派车修改—运单移动")
	@PostMapping("/sendcar_update_mobile_detail")
	public RestMessage<Object> updateMobileDetailSendCarExecute(@RequestBody SendCarUpdateMobileDetailVO sendCarUpdateMobileDetailVO) {
		try {
			service.updateMobileDetailSendCarData(sendCarUpdateMobileDetailVO);
			return RestMessage.newInstance(true, "移动成功！", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车运单弹窗")
	@ApiOperation(value = "已派车运单弹窗", notes = "已派车运单弹窗")
	@PostMapping("/shipbill_pop")
	public RestMessage<Page<SendCarShipbillPopDTODTO>> shipbillPopExecute(@RequestBody SendCarShipbillPopVO sendCarShipbillPopVO) {
		try {
			Page<SendCarShipbillPopDTODTO> page = service.shipbillPopData(sendCarShipbillPopVO);
			return RestMessage.newInstance(true, "查询成功！", page);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("调度单审核信息查询")
	@ApiOperation(value = "调度单审核信息查询", notes = "调度单审核信息查询")
	@PostMapping("/sendcar_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("/sendcar_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("/sendcar_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("/sendcar_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("/sendcar_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);
		}
	}

	@UserLog("已派车-运单地图查询")
	@ApiOperation(value = "已派车-运单地图查询", notes = "已派车-运单地图查询")
	@PostMapping("/loadingBillMap")
	public RestMessage<List<LoadingBillMapDTO>> loadingBillMap(@RequestBody List<String> ids) {
		List<LoadingBillMapDTO> list = service.loadingBillMap(ids);
		return RestMessage.newInstance(true, "查询成功！", list);
	}

	@UserLog("已派车-运单地图保存")
	@ApiOperation(value = "已派车-运单地图保存", notes = "已派车-运单地图保存")
	@PostMapping("/saveLoadingBillMapOrder")
	public RestMessage<Object> saveLoadingBillMapOrder(@RequestBody @Valid List<LoadingBillMapVO> loadingBillMapVOList) {
		service.saveLoadingBillMapOrder(loadingBillMapVOList);
		return RestMessage.newInstance(true, "保存成功！", null);
	}

	@UserLog("派车单站点信息查询")
	@ApiOperation(value = "派车单站点信息查询", notes = "派车单站点信息查询")
	@PostMapping("/site_info")
	public RestMessage<List<LoadingSiteInfoDTO>> loadingSiteQueryExecute(@RequestBody LoadingSiteInfoQueryVO vo) {
		try {
			List<LoadingSiteInfoDTO> siteDTOS = service.loadingSiteQuery(vo);
			return RestMessage.newInstance(true, "派车单站点信息查询成功", siteDTOS);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("已派车-反审核")
	@ApiOperation(value = "已派车-反审核", notes = "已派车-反审核")
	@PostMapping("/undoAudit")
	public RestMessage<Object> undoAuditSendCarExecute(@RequestBody List<String> loadingBillIds) {
		try {
			service.undoAuditSendCarData(loadingBillIds);
			return RestMessage.newInstance(true, "反审核成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}
}
