package cn.ey.biz.module.system.controller.admin.i18n;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.common.util.i18n.I18nContextHolder;
import cn.ey.biz.framework.datapermission.core.annotation.DataPermission;
import cn.ey.biz.framework.excel.core.util.ExcelUtils;
import cn.ey.biz.framework.operatelog.core.annotations.OperateLog;
import cn.ey.biz.module.system.controller.admin.i18n.vo.*;
import cn.ey.biz.module.system.convert.i18n.I18nConvert;
import cn.ey.biz.module.system.dal.dataobject.i18n.I18nDO;
import cn.ey.biz.module.system.service.i18n.I18nService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.ACCEPT_LANGUAGE_NOT_NULL;

@Api(tags = "管理后台 - 多语言")
@RestController
@RequestMapping("/infra/I18n")
@Validated
public class I18nController {

    @Resource
    private I18nService i18nService;

    @PostMapping("/create-list")
    @ApiOperation("创建多语言")
    @PreAuthorize("@ss.hasPermission('infra:I18n:create')")
    public R<Void> createI18nList(@Valid @RequestBody List<I18nCreateReqVO> createReqVO) {
        i18nService.createI18nList(createReqVO);
        return R.success(null);
    }

    @PostMapping("/create")
    @ApiOperation("创建多语言")
    @PreAuthorize("@ss.hasPermission('infra:I18n:create')")
    public R<Long> createI18n(@Valid @RequestBody I18nCreateReqVO createReqVO) {
        return success(i18nService.createI18n(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新多语言")
    @PreAuthorize("@ss.hasPermission('infra:I18n:update')")
    public R<Boolean> updateI18n(@Valid @RequestBody I18nUpdateReqVO updateReqVO) {
        i18nService.updateI18n(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除多语言")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('infra:I18n:delete')")
    public R<Boolean> deleteI18n(@RequestParam("id") Long id) {
        i18nService.deleteI18n(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得多语言")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('infra:I18n:query')")
    public R<I18nRespVO> getI18n(@RequestParam("id") Long id) {
        I18nDO i18n = i18nService.getI18n(id);
        return success(I18nConvert.INSTANCE.convert(i18n));
    }

    @GetMapping("/list")
    @ApiOperation("获得多语言列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('infra:I18n:query')")
    public R<List<I18nRespVO>> getI18nList(@RequestParam("ids") Collection<Long> ids) {
        List<I18nDO> list = i18nService.getI18nList(ids);
        return success(I18nConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得多语言分页")
    @PreAuthorize("@ss.hasPermission('infra:I18n:query')")
    @DataPermission
    public R<PageResult<I18nRespVO>> getI18nPage(@Valid I18nPageReqVO pageVO) {
        PageResult<I18nRespVO> pageResult = i18nService.getI18nPage(pageVO);
        return success(I18nConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出多语言 Excel")
    @PreAuthorize("@ss.hasPermission('infra:I18n:export')")
    @OperateLog(type = EXPORT)
    public void exportI18nExcel(@Valid I18nExportReqVO exportReqVO,
                                HttpServletResponse response) throws IOException {
        List<I18nDO> list = i18nService.getI18nList(exportReqVO);
        // 导出 Excel
        List<I18nExcelVO> datas = I18nConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "多语言.xls", "数据", I18nExcelVO.class, datas);
    }

    @DeleteMapping("/delete-by-key")
    @ApiOperation("根据Key删除多语言")
    @ApiImplicitParam(name = "key", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('infra:I18n:delete')")
    public R<Boolean> deleteI18nByKey(@RequestParam("key") String key) {
        i18nService.deleteI18nByKey(key);
        return success(true);
    }

    @GetMapping("/list-by-app")
    @ApiOperation("使用多语言")
    @ApiImplicitParam(name = "app", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('infra:I18n:query')")
    public R<List<I18nDO>> getI18nByApp(@RequestParam("app") String app) {
        // 获取语言
        String acceptLanguage = I18nContextHolder.getLangId();
        if (StringUtils.isEmpty(acceptLanguage)) {
            throw exception(ACCEPT_LANGUAGE_NOT_NULL);
        }
        List<I18nDO> i18nDO = i18nService.getI18nByApp(acceptLanguage, app);
        return success(i18nDO);
    }

    @PostMapping("/import")
    @ApiOperation("Excel导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "Excel 文件", required = true, dataTypeClass = MultipartFile.class)
    })
    @PreAuthorize("@ss.hasPermission('infra:I18n:import')")
    public R<Boolean> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<I18nExcelImportVO> list = ExcelUtils.read(file, I18nExcelImportVO.class);
        i18nService.importI18nListLabel(list);
        return success(true);
    }

}
