package com.blueland.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blueland.model.TPfAuth;
import com.blueland.service.IAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueland.bo.AuthEntity;
import com.blueland.dao.AuthDao;

@Service
public class AuthServiceImpl extends ServiceImpl<AuthDao, TPfAuth> implements IAuthService {
	@Autowired
	//Auth对应的DAO类,主要用于数据库的增删改查等操作
	private AuthDao authDao;

	@Override
	public int findAuthByMenuIdRoleId(String menuId, String roleId) {
		int result = authDao.findAuthByMenuIdRoleId(menuId, roleId);
		return result;
	}

	@Override
	public void deleteRole(String roleId,String appId) {
		authDao.deleteRole(roleId,appId);
	}
	
	/**
	 * 根据角色ID获取集合
	 * @param roleId
	 * @return
	 */
	public List<AuthEntity> getListByRoleId(String roleId){
		return authDao.getListByRoleId(roleId);
	}
	
	/**
	 * 根据角色ID获取MAP
	 * key---menuId
	 * @param roleId
	 * @return
	 */
	@Override
	public Map<String,AuthEntity> getMapByRoleId(String roleId){
		Map<String,AuthEntity> authMap = new HashMap<String,AuthEntity>();
		List<AuthEntity> authList = authDao.getListByRoleId(roleId);
		if(null != authList && authList.size() > 0) {
			for(AuthEntity auth:authList) {
				authMap.put(auth.getMenuId(), auth);
			}
		}
		return authMap;
	}

	@Override
	public TPfAuth get(AuthEntity  bo){
		return this.baseMapper.selectOne(buildQueryWrapper(bo));
	}

	@Override
	public Page<AuthEntity> findPage(AuthEntity  bo) {
		Page<AuthEntity> page = new Page(bo.getPageNum(), bo.getPageSize());
		return this.baseMapper.findPage(page, bo);
	}

    @Override
    public void deleteAuthByAppId(String appId) {
        this.baseMapper.deleteAuthByAppId(appId);
    }

	@Override
	public List<AuthEntity> findListByAppId(String appId) {
		return this.baseMapper.findListByAppId(appId);
	}

    private LambdaQueryWrapper<TPfAuth> buildQueryWrapper(AuthEntity bo) {
		LambdaQueryWrapper<TPfAuth> lqw = Wrappers.lambdaQuery();
		lqw.eq(ObjectUtil.isNotEmpty(bo.getAuthId()), TPfAuth::getAuthId, bo.getAuthId());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getAuthType()), TPfAuth::getAuthType, bo.getAuthType());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getAppId()), TPfAuth::getAppId, bo.getAppId());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getMenuId()), TPfAuth::getMenuId, bo.getMenuId());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getStatus()), TPfAuth::getStatus, bo.getStatus());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getCreatorId()), TPfAuth::getCreatorId, bo.getCreatorId());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getUpdatorId()), TPfAuth::getUpdatorId, bo.getUpdatorId());
		lqw.eq(ObjectUtil.isNotEmpty(bo.getRoleId()), TPfAuth::getRoleId, bo.getRoleId());
		return lqw;
	}

}
