package cn.iocoder.yudao.module.tkdata.controller.admin.bigbrother;

import cn.iocoder.yudao.module.tkdata.controller.admin.bigbrother.vo.BigBrotherPageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.bigbrother.vo.BigBrotherRespVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.bigbrother.vo.BigBrotherSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.bigbrother.BigBrotherDO;
import cn.iocoder.yudao.module.tkdata.service.bigbrother.BigBrotherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 大哥数据")
@RestController
@RequestMapping("/server/big-brother")
@Validated
@Slf4j
public class BigBrotherController {

    @Resource
    private BigBrotherService bigBrotherService;

    @PostMapping("/create")
    @Operation(summary = "创建大哥数据")
    @PreAuthorize("@ss.hasPermission('server:big-brother:create')")
    public CommonResult<Integer> createBigBrother(@Valid @RequestBody BigBrotherSaveReqVO createReqVO) {
        return success(bigBrotherService.createBigBrother(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新大哥数据")
    @PreAuthorize("@ss.hasPermission('server:big-brother:update')")
    public CommonResult<Boolean> updateBigBrother(@Valid @RequestBody BigBrotherSaveReqVO updateReqVO) {
        bigBrotherService.updateBigBrother(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除大哥数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:big-brother:delete')")
    public CommonResult<Boolean> deleteBigBrother(@RequestParam("id") Integer id) {
        bigBrotherService.deleteBigBrother(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除大哥数据")
                @PreAuthorize("@ss.hasPermission('server:big-brother:delete')")
    public CommonResult<Boolean> deleteBigBrotherList(@RequestParam("ids") List<Integer> ids) {
        bigBrotherService.deleteBigBrotherListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得大哥数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:big-brother:query')")
    public CommonResult<BigBrotherRespVO> getBigBrother(@RequestParam("id") Integer id) {
        BigBrotherDO bigBrother = bigBrotherService.getBigBrother(id);
        return success(BeanUtils.toBean(bigBrother, BigBrotherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得大哥数据分页")
    @PreAuthorize("@ss.hasPermission('server:big-brother:query')")
    public CommonResult<PageResult<BigBrotherRespVO>> getBigBrotherPage(@Valid BigBrotherPageReqVO pageReqVO) {
        PageResult<BigBrotherDO> pageResult = bigBrotherService.getBigBrotherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BigBrotherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出大哥数据 Excel")
    @PreAuthorize("@ss.hasPermission('server:big-brother:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBigBrotherExcel(@Valid BigBrotherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BigBrotherDO> list = bigBrotherService.getBigBrotherPage(pageReqVO).getList();
        // 导出 Excel
        if (pageReqVO.getDeleteFlag()){
            bigBrotherService.deleteExportData(list);
            log.info("删除导出数据{}",list.size() );
        }
        ExcelUtils.write(response, "大哥数据.xls", "数据", BigBrotherRespVO.class,
                        BeanUtils.toBean(list, BigBrotherRespVO.class));
    }



}