package nckd.jxccl.hrmp.hbss.plugin.form.bd;

import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hbp.common.util.HRDynamicObjectUtils;
import kd.hr.hbp.common.util.HRObjectUtils;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.base.common.utils.PatternUtil;
import nckd.jxccl.hrmp.hbss.common.bd.LawEntityConstant;

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

/**
 * 法律实体变更表单插件
 * @entity: hbss_lawentitychg
 * @author: jtd
 * @date: 2026/1/8 19:50
 */
public class LawEntityChgFormPlugin extends AbstractFormPlugin {
    Log log = LogFactory.getLog(LawEntityChgFormPlugin.class);

    private static final HRBaseServiceHelper helper = HRBaseServiceHelper.create(LawEntityConstant.HBSS_LAWENTITYCHG_ENTITY);

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);

        String showChgFlag = getView().getFormShowParameter().getCustomParam(LawEntityConstant.SHOWCHGFLAG_CUSTOM_PARAM);
        String chgProp = e.getProperty().getName();
        if (StringUtils.isNotEmpty(showChgFlag) && showChgFlag.equals("yes") && PatternUtil.isExProperty(chgProp)) {
            String mulType = (String) getModel().getValue(LawEntityConstant.TYPE_KEY);
            Object oldValue = helper.loadOne(chgProp, getModel().getValue(LawEntityConstant.ID_KEY)).get(chgProp);
            Object newValue = getModel().getValue(chgProp);

            boolean isChange;
            if (HRObjectUtils.isEmpty(oldValue) && HRObjectUtils.isEmpty(newValue)) {
                isChange = false;
            } else if ((HRObjectUtils.isEmpty(oldValue) && !HRObjectUtils.isEmpty(newValue)) || (!HRObjectUtils.isEmpty(oldValue) && HRObjectUtils.isEmpty(newValue))) {
                isChange = true;
            } else {
                isChange = HRDynamicObjectUtils.compareValues(oldValue, newValue);
            }
            getPageCache().put(chgProp, Boolean.toString(isChange));

            // 获取所有二开字段的变更属性
            List<Map.Entry<String, String>> dataEntityProperties = getPageCache().getAll().entrySet().stream().filter(entry -> PatternUtil.isExProperty(entry.getKey())).filter(entry -> HRStringUtils.equals(entry.getValue(), "true")).collect(Collectors.toList());
            if (dataEntityProperties.isEmpty() && HRStringUtils.containsIgnoreCase(mulType, "7")) {
                mulType = buildMulType(null, null, mulType, "7");
            } else {
                mulType = buildMulType(null, 1, mulType, "7");
            }

            getModel().setValue(LawEntityConstant.TYPE_KEY, mulType);
        }
    }

    // from: kd.hr.hbss.formplugin.web.lawentity.LawEntityEditPlugin.buildMulType
    private String buildMulType(Object oldValue, Object newValue, String mulType, String type) {
        if (!ObjectUtils.nullSafeEquals(oldValue, newValue)) {
            if (null == mulType) {
                mulType = "," + type + ",";
            } else if (!mulType.contains(type)) {
                mulType = mulType + type + ",";
            }
        } else if (null != mulType && mulType.contains(type)) {
            mulType = mulType.replaceAll(type + ",", "");
        }

        if (null != mulType && mulType.length() == 1) {
            mulType = null;
        }

        if (null != mulType) {
            mulType = mulSelectSort(mulType);
        }

        return mulType;
    }

    // from: kd.hr.hbss.formplugin.web.lawentity.LawEntityEditPlugin.mulSelectSort
    private String mulSelectSort(String mulSelect) {
        if (StringUtils.isEmpty(mulSelect)) {
            return mulSelect;
        } else {
            String[] mulTypeArr = mulSelect.split(",");
            String mulSelectSort = "";
            ArrayList mulTypeList = new ArrayList();

            for(int i = 0; i < mulTypeArr.length; ++i) {
                if (!mulTypeArr[i].equals("")) {
                    mulTypeList.add(mulTypeArr[i]);
                }
            }

            Collections.sort(mulTypeList);

            for(int i = 0; i < mulTypeList.size(); ++i) {
                if ("".equals(mulSelectSort)) {
                    mulSelectSort = "," + mulTypeList.get(i) + ",";
                } else {
                    mulSelectSort = mulSelectSort + mulTypeList.get(i) + ",";
                }
            }

            return mulSelectSort;
        }
    }
}
