package com.orion.visor.module.asset.controller;

import com.orion.lang.define.wrapper.DataGrid;
import com.orion.visor.framework.biz.operator.log.core.annotation.OperatorLog;
import com.orion.visor.framework.common.validator.group.Page;
import com.orion.visor.framework.log.core.annotation.IgnoreLog;
import com.orion.visor.framework.log.core.enums.IgnoreLogMode;
import com.orion.visor.framework.web.core.annotation.DemoDisableApi;
import com.orion.visor.framework.web.core.annotation.RestWrapper;
import com.orion.visor.module.asset.define.operator.HostOperatorType;
import com.orion.visor.module.asset.entity.request.host.*;
import com.orion.visor.module.asset.entity.vo.HostConfigVO;
import com.orion.visor.module.asset.entity.vo.HostVO;
import com.orion.visor.module.asset.service.HostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 主机 api
 *
 * @author Jiahang Li
 * @version 1.0.0
 * @since 2023-9-11 14:16
 */
@Tag(name = "asset - 主机服务")
@Slf4j
@Validated
@RestWrapper
@RestController
@RequestMapping("/asset/host")
public class HostController {

    @Resource
    private HostService hostService;

    @DemoDisableApi
    @OperatorLog(HostOperatorType.CREATE)
    @PostMapping("/create")
    @Operation(summary = "创建主机")
    @PreAuthorize("@ss.hasPermission('asset:host:create')")
    public Long createHost(@Validated @RequestBody HostCreateRequest request) {
        return hostService.createHost(request);
    }

    @DemoDisableApi
    @OperatorLog(HostOperatorType.UPDATE)
    @PutMapping("/update")
    @Operation(summary = "通过 id 更新主机")
    @PreAuthorize("@ss.hasPermission('asset:host:update')")
    public Integer updateHost(@Validated @RequestBody HostUpdateRequest request) {
        return hostService.updateHostById(request);
    }

    @DemoDisableApi
    @OperatorLog(HostOperatorType.UPDATE_STATUS)
    @PutMapping("/update-status")
    @Operation(summary = "更新主机状态")
    @PreAuthorize("@ss.hasPermission('asset:host:update-status')")
    public Integer updateHostStatus(@Validated @RequestBody HostUpdateStatusRequest request) {
        return hostService.updateHostStatus(request);
    }

    @DemoDisableApi
    @OperatorLog(HostOperatorType.UPDATE_CONFIG)
    @PutMapping("/update-config")
    @Operation(summary = "更新主机配置")
    @PreAuthorize("@ss.hasPermission('asset:host:update-config')")
    public Integer updateHostConfig(@Validated @RequestBody HostUpdateConfigRequest request) {
        return hostService.updateHostConfig(request);
    }

    @IgnoreLog(IgnoreLogMode.RET)
    @GetMapping("/get")
    @Operation(summary = "通过 id 查询主机")
    @Parameter(name = "id", description = "id", required = true)
    @PreAuthorize("@ss.hasPermission('asset:host:query')")
    public HostVO getHost(@RequestParam("id") Long id) {
        return hostService.getHostById(id);
    }

    @IgnoreLog(IgnoreLogMode.RET)
    @GetMapping("/get-config")
    @Operation(summary = "查询主机配置")
    @Parameter(name = "id", description = "id", required = true)
    @PreAuthorize("@ss.hasPermission('asset:host:query')")
    public HostConfigVO getHostConfig(@RequestParam("id") Long id) {
        return hostService.getHostConfig(id);
    }

    @IgnoreLog(IgnoreLogMode.RET)
    @GetMapping("/list")
    @Operation(summary = "查询主机")
    @Parameter(name = "type", description = "type", required = false)
    @PreAuthorize("@ss.hasPermission('asset:host:query')")
    public List<HostVO> getHostList(@RequestParam(value = "type", required = false) String type) {
        return hostService.getHostList(type);
    }

    @IgnoreLog(IgnoreLogMode.RET)
    @PostMapping("/query")
    @Operation(summary = "分页查询主机")
    @PreAuthorize("@ss.hasPermission('asset:host:query')")
    public DataGrid<HostVO> getHostPage(@Validated(Page.class) @RequestBody HostQueryRequest request) {
        return hostService.getHostPage(request);
    }

    @DemoDisableApi
    @OperatorLog(HostOperatorType.DELETE)
    @DeleteMapping("/delete")
    @Operation(summary = "通过 id 删除主机")
    @Parameter(name = "id", description = "id", required = true)
    @PreAuthorize("@ss.hasPermission('asset:host:delete')")
    public Integer deleteHost(@RequestParam("id") Long id) {
        return hostService.deleteHostById(id);
    }

    @DemoDisableApi
    @OperatorLog(HostOperatorType.DELETE)
    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量删除主机")
    @Parameter(name = "idList", description = "idList", required = true)
    @PreAuthorize("@ss.hasPermission('asset:host:delete')")
    public Integer batchDeleteHost(@RequestParam("idList") List<Long> idList) {
        return hostService.deleteHostByIdList(idList);
    }

}

