package com.docus.server.common;

import com.docus.infrastructure.core.db.enums.EnumItemView;
import com.docus.infrastructure.core.db.enums.IIntegerEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/common/")
@Api(value = "通用接口", tags = "通用接口")
@IgnoreValidate
public class CommonController {

    @Value("${mybatis-plus.type-enums-package:#{nu11}}")
    private String enumsPackage;
    @Value("${spring.application.name: #{null}}")
    private String appName;

    //返回数据库枚举定义的value和display，枚举需实现IIntegerEnum接口
    @GetMapping("enum/{enumName}")
    @ApiOperation("按枚举名称获取枚举列表值")
    public List<EnumItemView> getEnum(@PathVariable String enumName) throws ClassNotFoundException {
        if (!StringUtils.isEmpty(enumsPackage)) {
            String className = enumsPackage + "." + enumName;
            if (!className.endsWith("Enum")) {
                className += "Enum";
            }
            Class<IIntegerEnum> enumClass = (Class<IIntegerEnum>) Class.forName(className);
            IIntegerEnum[] enums = enumClass.getEnumConstants();
            List<EnumItemView> enumItemViews = new ArrayList<>();
            for (IIntegerEnum anEnum : enums) {
                EnumItemView enumItemView = new EnumItemView();
                enumItemView.setValue(anEnum.getValue());
                enumItemView.setDisplay(anEnum.getDisplay());
                enumItemViews.add(enumItemView);
            }
            return enumItemViews;
        }
        return null;
    }

    @GetMapping("excelTemplate/download/{fileName}/{filePath}")
    @ApiOperation("下载excel")
    public void downloadExcelTemplate(HttpServletResponse response, @PathVariable("fileName") String fileName, @PathVariable("filePath") String filePath) throws Exception {
        //得到该文件
        ClassPathResource classPathResource = new ClassPathResource(String.format("/%s/%s", filePath, fileName));
        InputStream fileInputStream = classPathResource.getInputStream();
        response.setHeader("Content-Disposition", "attachment;Filename=" + URLEncoder.encode(fileName, "UTF-8"));
        OutputStream outputStream = response.getOutputStream();
        byte[] bytes = new byte[2048];
        int len = 0;
        while ((len = fileInputStream.read(bytes)) > 0) {
            outputStream.write(bytes, 0, len);
        }
        outputStream.flush();
        fileInputStream.close();
        outputStream.close();
    }
}
