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

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

public class RoleAjax {

    @Autowired
    private RoleManager roleManager;

    public RoleCreateResponse create(RoleCreateRequest request) {
        return roleManager.create(request, LocalData.getToken());
    }

    public RoleDeleteResponse delete(RoleDeleteRequest request) {
        return roleManager.delete(request, LocalData.getToken());
    }

    public RoleUpdateResponse update(RoleUpdateRequest request) {
        return roleManager.update(request, LocalData.getToken());
    }

    public RoleFindResponse find(RoleFindRequest request) {
        return roleManager.find(request, LocalData.getToken());
    }

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

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

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