package org.agt.module.system.dal.mysql.permission;

import org.agt.framework.mybatis.core.mapper.BaseMapperX;
import org.agt.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.agt.framework.web.core.util.WebFrameworkUtils;
import org.agt.module.system.controller.admin.permission.vo.menu.MenuListReqVO;
import org.agt.module.system.dal.dataobject.permission.MenuDO;
import org.apache.ibatis.annotations.Mapper;

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

@Mapper
public interface MenuMapper extends BaseMapperX<MenuDO> {

    Integer[] menuIds = {1138, 1139, 1140, 1141, 1142, 1143, 1224, 1225, 1226, 1227, 1228, 1229, 5010, 108, 500, 501, 1040, 1042, 1043, 1045, 107, 1036, 1037, 1038, 1039, 1093, 1094, 1095, 1096, 1097, 1098, 1100, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138, 2139, 2140, 2141, 2142, 2143, 2144, 2145, 2146, 2147, 2148, 2149, 2150, 2151, 2152, 2739, 109, 1046, 1048, 1261, 1263, 1264, 1265, 1266, 1267, 2447, 2448, 2449, 2450, 2451, 2452, 2453, 2083, 2, 106, 110, 114, 115, 116, 1031, 1032, 1033, 1034, 1035, 1050, 1051, 1052, 1053, 1054, 1056, 1057, 1058, 1059, 1060, 1070, 1075, 1083, 1084, 1085, 1086, 1087, 1089, 1243, 1255, 1256, 1257, 1258, 1259, 1260, 2472, 2478, 2479, 2480, 2481, 2482, 2483, 2484, 2485, 2486, 2487, 2488, 2489, 2490, 2491, 2492, 2493, 2494, 2495, 2497, 2525, 2740,1237,1238,1239,1240,1241,1242,111,1078,1082,1088,113,1066,1067,112,1077,1090,1091,1092};

    default MenuDO selectByParentIdAndName(Long parentId, String name) {
        return selectOne(MenuDO::getParentId, parentId, MenuDO::getName, name);
    }

    default Long selectCountByParentId(Long parentId) {
        return selectCount(MenuDO::getParentId, parentId);
    }

    default List<MenuDO> selectList(MenuListReqVO reqVO) {
        List<MenuDO> menus = selectList(new LambdaQueryWrapperX<MenuDO>()
                .likeIfPresent(MenuDO::getName, reqVO.getName())
                .eqIfPresent(MenuDO::getStatus, reqVO.getStatus()));
        if (WebFrameworkUtils.getLoginUserId() != 2) {
            menus = menus.stream().filter(item -> Arrays.stream(menuIds).noneMatch(m -> Objects.equals(Long.valueOf(m), item.getId()))
            ).collect(Collectors.toList());
        }
        return menus;
    }

    default List<MenuDO> selectListByPermission(String permission) {
        return selectList(MenuDO::getPermission, permission);
    }

    default MenuDO selectByComponentName(String componentName) {
        return selectOne(MenuDO::getComponentName, componentName);
    }

}
