package cn.iocoder.byzs.module.system.controller.admin.userwebexpiretime;

import cn.iocoder.byzs.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.framework.excel.core.util.ExcelUtils;
import cn.iocoder.byzs.module.system.controller.admin.userwebexpiretime.vo.UserWebExpireTimePageReqVO;
import cn.iocoder.byzs.module.system.controller.admin.userwebexpiretime.vo.UserWebExpireTimeRespVO;
import cn.iocoder.byzs.module.system.controller.admin.userwebexpiretime.vo.UserWebExpireTimeSaveReqVO;
import cn.iocoder.byzs.module.system.dal.dataobject.userwebexpiretime.UserWebExpireTimeDO;
import cn.iocoder.byzs.module.system.service.userwebexpiretime.UserWebExpireTimeService;
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.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

@Tag(name = "管理后台 - 用户平台权限有效期")
@RestController
@RequestMapping("/system/user-web-expire-time")
@Validated
public class UserWebExpireTimeController {

    @Resource
    private UserWebExpireTimeService userWebExpireTimeService;

    @PostMapping("/create")
    @Operation(summary = "创建用户平台权限有效期")
    @PreAuthorize("@ss.hasPermission('system:user-web-expire-time:create')")
    public CommonResult<Long> createUserWebExpireTime(@Valid @RequestBody UserWebExpireTimeSaveReqVO createReqVO) {
        return success(userWebExpireTimeService.createUserWebExpireTime(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户平台权限有效期")
    @PreAuthorize("@ss.hasPermission('system:user-web-expire-time:update')")
    public CommonResult<Boolean> updateUserWebExpireTime(@Valid @RequestBody UserWebExpireTimeSaveReqVO updateReqVO) {
        userWebExpireTimeService.updateUserWebExpireTime(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户平台权限有效期")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-web-expire-time:delete')")
    public CommonResult<Boolean> deleteUserWebExpireTime(@RequestParam("id") Long id) {
        userWebExpireTimeService.deleteUserWebExpireTime(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户平台权限有效期")
                @PreAuthorize("@ss.hasPermission('system:user-web-expire-time:delete')")
    public CommonResult<Boolean> deleteUserWebExpireTimeList(@RequestParam("ids") List<Long> ids) {
        userWebExpireTimeService.deleteUserWebExpireTimeListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户平台权限有效期")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-web-expire-time:query')")
    public CommonResult<UserWebExpireTimeRespVO> getUserWebExpireTime(@RequestParam("id") Long id) {
        UserWebExpireTimeDO userWebExpireTime = userWebExpireTimeService.getUserWebExpireTime(id);
        return success(BeanUtils.toBean(userWebExpireTime, UserWebExpireTimeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户平台权限有效期分页")
    @PreAuthorize("@ss.hasPermission('system:user-web-expire-time:query')")
    public CommonResult<PageResult<UserWebExpireTimeRespVO>> getUserWebExpireTimePage(@Valid UserWebExpireTimePageReqVO pageReqVO) {
        PageResult<UserWebExpireTimeRespVO> pageResult = userWebExpireTimeService.getUserWebExpireTimePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户平台权限有效期 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-web-expire-time:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserWebExpireTimeExcel(@Valid UserWebExpireTimePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserWebExpireTimeRespVO> list = userWebExpireTimeService.getUserWebExpireTimePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户平台权限有效期.xls", "数据", UserWebExpireTimeRespVO.class,
                list);
    }

}