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

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

public class DeptAjax {

    @Autowired
    private DeptManager deptManager;

    public DeptCreateResponse create(DeptCreateRequest request) {
        return deptManager.create(request, LocalData.getToken());
    }

    public DeptDeleteResponse delete(DeptDeleteRequest request) {
        return deptManager.delete(request, LocalData.getToken());
    }

    public DeptUpdateResponse update(DeptUpdateRequest request) {
        return deptManager.update(request, LocalData.getToken());
    }

    public DeptFindResponse find(DeptFindRequest request) {
        return deptManager.find(request, LocalData.getToken());
    }

    public DeptTreeResponse tree(DeptTreeRequest request) {
        return deptManager.tree(request, LocalData.getToken());
    }

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

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

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