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.Res;
import ${basePackage}.module.system.mgr.ResManager;
import ${basePackage}.module.system.req.ResCreateRequest;
import ${basePackage}.module.system.req.ResDeleteRequest;
import ${basePackage}.module.system.req.ResFindRequest;
import ${basePackage}.module.system.req.ResGetRequest;
import ${basePackage}.module.system.req.ResTreeRequest;
import ${basePackage}.module.system.req.ResUpdateRequest;
import ${basePackage}.module.system.rsp.ResCreateResponse;
import ${basePackage}.module.system.rsp.ResDeleteResponse;
import ${basePackage}.module.system.rsp.ResFindResponse;
import ${basePackage}.module.system.rsp.ResGetResponse;
import ${basePackage}.module.system.rsp.ResTreeResponse;
import ${basePackage}.module.system.rsp.ResUpdateResponse;

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

public class ResAjax {

    @Autowired
    private ResManager resManager;

    public ResCreateResponse create(ResCreateRequest request, Token token) {
        return resManager.create(request, token);
    }

    public ResDeleteResponse delete(ResDeleteRequest request, Token token) {
        return resManager.delete(request, token);
    }

    public ResUpdateResponse update(ResUpdateRequest request, Token token) {
        return resManager.update(request, token);
    }

    public ResFindResponse find(ResFindRequest request, Token token) {
        return resManager.find(request, token);
    }

    public ResGetResponse get(ResGetRequest request, Token token) {
        return resManager.get(request, token);
    }

    public ResTreeResponse tree(ResTreeRequest request, Token token) {
        return resManager.tree(request, token);
    }

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

    public Object exports(ResFindRequest request, Token token) {
        ResFindResponse response = resManager.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<>(Res.class).loadData(response.getResult()));
    }

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