package cn.iocoder.yudao.module.llm.controller.admin.label;

import org.springframework.web.bind.annotation.*;
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 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.*;

import cn.iocoder.yudao.module.llm.controller.admin.label.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.label.LabelDO;
import cn.iocoder.yudao.module.llm.service.label.LabelService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 标签管理")
@RestController
@RequestMapping("/llm/label")
@Validated
public class LabelController {

    @Resource
    private LabelService labelService;

    @PostMapping("/create")
    @Operation(summary = "创建标签管理")
//    @PreAuthorize("@ss.hasPermission('llm:label:create')")
    public CommonResult<Long> createLabel(@Valid @RequestBody LabelSaveReqVO createReqVO) {
        return success(labelService.createLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签管理")
    @PreAuthorize("@ss.hasPermission('llm:label:update')")
    public CommonResult<Boolean> updateLabel(@Valid @RequestBody LabelSaveReqVO updateReqVO) {
        labelService.updateLabel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('llm:label:delete')")
    public CommonResult<Boolean> deleteLabel(@RequestParam("id") Long id) {
        labelService.deleteLabel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('llm:label:query')")
    public CommonResult<LabelRespVO> getLabel(@RequestParam("id") Long id) {
        LabelDO label = labelService.getLabel(id);
        return success(BeanUtils.toBean(label, LabelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签管理分页")
    @PreAuthorize("@ss.hasPermission('llm:label:query')")
    public CommonResult<PageResult<LabelRespVO>> getLabelPage(@Valid LabelPageReqVO pageReqVO) {
        PageResult<LabelDO> pageResult = labelService.getLabelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LabelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签管理 Excel")
    @PreAuthorize("@ss.hasPermission('llm:label:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLabelExcel(@Valid LabelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LabelDO> list = labelService.getLabelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签管理.xls", "数据", LabelRespVO.class,
                        BeanUtils.toBean(list, LabelRespVO.class));
    }

}