 package org.sky.system.wrapper;

 import org.sky.core.mp.support.BaseEntityWrapper;
 import org.sky.core.tool.constant.KdConstant;
 import org.sky.core.tool.node.ForestNodeMerger;
 import org.sky.core.tool.utils.BeanUtil;
 import org.sky.core.tool.utils.Func;
 import org.sky.system.cache.DictBizCache;
 import org.sky.system.pojo.entity.DictBiz;
 import org.sky.system.pojo.vo.DictBizVO;

 import java.util.List;
 import java.util.Objects;
 import java.util.stream.Collectors;


/**
 * 包装类,返回视图层所需的字段
 *
  */
public class DictBizWrapper extends BaseEntityWrapper<DictBiz, DictBizVO> {

	public static DictBizWrapper build() {
		return new DictBizWrapper();
	}

	@Override
	public DictBizVO entityVO(DictBiz dict) {
		DictBizVO dictVO = Objects.requireNonNull(BeanUtil.copyProperties(dict, DictBizVO.class));
		if (Func.equals(dict.getParentId(), KdConstant.TOP_PARENT_ID)) {
			dictVO.setParentName(KdConstant.TOP_PARENT_NAME);
		} else {
			DictBiz parent = DictBizCache.getById(dict.getParentId());
			dictVO.setParentName(parent.getDictValue());
		}
		return dictVO;
	}

	public List<DictBizVO> listNodeVO(List<DictBiz> list) {
		List<DictBizVO> collect = list.stream().map(dict -> BeanUtil.copyProperties(dict, DictBizVO.class)).collect(Collectors.toList());
		return ForestNodeMerger.merge(collect);
	}

}
