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.DictItem;
import ${basePackage}.module.system.mgr.DictItemManager;
import ${basePackage}.module.system.req.DictItemCreateRequest;
import ${basePackage}.module.system.req.DictItemDeleteRequest;
import ${basePackage}.module.system.req.DictItemFindRequest;
import ${basePackage}.module.system.req.DictItemGetRequest;
import ${basePackage}.module.system.req.DictItemUpdateRequest;
import ${basePackage}.module.system.rsp.DictItemCreateResponse;
import ${basePackage}.module.system.rsp.DictItemDeleteResponse;
import ${basePackage}.module.system.rsp.DictItemFindResponse;
import ${basePackage}.module.system.rsp.DictItemGetResponse;
import ${basePackage}.module.system.rsp.DictItemUpdateResponse;

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

public class DictItemAjax {

    @Autowired
    private DictItemManager dictItemManager;

    public DictItemCreateResponse create(DictItemCreateRequest request, Token token) {
        return dictItemManager.create(request, token);
    }

    public DictItemDeleteResponse delete(DictItemDeleteRequest request, Token token) {
        return dictItemManager.delete(request, token);
    }

    public DictItemUpdateResponse update(DictItemUpdateRequest request, Token token) {
        return dictItemManager.update(request, token);
    }

    public DictItemFindResponse find(DictItemFindRequest request, Token token) {
        return dictItemManager.find(request, token);
    }

    public DictItemGetResponse get(DictItemGetRequest request, Token token) {
        return dictItemManager.get(request, token);
    }

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

    public Object exports(DictItemFindRequest request, Token token) {
        DictItemFindResponse response = dictItemManager.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<DictItem>(DictItem.class).loadData(response.getResult()));
    }

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