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.DictItem;
import ${basePackage}.module.system.mgr.DictItemManager;
import ${basePackage}.module.system.req.*;
import ${basePackage}.module.system.rsp.*;

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

public class DictItemAjax {

    @Autowired
    private DictItemManager dictItemManager;

    public DictItemCreateResponse create(TreeNode jsonParam) {
        DictItemCreateRequest request = MapperUtil.toJava(jsonParam, DictItemCreateRequest.class);
        return dictItemManager.create(request, LocalData.getToken());
    }

    public DictItemDeleteResponse delete(TreeNode jsonParam) {
        DictItemDeleteRequest request = MapperUtil.toJava(jsonParam, DictItemDeleteRequest.class);
        return dictItemManager.delete(request, LocalData.getToken());
    }

    public DictItemUpdateResponse update(TreeNode jsonParam) {
        DictItemUpdateRequest request = MapperUtil.toJava(jsonParam, DictItemUpdateRequest.class);
        return dictItemManager.update(request, LocalData.getToken());
    }

    public DictItemFindResponse find(TreeNode jsonParam) {
        DictItemFindRequest request = MapperUtil.toJava(jsonParam, DictItemFindRequest.class);
        return dictItemManager.find(request, LocalData.getToken());
    }

    public DictItemGetResponse get(TreeNode jsonParam) {
        DictItemGetRequest request = MapperUtil.toJava(jsonParam, DictItemGetRequest.class);
        return dictItemManager.get(request, LocalData.getToken());
    }

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

    public Object exports(TreeNode jsonParam) {
        DictItemFindRequest request = MapperUtil.toJava(jsonParam, DictItemFindRequest.class);
        DictItemFindResponse response = dictItemManager.find(request, LocalData.getToken());
        if (response.hasError()) {
            return response;
        }
        return ResponseUtil.apply(new WSheet<>(response.getResult()));
    }

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