package com.pj.project.tb_vehicle;

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

import com.pj.api.dto.ShopDto;
import com.pj.api.dto.VehicleDto;
import com.pj.utils.so.SoMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pj.utils.sg.*;
import com.pj.project4sp.SP;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.multipart.MultipartFile;


/**
 * Controller: tb_vehicle -- 车辆
 * @author loovi 
 */
@RestController
@RequestMapping("/TbVehicle/")
public class TbVehicleController {

	/** 底层 Service 对象 */
	@Autowired
	TbVehicleService tbVehicleService;

	/** 增 */  
	@RequestMapping("add")
	@SaCheckPermission(TbVehicle.PERMISSION_CODE_ADD)
	public AjaxJson add(TbVehicle t){
		tbVehicleService.add(t);
		t = tbVehicleService.getById(SP.publicMapper.getPrimarykey());
		return AjaxJson.getSuccessData(t);
	}

	/** 删 */  
	@RequestMapping("delete")
	@SaCheckPermission(TbVehicle.PERMISSION_CODE_DEL)
	public AjaxJson delete(Long id){
		 tbVehicleService.delete(id);
		return AjaxJson.getSuccess();
	}
	
	/** 删 - 根据id列表 */  
	@RequestMapping("deleteByIds")
	@SaCheckPermission(TbVehicle.PERMISSION_CODE_DEL)
	public AjaxJson deleteByIds(){
		List<Long> ids = SoMap.getRequestSoMap().getListByComma("ids", long.class); 
		int line = SP.publicMapper.deleteByIds(TbVehicle.TABLE_NAME, ids);
		return AjaxJson.getByLine(line);
	}
	
	/** 改 */  
	@RequestMapping("update")
	@SaCheckPermission(TbVehicle.PERMISSION_CODE_EDIT)
	public AjaxJson update(TbVehicle t){
		tbVehicleService.update(t);
		return AjaxJson.getSuccess();
	}

	/** 查 - 根据id */  
	@RequestMapping("getById")
		@SaCheckPermission(TbVehicle.PERMISSION_CODE)
	public AjaxJson getById(Long id){
		TbVehicle t = tbVehicleService.getById(id);
		return AjaxJson.getSuccessData(t);
	}

	/** 查集合 - 根据条件（参数为空时代表忽略指定条件） */  
	@RequestMapping("getList")
		@SaCheckPermission(TbVehicle.PERMISSION_CODE)
	public AjaxJson getList() { 
		SoMap so = SoMap.getRequestSoMap();
		so.set("deleteStatus", 1);
		List<TbVehicle> list = tbVehicleService.getList(so.startPage());
		return AjaxJson.getPageData(so.getDataCount(), list);
	}




	/**
	 * 数据导入接口
	 * @param file
	 * @return
	 */
	@RequestMapping("vehicleImport")
	@ResponseBody
	public AjaxJson dataImport(@RequestParam("file") MultipartFile file){
		try {
			String importData = tbVehicleService.importData(file);
			AjaxJson.getSuccess(importData);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return AjaxJson.getError();
	}

	/**
	 * 导出
	 * @param keyword
	 * @return
	 */
	@RequestMapping("vehicleOutport")
	@ResponseBody
	public AjaxJson dataOutport(@RequestParam(value = "keyword",required = false) String keyword,@RequestParam(value = "filepath",required = false)String filepath){
		try {
			String importData = tbVehicleService.outportExcel(keyword,filepath);
			AjaxJson.getSuccess(importData);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return AjaxJson.getError();
	}


	/*------            正常业务👆👆    rpc远程调用👇👇                         -----------*/

	/**
	 * 同步航通车辆信息
	 * @param vehicleDto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("rpc/vehicleDto")
	public boolean vehicleDto(@RequestBody VehicleDto vehicleDto){
		return tbVehicleService.vehicleDto(vehicleDto);
	}




}
