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

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

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

public class LogErrAjax {

    @Autowired
    private LogErrManager logErrManager;

    public LogErrCreateResponse create(LogErrCreateRequest request) {
        return logErrManager.create(request, LocalData.getToken());
    }

    public LogErrDeleteResponse delete(LogErrDeleteRequest request) {
        return logErrManager.delete(request, LocalData.getToken());
    }

    public LogErrUpdateResponse update(LogErrUpdateRequest request) {
        return logErrManager.update(request, LocalData.getToken());
    }

    public LogErrFindResponse find(LogErrFindRequest request) {
        return logErrManager.find(request, LocalData.getToken());
    }

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

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

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