package com.hotent.orgext.manager.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.query.PageBean;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.orgext.dao.OrgAuthExtDao;
import com.hotent.orgext.manager.OrgAuthExtManager;
import com.hotent.orgext.model.OrgAuthExt;
import com.hotent.orgext.vo.AppModelVo;
import com.hotent.orgext.vo.RoleVo;
import com.hotent.orgext.vo.SysAppVo;
import com.hotent.uc.model.Role;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 分级组织和角色/菜单/应用授权表
 * @author: 梁宏锦
 * @date: 2024/07/13
 **/
@Service
public class OrgAuthExtManagerImpl extends BaseManagerImpl<OrgAuthExtDao, OrgAuthExt> implements OrgAuthExtManager {
	private final Logger logger= LoggerFactory.getLogger(this.getClass());

	@Override
	public void removeByOrgIdAndType(String orgId, String authType) {
		baseMapper.removeByOrgIdAndType(orgId, authType);
	}

	@Override
	public void removeByAppIdAndType(String appId, String authType) {
		baseMapper.removeByAppIdAndType(appId, authType);
	}

	@Override
	public void removeByOrgIdAndRoleId(String orgId, String roleId) {
		baseMapper.removeByOrgIdAndRoleId(orgId, roleId);
	}

	@Override
	public PageList<RoleVo> queryRoleAuthExt(QueryFilter queryFilter) {
		PageBean pageBean = queryFilter.getPageBean();
		IPage<RoleVo> result = baseMapper.queryRoleAuthExt(convert2IPageNew(pageBean), convert2Wrapper(queryFilter, currentModelClass(RoleVo.class.getClass())));
		return new PageList<RoleVo>(result);
	}

	@Override
	public PageList<SysAppVo> getSysAppAuthExtPage(QueryFilter queryFilter) {
		PageBean pageBean = queryFilter.getPageBean();
		IPage<SysAppVo> result = baseMapper.getSysAppAuthExtPage(convert2IPageNew(pageBean), convert2Wrapper(queryFilter, currentModelClass(SysAppVo.class.getClass())));
		return new PageList<SysAppVo>(result);
	}

	@Override
	public PageList<SysAppVo> querySysAppExtListFirst(QueryFilter queryFilter, String extOrgId) {
		PageBean pageBean = queryFilter.getPageBean();
		IPage<SysAppVo> result = baseMapper.querySysAppExtListFirst(convert2IPageNew(pageBean), convert2Wrapper(queryFilter, currentModelClass(SysAppVo.class.getClass())), extOrgId);
		return new PageList<SysAppVo>(result);
	}

	@Override
	public PageList<SysAppVo> querySysAppExtListOther(QueryFilter queryFilter, String extParentOrgId, String extOrgId) {
		PageBean pageBean = queryFilter.getPageBean();
		IPage<SysAppVo> result = baseMapper.querySysAppExtListOther(convert2IPageNew(pageBean), convert2Wrapper(queryFilter, currentModelClass(SysAppVo.class.getClass())),
				extParentOrgId, extOrgId);
		return new PageList<SysAppVo>(result);
	}

	@Override
	public PageList<AppModelVo> getMobileAppAuthExtPage(QueryFilter queryFilter) {
		PageBean pageBean = queryFilter.getPageBean();
		IPage<AppModelVo> result = baseMapper.getMobileAppAuthExtPage(convert2IPageNew(pageBean), convert2Wrapper(queryFilter, currentModelClass(AppModelVo.class.getClass())));
		return new PageList<AppModelVo>(result);
	}

	@Override
	public PageList<AppModelVo> queryMobileAppExtListFirst(QueryFilter queryFilter, String extOrgId) {
		PageBean pageBean = queryFilter.getPageBean();
		IPage<AppModelVo> result = baseMapper.queryMobileAppExtListFirst(convert2IPageNew(pageBean), convert2Wrapper(queryFilter, currentModelClass(AppModelVo.class.getClass())), extOrgId);
		return new PageList<AppModelVo>(result);
	}

	@Override
	public PageList<AppModelVo> queryMobileAppExtListOther(QueryFilter queryFilter, String extParentOrgId, String extOrgId) {
		PageBean pageBean = queryFilter.getPageBean();
		IPage<AppModelVo> result = baseMapper.queryMobileAppExtListOther(convert2IPageNew(pageBean), convert2Wrapper(queryFilter, currentModelClass(AppModelVo.class.getClass())),
				extParentOrgId, extOrgId);
		return new PageList<AppModelVo>(result);
	}

	public IPage convert2IPageNew(PageBean pageBean) {
		return pageBean == null ? new Page<>()
				: new Page(pageBean.getPage(), pageBean.getPageSize(), pageBean.showTotal());
	}

	public Class currentModelClass(Class<?> cls) {
		return ReflectionKit.getSuperClassGenericType(cls, 1);
	}
}
