package com.yem.wm.bc.change.from;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.yem.wm.bc.change.common.consts.BizChangeTypeEnum;
import com.yem.wm.bc.common.CommonUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.BindingContext;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.Label;
import kd.bos.form.control.TreeEntryGrid;
import kd.bos.form.control.events.ClickListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.util.StringUtils;

import java.util.*;

/**
 * @author 夜子
 * @ClassName ChangeResumeDetailPlugin
 * @description: 变更日志表单插件
 * @date 2023年08月10日
 * @version: 1.0
 */
public class ChangeResumeDetailPlugin extends AbstractFormPlugin implements ClickListener {
    private static final String key_update = "*";
    private static final String key_add = "+";
    private static final String key_delete = "-";
    private static final String key_equal = "=";
    public static final String key_oldValue = "o";
    public static final String key_newValue = "n";
    public static final String key_fieldname = "c";
    public static final String key_isequals = "e";
    int rowCount = 10;

    public ChangeResumeDetailPlugin() {
    }

    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        IDataModel dataModel = this.getModel();
        String xbillHeardJson = (String)dataModel.getValue("crdheadjson_tag");
        if (!CommonUtils.isNull(xbillHeardJson)) {
            this.setXBillHeard(xbillHeardJson);
        }

        String xbillEntryJson = (String)dataModel.getValue("crdentryjson_tag");
        if (!CommonUtils.isNull(xbillEntryJson)) {
            this.getView().getPageCache().put("xbillEntryJson", xbillEntryJson);
            LinkedHashMap<String, Object> xbillEntryJsonMap =  JSON.parseObject(xbillEntryJson, new TypeReference<LinkedHashMap<String, Object>>() {
            }, new Feature[0]);
            this.countLabel(xbillEntryJsonMap);

            for(int i = 0; i < 6; ++i) {
                String treeentryEntityKey = "treeentryentity" + i;
                String entrykey = this.getView().getPageCache().get(treeentryEntityKey + "_entrykey");
                if (!CommonUtils.isNull(entrykey)) {
                    Object obj = xbillEntryJsonMap.get(entrykey);
                    this.batchSetEntry(obj, String.valueOf(i), 1);
                    EntryGrid entryGrid = (EntryGrid)this.getView().getControl(treeentryEntityKey);
                    if (entryGrid.isStopUpdateCell()) {
                        entryGrid.setStopUpdateCell(false);
                        entryGrid.bindData(new BindingContext(dataModel.getEntryEntity(treeentryEntityKey)));
                    }
                }
            }
        }

        if (CommonUtils.isNull(xbillHeardJson)) {
            this.getView().setVisible(false, new String[]{"entity"});
        }

        if (CommonUtils.isNull(xbillEntryJson)) {
            for(int i = 0; i < 6; ++i) {
                this.getView().setVisible(false, new String[]{"entity" + i});
            }
        }

