package com.ktg.iscs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.pojo.CommonResult;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.common.utils.poi.ExcelUtil;
import com.ktg.iscs.domain.IsHardware;
import com.ktg.iscs.domain.dto.hardware.HardwarePageDTO;
import com.ktg.iscs.service.IIsHardwareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
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 cgj
 * @date 2024-10-16
 */
@Api(tags = "硬件")
@RestController
@RequestMapping("/iscs/hardware")
public class IsHardwareController extends BaseController
{
    @Autowired
    private IIsHardwareService isHardwareService;

    /**
     * 查询硬件分页
     */
    @ApiOperation("查询硬件-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "dto", description = "实体参数")
    })
    // @PreAuthorize("@ss.hasPermi('is:hardware:list')")
    @GetMapping("/getIsHardwarePage")
    public CommonResult<Page<IsHardware>> getIsHardwarePage(Page page, HardwarePageDTO dto)
    {
        Page<IsHardware> isHardwarePage = isHardwareService.getIsHardwarePage(page, dto);
        return CommonResult.success(isHardwarePage);
    }

    /**
     * 导出硬件列表
     */
    @ApiOperation("导出硬件列表")
    @Parameter(name = "isHardware", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('is:hardware:export')")
    @Log(title = "硬件", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsHardware")
    public void exportIsHardware(HttpServletResponse response, IsHardware isHardware)
    {
        List<IsHardware> list = isHardwareService.selectIsHardwareList(isHardware);
        ExcelUtil<IsHardware> util = new ExcelUtil<IsHardware>(IsHardware.class);
        util.exportExcel(response, list, "硬件数据");
    }

    /**
     * 获取硬件详细信息
     */
    @ApiOperation("获取硬件详细信息")
    @Parameter(name = "id", description = "id")
    // @PreAuthorize("@ss.hasPermi('is:hardware:query')")
    @GetMapping(value = "/selectIsHardwareById")
    public CommonResult<IsHardware> selectIsHardwareById(Long id)
    {
        return CommonResult.success(isHardwareService.selectIsHardwareById(id));
    }

    /**
     * 新增硬件
     */
    @ApiOperation("新增硬件")
    @PreAuthorize("@ss.hasPermi('mes:hw:information:add')")
    @Log(title = "硬件", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsHardware")
    public CommonResult<Boolean> insertIsHardware(@RequestBody @Parameter(name = "isHardware", description = "新增数据类,放到body") IsHardware isHardware)
    {
        return CommonResult.success(isHardwareService.insertIsHardware(isHardware) == 1);
    }

    /**
     * 修改硬件
     */
    @ApiOperation("修改硬件")
    @PreAuthorize("@ss.hasPermi('mes:hw:information:edit')")
    @Log(title = "硬件", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsHardware")
    public CommonResult<Boolean> updateIsHardware(@RequestBody @Parameter(name = "isHardware", description = "修改数据类,放到body") IsHardware isHardware)
    {
        return CommonResult.success(isHardwareService.updateIsHardware(isHardware) == 1);
    }

    /**
     * 删除硬件
     */
    @ApiOperation("删除硬件")
    // @PreAuthorize("@ss.hasPermi('mes:hw:information:batchremove')")
    @Log(title = "硬件", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsHardwareByIds")
    public CommonResult<Boolean> deleteIsHardwareByIds(String ids)
    {
        return CommonResult.success(isHardwareService.deleteIsHardwareByIds(ids) != 0);
    }

    @ApiOperation("导出或更新硬件模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<IsHardware> util = new ExcelUtil<>(IsHardware.class);
        util.importTemplateExcel(response, "硬件数据");
    }

    @ApiOperation("导出或更新硬件")
    @PreAuthorize("@ss.hasPermi('is:hardware:import')")
    @Log(title = "硬件", businessType = BusinessType.IMPORT)
    @PostMapping("/importHardware")
    public CommonResult<String> importHardware(MultipartFile file,
                                 @RequestParam(name = "updateSupport", defaultValue = "false") boolean updateSupport) throws Exception {
        ExcelUtil<IsHardware> util = new ExcelUtil<>(IsHardware.class);
        List<IsHardware> hardwareList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = isHardwareService.importHardware(hardwareList, updateSupport, operName);
        return CommonResult.success("导入成功，共导入 " + message + " 条数据");
    }
}
