package ${basePackage}.action.ajax.system;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.excel.WExcel;
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.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.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) {
        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;
    }
}
