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.Visitor;
import ${domain}.module.wsys.mgr.VisitorManager;
import ${domain}.module.wsys.req.VisitorCreateRequest;
import ${domain}.module.wsys.req.VisitorDeleteRequest;
import ${domain}.module.wsys.req.VisitorFindRequest;
import ${domain}.module.wsys.req.VisitorUpdateRequest;
import ${domain}.module.wsys.rsp.VisitorCreateResponse;
import ${domain}.module.wsys.rsp.VisitorDeleteResponse;
import ${domain}.module.wsys.rsp.VisitorFindResponse;
import ${domain}.module.wsys.rsp.VisitorUpdateResponse;

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

public class VisitorAjax{

    @Autowired
    private VisitorManager visitorManager;

    public VisitorCreateResponse create(VisitorCreateRequest request) {
        return visitorManager.create(request, LocalData.getToken());
    }

    public VisitorDeleteResponse delete(VisitorDeleteRequest request) {
        return visitorManager.delete(request, LocalData.getToken());
    }

    public VisitorUpdateResponse update(VisitorUpdateRequest request) {
        return visitorManager.update(request, LocalData.getToken());
    }

    public VisitorFindResponse find(VisitorFindRequest request) {
        return visitorManager.find(request, LocalData.getToken());
    }

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

    public Object exports(VisitorFindRequest request) {
        VisitorFindResponse response = visitorManager.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<>(Visitor.class).addDatas(response.getResult()));
    }

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