package cd.casic.module.machine.controller;

import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageResult;
import cd.casic.framework.commons.util.object.BeanUtils;
import cd.casic.module.machine.controller.vo.SecretKeyVO;
import cd.casic.module.machine.dal.dataobject.MachineInfoDO;
import cd.casic.module.machine.service.MachineInfoService;
import cd.casic.module.machine.controller.vo.MachineInfoVO;
import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cd.casic.framework.commons.pojo.CommonResult.success;

@RestController
@Tag(name = "机器信息管理")
@RequestMapping("/ci/machineInfo")
@Validated
public class MachineInfoController {
    @Resource
    private MachineInfoService machineInfoService;

    @PostMapping("/create")
    @Operation(summary = "新增机器信息")
//    @PreAuthorize("@ss.hasPermission('ci:machineInfo:create')")
    public CommonResult<Long> createMachine(@Valid @RequestBody MachineInfoVO machineInfoVO) {
        Long id = machineInfoService.createMachine(machineInfoVO);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "编辑机器信息")
//    @PreAuthorize("@ss.hasPermission('ci:machineInfo:update')")
    public CommonResult<Boolean> updateMachineInfo(@Valid @RequestBody MachineInfoVO machineInfoVO) {
        machineInfoService.updateMachineInfo(machineInfoVO);
        return success(true);
    }

    @PutMapping("/updateStatus")
    @Operation(summary = "机器启用/停用")
//    @PreAuthorize("@ss.hasPermission('ci:machineInfo:status')")
    public CommonResult<Integer> updateStatus(@Valid @RequestBody MachineInfoVO machineInfoVO) {
        Integer newStatus = machineInfoService.updateStatus(machineInfoVO);
        return success(newStatus);
    }

    @PostMapping("/list")
    @Operation(summary = "获取机器信息列表")
    public CommonResult<PageResult<MachineInfoVO>> list(@Valid @RequestBody MachineInfoVO machineInfoVO) {
        PageResult<MachineInfoDO> pageResult = machineInfoService.listMachineInfo(machineInfoVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        return success(BeanUtils.toBean(pageResult, MachineInfoVO.class));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "机器信息删除")
//    @PreAuthorize("@ss.hasPermission('ci:machineInfo:delete')")
    public CommonResult<Boolean> deleteMachineInfo(@RequestParam("machineInfoId") Long machineInfoId) {
        machineInfoService.deleteMachineInfo(machineInfoId);
        return success(true);
    }

    @DeleteMapping("/deleteList")
    @Operation(summary = "批量删除机器信息")
//    @PreAuthorize("@ss.hasPermission('ci:machineInfo:delete')")
    public CommonResult<Boolean> deleteMachineInfoList(@RequestParam("machineInfoIds") String ids) {
        machineInfoService.deleteMachineInfoList(ids);
        return success(true);
    }

    @PutMapping("/bindingSecretKey")
    @Operation(summary = "绑定/解绑密钥")
    public void bindingSecretKey(@RequestBody SecretKeyVO secretKeyVO) {
        machineInfoService.bindingSecretKey(secretKeyVO);
    }
}
