package com.ktg.iscs.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.IsLockCabinet;
import com.ktg.iscs.domain.dto.hardwareApi.SendDataDTO;
import com.ktg.iscs.service.IIsLockCabinetService;
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 2025-05-14
 */
@Api(tags = "锁控机柜")
@RestController
@RequestMapping("/iscs/cabinet")
public class IsLockCabinetController extends BaseController
{
    @Autowired
    private IIsLockCabinetService isLockCabinetService;

    @ApiOperation("查询锁控机柜-分页")
    @Parameters({
            @Parameter(name = "page", description = "Page"),
            @Parameter(name = "isLockCabinet", description = "实体参数")
    })
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:list')")
    @GetMapping("/getIsLockCabinetPage")
    public CommonResult<Page<IsLockCabinet>> getIsLockCabinetPage(Page<IsLockCabinet> page, IsLockCabinet isLockCabinet)
    {
        Page<IsLockCabinet> result = isLockCabinetService.getIsLockCabinetPage(page, isLockCabinet);
        return CommonResult.success(result);
    }

    @ApiOperation("导出锁控机柜列表")
    @Parameter(name = "isLockCabinet", description = "实体参数")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:export')")
    @Log(title = "锁控机柜", businessType = BusinessType.EXPORT)
    @PostMapping("/exportIsLockCabinet")
    public void exportIsLockCabinet(HttpServletResponse response, IsLockCabinet isLockCabinet)
    {
        Page<IsLockCabinet> page = new Page<>();
        page.setSize(-1);
        page.setCurrent(1);
        List<IsLockCabinet> list = isLockCabinetService.page(page, Wrappers.<IsLockCabinet>lambdaQuery()
                .orderByDesc(IsLockCabinet::getCabinetId)).getRecords();
        ExcelUtil<IsLockCabinet> util = new ExcelUtil<IsLockCabinet>(IsLockCabinet.class);
        util.exportExcel(response, list, "锁控机柜数据");
    }

    @ApiOperation("获取锁控机柜详细信息")
    @Parameter(name = "cabinetId", description = "cabinetId")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:query')")
    @GetMapping(value = "/selectIsLockCabinetById")
    public CommonResult<IsLockCabinet> selectIsLockCabinetById(Long cabinetId)
    {
        return CommonResult.success(isLockCabinetService.getById(cabinetId));
    }

    @ApiOperation("新增锁控机柜")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:add')")
    @Log(title = "锁控机柜", businessType = BusinessType.INSERT)
    @PostMapping("/insertIsLockCabinet")
    public CommonResult<Boolean> insertIsLockCabinet(@RequestBody @Parameter(name = "isLockCabinet", description = "新增数据类,放到body") IsLockCabinet isLockCabinet)
    {
        return CommonResult.success(isLockCabinetService.insertIsLockCabinet(isLockCabinet));
    }

    @ApiOperation("修改锁控机柜")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:edit')")
    @Log(title = "锁控机柜", businessType = BusinessType.UPDATE)
    @PostMapping("/updateIsLockCabinet")
    public CommonResult<Boolean> updateIsLockCabinet(@RequestBody @Parameter(name = "isLockCabinet", description = "修改数据类,放到body") IsLockCabinet isLockCabinet)
    {
        return CommonResult.success(isLockCabinetService.updateIsLockCabinet(isLockCabinet));
    }

    @ApiOperation("删除锁控机柜")
    @PreAuthorize("@ss.hasPermi('iscs:cabinet:remove')")
    @Log(title = "锁控机柜", businessType = BusinessType.DELETE)
	@PostMapping("/deleteIsLockCabinetByCabinetIds")
    public CommonResult<Boolean> deleteIsLockCabinetByCabinetIds(String cabinetIds)
    {
        return CommonResult.success(isLockCabinetService.deleteIsLockCabinetByCabinetIds(cabinetIds));
    }


    @ApiOperation("和机柜的ws通信")
    @GetMapping("/sendDataToLoto")
    public CommonResult<Boolean> sendDataToLoto(SendDataDTO dto)
    {
        return CommonResult.success(isLockCabinetService.sendDataToLoto(dto));
    }

}
