package ${domain}.action.ajax.wsys;

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

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

public class TokensAjax {

    @Autowired
    private TokensManager tokensManager;

    public TokensCreateResponse create(TokensCreateRequest request) {
        return tokensManager.create(request, LocalData.getToken());
    }

    public TokensDeleteResponse delete(TokensDeleteRequest request) {
        return tokensManager.delete(request, LocalData.getToken());
    }

    public TokensUpdateResponse update(TokensUpdateRequest request) {
        return tokensManager.update(request, LocalData.getToken());
    }

    public TokensFindResponse find(TokensFindRequest request) {
        return tokensManager.find(request, LocalData.getToken());
    }

    public TokensLogoutResponse logout(TokensLogoutRequest request) {
        return tokensManager.logout(request, LocalData.getToken());
    }

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

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

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            WExcel sheet = new WExcel<>(Tokens.class).read(file.getBytes(), new WExcel.Processor<Tokens>() {
                @Override
                public List<String> exec(Tokens o, int index) {
                    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 e) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, e.getMessage());
        }
        return baseResponse;
    }
}
