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

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

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

import com.prolog.common.util.ToolKits;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.service.SysCodeengineService;
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.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.dto.fleetManagement.DriverCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverLineCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverPagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverPaperCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverPaperLinkCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverPopupDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverVehicleLinkDTO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverLineCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverModifyDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverPagingQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverPaperAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverPaperCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverPaperDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverPaperLinkCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverPaperModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverPaperWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverPopupVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverVehicleDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverVehicleLinkVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.DriverImportStdVO;
import com.prolog.rdc.tms.service.fleetManagement.DriverService;

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

/**
 *
 *
 * 车队管理-司机资料控制层
 *
 * @className :DriverController
 * @author :fanzw
 * @creationTime :2022/03/15
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/03/15
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Api(tags = "车队管理-司机资料控制层")
@RestController
@RequestMapping("/driver")
public class DriverController {

	@Autowired
	private DriverService driverService;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@UserLog("司机资料分页查询")
	@ApiOperation(value = "司机资料分页查询", notes = "司机资料分页查询")
	@PostMapping("/paging_query")
	public RestMessage<Page<DriverPagingQueryDTO>> pageDriverExecute(@RequestBody DriverPagingQueryVO driverPagingQueryVO) {
		try {
			Page<DriverPagingQueryDTO> driverPagingQueryDTOs = driverService.pageDriverData(driverPagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", driverPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机资料弹窗")
	@ApiOperation(value = "司机资料弹窗", notes = "司机资料弹窗")
	@PostMapping("/popup")
	public RestMessage<Page<DriverPopupDTO>> popupDriverExecute(@RequestBody DriverPopupVO driverPopupVO) {
		try {
			Page<DriverPopupDTO> driverPopupDTOs = driverService.popupDriverData(driverPopupVO);
			return RestMessage.newInstance(true, "查询成功", driverPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("承运商自动调度选择司机")
	@ApiOperation(value = "承运商自动调度选择司机", notes = "承运商自动调度选择司机")
	@PostMapping("/getDriverForCrriersAutoDis")
	public RestMessage<DriverPopupDTO> getDriverForCarriersAutoDis(@RequestBody DriverPopupVO driverPopupVO) {
		try {
			DriverPopupDTO driverPopupDTO = driverService.getDriverForCarriersAutoDis(driverPopupVO);
			return RestMessage.newInstance(true, "查询成功", driverPopupDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机资料查看")
	@ApiOperation(value = "司机资料查看", notes = "司机资料查看")
	@PostMapping("/check")
	public RestMessage<DriverCheckDTO> getDriverExecute(@RequestBody DriverCheckVO driverCheckVO) {
		try {
			DriverCheckDTO driverCheckDTO = driverService.getDriverData(driverCheckVO);
			return RestMessage.newInstance(true, "查询成功", driverCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机资料是否启用")
	@ApiOperation(value = "司机资料是否启用", notes = "司机资料是否启用")
	@PostMapping("/whether_to_enable")
	public RestMessage<String> whetherEnableDriverExecute(@RequestBody DriverWhetherEnableVO driverWhetherEnableVO) {
		try {
			driverService.whetherEnableDriverData(driverWhetherEnableVO);
			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> addDriverExecute(@RequestBody DriverAddVO driverAddVO) {
		try {
			driverService.addDriverData(driverAddVO);
			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> modifyDriverExecute(@RequestBody DriverModifyVO driverModifyVO) {
		try {
			driverService.modifyDriverData(driverModifyVO);
			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> deleteDriverExecute(@RequestBody List<DriverModifyDeleteVO> list) {
		try {
			driverService.deleteDriverData(list);
			return RestMessage.newInstance(true, "操作成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机常跑线路查看")
	@ApiOperation(value = "司机常跑线路查看", notes = "司机常跑线路查看")
	@PostMapping("/line_check")
	public RestMessage<List<DriverLineCheckDTO>> listDriverLineExecute(@RequestBody DriverLineCheckVO driverLineCheckVO) {
		try {
			List<DriverLineCheckDTO> driverLineCheckDTOs = driverService.listDriverLineData(driverLineCheckVO);
			return RestMessage.newInstance(true, "查询成功", driverLineCheckDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("未绑/已绑车辆查询")
	@ApiOperation(value = "未绑/已绑车辆查询", notes = "未绑/已绑车辆查询")
	@PostMapping("/vehicle_binding")
	public RestMessage<List<DriverVehicleLinkDTO>> listDriverVehicleExecute(@RequestBody DriverVehicleLinkVO driverVehicleLinkVO) {
		try {
			List<DriverVehicleLinkDTO> driverVehicleLinkDTOs = driverService.listDriverVehicleData(driverVehicleLinkVO);
			return RestMessage.newInstance(true, "查询成功", driverVehicleLinkDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机绑定车辆删除")
	@ApiOperation(value = "司机绑定车辆删除", notes = "司机绑定车辆删除")
	@PostMapping("/vehicle_delete")
	public RestMessage<String> deleteDriverVehicleExecute(@RequestBody List<DriverVehicleDeleteVO> driverVehicleDeleteVOs) {
		try {
			driverService.deleteDriverVehicleData(driverVehicleDeleteVOs);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机证件关联查看")
	@ApiOperation(value = "司机证件关联查看", notes = "司机证件关联查看")
	@PostMapping("/paper_link_check")
	public RestMessage<List<DriverPaperLinkCheckDTO>> listDriverPaperLinkCheckExecute(@RequestBody DriverPaperLinkCheckVO driverPaperLinkCheckVO) {
		try {
			List<DriverPaperLinkCheckDTO> driverPaperLinkCheckDTOs = driverService.listDriverPaperLinkCheckData(driverPaperLinkCheckVO);
			return RestMessage.newInstance(true, "查询成功", driverPaperLinkCheckDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机证件资料查看")
	@ApiOperation(value = "司机证件资料查看", notes = "司机证件资料查看")
	@PostMapping("/paper_check")
	public RestMessage<DriverPaperCheckDTO> getDriverPaperCheckExecute(@RequestBody DriverPaperCheckVO driverPaperCheckVO) {
		try {
			DriverPaperCheckDTO driverPaperCheckDTO = driverService.getDriverPaperCheckData(driverPaperCheckVO);
			return RestMessage.newInstance(true, "查询成功", driverPaperCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机证件资料新增")
	@ApiOperation(value = "司机证件资料新增", notes = "司机证件资料新增")
	@PostMapping("/paper_add")
	public RestMessage<String> addDriverPaperExecute(@RequestBody DriverPaperAddVO driverPaperAddVO) {
		try {
			driverService.addDriverPaperData(driverPaperAddVO);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机证件资料修改")
	@ApiOperation(value = "司机证件资料修改", notes = "司机证件资料修改")
	@PostMapping("/paper_modify")
	public RestMessage<String> modifyDriverPaperExecute(@RequestBody DriverPaperModifyVO driverPaperModifyVO) {
		try {
			driverService.modifyDriverPaperData(driverPaperModifyVO);
			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> whetherEnableDriverPaperExecute(@RequestBody DriverPaperWhetherEnableVO driverPaperWhetherEnableVO) {
		try {
			driverService.whetherEnableDriverPaperData(driverPaperWhetherEnableVO);
			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> deleteDriverPaperExecute(@RequestBody List<DriverPaperDeleteVO> driverPaperDeleteVOs) {
		try {
			driverService.deleteDriverPaperData(driverPaperDeleteVOs);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("司机批量导入")
	@ApiOperation(value = "司机批量导入", notes = "司机批量导入")
	@PostMapping("/batchImportDriver")
	public RestMessage<Object> batchImportDriverExcel(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception {
		// 导入批次
		String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
		ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
		driverService.batchImportDriverData(file,importBatchNumber);
		return RestMessage.newInstance(true, "导入成功", null);
	}

	@UserLog("司机模板")
	@ApiOperation(value = "司机模板", notes = "司机模板")
	@PostMapping("/download_template")
	public RestMessage<Object> downloadTemplateExecute(HttpServletResponse response) throws IOException {
		ExcelUtils.export(response, DriverImportStdVO.class, "司机模板", null, null, null, true);
		return null;
	}

	@UserLog("司机资料弹窗自提专用")
	@ApiOperation(value = "司机资料弹窗自提专用", notes = "司机资料弹窗自提专用")
	@PostMapping("/popup_self_reg")
	public RestMessage<Page<DriverPopupDTO>> popupDriverSelfRegExecute(@RequestBody DriverPopupVO driverPopupVO) {
		try {
			Page<DriverPopupDTO> driverPopupDTOs = driverService.popupDriverSelfRegData(driverPopupVO);
			return RestMessage.newInstance(true, "查询成功", driverPopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
