package cn.sh.stc.sict.cloud.upms.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.sh.stc.sict.cloud.upms.model.HpMenu;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;

import cn.sh.stc.sict.cloud.upms.dao.HpRoleMenuMapper;
import cn.sh.stc.sict.cloud.upms.model.HpRoleMenu;
import cn.sh.stc.sict.cloud.upms.service.HpRoleMenuService;

import java.util.ArrayList;
import java.util.List;


/**
 * @author F_xh
 */
@Service("hpRoleMenuService")
@AllArgsConstructor
public class HpRoleMenuServiceImpl extends ServiceImpl<HpRoleMenuMapper, HpRoleMenu> implements HpRoleMenuService {

    private final HpRoleMenuMapper hpRoleMenuMapper;

    @Override
    public boolean saveBatch(Long roleId, List<HpMenu> menuList) {
        this.removeByRoleId(roleId);
        if (CollUtil.isNotEmpty(menuList)) {
            List<HpRoleMenu> list = new ArrayList<>();
            menuList.forEach(m -> {
                HpRoleMenu rm = new HpRoleMenu();
                rm.setRoleId(roleId);
                rm.setMenuId(m.getId());
                list.add(rm);
            });
            this.saveBatch(list);
        }

        return true;
    }

    @Override
    public boolean removeByRoleId(Long roleId) {
        LambdaQueryWrapper<HpRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HpRoleMenu::getRoleId, roleId);

        return this.remove(wrapper);
    }

    @Override
    public boolean exist(Long menuId) {
        LambdaQueryWrapper<HpRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HpRoleMenu::getMenuId, menuId);
        return this.count(wrapper) > 0;
    }
}
