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

import com.fasterxml.jackson.core.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import ${basePackage}.frame.excel.WSheet;
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.ReadErrorException;
import ${basePackage}.frame.excel.exception.TemplateNotMatchException;
import ${basePackage}.module.system.ent.Dict;
import ${basePackage}.module.system.mgr.DictManager;
import ${basePackage}.module.system.req.*;
import ${basePackage}.module.system.rsp.*;

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 DictGetResponse get(DictGetRequest request) {
        return dictManager.get(request, LocalData.getToken());
    }

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

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

    public Object exports(TreeNode jsonParam) {
        DictFindRequest request = MapperUtil.toJava(jsonParam, DictFindRequest.class);
        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 WSheet<>(response.getResult()));
    }

    public Object imports(MultipartFile file) {
        BaseResponse baseResponse = new BaseResponse();
        try {
            WSheet<Dict> sheet = new WSheet<>(file.getBytes(), Dict.class, new WSheet.Validator<Dict>() {
                @Override
                public List<String> validate(Dict o) {
                    DictCreateRequest request = MapperUtil.map(o, DictCreateRequest.class);
                    return ValidationUtil.validate(request);
                }
            });

            if (sheet.hasError()) {
                return ResponseUtil.apply(sheet.getBytes(true), 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;
    }
}
