package com.yem.em.form;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.IFormView;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhouc
 * @date 2023/12/24 16:05
 * @className WKISVFormEdit
 * @description
 */
public class WKISVFormEdit extends AbstractFormPlugin {
    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String key = e.getOperateKey();
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        if ("jx".equals(key)) {
            QFilter filter = new QFilter("billstatus", QCP.not_equals, null);
            DynamicObjectCollection yem_em_assatlasdet = QueryServiceHelper.query(dataEntity.getDynamicObjectType().getName(),
                    "id,yem_orltext,yem_isvid,yem_bizmodule,yem_applicationgroupid,yem_tretext"
                    , new QFilter[]{filter}, null);
            for (int i = 0; i < yem_em_assatlasdet.size(); i++) {
                DynamicObject assatlasdet = yem_em_assatlasdet.get(i);
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(assatlasdet.getLong("id"), dataEntity.getDynamicObjectType().getName());
                if (dynamicObject != null) {
                    String yem_orltext = dynamicObject.getString("yem_orltext");
                    String yem_isvid = idGetstr("Id=\"(.*?)\"", yem_orltext);
                    String yem_bizmodule = idGetstr("BizModule=\"(.*?)\"", yem_orltext);
                    String yem_applicationgroupid = idGetstr("ApplicationGroupId=\"(.*?)\"", yem_orltext);
                    dynamicObject.set("yem_isvid", yem_isvid);
                    dynamicObject.set("yem_bizmodule", yem_bizmodule);
                    dynamicObject.set("yem_applicationgroupid", yem_applicationgroupid);
                    String pjg = "<Objects Id=\"" + yem_isvid + "\" ApplicationGroupId=\"" + yem_applicationgroupid + "\" BizModule=\"" + yem_bizmodule + "\"/>";
                    dynamicObject.set("yem_tretext", pjg);
                    OperationResult Operate = OperationServiceHelper.executeOperate("save",
                            dynamicObject.getDynamicObjectType().getName(),
                            new DynamicObject[]{dynamicObject}, OperateOption.create());
                    if (Operate.isSuccess()) {

                    }
                }
            }
        }
        if ("pj".equals(key)) {
            QFilter filter = new QFilter("billstatus", QCP.not_equals, null);
            DynamicObjectCollection yem_em_assatlasdet = QueryServiceHelper.query(dataEntity.getDynamicObjectType().getName(),
                    "id,yem_orltext,yem_isvid,yem_bizmodule,yem_applicationgroupid,yem_tretext"
                    , new QFilter[]{filter}, null);
            for (int i = 0; i < yem_em_assatlasdet.size(); i++) {
                DynamicObject assatlasdet = yem_em_assatlasdet.get(i);
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(assatlasdet.getLong("id"), dataEntity.getDynamicObjectType().getName());
                if (dynamicObject != null) {
                    String yem_orltext = dynamicObject.getString("yem_orltext");
                    String yem_isvid = dynamicObject.getString("yem_isvid");
                    String yem_bizmodule = dynamicObject.getString("yem_bizmodule");
                    String yem_applicationgroupid = dynamicObject.getString("yem_applicationgroupid");
                    String pjg = "<Objects Id=\"" + yem_isvid + "\" ApplicationGroupId=\"" + yem_applicationgroupid + "\" BizModule=\"" + yem_bizmodule + "\"/>";
                    dynamicObject.set("yem_tretext", pjg);
                    OperationResult Operate = OperationServiceHelper.executeOperate("save",
                            dynamicObject.getDynamicObjectType().getName(),
                            new DynamicObject[]{dynamicObject}, OperateOption.create());
                    if (Operate.isSuccess()) {

                    }
                }
            }
        }

    }

    public String Getstr(String Idpattern, String str) {
        String group = null;
        Pattern pattern = Pattern.compile("\"(.*?)\"");
        Pattern p = Pattern.compile(Idpattern);
        Matcher m = p.matcher(str);
        boolean b = m.find();
        while (m.find()) {
            Matcher matcher = pattern.matcher(m.group(0));
            while (matcher.find()) {
                group = matcher.group().replace("\"", "");
            }
        }
        return group;
    }

    public String idGetstr(String Idpattern, String str) {
        String group = null;
        Pattern pattern = Pattern.compile("\"(.*?)\"");
        Pattern p = Pattern.compile(Idpattern);
        Matcher m = p.matcher(str);
        boolean b = m.find();
        if (b) {
            group = m.group(1);
        }

        return group;
    }
}
