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.Dict;
import ${basePackage}.module.system.mgr.DictManager;
import ${basePackage}.module.system.req.DictCreateRequest;
import ${basePackage}.module.system.req.DictDeleteRequest;
import ${basePackage}.module.system.req.DictFindRequest;
import ${basePackage}.module.system.req.DictGetRequest;
import ${basePackage}.module.system.req.DictLoadRequest;
import ${basePackage}.module.system.req.DictUpdateRequest;
import ${basePackage}.module.system.rsp.DictCreateResponse;
import ${basePackage}.module.system.rsp.DictDeleteResponse;
import ${basePackage}.module.system.rsp.DictFindResponse;
import ${basePackage}.module.system.rsp.DictGetResponse;
import ${basePackage}.module.system.rsp.DictLoadResponse;
import ${basePackage}.module.system.rsp.DictUpdateResponse;

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

public class DictAjax {

    @Autowired
    private DictManager dictManager;

    public DictCreateResponse create(DictCreateRequest request, Token token) {
        return dictManager.create(request, token);
    }

    public DictDeleteResponse delete(DictDeleteRequest request, Token token) {
        return dictManager.delete(request, token);
    }

    public DictUpdateResponse update(DictUpdateRequest request, Token token) {
        return dictManager.update(request, token);
    }

    public DictFindResponse find(DictFindRequest request, Token token) {
        return dictManager.find(request, token);
    }

    public DictGetResponse get(DictGetRequest request, Token token) {
        return dictManager.get(request, token);
    }

    public DictLoadResponse load(DictLoadRequest request, Token token) {
        return dictManager.load(request, token);
    }

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

    public Object exports(DictFindRequest request, Token token) {
        DictFindResponse response = dictManager.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<>(Dict.class).loadData(response.getResult()));
    }

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