package com.ym.mec.biz.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.MemberPrivilegesItemDao;
import com.ym.mec.biz.dal.dao.MemberRankPrivilegesDao;
import com.ym.mec.biz.dal.entity.MemberPrivilegesItem;
import com.ym.mec.biz.dal.entity.MemberRankPrivileges;
import com.ym.mec.biz.service.MemberRankPrivilegesService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class MemberRankPrivilegesServiceImpl extends BaseServiceImpl<Integer, MemberRankPrivileges> implements MemberRankPrivilegesService {

	@Autowired
	private MemberRankPrivilegesDao memberRankPrivilegesDao;

	@Autowired
	private MemberPrivilegesItemDao memberPrivilegesItemDao;

	@Override
	public BaseDAO<Integer, MemberRankPrivileges> getDAO() {
		return memberRankPrivilegesDao;
	}

	@Override
	public List<MemberPrivilegesItem> queryByMemberRankId(Integer memberRankId) {
		List<Integer> memberPrivilegesIdList = memberRankPrivilegesDao.findByRankSettingId(memberRankId).stream().map(e -> e.getMemberPrivilegesId())
				.collect(Collectors.toList());
		
		if (memberPrivilegesIdList == null || memberPrivilegesIdList.size() == 0) {
			return new ArrayList<MemberPrivilegesItem>();
		}

		List<MemberPrivilegesItem> memberPrivilegesItemList = memberPrivilegesItemDao.findById(memberPrivilegesIdList);

		Map<Integer, MemberPrivilegesItem> map = new HashMap<Integer, MemberPrivilegesItem>();

		for (MemberPrivilegesItem item : memberPrivilegesItemList) {
			
			if(!map.containsKey(item.getId())){
				MemberPrivilegesItem memberPrivilegesItem = queryParentBysubId(item);
				
				if (map.containsKey(memberPrivilegesItem.getId())) {

					memberPrivilegesItem = addSubItemToParent(map.get(memberPrivilegesItem.getId()), item);
				}
				map.put(memberPrivilegesItem.getId(), memberPrivilegesItem);
			}
		}

		return map.values().stream().collect(Collectors.toList());
	}

	private MemberPrivilegesItem queryParentBysubId(MemberPrivilegesItem memberPrivilegesItem) {

		if (memberPrivilegesItem != null && (memberPrivilegesItem.getParentId() != 0 && memberPrivilegesItem.getParentId() != null)) {
			Integer parentId = memberPrivilegesItem.getParentId();
			MemberPrivilegesItem parentItem = memberPrivilegesItemDao.get(parentId);
			if (parentItem != null) {
				parentItem.getMemberPrivilegesItems().add(memberPrivilegesItem);
				return queryParentBysubId(parentItem);
			}
		}

		return memberPrivilegesItem;
	}

	private MemberPrivilegesItem addSubItemToParent(MemberPrivilegesItem parentItem, MemberPrivilegesItem subItem) {
		if (subItem.getParentId().equals(parentItem.getId())) {
			parentItem.getMemberPrivilegesItems().add(subItem);
		} else {
			for (MemberPrivilegesItem item : parentItem.getMemberPrivilegesItems()) {
				addSubItemToParent(item, subItem);
			}
		}

		return parentItem;
	}

}