        this.getView().setEnable(false, new String[]{"fs_xinfo"});
        this.getView().setEnable(false, new String[]{"billxinfo"});
    }

    public void registerListener(EventObject e) {
        super.registerListener(e);

        for(int i = 0; i < 6; ++i) {
            Label nextPageLabel = (Label)this.getView().getControl("nextpage" + i);
            nextPageLabel.addClickListener(this);
            Label lastPageLabel = (Label)this.getView().getControl("lastpage" + i);
            lastPageLabel.addClickListener(this);
            Label previousPageLabel = (Label)this.getView().getControl("previouspage" + i);
            previousPageLabel.addClickListener(this);
            Label firstPageLabel = (Label)this.getView().getControl("firstpage" + i);
            firstPageLabel.addClickListener(this);
        }

    }

    public void click(EventObject evt) {
        super.click(evt);
        Control control = (Control)evt.getSource();
        String key = control.getKey();
        if (control instanceof Label && key.contains("page")) {
            String entryNum = String.valueOf(key.charAt(key.length() - 1));
            String pageLabelKey = key.substring(0, key.length() - 1);
            String currentPageText = this.getView().getPageCache().get("currentpage" + entryNum);
            String pageCountText = this.getView().getPageCache().get("pagecount" + entryNum);
            if (!CommonUtils.isNull(currentPageText) && !CommonUtils.isNull(pageCountText)) {
                int currentPage = Integer.parseInt(currentPageText);
                int pageCount = Integer.parseInt(pageCountText);
                String xbillEntryJson = this.getView().getPageCache().get("xbillEntryJson");
                if (CommonUtils.isNull(xbillEntryJson)) {
                    xbillEntryJson = (String)this.getModel().getValue("crdentryjson_tag");
                }

                LinkedHashMap<String, Object> xbillEntryJsonMap = JSON.parseObject(xbillEntryJson, new TypeReference<LinkedHashMap<String, Object>>() {
                }, new Feature[0]);
                String treeentryEntityKey = "treeentryentity" + entryNum;
                EntryGrid entryGrid = (EntryGrid)this.getView().getControl(treeentryEntityKey);
                String entrykey = this.getView().getPageCache().get(treeentryEntityKey + "_entrykey");
                if (!CommonUtils.isNull(entrykey)) {
                    Object entry = xbillEntryJsonMap.get(entrykey);
                    switch (pageLabelKey) {
                        case "nextpage":
                            if (currentPage != pageCount) {
                                ++currentPage;
                                if (currentPage == pageCount) {
                                    this.getView().setEnable(false, new String[]{"lastpage" + entryNum, "nextpage" + entryNum});
                                }

                                this.getView().setEnable(true, new String[]{"previouspage" + entryNum, "firstpage" + entryNum});
                                this.getModel().setValue("currentpagenum" + entryNum, currentPage);
                                this.batchSetEntry(entry, entryNum, currentPage);
                                this.getView().getPageCache().put("currentpage" + entryNum, String.valueOf(currentPage));
                            }
                            break;
                        case "lastpage":
                            if (currentPage != pageCount) {
                                this.getView().setEnable(true, new String[]{"previouspage" + entryNum, "firstpage" + entryNum});
                                this.getView().setEnable(false, new String[]{"lastpage" + entryNum, "nextpage" + entryNum});
                                this.batchSetEntry(entry, entryNum, pageCount);
                                this.getModel().setValue("currentpagenum" + entryNum, pageCount);
                                this.getView().getPageCache().put("currentpage" + entryNum, pageCountText);
                            }
                            break;
                        case "previouspage":
                            if (currentPage != 1) {
                                --currentPage;
                                if (currentPage == 1) {
                                    this.getView().setEnable(false, new String[]{"previouspage" + entryNum, "firstpage" + entryNum});
                                }

                                this.getView().setEnable(true, new String[]{"lastpage" + entryNum, "nextpage" + entryNum});
                                this.batchSetEntry(entry, entryNum, currentPage);
                                this.getModel().setValue("currentpagenum" + entryNum, currentPage);
                                this.getView().getPageCache().put("currentpage" + entryNum, String.valueOf(currentPage));
                            }
                            break;
                        case "firstpage":
                            if (currentPage != 1) {
                                currentPage = 1;
                                this.getView().setEnable(true, new String[]{"lastpage" + entryNum, "nextpage" + entryNum});
                                this.getView().setEnable(false, new String[]{"previouspage" + entryNum, "firstpage" + entryNum});
                                this.batchSetEntry(entry, entryNum, currentPage);
                                this.getModel().setValue("currentpagenum" + entryNum, Integer.valueOf(currentPage));
                                this.getView().getPageCache().put("currentpage" + entryNum, String.valueOf(currentPage));
                            }
                    }

                    if (entryGrid.isStopUpdateCell()) {
                        entryGrid.setStopUpdateCell(false);
                        entryGrid.bindData(new BindingContext(this.getModel().getEntryEntity(treeentryEntityKey)));
                    }
                }
            }
        }

    }

    public void countLabel(LinkedHashMap<String, Object> xbillEntryJsonMap) {
        int treeEntryNum = 0;
        if (!CommonUtils.isNull(xbillEntryJsonMap)) {
            Iterator var3 = xbillEntryJsonMap.entrySet().iterator();

            label124:
            while(true) {
                Map.Entry entry;
                Object entryJSONArray;
                do {
                    if (!var3.hasNext()) {
                        break label124;
                    }

                    entry = (Map.Entry)var3.next();
                    entryJSONArray = entry.getValue();
                } while(!(entryJSONArray instanceof JSONArray));

                int sum = 0;
                int upSum = 0;
                int newSum = 0;
                int delSum = 0;
                String entryName = "";
                JSONArray treeEntrys = (JSONArray)entryJSONArray;
                if (treeEntryNum > 5) {
                    treeEntryNum = 5;
                }

                Iterator var12 = treeEntrys.iterator();

                while(true) {
                    label119:
                    while(true) {
                        Map lineEntryjsonmap;
                        String lineChangeType;
                        do {
                            do {
                                Object entryjson;
                                do {
                                    if (!var12.hasNext()) {
                                        if (sum != 0) {
                                            String treeentryEntityKey = "treeentryentity" + treeEntryNum;
                                            this.getView().getPageCache().put(treeentryEntityKey + "_entrykey", (String)entry.getKey());
                                            this.setCountLabel(sum, upSum, newSum, delSum, treeEntryNum);
                                            this.setEntryenName("entity" + treeEntryNum, entryName);
                                            this.isNeedPaged(sum, String.valueOf(treeEntryNum));
                                            ++treeEntryNum;
                                        }
                                        continue label124;
                                    }

                                    entryjson = var12.next();
                                } while(!(entryjson instanceof Map));

                                lineEntryjsonmap = (Map)entryjson;
                            } while(CommonUtils.isNull(lineEntryjsonmap));

                            lineChangeType = (String)lineEntryjsonmap.get("f");
                            entryName = getTransString((String)lineEntryjsonmap.get("n"));
                        } while(CommonUtils.isNull(lineChangeType));

                        switch (lineChangeType) {
                            case "+":
                                ++newSum;
                                break;
                            case "-":
                                ++delSum;
                                ++sum;
                                continue;
                            case "*":
                                ++upSum;
                        }

                        if (!"=".equals(lineChangeType)) {
                            ++sum;
                        }

                        if ("=".equals(lineChangeType)) {
                            Iterator var16 = lineEntryjsonmap.values().iterator();

                            while(true) {
                                Object fieldjson;
                                do {
                                    if (!var16.hasNext()) {
                                        continue label119;
                                    }

                                    fieldjson = var16.next();
                                } while(!(fieldjson instanceof JSONArray));

                                JSONArray childEntrys = (JSONArray)fieldjson;
                                String childChangeType = "=";
                                Iterator var20 = childEntrys.iterator();

                                while(var20.hasNext()) {
                                    Object childEntryjson = var20.next();
                                    if (childEntryjson instanceof Map) {
                                        Map<String, Object> childMaps = (Map)childEntryjson;
                                        if (!CommonUtils.isNull(childMaps) && !"=".equals(childMaps.get("f"))) {
                                            childChangeType = "*";
                                            break;
                                        }
                                    }
                                }

                                if ("*".equals(childChangeType)) {
                                    ++sum;
                                    ++upSum;
                                }
                            }
                        }
                    }
                }
            }
        }

        for(int i = treeEntryNum; i < 6; ++i) {
            this.getView().setVisible(false, new String[]{"entity" + i});
        }

    }

    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String fieldName = e.getProperty().getName();
        if (fieldName.contains("currentpagenum")) {
            String entryNum = String.valueOf(fieldName.charAt(fieldName.length() - 1));
            int currentPage = (Integer)this.getModel().getValue(fieldName);
            if (currentPage < 1) {
                currentPage = 1;
                this.getModel().setValue(fieldName, 1);
            }

            String pageCountText = this.getView().getPageCache().get("pagecount" + entryNum);
            if (StringUtils.isNull(pageCountText)) {
                return;
            }

            int pageCountNum = Integer.parseInt(pageCountText);
            if (currentPage > pageCountNum) {
                currentPage = pageCountNum;
                this.getModel().setValue(fieldName, pageCountNum);
            }

            String currentPageText = this.getView().getPageCache().get("currentpage" + entryNum);
            if (StringUtils.isNull(currentPageText)) {
                return;
            }

            int currentPageNum = Integer.parseInt(currentPageText);
            if (currentPage == currentPageNum) {
                return;
            }

            String xbillEntryJson = this.getView().getPageCache().get("xbillEntryJson");
            if (CommonUtils.isNull(xbillEntryJson)) {
                xbillEntryJson = (String)this.getModel().getValue("crdentryjson_tag");
            }

            LinkedHashMap<String, Object> xbillEntryJsonMap = (LinkedHashMap)JSON.parseObject(xbillEntryJson, new TypeReference<LinkedHashMap<String, Object>>() {
            }, new Feature[0]);
            String treeentryEntityKey = "treeentryentity" + entryNum;
            EntryGrid entryGrid = (EntryGrid)this.getView().getControl(treeentryEntityKey);
            String entrykey = this.getView().getPageCache().get(treeentryEntityKey + "_entrykey");
            Object entry = xbillEntryJsonMap.get(entrykey);
            if (currentPage == 1) {
                this.getView().setEnable(true, new String[]{"lastpage" + entryNum, "nextpage" + entryNum});
                this.getView().setEnable(false, new String[]{"previouspage" + entryNum, "firstpage" + entryNum});
                this.batchSetEntry(entry, entryNum, currentPage);
                this.getView().getPageCache().put("currentpage" + entryNum, String.valueOf(currentPage));
            } else if (currentPage == pageCountNum) {
                this.getView().setEnable(true, new String[]{"previouspage" + entryNum, "firstpage" + entryNum});
                this.getView().setEnable(false, new String[]{"lastpage" + entryNum, "nextpage" + entryNum});
                this.batchSetEntry(entry, entryNum, currentPage);
                this.getView().getPageCache().put("currentpage" + entryNum, pageCountText);
            } else {
                this.getView().setEnable(true, new String[]{"previouspage" + entryNum, "firstpage" + entryNum});
                this.getView().setEnable(true, new String[]{"lastpage" + entryNum, "nextpage" + entryNum});
                this.batchSetEntry(entry, entryNum, currentPage);
                this.getView().getPageCache().put("currentpage" + entryNum, String.valueOf(currentPage));
            }

            if (entryGrid.isStopUpdateCell()) {
                entryGrid.setStopUpdateCell(false);
                entryGrid.bindData(new BindingContext(this.getModel().getEntryEntity(treeentryEntityKey)));
            }
        }

    }

    public void setXBillHeard(String xbillHeardJson) {
        LinkedHashMap<String, Object> billHeardChangeJson = (LinkedHashMap)JSON.parseObject(xbillHeardJson, new TypeReference<LinkedHashMap<String, Object>>() {
        }, new Feature[0]);
        if (billHeardChangeJson != null) {
            String enttryName = (String)billHeardChangeJson.get("n");
            if (!CommonUtils.isNull(enttryName)) {
                this.setEntryenName("entity", enttryName);
            }

            EntryGrid billHeardEntry = (EntryGrid)this.getView().getControl("billxinfo");
            billHeardEntry.setSplitPage(false);
            String lineChangeType = (String)billHeardChangeJson.get("f");
            if ("=".equals(lineChangeType)) {
                this.getView().setVisible(false, new String[]{"billxinfo"});
            }

            int upSum = 0;
            Iterator var7 = billHeardChangeJson.values().iterator();

            while(var7.hasNext()) {
                Object entry = var7.next();
                if (entry instanceof Map) {
                    Map<String, Object> billHeardFieldMap = (Map)entry;
                    if (!CommonUtils.isNull(billHeardFieldMap)) {
                        String fieldIsequal = (String)billHeardFieldMap.get("e");
                        if (!"=".equals(fieldIsequal)) {
                            int row = this.getModel().createNewEntryRow("billxinfo");
                            if (!CommonUtils.isNull(billHeardFieldMap.get("c"))) {
                                this.getModel().setValue("fieldname", getTransString((String)billHeardFieldMap.get("c")), row);
                            }

                            if (!CommonUtils.isNull(billHeardFieldMap.get("o"))) {
                                this.getModel().setValue("oldvalue", getTransString((String)billHeardFieldMap.get("o")), row);
                            }

                            if (!CommonUtils.isNull(billHeardFieldMap.get("n"))) {
                                this.getModel().setValue("newvalue", getTransString((String)billHeardFieldMap.get("n")), row);
                            }

                            if (row == 0) {
                                this.getModel().setValue("changetype", BizChangeTypeEnum.UPDATE.getName(), row);
                            }

                            if ("*".equals(fieldIsequal)) {
                                this.getModel().setValue("isrealchange", true, row);
                                ++upSum;
                            }

                            billHeardEntry.setRowBackcolor("#ecf6ff", new int[]{row});
                        }
                    }
                }
            }

            Label xsum = (Label)this.getView().getControl("xsum");
            xsum.setText(String.format(ResManager.loadKDString("共(%s)条", "ChangeResumeDetailPlugin_0", "yem-yc-changemodel", new Object[0]), upSum));
            Label xupdate = (Label)this.getView().getControl("xupdate");
            xupdate.setText(BizChangeTypeEnum.UPDATE.getName() + "(" + upSum + ")");
            if (upSum == 0) {
                this.getView().setVisible(false, new String[]{"entity"});
            }

        }
    }

    public void batchSetEntry(Object entry, String treeEntryNum, int currentPage) {
        if (!CommonUtils.isNull(entry) && !CommonUtils.isNull(treeEntryNum)) {
            String entryKeyName = "treeentryentity" + treeEntryNum;
            if (entry instanceof JSONArray) {
                IDataModel dataModel = this.getModel();
                int startIndex = this.rowCount * (currentPage - 1);
                if (startIndex == 0) {
                    startIndex = -1;
                }

                int endIndex = this.rowCount * currentPage;
                int sum = 0;
                JSONArray treeEntrys = (JSONArray)entry;
                TreeEntryGrid entryGrid = (TreeEntryGrid)this.getView().getControl(entryKeyName);
                this.getModel().deleteEntryData(entryKeyName);
                entryGrid.setStopUpdateCell(true);
                Iterator var11 = treeEntrys.iterator();

                while(true) {
                    while(true) {
                        label190:
                        while(true) {
                            Map lineEntryjsonmap;
                            String lineColor;
                            String lineChangeType;
                            String entryName;
                            do {
                                do {
                                    Object entryjson;
                                    do {
                                        if (!var11.hasNext()) {
                                            return;
                                        }

                                        entryjson = var11.next();
                                    } while(!(entryjson instanceof Map));

                                    lineEntryjsonmap = (Map)entryjson;
                                } while(CommonUtils.isNull(lineEntryjsonmap));

                                lineColor = "#ecf6ff";
                                lineChangeType = (String)lineEntryjsonmap.get("f");
                                entryName = getTransString((String)lineEntryjsonmap.get("n"));
                            } while(CommonUtils.isNull(lineChangeType));

                            switch (lineChangeType) {
                                case "+":
                                    lineColor = "#d5fbe2";
                                    break;
                                case "-":
                                    ++sum;
                                    lineColor = "#fef1d0";
                                    if (startIndex < sum && endIndex >= sum) {
                                        int row = dataModel.createNewEntryRow(entryKeyName);
                                        dataModel.setValue("seq_entry" + treeEntryNum, entryName + lineEntryjsonmap.get("l") + "_" + this.getChangeType(lineChangeType), row);
                                        entryGrid.setRowBackcolor(lineColor, new int[]{row});
                                        continue;
                                    }

                                    if (sum > endIndex) {
                                        return;
                                    }
                                    continue;
                                case "*":
                                    lineColor = "#ecf6ff";
                            }

                            int rowNum = -1;
                            if (!"=".equals(lineChangeType)) {
                                ++sum;
                                if (startIndex < sum && endIndex >= sum) {
                                    List<JSONArray> JSONArrayList = new ArrayList();
                                    List<Map<String, Object>> fields = new ArrayList();
                                    Iterator var20 = lineEntryjsonmap.values().iterator();

                                    label180:
                                    while(true) {
                                        Map fieldMap;
                                        String isequals;
                                        do {
                                            label172:
                                            do {
                                                while(var20.hasNext()) {
                                                    Object fieldjson = var20.next();
                                                    if (fieldjson instanceof Map) {
                                                        fieldMap = (Map)fieldjson;
                                                        continue label172;
                                                    }

                                                    if (fieldjson instanceof JSONArray) {
                                                        JSONArrayList.add((JSONArray)fieldjson);
                                                    }
                                                }

                                                int[] lines = dataModel.batchInsertEntryRow(entryKeyName, rowNum, fields.size());

                                                for(int lineNum = 0; lineNum < lines.length; ++lineNum) {
                                                    this.setEntryValue(treeEntryNum, (Map)fields.get(lineNum), lines[lineNum]);
                                                }

                                                entryGrid.setRowBackcolor(lineColor, lines);
                                                Iterator var32 = JSONArrayList.iterator();

                                                while(var32.hasNext()) {
                                                    JSONArray childTreeEntry = (JSONArray)var32.next();
                                                    int r = dataModel.insertEntryRow(entryKeyName, rowNum);
                                                    entryGrid.setRowBackcolor(lineColor, new int[]{r});
                                                    this.childTreeEntrys(childTreeEntry, r, treeEntryNum, lineEntryjsonmap, entryGrid);
                                                }
                                                break label180;
                                            } while(CommonUtils.isNull(fieldMap));

                                            isequals = (String)fieldMap.get("e");
                                        } while(!CommonUtils.isNull(isequals) && "=".equals(isequals));

                                        if (rowNum == -1) {
                                            rowNum = dataModel.createNewEntryRow(entryKeyName);
                                            dataModel.setValue("seq_entry" + treeEntryNum, entryName + lineEntryjsonmap.get("l") + "_" + this.getChangeType(lineChangeType), rowNum);
                                            entryGrid.setRowBackcolor(lineColor, new int[]{rowNum});
                                        }

                                        fields.add(fieldMap);
                                    }
                                } else if (sum > endIndex) {
                                    return;
                                }
                            }

                            if (startIndex < sum && endIndex >= sum) {
                                if ("=".equals(lineChangeType)) {
                                    Iterator var28 = lineEntryjsonmap.values().iterator();

                                    while(true) {
                                        while(true) {
                                            JSONArray childEntrys;
                                            String childChangeType;
                                            do {
                                                Object fieldjson;
                                                do {
                                                    if (!var28.hasNext()) {
                                                        continue label190;
                                                    }

                                                    fieldjson = var28.next();
                                                } while(!(fieldjson instanceof JSONArray));

                                                childEntrys = (JSONArray)fieldjson;
                                                childChangeType = "=";
                                                Iterator var35 = childEntrys.iterator();

                                                while(var35.hasNext()) {
                                                    Object childEntryjson = var35.next();
                                                    if (childEntryjson instanceof Map) {
                                                        Map<String, Object> childMaps = (Map)childEntryjson;
                                                        if (!CommonUtils.isNull(childMaps) && !"=".equals(childMaps.get("f"))) {
                                                            childChangeType = "*";
                                                            break;
                                                        }
                                                    }
                                                }
                                            } while(!"*".equals(childChangeType));

                                            ++sum;
                                            if (startIndex < sum && endIndex >= sum) {
                                                rowNum = dataModel.createNewEntryRow(entryKeyName);
                                                dataModel.setValue("seq_entry" + treeEntryNum, entryName + lineEntryjsonmap.get("l") + "_" + this.getChangeType(childChangeType), rowNum);
                                                int row = dataModel.insertEntryRow(entryKeyName, rowNum);
                                                entryGrid.setRowBackcolor("#ecf6ff", new int[]{rowNum, row});
                                                this.childTreeEntrys(childEntrys, row, treeEntryNum, lineEntryjsonmap, entryGrid);
                                            } else if (sum > endIndex) {
                                                return;
                                            }
                                        }
                                    }
                                }
                            } else if (endIndex < sum) {
                                return;
                            }
                        }
                    }
                }
            }
        }

    }

    public void childTreeEntrys(JSONArray childTreeEntry, int rowNum, String treeEntryNum, Map<String, Object> jsonmap, EntryGrid entryGrid) {
        IDataModel dataModel = this.getModel();
        String treeentryentity = "treeentryentity" + treeEntryNum;
        int sum = 0;
        Iterator var9 = childTreeEntry.iterator();

        int newSum;
        while(var9.hasNext()) {
            Object childEntryjson = var9.next();
            if (childEntryjson instanceof Map) {
                Map<String, Object> lineChildEntryMaps = (Map)childEntryjson;
                if (!CommonUtils.isNull(lineChildEntryMaps)) {
                    String lineChangeType = (String)lineChildEntryMaps.get("f");
                    if (!CommonUtils.isNull(lineChangeType)) {
                        switch (lineChangeType) {
                            case "+":
                            case "-":
                            case "*":
                                ++sum;
                        }
                    }
                }
            }
        }

        if (sum == 0) {
            dataModel.deleteEntryRow(treeentryentity, rowNum);
        } else {
            int[] lineNums = dataModel.batchInsertEntryRow(treeentryentity, rowNum, sum);
            int lineSign = 0;
            Iterator var33 = childTreeEntry.iterator();

            label227:
            while(true) {
                Map lineChildEntryMaps;
                int rows;
                label205:
                while(true) {
                    String lineChangeType;
                    do {
                        do {
                            Object childEntryjson;
                            do {
                                if (!var33.hasNext()) {
                                    DynamicObjectCollection dynamicObjects = this.getModel().getEntryEntity(treeentryentity);
                                    lineSign = 0;
                                    int deleteSum = 0;
                                    int updateSum = 0;
                                    newSum = 0;
                                    String childEntryName = null;
                                    Iterator var40 = childTreeEntry.iterator();

                                    while(true) {
                                        while(true) {
                                            do {
                                                do {
                                                    do {
                                                        if (!var40.hasNext()) {
                                                            if (deleteSum == 0 && updateSum == 0 && newSum == 0) {
                                                                dataModel.deleteEntryRow(treeentryentity, rowNum);
                                                            } else {
                                                                dataModel.setValue("seq_entry" + treeEntryNum, (String)jsonmap.get("n") + jsonmap.get("l") + "_" + childEntryName + "(" + BizChangeTypeEnum.CANCEL.getName() + deleteSum + "、" + BizChangeTypeEnum.UPDATE.getName() + updateSum + "、" + BizChangeTypeEnum.ADDNEW.getName() + newSum + ")", rowNum);
                                                            }

                                                            return;
                                                        }

                                                        childEntryjson = var40.next();
                                                    } while(!(childEntryjson instanceof Map));

                                                    lineChildEntryMaps = (Map)childEntryjson;
                                                } while(CommonUtils.isNull(lineChildEntryMaps));

                                                lineChangeType = (String)lineChildEntryMaps.get("f");
                                            } while(!CommonUtils.isNull(lineChangeType) && "=".equals(lineChangeType));

                                            if (lineNums.length <= lineSign) {
                                                return;
                                            }

                                            rows = lineNums[lineSign];
                                            ++lineSign;
                                            childEntryName = getTransString((String)lineChildEntryMaps.get("n"));
                                            DynamicObject rowsEntryObj = (DynamicObject)dynamicObjects.get(rows);
                                            rowsEntryObj.set("seq_entry" + treeEntryNum, childEntryName + lineChildEntryMaps.get("l") + "_" + this.getChangeType(lineChangeType));
                                            String childLineColor = null;
                                            if (!CommonUtils.isNull(lineChangeType)) {
                                                switch (lineChangeType) {
                                                    case "+":
                                                        childLineColor = "#d5fbe2";
                                                        ++newSum;
                                                        break;
                                                    case "-":
                                                        ++deleteSum;
                                                        childLineColor = "#fef1d0";
                                                        entryGrid.setRowBackcolor(childLineColor, new int[]{rows});
                                                        continue;
                                                    case "*":
                                                        childLineColor = "#ecf6ff";
                                                        ++updateSum;
                                                }
                                            }

                                            entryGrid.setRowBackcolor(childLineColor, new int[]{rows});
                                            List<Map<String, Object>> fields = new ArrayList();
                                            Iterator var48 = lineChildEntryMaps.values().iterator();

                                            while(var48.hasNext()) {
                                                Object entry = var48.next();
                                                if (entry instanceof Map) {
                                                    Map<String, Object> childFieldMap = (Map)entry;
                                                    if (!CommonUtils.isNull(childFieldMap)) {
                                                        String childFieldIsequals = (String)childFieldMap.get("e");
                                                        if (!CommonUtils.isNull(childFieldIsequals) && !"=".equals(childFieldIsequals)) {
                                                            fields.add(childFieldMap);
                                                        }
                                                    }
                                                }
                                            }

                                            int lineNum = 0;
                                            int[] RowBackcolors = new int[fields.size()];

                                            for(int x = 0; x < dynamicObjects.size(); ++x) {
                                                DynamicObject childRowsEntry = (DynamicObject)dynamicObjects.get(x);
                                                if ((Long)rowsEntryObj.getPkValue() == childRowsEntry.getLong("pid")) {
                                                    RowBackcolors[lineNum] = x;
                                                    String fieldName = (String)((Map)fields.get(lineNum)).get("c");
                                                    String oldValue = (String)((Map)fields.get(lineNum)).get("o");
                                                    String newValue = (String)((Map)fields.get(lineNum)).get("n");
                                                    childRowsEntry.set("fieldname_entry" + treeEntryNum, fieldName);
                                                    childRowsEntry.set("oldvalue_entry" + treeEntryNum, oldValue);
                                                    childRowsEntry.set("newvalue_entry" + treeEntryNum, newValue);
                                                    ++lineNum;
                                                }
                                            }

                                            entryGrid.setRowBackcolor(childLineColor, RowBackcolors);
                                        }
                                    }
                                }

                                childEntryjson = var33.next();
                            } while(!(childEntryjson instanceof Map));

                            lineChildEntryMaps = (Map)childEntryjson;
                        } while(CommonUtils.isNull(lineChildEntryMaps));

                        lineChangeType = (String)lineChildEntryMaps.get("f");
                    } while(!CommonUtils.isNull(lineChangeType) && "=".equals(lineChangeType));

                    rows = lineNums[lineSign];
                    ++lineSign;
                    if (CommonUtils.isNull(lineChangeType)) {
                        break;
                    }

                    switch (lineChangeType) {
                        case "-":
                        case "=":
                            break;
                        default:
                            break label205;
                    }
                }

                List<Map<String, Object>> fields = new ArrayList();
                Iterator var42 = lineChildEntryMaps.values().iterator();

                while(true) {
                    Map childFieldMap;
                    String childFieldIsequals;
                    do {
                        do {
                            Object entry;
                            do {
                                if (!var42.hasNext()) {
                                    int[] childLineNums = dataModel.batchInsertEntryRow(treeentryentity, rows, fields.size());
                                    if (lineNums.length > lineSign && childLineNums.length > 0) {
                                        lineNums[lineSign] = lineNums[lineSign - 1] + childLineNums.length + 1;
                                    }
                                    continue label227;
                                }

                                entry = var42.next();
                            } while(!(entry instanceof Map));

                            childFieldMap = (Map)entry;
                        } while(CommonUtils.isNull(childFieldMap));

                        childFieldIsequals = (String)childFieldMap.get("e");
                    } while(!CommonUtils.isNull(childFieldIsequals) && "=".equals(childFieldIsequals));

                    fields.add(childFieldMap);
                }
            }
        }
    }

    public void setEntryenName(String entryen, String name) {
        Map<String, Object> map1 = new HashMap();
        map1.put("text", new LocaleString(name));
        this.getView().updateControlMetadata(entryen, map1);
    }

    public void setEntryValue(String treeEntryNum, Map<String, Object> entryMap, int rows) {
        if (!CommonUtils.isNull(entryMap) && !CommonUtils.isNull(rows)) {
            String fieldName = (String)entryMap.get("c");
            String oldValue = (String)entryMap.get("o");
            String newValue = (String)entryMap.get("n");
            if (!CommonUtils.isNull(fieldName)) {
                this.getModel().setValue("fieldname_entry" + treeEntryNum, getTransString(fieldName), rows);
            }
            if (!CommonUtils.isNull(oldValue)) {
                this.getModel().setValue("oldvalue_entry" + treeEntryNum, getTransString(oldValue), rows);
            }

            if (!CommonUtils.isNull(newValue)) {
                this.getModel().setValue("newvalue_entry" + treeEntryNum, getTransString(newValue), rows);
            }

        }
    }

    public String getChangeType(String f) {
        if (CommonUtils.isNull(f)) {
            return "";
        } else if ("+".equals(f)) {
            return BizChangeTypeEnum.ADDNEW.getName();
        } else if ("-".equals(f)) {
            return BizChangeTypeEnum.CANCEL.getName();
        } else {
            return "*".equals(f) ? BizChangeTypeEnum.UPDATE.getName() : f;
        }
    }

    public void setCountLabel(int sum, int upSum, int newSum, int delSum, int treeEntryNum) {
        Label xsum = (Label)this.getView().getControl("sum" + treeEntryNum);
        xsum.setText(String.format(ResManager.loadKDString("共(%s)条", "ChangeResumeDetailPlugin_0", "yem-yc-changemodel", new Object[0]), sum));
        Label updatesum = (Label)this.getView().getControl("update" + treeEntryNum);
        updatesum.setText(BizChangeTypeEnum.UPDATE.getName() + "(" + upSum + ")");
        Label newsum = (Label)this.getView().getControl("new" + treeEntryNum);
        newsum.setText(BizChangeTypeEnum.ADDNEW.getName() + "(" + newSum + ")");
        Label deletesum = (Label)this.getView().getControl("delete" + treeEntryNum);
        deletesum.setText(BizChangeTypeEnum.CANCEL.getName() + "(" + delSum + ")");
    }

    public static String getTransString(String str) {
        if (StringUtils.isNotEmpty(str)) {
            if (str.contains("&#123;")) {
                str = str.replaceAll("&#123;", "\\\\");
            }

            if (str.contains("&#124;")) {
                str = str.replaceAll("&#124;", "\"");
            }
        } else {
            str = "";
        }

        return str;
    }

    public void isNeedPaged(int sum, String treeEntryNum) {
        if (sum > this.rowCount) {
            int pageCount = sum / this.rowCount + (sum % this.rowCount > 0 ? 1 : 0);
            Label pageCountLabel = (Label)this.getView().getControl("pagecount" + treeEntryNum);
            pageCountLabel.setText(String.format(ResManager.loadKDString("共(%s)页", "ChangeResumeDetailPlugin_1", "yem-yc-changemodel", new Object[0]), pageCount));
            this.getView().getPageCache().put("pagecount" + treeEntryNum, String.valueOf(pageCount));
            Label nextPageLabel = (Label)this.getView().getControl("nextpage" + treeEntryNum);
            nextPageLabel.setText(">");
            Label lastPageLabel = (Label)this.getView().getControl("lastpage" + treeEntryNum);
            lastPageLabel.setText(">|");
            Label previousPageLabel = (Label)this.getView().getControl("previouspage" + treeEntryNum);
            previousPageLabel.setText("<");
            Label firstPageLabel = (Label)this.getView().getControl("firstpage" + treeEntryNum);
            firstPageLabel.setText("|<");
            this.getModel().setValue("currentpagenum" + treeEntryNum, 1);
            this.getView().getPageCache().put("currentpage" + treeEntryNum, String.valueOf(1));
            this.getView().setEnable(false, new String[]{"previouspage" + treeEntryNum, "firstpage" + treeEntryNum});
            this.getView().setEnable(false, new String[]{"treeentryentity" + treeEntryNum});
        } else {
            this.getView().setVisible(false, new String[]{"pageflexpanelap" + treeEntryNum});
            this.getView().setEnable(false, new String[]{"treeentryentity" + treeEntryNum});
        }

    }
}
