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.LogErr;
import ${basePackage}.module.system.mgr.LogErrManager;
import ${basePackage}.module.system.req.LogErrCreateRequest;
import ${basePackage}.module.system.req.LogErrDeleteRequest;
import ${basePackage}.module.system.req.LogErrFindRequest;
import ${basePackage}.module.system.req.LogErrGetRequest;
import ${basePackage}.module.system.req.LogErrUpdateRequest;
import ${basePackage}.module.system.rsp.LogErrCreateResponse;
import ${basePackage}.module.system.rsp.LogErrDeleteResponse;
import ${basePackage}.module.system.rsp.LogErrFindResponse;
import ${basePackage}.module.system.rsp.LogErrGetResponse;
import ${basePackage}.module.system.rsp.LogErrUpdateResponse;

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

public class LogErrAjax {

    @Autowired
    private LogErrManager logErrManager;

    public LogErrCreateResponse create(LogErrCreateRequest request, Token token) {
        return logErrManager.create(request, token);
    }

    public LogErrDeleteResponse delete(LogErrDeleteRequest request, Token token) {
        return logErrManager.delete(request, token);
    }

    public LogErrUpdateResponse update(LogErrUpdateRequest request, Token token) {
        return logErrManager.update(request, token);
    }

    public LogErrFindResponse find(LogErrFindRequest request, Token token) {
        return logErrManager.find(request, token);
    }

    public LogErrGetResponse get(LogErrGetRequest request, Token token) {
        return logErrManager.get(request, token);
    }

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

    public Object exports(LogErrFindRequest request, Token token) {
        LogErrFindResponse response = logErrManager.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<>(LogErr.class).loadData(response.getResult()));
    }

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            WExcel sheet = new WExcel<>(LogErr.class).loadData(file.getBytes(), new WExcel.Processor<LogErr>() {
                @Override
                public List<String> exec(LogErr o) {
                    LogErrCreateRequest request = MapperUtil.map(o, LogErrCreateRequest.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;
    }
}
