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.Role;
import ${basePackage}.module.system.mgr.RoleManager;
import ${basePackage}.module.system.req.RoleCreateRequest;
import ${basePackage}.module.system.req.RoleDeleteRequest;
import ${basePackage}.module.system.req.RoleFindRequest;
import ${basePackage}.module.system.req.RoleGetRequest;
import ${basePackage}.module.system.req.RoleUpdateRequest;
import ${basePackage}.module.system.rsp.RoleCreateResponse;
import ${basePackage}.module.system.rsp.RoleDeleteResponse;
import ${basePackage}.module.system.rsp.RoleFindResponse;
import ${basePackage}.module.system.rsp.RoleGetResponse;
import ${basePackage}.module.system.rsp.RoleUpdateResponse;

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

public class RoleAjax {

    @Autowired
    private RoleManager roleManager;

    public RoleCreateResponse create(RoleCreateRequest request, Token token) {
        return roleManager.create(request, token);
    }

    public RoleDeleteResponse delete(RoleDeleteRequest request, Token token) {
        return roleManager.delete(request, token);
    }

    public RoleUpdateResponse update(RoleUpdateRequest request, Token token) {
        return roleManager.update(request, token);
    }

    public RoleFindResponse find(RoleFindRequest request, Token token) {
        return roleManager.find(request, token);
    }

    public RoleGetResponse get(RoleGetRequest request, Token token) {
        return roleManager.get(request, token);
    }

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

    public Object exports(RoleFindRequest request, Token token) {
        RoleFindResponse response = roleManager.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<>(Role.class).loadData(response.getResult()));
    }

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