package cn.iocoder.yudao.module.system.controller.admin.dict;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.language.LanguageDictPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.language.LanguageDictImportVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.LanguageDictDO;
import cn.iocoder.yudao.module.system.service.dict.LanguageDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.multipart.MultipartFile;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 系统语言字典")
@RestController
@RequestMapping("/system/languageDict")
@Validated
public class LanguageDictController {

    @Resource
    private LanguageDictService languageDictService;

    @PostMapping("/create")
    @Operation(summary = "创建系统语言字典")
    public CommonResult<Boolean> createLanguageDict(@Valid @RequestBody List<LanguageDictDO> createReqVO) {
        languageDictService.createLanguageDict(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统语言字典")
    public CommonResult<Boolean> updateLanguageDict(@Valid @RequestBody LanguageDictDO updateReqVO) {
        languageDictService.updateLanguageDict(updateReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除系统语言字典")
    public CommonResult<Boolean> deleteLanguageDict(@RequestBody Long[] ids) {
        languageDictService.deleteLanguageDict(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统语言字典详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<LanguageDictDO> getLanguageDict(@RequestParam("id") Long id) {
        LanguageDictDO languageDict = languageDictService.getLanguageDict(id);
        return success(languageDict);
    }

    @GetMapping("/page")
    @Operation(summary = "获得系统语言字典分页")
    public CommonResult<PageResult<LanguageDictDO>> getLanguageDictPage(@Valid LanguageDictPageReqVO pageReqVO) {
        PageResult<LanguageDictDO> pageResult = languageDictService.getLanguageDictPage(pageReqVO);
        return success(pageResult);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出系统语言字典 Excel")
//    @PreAuthorize("@ss.hasPermission('system:language-dict:export')")
//    public void exportLanguageDictExcel(@Valid LanguageDictPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<LanguageDictDO> list = languageDictService.getLanguageDictPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "系统语言字典.xls", "数据", LanguageDictRespVO.class,
//                        BeanUtils.toBean(list, LanguageDictRespVO.class));
//    }

    @PostMapping("/importLanguageDictExcel")
    @Operation(summary = "导入语言字典")
    public CommonResult<Boolean> importLanguageDictExcel(@RequestParam("file") MultipartFile file) throws IOException {
        List<LanguageDictImportVO> list = ExcelUtils.read(file, LanguageDictImportVO.class);
        languageDictService.importLanguageDictExcel(list);
        return success(true);
    }


}