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.poi.ExcelUtil;
import com.ktg.iscs.domain.IsKey;
import com.ktg.iscs.domain.vo.key.IsKeyVO;
import com.ktg.iscs.service.IIsKeyService;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 钥匙Controller
 *
 * @author cgj
 * @date 2024-11-19
 */
@Api(tags = "钥匙")
@RestController
@RequestMapping("/iscs/key")
public class IsKeyController extends BaseController
{
    @Autowired
    private IIsKeyService isKeyService;

    /**
     * 查询钥匙分页
     */
    @ApiOperation("查询钥匙-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isKey", description = "实体参数")
    })
    // @PreAuthorize("@ss.hasPermi('iscs:key:list')")
    @GetMapping("/getIsKeyPage")
    public CommonResult<Page<IsKeyVO>> getIsKeyPage(Page<IsKey> page, IsKey isKey)
    {
        Page<IsKeyVO> result = isKeyService.getIsKeyPage(page, isKey);
        return CommonResult.success(result);
    }

    /**
     * 导出钥匙列表
     */
    @ApiOperation("导出钥匙列表")
    @Parameter(name = "isKey", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:key:export')")
    @Log(title = "钥匙", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsKey")
    public void exportIsKey(HttpServletResponse response, IsKey isKey)
    {
        List<IsKey> list = isKeyService.selectIsKeyList(isKey);
        ExcelUtil<IsKey> util = new ExcelUtil<IsKey>(IsKey.class);
        util.exportExcel(response, list, "钥匙数据");
    }

    /**
     * 获取钥匙详细信息
     */
    @ApiOperation("获取钥匙详细信息")
    @Parameter(name = "keyId", description = "keyId")
    // @PreAuthorize("@ss.hasPermi('iscs:key:query')")
    @GetMapping(value = "/selectIsKeyById")
    public CommonResult<IsKey> selectIsKeyById(Long keyId)
    {
        return CommonResult.success(isKeyService.selectIsKeyByKeyId(keyId));
    }

    @ApiOperation("通过NFC获取钥匙详细信息")
    @Parameter(name = "nfc", description = "nfc")
    @GetMapping("/selectIsKeyByNfc")
    public CommonResult<IsKey> selectIsKeyByNfc(String nfc) {
        return CommonResult.success(isKeyService.selectIsKeyByNfc(nfc));
    }

    @ApiOperation("通过NFC获取钥匙详细信息 - 无需token")
    @Parameter(name = "nfc", description = "nfc")
    @GetMapping("/selectIsKeyByNfcWithoutAuth")
    public CommonResult<IsKey> selectIsKeyByNfcWithoutAuth(String nfc) {
        return CommonResult.success(isKeyService.selectIsKeyByNfc(nfc));
    }

    /**
     * 新增钥匙
     */
    @ApiOperation("新增钥匙")
    // @PreAuthorize("@ss.hasPermi('iscs:key:add')")
    @Log(title = "钥匙", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsKey")
    public CommonResult<Boolean> insertIsKey(@RequestBody @Parameter(name = "isKey", description = "新增数据类,放到body") IsKey isKey)
    {
        return CommonResult.success(isKeyService.insertIsKey(isKey) == 1);
    }

    /**
     * 修改钥匙
     */
    @ApiOperation("修改钥匙")
    // @PreAuthorize("@ss.hasPermi('iscs:key:edit')")
    @Log(title = "钥匙", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsKey")
    public CommonResult<Boolean> updateIsKey(@RequestBody @Parameter(name = "isKey", description = "修改数据类,放到body") IsKey isKey)
    {
        return CommonResult.success(isKeyService.updateIsKey(isKey) == 1);
    }

    /**
     * 删除钥匙
     */
    @ApiOperation("删除钥匙")
    // @PreAuthorize("@ss.hasPermi('iscs:key:remove')")
    @Log(title = "钥匙", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsKeyByKeyIds")
    public CommonResult<Boolean> deleteIsKeyByKeyIds(String keyIds)
    {
        return CommonResult.success(isKeyService.deleteIsKeyByKeyIds(keyIds) != 0);
    }

    @ApiOperation("机柜录入钥匙")
    // @PreAuthorize("@ss.hasPermi('iscs:key:add')")
    @Log(title = "机柜录入钥匙", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsKeyByCabinet")
    public CommonResult<Boolean> insertIsKeyByCabinet(@RequestBody @Parameter(name = "isKey", description = "新增数据类,放到body") IsKey isKey)
    {
        return CommonResult.success(isKeyService.insertIsKeyByCabinet(isKey));
    }

}
