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

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.wsvr.ent.Logerr;
import ${domain}.module.wsvr.mgr.LogerrManager;
import ${domain}.module.wsvr.req.LogerrCreateRequest;
import ${domain}.module.wsvr.req.LogerrDeleteRequest;
import ${domain}.module.wsvr.req.LogerrFindRequest;
import ${domain}.module.wsvr.req.LogerrUpdateRequest;
import ${domain}.module.wsvr.rsp.LogerrCreateResponse;
import ${domain}.module.wsvr.rsp.LogerrDeleteResponse;
import ${domain}.module.wsvr.rsp.LogerrFindResponse;
import ${domain}.module.wsvr.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;
    }
}
