package nckd.jimin.jyyy.fi.plugin.operate;

import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.validate.ErrorLevel;
import kd.bos.entity.validate.ValidationErrorInfo;
import kd.sdk.plugin.Plugin;
import nckd.jimin.jyyy.fi.mservice.impl.SynSapFIServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 单据操作插件
 */
public class SynVoucherSapOpPlugin extends AbstractOperationServicePlugIn implements Plugin {
    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        //获取列表中选择的单据内码
        List<Long> billids = (List) Arrays.asList(e.getDataEntities()).stream().map((entityx) -> {
            return entityx.getLong("id");
        }).collect(Collectors.toList());

        if(billids != null || billids.size() > 0) {
            SynSapFIServiceImpl synSapService = new SynSapFIServiceImpl();
            Map<String, String> result=synSapService.synVoucherForSap(billids);

            if(result.get("code").equals("200")){
                this.operationResult.setShowMessage(true);
                this.operationResult.setSuccess(false);
                this.operationResult.setMessage(result.get("msg"));
            }else{
                this.operationResult.addErrorInfo(new ValidationErrorInfo("", null,
                        0, 0, "同步失败", "billtitle", result.get("msg"), ErrorLevel.Error));
            }
        }else{
            this.operationResult.setShowMessage(true);
            this.operationResult.setSuccess(false);
            this.operationResult.setMessage("没选中数据！");
        }
    }

}