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

import com.prolog.product.core.easyexcel.util.ExcelUtils;
import com.prolog.rdc.tms.model.vo.xlsimport.DeviceImportVO;
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.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.fleetManagement.DeviceByOneDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeviceDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DeviceViewDTO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeviceAddVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeviceDeleteVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DevicePopVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeviceUpdateVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeviceVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeviceViewVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DeviceWhetherEnableVO;
import com.prolog.rdc.tms.service.fleetManagement.DeviceService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 
 *
 * 车队管理-设备资料控制层
 *
 * @className :DeviceController
 * @author :zhangsj
 * @creationTime :2022/03/23
 * @description :
 *
 * @modifier :zhangsj
 * @modifyTime :2022/03/23
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@RestController
@Api(tags = "车队管理-设备资料控制层")
@RequestMapping("/device")
public class DeviceController {
	@Autowired
	private DeviceService service;

	@UserLog("设备资料分页查询")
	@ApiOperation(value = "设备资料分页查询", notes = "设备资料分页查询")
	@PostMapping("/query")
	public RestMessage<Page<DeviceDTO>> query(@RequestBody DeviceVO vo) {
		try {
			Page<DeviceDTO> list = service.query(vo);
			return RestMessage.newInstance(true, "查询成功", list);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("设备资料弹窗")
	@ApiOperation(value = "设备资料弹窗", notes = "设备资料弹窗")
	@PostMapping("/pop")
	public RestMessage<Page<DeviceDTO>> pop(@RequestBody DevicePopVO vo) {
		try {
			Page<DeviceDTO> list = service.pop(vo);
			return RestMessage.newInstance(true, "查询成功", list);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("车辆设备查询")
	@ApiOperation(value = "车辆设备查询", notes = "车辆设备查询")
	@PostMapping("/queryByVehicle")
	public RestMessage<Page<DeviceDTO>> queryByVehicle(@RequestBody DeviceVO vo) {
		return RestMessage.newInstance(true, "查询成功", service.queryByVehicle(vo));
	}

	@UserLog("设备资料查看")
	@ApiOperation(value = "设备资料查看", notes = "设备资料查看")
	@PostMapping("/view")
	public RestMessage<DeviceViewDTO> query(@RequestBody DeviceViewVO vo) {
		try {
			DeviceViewDTO dto = service.view(vo);
			return RestMessage.newInstance(true, "查询成功", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("设备资料新增")
	@ApiOperation(value = "设备资料新增", notes = "设备资料新增")
	@PostMapping("/save")
	public RestMessage<Object> save(@RequestBody DeviceAddVO vo) {
		try {
			service.save(vo);
			return RestMessage.newInstance(true, "新增成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("设备资料修改")
	@ApiOperation(value = "设备资料修改", notes = "设备资料修改")
	@PostMapping("/update")
	public RestMessage<Object> update(@RequestBody DeviceUpdateVO vo) {
		try {
			service.update(vo);
			return RestMessage.newInstance(true, "修改成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("设备资料是否启用")
	@ApiOperation(value = "设备资料是否启用", notes = "设备资料是否启用")
	@PostMapping("/whether_to_enable")
	public RestMessage<Object> whetherEnableExecute(@RequestBody DeviceWhetherEnableVO deviceWhetherEnableVO) {
		try {
			service.whetherEnable(deviceWhetherEnableVO);
			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<Object> deleteExecute(@RequestBody DeviceDeleteVO deviceDeleteVO) {
		try {
			service.delete(deviceDeleteVO);
			return RestMessage.newInstance(true, "删除成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@PostMapping("/getOne")
	@UserLog("设备单个查询")
	@ApiOperation(value = "设备单个查询", notes = "设备单个查询")
	public RestMessage<DeviceByOneDTO> getOne(@RequestParam("id") Long id) {
		try {
			DeviceByOneDTO dto = service.getOne(id);
			return RestMessage.newInstance(true, "查询成功", dto);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}

	@UserLog("设备批量导入")
	@ApiOperation(value = "设备批量导入", notes = "设备批量导入")
	@PostMapping("/importDevice")
	public RestMessage<Object> importDevice(@RequestParam("file") MultipartFile file) {
		service.importDevice(file);
		return RestMessage.newInstance(true, "导入成功", null);
	}

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

	@UserLog("设备资料分页查询")
	@ApiOperation(value = "设备资料查询", notes = "设备资料查询")
	@PostMapping("/device_query")
	public RestMessage<Page<DeviceDTO>> deviceQuery(@RequestBody DeviceVO vo) {
		try {
			Page<DeviceDTO> list = service.deviceQuery(vo);
			return RestMessage.newInstance(true, "查询成功", list);
		} catch (Exception e) {
			e.printStackTrace();
			return RestMessage.newInstance(false, e.getMessage(), null);
		}
	}
}
