package ${basePackage}.action.ajax.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.Token;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.excel.exception.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.frame.utils.LogUtil;
import ${basePackage}.frame.utils.MapperUtil;
import ${basePackage}.frame.utils.ResponseUtil;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.module.system.ent.Tokens;
import ${basePackage}.module.system.mgr.TokensManager;
import ${basePackage}.module.system.req.TokensCreateRequest;
import ${basePackage}.module.system.req.TokensDeleteRequest;
import ${basePackage}.module.system.req.TokensFindRequest;
import ${basePackage}.module.system.req.TokensGetRequest;
import ${basePackage}.module.system.req.TokensLogoutRequest;
import ${basePackage}.module.system.req.TokensUpdateRequest;
import ${basePackage}.module.system.rsp.TokensCreateResponse;
import ${basePackage}.module.system.rsp.TokensDeleteResponse;
import ${basePackage}.module.system.rsp.TokensFindResponse;
import ${basePackage}.module.system.rsp.TokensGetResponse;
import ${basePackage}.module.system.rsp.TokensLogoutResponse;
import ${basePackage}.module.system.rsp.TokensUpdateResponse;

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

public class TokensAjax {

    @Autowired
    private TokensManager tokensManager;

    public TokensCreateResponse create(TokensCreateRequest request, Token token) {
        return tokensManager.create(request, token);
    }

    public TokensDeleteResponse delete(TokensDeleteRequest request, Token token) {
        return tokensManager.delete(request, token);
    }

    public TokensUpdateResponse update(TokensUpdateRequest request, Token token) {
        return tokensManager.update(request, token);
    }

    public TokensFindResponse find(TokensFindRequest request, Token token) {
        return tokensManager.find(request, token);
    }

    public TokensGetResponse get(TokensGetRequest request, Token token) {
        return tokensManager.get(request, token);
    }

    public TokensLogoutResponse logout(TokensLogoutRequest request, Token token) {
        return tokensManager.logout(request, token);
    }

    public Object template() {
        return ResponseUtil.apply(new WExcel<>(Tokens.class));
    }

    public Object exports(TokensFindRequest request, Token token) {
        TokensFindResponse response = tokensManager.find(request, token);
        if (response.hasError()) {
            return response;
        } else if (response.getTotalCount() == 0) {
            response.addError(ErrorType.BUSINESS_ERROR, "导出数据为空");
            return response;
        }
        return ResponseUtil.apply(new WExcel<>(Tokens.class).loadData(response.getResult()));
    }

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            WExcel sheet = new WExcel<>(Tokens.class).loadData(file.getBytes(), new WExcel.Processor<Tokens>() {
                @Override
                public List<String> exec(Tokens o) {
                    TokensCreateRequest request = MapperUtil.map(o, TokensCreateRequest.class);
                    return ValidationUtil.validate(request);
                }
            });

            if (sheet.hasError()) {
                return ResponseUtil.apply(sheet.getBytes(), sheet.getName() + "-检查.xlsx");
            } else {
                return baseResponse;
            }
        } catch (IOException e) {
            e.printStackTrace();
            LogUtil.dumpException(e);
            baseResponse.addError(ErrorType.BUSINESS_ERROR, "上传文件出错");
        } catch (TemplateNotMatchException | ReadErrorException e) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, e.getMessage());
        }
        return baseResponse;
    }
}
