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

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.page.MemberPrivilegesItemQueryInfo;
import com.ym.mec.biz.service.MemberPrivilegesItemService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class MemberPrivilegesItemServiceImpl extends BaseServiceImpl<Integer, MemberPrivilegesItem>  implements MemberPrivilegesItemService {
	
	@Autowired
	private MemberPrivilegesItemDao memberPrivilegesItemDao;
	@Autowired
	private MemberRankPrivilegesDao memberRankPrivilegesDao;

	@Override
	public BaseDAO<Integer, MemberPrivilegesItem> getDAO() {
		return memberPrivilegesItemDao;
	}

	private MemberPrivilegesItem getTree(MemberPrivilegesItem item){
		List<MemberPrivilegesItem> privilegesItems = memberPrivilegesItemDao.findList(item.getId());
		if(privilegesItems != null && privilegesItems.size() > 0) {
			item.setMemberPrivilegesItems(privilegesItems);
			for (MemberPrivilegesItem privilegesItem : privilegesItems) {
				getTree(privilegesItem);
			}
		}
		return item;
	}

    @Override
	@Transactional(rollbackFor = Exception.class)
    public void recursiveDel(Integer memberPrivilegesItemId) {
//		MemberPrivilegesItem privilegesItem = memberPrivilegesItemDao.get(memberPrivilegesItemId);
//		Integer parentId = privilegesItem.getParentId();
		//当前功能原有父级是否被会员使用
//		if(memberRankPrivilegesDao.hasPrivileges(parentId)){
//			throw new BizException("修改失败：该功能已被使用");
//		}
		if(memberRankPrivilegesDao.hasPrivileges(memberPrivilegesItemId)){
			throw new BizException("删除失败：该功能已被使用");
		}
		List<MemberPrivilegesItem> memberPrivilegesItems = memberPrivilegesItemDao.findList(memberPrivilegesItemId);
		for (MemberPrivilegesItem item : memberPrivilegesItems) {
			item = getTree(item);
		}
		List<Integer> collect = memberPrivilegesItems.stream().map(e -> e.getId()).collect(Collectors.toList());
		if(collect == null || collect.size() <= 0){
			memberPrivilegesItemDao.delete(memberPrivilegesItemId);
			return;
		}
		collect.add(memberPrivilegesItemId);
		memberPrivilegesItemDao.batchDel(collect);
    }

	@Override
	public PageInfo<MemberPrivilegesItem> queryItemPage(MemberPrivilegesItemQueryInfo queryInfo) {
		PageInfo<MemberPrivilegesItem> memberPrivilegesItemPageInfo = this.queryPage(queryInfo);
		List<MemberPrivilegesItem> rows = memberPrivilegesItemPageInfo.getRows();
		if(rows == null || rows.size() == 0){
			return memberPrivilegesItemPageInfo;
		}
		for (MemberPrivilegesItem item : rows) {
			item = getTree(item);
		}
		return memberPrivilegesItemPageInfo;
	}

	@Override
	public List<MemberPrivilegesItem> findAllItem(MemberPrivilegesItemQueryInfo queryInfo) {
		List<MemberPrivilegesItem> rows = memberPrivilegesItemDao.findList(queryInfo.getParentId());
		if(rows == null || rows.size() == 0){
			return rows;
		}
		for (MemberPrivilegesItem item : rows) {
			item = getTree(item);
		}
		return rows;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateItem(MemberPrivilegesItem memberPrivilegesItem) {
		List<MemberPrivilegesItem> itemList = memberPrivilegesItemDao.findList(memberPrivilegesItem.getParentId());
		if(itemList.size() == 0){
			if(memberRankPrivilegesDao.hasPrivileges(memberPrivilegesItem.getParentId())){
				throw new BizException("修改失败：被关联的子集不能被添加子功能");
			}
		}
//		MemberPrivilegesItem privilegesItem = memberPrivilegesItemDao.get(memberPrivilegesItem.getId());
//		Integer parentId = memberPrivilegesItem.getParentId();
//		if(privilegesItem.getParentId() != parentId){
			//当前功能原有父级是否被会员使用
//			if(memberRankPrivilegesDao.hasPrivileges(parentId)){
//				throw new BizException("修改失败：该功能已被使用");
//			}
//		}
		if(memberRankPrivilegesDao.hasPrivileges(memberPrivilegesItem.getId())){
			throw new BizException("修改失败：被关联的功能不能修改");
		}
		memberPrivilegesItemDao.update(memberPrivilegesItem);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(MemberPrivilegesItem memberPrivilegesItem) {
		List<MemberPrivilegesItem> itemList = memberPrivilegesItemDao.findList(memberPrivilegesItem.getParentId());
		if(itemList.size() == 0){
			if(memberRankPrivilegesDao.hasPrivileges(memberPrivilegesItem.getParentId())){
				throw new BizException("修改失败：被关联的子集不能被添加子功能");
			}
		}
		memberPrivilegesItemDao.insert(memberPrivilegesItem);
	}
}