package com.prolog.rdc.tms.controller.fleetManagement;

import com.prolog.common.util.ToolKits;
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.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.model.dto.fleetManagement.*;
import com.prolog.rdc.tms.model.vo.fleetManagement.*;
import com.prolog.rdc.tms.model.vo.xlsimport.VehicleImportStdVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.fleetManagement.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 车队管理-车辆管理控制层
 *
 * @author Eton Woo
 * @date 2022/03/15
 */
@Api(tags = "车队管理-车辆管理控制层")
@RestController
@RequestMapping("/vehicle")
public class VehicleController {

	@Autowired
	private VehicleService vehicleService;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@UserLog("车辆资料分页查询")
	@ApiOperation(value = "车辆资料分页查询", notes = "车辆资料分页查询")
	@PostMapping("/paging_query")
	public RestMessage<Page<VehiclePagingQueryDTO>> pageVehicleExecute(@RequestBody VehiclePagingQueryVO vehiclePagingQueryVO) {
		try {
			Page<VehiclePagingQueryDTO> vehiclePagingQueryDTOs = vehicleService.pageVehicleData(vehiclePagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", vehiclePagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆资料弹窗")
	@ApiOperation(value = "车辆资料弹窗", notes = "车辆资料弹窗")
	@PostMapping("/popup")
	public RestMessage<Page<VehiclePopupDTO>> popupVehicleExecute(@RequestBody VehiclePopupVO vehiclePopupVO) {
		try {
			Page<VehiclePopupDTO> vehiclePopupDTOs = vehicleService.popupVehicleData(vehiclePopupVO);
			return RestMessage.newInstance(true, "查询成功", vehiclePopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}
	
	@UserLog("车辆-已绑司机资料分页查询")
	@ApiOperation(value = "车辆-已绑司机资料分页查询", notes = "车辆-已绑司机资料分页查询")
	@PostMapping("/paging_query_driver")
	public RestMessage<Page<VehicleDriverPagingQueryDTO>> pageVehicleDriverExecute(@RequestBody VehicleDriverPagingQueryVO vehicleDriverPagingQueryVO) {
		try {
			Page<VehicleDriverPagingQueryDTO> vehicleDriverPagingQueryDTOs = vehicleService.pageVehicleDriverData(vehicleDriverPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", vehicleDriverPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆-未绑司机资料分页查询")
	@ApiOperation(value = "车辆-未绑司机资料分页查询", notes = "车辆-未绑司机资料分页查询")
	@PostMapping("/paging_query_notdriver")
	public RestMessage<Page<VehicleDriverPagingQueryDTO>> pageVehicleNotDriverExecute(@RequestBody VehicleDriverPagingQueryVO vehicleDriverPagingQueryVO) {
		try {
			Page<VehicleDriverPagingQueryDTO> vehicleDriverPagingQueryDTOs = vehicleService.pageVehicleNotDriverData(vehicleDriverPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", vehicleDriverPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆-线路资料分页查询")
	@ApiOperation(value = "车辆-线路资料分页查询", notes = "车辆-线路资料分页查询")
	@PostMapping("/paging_query_line")
	public RestMessage<Page<VehicleLinePagingQueryDTO>> pageVehicleLineExecute(@RequestBody VehicleLinePagingQueryVO vehicleLinePagingQueryVO) {
		try {
			Page<VehicleLinePagingQueryDTO> vehicleLinePagingQueryDTOs = vehicleService.pageVehicleLineData(vehicleLinePagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", vehicleLinePagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆-证件资料分页查询")
	@ApiOperation(value = "车辆-证件资料分页查询", notes = "车辆-证件资料分页查询")
	@PostMapping("/paging_query_paper")
	public RestMessage<Page<VehiclePaperPagingQueryDTO>> pageVehiclePaperExecute(@RequestBody VehiclePaperPagingQueryVO vehiclePaperPagingQueryVO) {
		try {
			Page<VehiclePaperPagingQueryDTO> vehiclePaperPagingQueryDTOs = vehicleService.pageVehiclePaperData(vehiclePaperPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", vehiclePaperPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆-设备资料分页查询")
	@ApiOperation(value = "车辆-设备资料分页查询", notes = "车辆-设备资料分页查询")
	@PostMapping("/paging_query_device")
	public RestMessage<Page<VehicleDeviceLinkViewDTO>> pageVehicleDeviceExecute(@RequestBody VehiclePaperPagingQueryVO vehiclePaperPagingQueryVO) {
		try {
			Page<VehicleDeviceLinkViewDTO> deviceLinkViewVOPage = vehicleService.pageVehicleDeviceData(vehiclePaperPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", deviceLinkViewVOPage);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆-容器资料分页查询")
	@ApiOperation(value = "车辆-容器资料分页查询", notes = "车辆-容器资料分页查询")
	@PostMapping("/pageVehicleContainerData")
	public RestMessage<Page<ContainerDTO>> pageVehicleContainerData(@RequestBody VehiclePaperPagingQueryVO vehiclePaperPagingQueryVO) {
		return RestMessage.newInstance(true, "查询成功", vehicleService.pageVehicleContainerData(vehiclePaperPagingQueryVO));
	}

	@UserLog("车辆-设备资料弹窗查询")
	@ApiOperation(value = "车辆-设备资料弹窗查询", notes = "车辆-设备资料弹窗查询")
	@PostMapping("/vehicle_device_popup")
	public RestMessage<Page<VehicleDeviceLinkViewDTO>> pageVehicleDevicePopupExecute(@RequestBody VehiclePaperPagingQueryVO vehiclePaperPagingQueryVO) {
		try {
			Page<VehicleDeviceLinkViewDTO> deviceLinkViewVOPage = vehicleService.pageVehicleDevicePopupData(vehiclePaperPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", deviceLinkViewVOPage);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("设备资料弹窗查询")
	@ApiOperation(value = "设备资料弹窗查询", notes = "设备资料弹窗查询")
	@PostMapping("/device_popup")
	public RestMessage<Page<VehicleDeviceLinkViewDTO>> pageDevicePopupExecute(@RequestBody VehiclePaperPagingQueryVO vehiclePaperPagingQueryVO) {
		try {
			Page<VehicleDeviceLinkViewDTO> deviceLinkViewVOPage = vehicleService.pageDevicePopupData(vehiclePaperPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", deviceLinkViewVOPage);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆资料查看")
	@ApiOperation(value = "车辆资料查看", notes = "车辆资料查看")
	@PostMapping("/check")
	public RestMessage<VehicleCheckDTO> getVehicleTypeExecute(@RequestBody VehicleCheckVO vehicleCheckVO) {
		try {
			VehicleCheckDTO vehicleCheckDTO = vehicleService.getVehicleData(vehicleCheckVO);
			return RestMessage.newInstance(true, "查询成功", vehicleCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆资料是否启用")
	@ApiOperation(value = "车辆资料是否启用", notes = "车辆资料是否启用")
	@PostMapping("/whether_to_enable")
	public RestMessage<String> whetherEnableVehicleTypeExecute(@RequestBody VehicleWhetherEnableVO vehicleWhetherEnableVO) {
		try {
			vehicleService.whetherEnableVehicleData(vehicleWhetherEnableVO);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆资料新增")
	@ApiOperation(value = "车辆资料新增", notes = "车辆资料新增")
	@PostMapping("/add")
	public RestMessage<String> addVehicleExecute(@RequestBody VehicleAddVO vehicleAddVO) {
		try {
			vehicleService.addVehicleData(vehicleAddVO);
			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> modifyVehicleExecute(@RequestBody VehicleModifyVO vehicleModifyVO) {
		try {
			vehicleService.modifyVehicleData(vehicleModifyVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆资料删除")
	@ApiOperation(value = "车辆资料删除", notes = "车辆资料删除")
	@PostMapping("/delete")
	public RestMessage<String> deleteVehicleExecute(@RequestBody List<VehicleDeleteVO> list) {
		try {
			vehicleService.deleteVehicleData(list);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆绑定司机删除")
	@ApiOperation(value = "车辆绑定司机删除", notes = "车辆绑定司机删除")
	@PostMapping("/driver_delete")
	public RestMessage<String> deleteVehicleDriverExecute(@RequestBody List<VehicleDriverDeleteVO> vehicleDriverDeleteVOs) {
		try {
			vehicleService.deleteVehicleDriverData(vehicleDriverDeleteVOs);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆绑定设备删除")
	@ApiOperation(value = "车辆绑定设备删除", notes = "车辆绑定设备删除")
	@PostMapping("/device_delete")
	public RestMessage<String> deleteVehicleDeviceExecute(@RequestBody List<VehicleDeviceDeleteVO> vehicleDeviceDeleteVOs) {
		try {
			vehicleService.deleteVehicleDeviceData(vehicleDeviceDeleteVOs);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆绑定容器删除")
	@ApiOperation(value = "车辆绑定容器删除", notes = "车辆绑定容器删除")
	@PostMapping("/deleteVehicleContainer")
	public RestMessage<String> deleteVehicleContainer(@RequestBody List<String> idList) {
		vehicleService.deleteVehicleContainerData(idList);
		return RestMessage.newInstance(true, "删除成功", null);
	}

	@UserLog("车辆-证件资料新增")
	@ApiOperation(value = "车辆-证件资料新增", notes = "车辆-证件资料新增")
	@PostMapping("/paper_add")
	public RestMessage<String> addVehiclePaperExecute(@RequestBody VehiclePaperAddVO vehiclePaperAddVO) {
		try {
			vehicleService.addVehiclePaperData(vehiclePaperAddVO);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆证件资料查看")
	@ApiOperation(value = "车辆证件资料查看", notes = "车辆证件资料查看")
	@PostMapping("/paper_check")
	public RestMessage<VehiclePaperCheckDTO> getVehiclePaperCheckExecute(@RequestBody VehiclePaperCheckVO vehiclePaperCheckVO) {
		try {
			VehiclePaperCheckDTO vehiclePaperCheckDTO = vehicleService.getVehiclePaperCheckData(vehiclePaperCheckVO);
			return RestMessage.newInstance(true, "查询成功", vehiclePaperCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆证件资料修改")
	@ApiOperation(value = "车辆证件资料修改", notes = "车辆证件资料修改")
	@PostMapping("/paper_modify")
	public RestMessage<String> modifyVehiclePaperExecute(@RequestBody VehiclePaperModifyVO vehiclePaperModifyVO) {
		try {
			vehicleService.modifyVehiclePaperData(vehiclePaperModifyVO);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆证件资料是否启用")
	@ApiOperation(value = "车辆证件资料是否启用", notes = "车辆证件资料是否启用")
	@PostMapping("/paper_whether_to_enable")
	public RestMessage<String> whetherEnableVehiclePaperExecute(@RequestBody VehiclePaperWhetherEnableVO vehiclePaperWhetherEnableVO) {
		try {
			vehicleService.whetherEnableVehiclePaperData(vehiclePaperWhetherEnableVO);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆证件资料删除")
	@ApiOperation(value = "车辆证件资料删除", notes = "车辆证件资料删除")
	@PostMapping("/paper_delete")
	public RestMessage<String> deleteVehiclePaperExecute(@RequestBody List<VehiclePaperDeleteVO> vehiclePaperDeleteVOs) {
		try {
			vehicleService.deleteVehiclePaperData(vehiclePaperDeleteVOs);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆导入")
	@ApiOperation(value = "车辆批量导入", notes = "车辆批量导入")
	@PostMapping("/batchImportVehicle")
	public RestMessage<Object> batchImportVehicleExcel(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception {
		try {
			// 导入批次
			String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
			ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
			vehicleService.batchImportVehicleData(file, importBatchNumber);
			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, VehicleImportStdVO.class, "车辆模板", null, null, null, true);
		return null;
	}

	@UserLog("司机车辆查询或新增")
	@ApiOperation(value = "司机车辆查询或新增", notes = "司机车辆查询或新增")
	@PostMapping("/automatic")
	public RestMessage<VehicleAndDriverDTO> vehicleAutomaticExecute(@RequestBody VehicleAndVehicleVO vehicleAndVehicleVO) {
		try {
			VehicleAndDriverDTO vehicleAndDriverDTO = vehicleService.vehicleAutomaticData(vehicleAndVehicleVO);
			return RestMessage.newInstance(true, "操作成功", vehicleAndDriverDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆资料弹窗自提专用")
	@ApiOperation(value = "车辆资料弹窗自提专用", notes = "车辆资料弹窗自提专用")
	@PostMapping("/popup_self_reg")
	public RestMessage<Page<VehiclePopupDTO>> popupVehicleSelfRegExecute(@RequestBody VehiclePopupVO vehiclePopupVO) {
		try {
			Page<VehiclePopupDTO> vehiclePopupDTOs = vehicleService.popupVehicleSelfRegData(vehiclePopupVO);
			return RestMessage.newInstance(true, "查询成功", vehiclePopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
