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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.excel.WExcel;
import ${basePackage}.frame.utils.LogUtil;
import ${basePackage}.frame.auth.LocalData;
import ${basePackage}.frame.base.ErrorType;
import ${basePackage}.frame.utils.MapperUtil;
import ${basePackage}.frame.base.BaseResponse;
import ${basePackage}.frame.utils.ResponseUtil;
import ${basePackage}.frame.utils.ValidationUtil;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.module.system.ent.Profiles;
import ${basePackage}.module.system.mgr.ProfilesManager;
import ${basePackage}.module.system.req.ProfilesCreateRequest;
import ${basePackage}.module.system.req.ProfilesDeleteRequest;
import ${basePackage}.module.system.req.ProfilesFindRequest;
import ${basePackage}.module.system.req.ProfilesUpdateRequest;
import ${basePackage}.module.system.rsp.ProfilesCreateResponse;
import ${basePackage}.module.system.rsp.ProfilesDeleteResponse;
import ${basePackage}.module.system.rsp.ProfilesFindResponse;
import ${basePackage}.module.system.rsp.ProfilesUpdateResponse;

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

public class ProfilesAjax{

    @Autowired
    private ProfilesManager profilesManager;

    public ProfilesCreateResponse create(ProfilesCreateRequest request) {
        return profilesManager.create(request, LocalData.getToken());
    }

    public ProfilesDeleteResponse delete(ProfilesDeleteRequest request) {
        return profilesManager.delete(request, LocalData.getToken());
    }

    public ProfilesUpdateResponse update(ProfilesUpdateRequest request) {
        return profilesManager.update(request, LocalData.getToken());
    }

    public ProfilesFindResponse find(ProfilesFindRequest request) {
        return profilesManager.find(request, LocalData.getToken());
    }

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

    public Object exports(ProfilesFindRequest request) {
        ProfilesFindResponse response = profilesManager.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<>(Profiles.class).addDatas(response.getResult()));
    }

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