package org.agt.module.license.controller.admin.alert;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.agt.framework.apilog.core.annotation.ApiAccessLog;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.common.pojo.PageParam;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.util.object.BeanUtils;
import org.agt.framework.excel.core.util.ExcelUtils;
import org.agt.module.license.controller.admin.alert.vo.AlertPageReqVO;
import org.agt.module.license.controller.admin.alert.vo.AlertRespVO;
import org.agt.module.license.controller.admin.alert.vo.AlertSaveReqVO;
import org.agt.module.license.dal.dataobject.alert.AlertDO;
import org.agt.module.license.service.alert.AlertService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static org.agt.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static org.agt.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 告警")
@RestController
@RequestMapping("/license/alert")
@Validated
public class AlertController {

    @Resource
    private AlertService alertService;

    @PostMapping("/create")
    @Operation(summary = "创建告警")
    @PreAuthorize("@ss.hasPermission('license:alert:create')")
    public CommonResult<Long> createAlert(@Valid @RequestBody AlertSaveReqVO createReqVO) {
        return success(alertService.createAlert(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新告警")
    @PreAuthorize("@ss.hasPermission('license:alert:update')")
    public CommonResult<Boolean> updateAlert(@Valid @RequestBody AlertSaveReqVO updateReqVO) {
        alertService.updateAlert(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除告警")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('license:alert:delete')")
    public CommonResult<Boolean> deleteAlert(@RequestParam("id") Long id) {
        alertService.deleteAlert(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得告警")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('license:alert:query')")
    public CommonResult<AlertRespVO> getAlert(@RequestParam("id") Long id) {
        AlertDO alert = alertService.getAlert(id);
        return success(BeanUtils.toBean(alert, AlertRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得告警分页")
    @PreAuthorize("@ss.hasPermission('license:alert:query')")
    public CommonResult<PageResult<AlertRespVO>> getAlertPage(@Valid AlertPageReqVO pageReqVO) {
        PageResult<AlertDO> pageResult = alertService.getAlertPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AlertRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出告警 Excel")
    @PreAuthorize("@ss.hasPermission('license:alert:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAlertExcel(@Valid AlertPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AlertDO> list = alertService.getAlertPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "告警.xls", "数据", AlertRespVO.class,
                        BeanUtils.toBean(list, AlertRespVO.class));
    }

}