package com.pwt.web.controller;

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.PwtBattery;
import com.pwt.web.service.IPwtBatteryService;
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.Date;
import java.util.List;

/**
 * 电池管理Controller
 * 
 * @author linyun
 * @date 2022-11-17
 */
@Api("电池管理")
@RestController
@RequestMapping("/web/battery")
public class PwtBatteryController extends BaseController
{
    @Autowired
    private IPwtBatteryService pwtBatteryService;

    /**
     * 导入电池数据
     */
    @ApiOperation("导入电池信息")
    @ApiImplicitParam(name = "file", value = "导入的电池excel文件", required = true, dataType = "__file")
    @Log(title = "导入电池信息", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", headers = "ContentType=multipart/form-data")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<PwtBattery> util = new ExcelUtil<>(PwtBattery.class);
        List<PwtBattery> list = util.importExcel(file.getInputStream());
        return success(pwtBatteryService.importData(list));
    }

    /**
     * 导出模板
     */
    @ApiOperation("导出模板")
    @PreAuthorize("@ss.hasPermi('web:battery:exportTemplate')")
    @PostMapping(value = "/exportTemplate")
    public void importData(HttpServletResponse response)
    {
        ExcelUtil<PwtBattery> util = new ExcelUtil<>(PwtBattery.class);
        util.importTemplateExcel(response, "电池数据");
    }

    /**
     * 查询电池管理列表
     */
    @ApiOperation("查询电池管理列表")
    @PreAuthorize("@ss.hasPermi('web:battery:list')")
    @GetMapping("/list")
    public TableDataInfo list(PwtBattery pwtBattery)
    {
        startPage();
        List<PwtBattery> list = pwtBatteryService.selectPwtBatteryList(pwtBattery);
        return getDataTable(list);
    }

    /**
     * 导出电池管理列表
     */
    @PreAuthorize("@ss.hasPermi('web:battery:export')")
    @Log(title = "电池管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<PwtBattery> list = pwtBatteryService.selectPwtBatteryList(new PwtBattery());
        ExcelUtil<PwtBattery> util = new ExcelUtil<PwtBattery>(PwtBattery.class);
        util.exportExcel(response, list, "电池信息");
    }

    /**
     * 获取电池管理详细信息
     */
    @ApiOperation("获取电池管理详细信息")
    @ApiImplicitParam(name = "batteryId", value = "电池id", required = true, dataType = "Long", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('web:battery:query')")
    @GetMapping(value = "/{batteryId}")
    public AjaxResult getInfo(@PathVariable("batteryId") Long batteryId)
    {
        return AjaxResult.success(pwtBatteryService.selectPwtBatteryById(batteryId));
    }

    /**
     * 新增电池管理
     */
    @ApiOperation("新增电池管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "batteryId", value = "电池id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "batteryName", value = "名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "batteryType", value = "电池类型", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "batteryFactory", value = "电池厂商", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "batterySerial", value = "电池组序号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "productionTime", value = "生产日期", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "vin", value = "vin", dataType = "String", dataTypeClass = String.class)

    })
    @PreAuthorize("@ss.hasPermi('web:battery:add')")
    @Log(title = "电池管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PwtBattery pwtBattery)
    {
        return toAjax(pwtBatteryService.insertPwtBattery(pwtBattery));
    }

    /**
     * 修改电池管理
     */
    @ApiOperation("修改电池管理")
    @PreAuthorize("@ss.hasPermi('web:battery:edit')")
    @Log(title = "电池管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PwtBattery pwtBattery)
    {
        return toAjax(pwtBatteryService.updatePwtBattery(pwtBattery));
    }

    /**
     * 删除电池管理
     */
    @ApiOperation("删除电池管理")
    @PreAuthorize("@ss.hasPermi('web:battery:remove')")
    @Log(title = "电池管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{batteryIds}")
    public AjaxResult remove(@PathVariable Long[] batteryIds)
    {
        return toAjax(pwtBatteryService.deletePwtBatteryByIds(batteryIds));
    }
}
