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

import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.CommentPageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.CommentRespVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.CommentSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.comment.CommentDO;
import cn.iocoder.yudao.module.tkdata.service.comment.CommentService;
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 = "管理后台 - AI 评论内容")
@RestController
@RequestMapping("/server/comment")
@Validated
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/create")
    @Operation(summary = "创建AI 评论内容")
    @PreAuthorize("@ss.hasPermission('server:comment:create')")
    public CommonResult<Integer> createComment(@Valid @RequestBody CommentSaveReqVO createReqVO) {
        return success(commentService.createComment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI 评论内容")
    @PreAuthorize("@ss.hasPermission('server:comment:update')")
    public CommonResult<Boolean> updateComment(@Valid @RequestBody CommentSaveReqVO updateReqVO) {
        commentService.updateComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI 评论内容")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:comment:delete')")
    public CommonResult<Boolean> deleteComment(@RequestParam("id") Integer id) {
        commentService.deleteComment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除AI 评论内容")
                @PreAuthorize("@ss.hasPermission('server:comment:delete')")
    public CommonResult<Boolean> deleteCommentList(@RequestParam("ids") List<Integer> ids) {
        commentService.deleteCommentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI 评论内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:comment:query')")
    public CommonResult<CommentRespVO> getComment(@RequestParam("id") Integer id) {
        CommentDO comment = commentService.getComment(id);
        return success(BeanUtils.toBean(comment, CommentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI 评论内容 分页")
    @PreAuthorize("@ss.hasPermission('server:comment:query')")
    public CommonResult<PageResult<CommentRespVO>> getCommentPage(@Valid CommentPageReqVO pageReqVO) {
        PageResult<CommentDO> pageResult = commentService.getCommentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CommentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI 评论内容 Excel")
    @PreAuthorize("@ss.hasPermission('server:comment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCommentExcel(@Valid CommentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommentDO> list = commentService.getCommentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI 评论内容 .xls", "数据", CommentRespVO.class,
                        BeanUtils.toBean(list, CommentRespVO.class));
    }

}