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.VehicleTypeCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleTypePagingQueryDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleTypePopupDTO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypeAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypeCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypeDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypeModifyVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypePagingQueryVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypePopupVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypeWhetherEnableVO;
import com.prolog.rdc.tms.model.vo.xlsimport.VehicleTypeImportVO;
import com.prolog.rdc.tms.service.fleetManagement.VehicleTypeService;

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

/**
 * 车队管理-车型管理控制层
 *
 * @author Eton Woo
 * @date 2022/03/11
 */
@Api(tags = "车队管理-车型管理控制层")
@RestController
@RequestMapping("/vehicle_type")
public class VehicleTypeController {

	@Autowired
	private VehicleTypeService vehicleTypeService;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	@UserLog("车型资料分页查询")
	@ApiOperation(value = "车型资料分页查询", notes = "车型资料分页查询")
	@PostMapping("/paging_query")
	public RestMessage<Page<VehicleTypePagingQueryDTO>> pageVehicleTypeExecute(@RequestBody VehicleTypePagingQueryVO vehicleTypePagingQueryVO) {
		try {
			Page<VehicleTypePagingQueryDTO> itemPagingQueryDTOs = vehicleTypeService.pageVehicleTypeData(vehicleTypePagingQueryVO);
			return RestMessage.newInstance(true, "查询成功", itemPagingQueryDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车型资料弹窗")
	@ApiOperation(value = "车型资料弹窗", notes = "车型资料弹窗")
	@PostMapping("/popup")
	public RestMessage<Page<VehicleTypePopupDTO>> popupVehicleExecute(@RequestBody VehicleTypePopupVO vehicleTypePopupVO) {
		try {
			Page<VehicleTypePopupDTO> vehicleTypePopupDTOs = vehicleTypeService.popupVehicleTypeData(vehicleTypePopupVO);
			return RestMessage.newInstance(true, "查询成功", vehicleTypePopupDTOs);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车型资料查看")
	@ApiOperation(value = "车型资料查看", notes = "车型资料查看")
	@PostMapping("/check")
	public RestMessage<VehicleTypeCheckDTO> getVehicleTypeExecute(@RequestBody VehicleTypeCheckVO vehicleTypeCheckVO) {
		try {
			VehicleTypeCheckDTO vehicleTypeCheckDTO = vehicleTypeService.getVehicleTypeData(vehicleTypeCheckVO);
			return RestMessage.newInstance(true, "查询成功", vehicleTypeCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("根据载重载方查询车型资料")
	@ApiOperation(value = "根据载重载方查询车型资料", notes = "根据载重载方查询车型资料")
	@PostMapping("/queryByWeightVolume")
	public RestMessage<VehicleTypeCheckDTO> queryByWeightVolumeExecute(@RequestBody VehicleTypeCheckVO vehicleTypeCheckVO) {
		try {
			VehicleTypeCheckDTO vehicleTypeCheckDTO = vehicleTypeService.queryVehicleTypeByWeightVolume(vehicleTypeCheckVO);
			return RestMessage.newInstance(true, "查询成功", vehicleTypeCheckDTO);
		} 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 VehicleTypeWhetherEnableVO vehicleTypeWhetherEnableVO) {
		try {
			vehicleTypeService.whetherEnableVehicleTypeData(vehicleTypeWhetherEnableVO);
			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> addVehicleTypeExecute(@RequestBody VehicleTypeAddVO vehicleTypeAddVO) {
		try {
			vehicleTypeService.addVehicleTypeData(vehicleTypeAddVO);
			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> modifyVehicleTypeExecute(@RequestBody VehicleTypeModifyVO vehicleTypeModifyVO) {
		try {
			vehicleTypeService.modifyVehicleTypeData(vehicleTypeModifyVO);
			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> deleteVehicleTypeExecute(@RequestBody List<VehicleTypeDeleteVO> list) {
		try {
			vehicleTypeService.deleteVehicleTypeData(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, VehicleTypeImportVO.class, "车型模板导入", null, null, null, true);
		return null;
	}

	@UserLog("车型导入")
	@ApiOperation(value = "车型导入", notes = "车型导入")
	@PostMapping("/batchImportVehicleType")
	public RestMessage<Object> batchImportVehicleTypeExcel(@RequestParam("file") MultipartFile file, HttpServletRequest req) throws Exception{
		// 导入批次
		String importBatchNumber = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.IMPORT_BATCHNO, "L");
		ToolKits.isParamNull(importBatchNumber, "导入批次获取失败");
		vehicleTypeService.batchImportVehicleTypeData(file,importBatchNumber);
		return RestMessage.newInstance(true, "导入成功", null);
	}

	@UserLog("根据车型名称查看车型资料")
	@ApiOperation(value = "根据车型名称查看车型资料", notes = "根据车型名称查看车型资料")
	@PostMapping("/by_name")
	public RestMessage<VehicleTypeCheckDTO> getVehicleTypeByNameExecute(@RequestBody VehicleTypeCheckVO vehicleTypeCheckVO) {
		try {
			VehicleTypeCheckDTO vehicleTypeCheckDTO = vehicleTypeService.getVehicleTypeByNameData(vehicleTypeCheckVO);
			return RestMessage.newInstance(true, "查询成功", vehicleTypeCheckDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

}
