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

import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.LanguagePageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.LanguageRespVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.LanguageSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.language.LanguageDO;
import cn.iocoder.yudao.module.tkdata.service.language.LanguageService;
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/language")
@Validated
public class LanguageController {

    @Resource
    private LanguageService languageService;

    @PostMapping("/create")
    @Operation(summary = "创建AI 语言种类")
    @PreAuthorize("@ss.hasPermission('server:language:create')")
    public CommonResult<Integer> createLanguage(@Valid @RequestBody LanguageSaveReqVO createReqVO) {
        return success(languageService.createLanguage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI 语言种类")
    @PreAuthorize("@ss.hasPermission('server:language:update')")
    public CommonResult<Boolean> updateLanguage(@Valid @RequestBody LanguageSaveReqVO updateReqVO) {
        languageService.updateLanguage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI 语言种类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('server:language:delete')")
    public CommonResult<Boolean> deleteLanguage(@RequestParam("id") Integer id) {
        languageService.deleteLanguage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除AI 语言种类")
                @PreAuthorize("@ss.hasPermission('server:language:delete')")
    public CommonResult<Boolean> deleteLanguageList(@RequestParam("ids") List<Integer> ids) {
        languageService.deleteLanguageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI 语言种类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('server:language:query')")
    public CommonResult<LanguageRespVO> getLanguage(@RequestParam("id") Integer id) {
        LanguageDO language = languageService.getLanguage(id);
        return success(BeanUtils.toBean(language, LanguageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI 语言种类分页")
    @PreAuthorize("@ss.hasPermission('server:language:query')")
    public CommonResult<PageResult<LanguageRespVO>> getLanguagePage(@Valid LanguagePageReqVO pageReqVO) {
        PageResult<LanguageDO> pageResult = languageService.getLanguagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LanguageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出AI 语言种类 Excel")
    @PreAuthorize("@ss.hasPermission('server:language:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLanguageExcel(@Valid LanguagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LanguageDO> list = languageService.getLanguagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "AI 语言种类.xls", "数据", LanguageRespVO.class,
                        BeanUtils.toBean(list, LanguageRespVO.class));
    }

}