package com.jy.system.dao;

import com.jy.framework.model.base.BaseDao;
import com.jy.framework.model.base.BaseIdPo;
import com.jy.framework.model.constants.CommonConstant;
import com.jy.system.mapper.SysMenuMapper;
import com.jy.system.model.dto.SysMenuSelectDto;
import com.jy.system.model.entity.SysMenu;
import com.jy.system.model.table.SysMenuTable;
import com.jy.system.model.table.SysRoleMenuTable;
import com.jy.system.model.vo.SysMenuVo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class SysMenuDao extends BaseDao<SysMenuMapper, SysMenu> {

    /**
     * 菜单列表
     */
    public List<SysMenuVo> getList(SysMenuSelectDto dto) {
        SysMenuTable sm = SysMenuTable.sm;

        return sql(SysMenuVo.class)
                .select(
                        sm.id,
                        sm.parentId,
                        sm.type,
                        sm.title,
                        sm.name,
                        sm.path,
                        sm.status,
                        sm.sort
                )
                .from(sm)
                .where(
                        sm.type.in(dto.getTypes()),
                        sm.title.like(dto.getTitle()),
                        sm.name.like(dto.getName()),
                        sm.status.eq(dto.getStatus())
                )
                .orderBy(
                        sm.sort.asc(),
                        sm.id.asc()
                )
                .list();
    }

    /**
     * 菜单明细
     */
    public SysMenuVo getDetail(Long id) {
        SysMenuTable sm = SysMenuTable.sm;

        return sql(SysMenuVo.class)
                .select(
                        sm.id,
                        sm.parentId,
                        sm.type,
                        sm.title,
                        sm.icon,
                        sm.name,
                        sm.path,
                        sm.status,
                        sm.sort,
                        sm.isHide,
                        sm.isKeepAlive,
                        sm.isLink,
                        sm.isFull,
                        sm.perms
                )
                .from(sm)
                .where(sm.id.eq(id))
                .one();
    }

    /**
     * 同级菜单名称是否存在
     */
    public boolean peerTitleExists(Long parentId, String title) {
        return lambdaQuery().eq(SysMenu::getParentId, parentId).eq(SysMenu::getTitle, title).exists();
    }

    /**
     * 同级菜单名称是否存在（排除id）
     */
    public boolean peerTitleExists(Long parentId, String title, Long exclusionId) {
        return lambdaQuery().eq(SysMenu::getParentId, parentId).eq(SysMenu::getTitle, title)
                .ne(BaseIdPo::getId, exclusionId).exists();
    }

    /**
     * 子目录是否存在
     */
    public boolean childrenExists(Long id) {
        return lambdaQuery().eq(SysMenu::getParentId, id).exists();
    }

    /**
     * 删除菜单下所有子目录
     */
    public void removeByParentId(Long id) {
        lambdaUpdate().eq(SysMenu::getParentId, id).remove();
    }

    /**
     * 获取启用菜单
     */
    public List<SysMenuVo> getEnableList() {
        SysMenuTable sm = SysMenuTable.sm;

        return sql(SysMenuVo.class)
                .select(
                        sm.id,
                        sm.parentId,
                        sm.type,
                        sm.title,
                        sm.icon,
                        sm.name,
                        sm.path,
                        sm.isHide,
                        sm.isKeepAlive,
                        sm.isLink,
                        sm.isFull,
                        sm.perms
                )
                .from(sm)
                .where(
                        sm.status.eq(CommonConstant.ENABLE)
                )
                .orderBy(sm.sort.asc())
                .list();
    }

    /**
     * 获取启用菜单
     */
    public List<SysMenuVo> getEnableListByRoleIds(Collection<Long> sysRoleIds) {
        SysMenuTable sm = SysMenuTable.sm;
        SysRoleMenuTable srm = SysRoleMenuTable.srm;

        return sql(SysMenuVo.class)
                .distinct()
                .select(
                        sm.id,
                        sm.parentId,
                        sm.type,
                        sm.title,
                        sm.icon,
                        sm.name,
                        sm.path,
                        sm.isHide,
                        sm.isKeepAlive,
                        sm.isLink,
                        sm.isFull,
                        sm.sort,
                        sm.perms
                )
                .from(sm)
                .innerJoin(srm).on(sm.id.eq(srm.menuId))
                .where(
                        sm.status.eq(CommonConstant.ENABLE),
                        srm.roleId.in(sysRoleIds)
                )
                .orderBy(
                        sm.sort.asc(),
                        sm.id.asc()
                )
                .list();
    }

}
