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

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

public class DictAjax {

    @Autowired
    private DictManager dictManager;

    public DictCreateResponse create(DictCreateRequest request) {
        return dictManager.create(request, LocalData.getToken());
    }

    public DictDeleteResponse delete(DictDeleteRequest request) {
        return dictManager.delete(request, LocalData.getToken());
    }

    public DictUpdateResponse update(DictUpdateRequest request) {
        return dictManager.update(request, LocalData.getToken());
    }

    public DictFindResponse find(DictFindRequest request) {
        return dictManager.find(request, LocalData.getToken());
    }

    public DictLoadResponse load(DictLoadRequest request) {
        return dictManager.load(request, LocalData.getToken());
    }

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

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

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