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

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

public class ResAjax {

    @Autowired
    private ResManager resManager;

    public ResCreateResponse create(ResCreateRequest request) {
        return resManager.create(request, LocalData.getToken());
    }

    public ResDeleteResponse delete(ResDeleteRequest request) {
        return resManager.delete(request, LocalData.getToken());
    }

    public ResUpdateResponse update(ResUpdateRequest request) {
        return resManager.update(request, LocalData.getToken());
    }

    public ResFindResponse find(ResFindRequest request) {
        return resManager.find(request, LocalData.getToken());
    }

    public ResTreeResponse tree(ResTreeRequest request) {
        return resManager.tree(request, LocalData.getToken());
    }

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

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

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