package com.mx.cneeds.server.user.service.impl;

import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.pager.Query;
import com.mx.cneeds.server.dao.SysUserFuncDao;
import com.mx.cneeds.server.entity.SysRoleFuncEntity;
import com.mx.cneeds.server.entity.SysUserFuncEntity;
import com.mx.cneeds.server.user.service.SysUserFuncService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service("sysUserFuncService")
public class SysUserFuncServiceImpl extends ServiceImpl<SysUserFuncDao, SysUserFuncEntity> implements SysUserFuncService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysUserFuncEntity> page = this.page(
                new Query<SysUserFuncEntity>().getPage(params),
                new QueryWrapper<SysUserFuncEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public int deleteBatch(Long[] userIds){
        return baseMapper.deleteBatch(userIds);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long userId, List<Long> menuIdList) {
        //先删除角色与菜单关系
        deleteBatch(new Long[]{userId});

        if( menuIdList == null || menuIdList.size() == 0){
            return ;
        }

        //保存用户与菜单关系
        for(Long funcId : menuIdList){
            SysUserFuncEntity sysUserFuncEntity = new SysUserFuncEntity();
            sysUserFuncEntity.setFuncId(funcId);
            sysUserFuncEntity.setUserId(userId);

            this.save(sysUserFuncEntity);
        }
    }

    @Override
    public List<Long> queryFuncIdList(Long userId) {
        return baseMapper.queryFuncIdList(userId);
    }

}
