package com.pwt.web.controller;

import com.pwt.Globals;
import com.pwt.common.annotation.Log;
import com.pwt.common.core.controller.BaseController;
import com.pwt.common.core.domain.AjaxResult;
import com.pwt.common.core.page.TableDataInfo;
import com.pwt.common.enums.BusinessType;
import com.pwt.common.utils.poi.ExcelUtil;
import com.pwt.web.domain.PwtVehicle;
import com.pwt.web.service.IPwtVehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 车辆管理Controller
 * 
 * @author linyun
 * @date 2022-11-17
 */
@Api("车辆管理")
@RestController
@RequestMapping("/web/vehicle")
public class PwtVehicleController extends BaseController
{
    @Autowired
    private IPwtVehicleService pwtVehicleService;

    /**
     * 导入车辆数据
     */
    @ApiOperation("导入车辆数据")
    @ApiImplicitParam(name = "file", value = "导入的车辆excel文件", required = true, dataType = "__file")
    @PreAuthorize("@ss.hasPermi('web:vehicle:importData')")
    @Log(title = "导入车辆信息", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", headers = "ContentType=multipart/form-data")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<PwtVehicle> util = new ExcelUtil<>(PwtVehicle.class);
        List<PwtVehicle> list = util.importExcel(file.getInputStream());
        // TODO 导入时计算注册车辆数
        return success(pwtVehicleService.importData(list));
    }

    /**
     * 导出模板
     */
    @ApiOperation("导出模板")
    @PostMapping(value = "/exportTemplate")
    public void importData(HttpServletResponse response)
    {
        ExcelUtil<PwtVehicle> util = new ExcelUtil<>(PwtVehicle.class);
        util.importTemplateExcel(response, "车辆数据");
    }

    /**
     * 查询车辆管理列表
     */
    @ApiOperation("查询车辆管理列表")
    @PreAuthorize("@ss.hasPermi('web:vehicle:list')")
    @GetMapping("/list")
    public TableDataInfo list(PwtVehicle pwtVehicle)
    {
        startPage();
        List<PwtVehicle> list = pwtVehicleService.selectPwtVehicleList(pwtVehicle);
        return getDataTable(list);
    }

    /**
     * 导出车辆管理列表
     */
    @PreAuthorize("@ss.hasPermi('web:vehicle:export')")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<PwtVehicle> list = pwtVehicleService.selectPwtVehicleList(new PwtVehicle());
        ExcelUtil<PwtVehicle> util = new ExcelUtil<PwtVehicle>(PwtVehicle.class);
        util.exportExcel(response, list, "车辆信息");
    }

    /**
     * 获取车辆管理详细信息
     */
    @ApiOperation("获取车辆管理详细信息")
    @ApiImplicitParam(name = "vehicleId", value = "车辆id", required = true, dataType = "Long", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('web:vehicle:query')")
    @GetMapping(value = "/{vehicleId}")
    public AjaxResult getInfo(@PathVariable("vehicleId") Long vehicleId)
    {
        return AjaxResult.success(pwtVehicleService.selectPwtVehicleById(vehicleId));
    }

    /**
     * 新增车辆管理
     */
    @ApiOperation("新增车辆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vehicleId", value = "车辆id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "vehicleName", value = "名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "车牌号", value = "车牌号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "vin", value = "vin", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "rfid", value = "rfid", dataType = "String", dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('web:vehicle:add')")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PwtVehicle pwtVehicle)
    {
        int i = pwtVehicleService.insertPwtVehicle(pwtVehicle);
        if(i > 0) {
            Globals.OP_REGISTER_CAR_COUNT.getAndIncrement();
        }
        return toAjax(i);
    }

    /**
     * 修改车辆管理
     */
    @ApiOperation("修改车辆管理")
    @PreAuthorize("@ss.hasPermi('web:vehicle:edit')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PwtVehicle pwtVehicle)
    {
        return toAjax(pwtVehicleService.updatePwtVehicle(pwtVehicle));
    }

    /**
     * 删除车辆管理
     */
    @ApiOperation("删除车辆管理")
    @PreAuthorize("@ss.hasPermi('web:vehicle:remove')")
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{vehicleIds}")
    public AjaxResult remove(@PathVariable Long[] vehicleIds)
    {
        int i = pwtVehicleService.deletePwtVehicleByIds(vehicleIds);
        if(i > 0) {
            Globals.OP_REGISTER_CAR_COUNT.getAndDecrement();
        }
        return toAjax(i);
    }
}
