package cc.smtweb.system.bpm.web.sys.user.dataRightGroup.impl;

import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.system.bpm.web.common.BpmConst;
import cc.smtweb.system.bpm.web.sys.user.dataRightGroup.IDataRightTreeWorker;
import cc.smtweb.system.bpm.web.sys.user.party.Party;
import cc.smtweb.system.bpm.web.sys.user.party.PartyCache;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author yaoq
 * @Date 2022年09月22日 15:48
 * @Description
 */
public class PartyDataRightTreeWork implements IDataRightTreeWorker {

    @Override
    public SwMap buildExtraValue(long key, String value) {
        SwMap swMap = new SwMap();
        swMap.put("id", key);
        swMap.put("text", value);
        return swMap;
    }

    @Override
    public List<SwMap> getExtraValues() {
        List<SwMap> list = new ArrayList<>();
        list.add(buildExtraValue(BpmConst.DataRight.CUR_VALUE, "所属机构"));
        return list;
    }

    @Override
    public List<SwMap> getChildren(long id) {
        List<SwMap> list = new ArrayList<>();
        long key = id;
        if (key <= 0) {
            key = -1;
            list.addAll(getExtraValues());
        }
        Set<Party> partys = PartyCache.getInstance().getChildren(key);
        if (CommUtil.isEmpty(partys)) return list;
        partys.forEach(party -> {
            list.add(buildExtraValue(party.getId(), party.getName()));
        });
        return list;
    }

    @Override
    public String getText(long value) {
        if (BpmConst.DataRight.CUR_VALUE == value) return "所属机构";
        return PartyCache.getInstance().getNameById(value);
    }
}
