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.Dept;
import ${basePackage}.module.system.mgr.DeptManager;
import ${basePackage}.module.system.req.DeptCreateRequest;
import ${basePackage}.module.system.req.DeptDeleteRequest;
import ${basePackage}.module.system.req.DeptFindRequest;
import ${basePackage}.module.system.req.DeptGetRequest;
import ${basePackage}.module.system.req.DeptTreeRequest;
import ${basePackage}.module.system.req.DeptUpdateRequest;
import ${basePackage}.module.system.rsp.DeptCreateResponse;
import ${basePackage}.module.system.rsp.DeptDeleteResponse;
import ${basePackage}.module.system.rsp.DeptFindResponse;
import ${basePackage}.module.system.rsp.DeptGetResponse;
import ${basePackage}.module.system.rsp.DeptTreeResponse;
import ${basePackage}.module.system.rsp.DeptUpdateResponse;

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

public class DeptAjax {

    @Autowired
    private DeptManager deptManager;

    public DeptCreateResponse create(DeptCreateRequest request, Token token) {
        return deptManager.create(request, token);
    }

    public DeptDeleteResponse delete(DeptDeleteRequest request, Token token) {
        return deptManager.delete(request, token);
    }

    public DeptUpdateResponse update(DeptUpdateRequest request, Token token) {
        return deptManager.update(request, token);
    }

    public DeptFindResponse find(DeptFindRequest request, Token token) {
        return deptManager.find(request, token);
    }

    public DeptGetResponse get(DeptGetRequest request, Token token) {
        return deptManager.get(request, token);
    }

    public DeptTreeResponse tree(DeptTreeRequest request, Token token) {
        return deptManager.tree(request, token);
    }

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

    public Object exports(DeptFindRequest request, Token token) {
        DeptFindResponse response = deptManager.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<>(Dept.class).loadData(response.getResult()));
    }

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