package cn.iocoder.yudao.module.system.controller.admin.authentication;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.authentication.vo.AuthenticationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.authentication.vo.AuthenticationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.authentication.vo.AuthenticationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.authentication.AuthenticationDO;
import cn.iocoder.yudao.module.system.service.authentication.AuthenticationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 认证")
@RestController
@RequestMapping("/system/authentication")
@Validated
public class AuthenticationController {

    @Resource
    private AuthenticationService authenticationService;

    @PostMapping("/create")
    @Operation(summary = "创建认证")
    @PreAuthorize("@ss.hasPermission('system:authentication:create')")
    public CommonResult<Long> createAuthentication(@Valid @RequestBody AuthenticationSaveReqVO createReqVO) {
        return success(authenticationService.createAuthentication(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新认证")
    @PreAuthorize("@ss.hasPermission('system:authentication:update')")
    public CommonResult<Boolean> updateAuthentication(@Valid @RequestBody AuthenticationSaveReqVO updateReqVO) {
        authenticationService.updateAuthentication(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除认证")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:authentication:delete')")
    public CommonResult<Boolean> deleteAuthentication(@RequestParam("id") Long id) {
        authenticationService.deleteAuthentication(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得认证")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:authentication:query')")
    public CommonResult<AuthenticationRespVO> getAuthentication(@RequestParam("id") Long id) {
        AuthenticationDO authentication = authenticationService.getAuthentication(id);
        return success(BeanUtils.toBean(authentication, AuthenticationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得认证分页")
    @PreAuthorize("@ss.hasPermission('system:authentication:query')")
    public CommonResult<PageResult<AuthenticationRespVO>> getAuthenticationPage(@Valid AuthenticationPageReqVO pageReqVO) {
        PageResult<AuthenticationDO> pageResult = authenticationService.getAuthenticationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuthenticationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出认证 Excel")
    @PreAuthorize("@ss.hasPermission('system:authentication:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuthenticationExcel(@Valid AuthenticationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuthenticationDO> list = authenticationService.getAuthenticationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "认证.xls", "数据", AuthenticationRespVO.class,
                        BeanUtils.toBean(list, AuthenticationRespVO.class));
    }

}