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.SysUserDeptDao;
import com.mx.cneeds.server.entity.SysRoleDeptEntity;
import com.mx.cneeds.server.entity.SysUserDeptEntity;
import com.mx.cneeds.server.user.service.SysUserDeptService;
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("sysUserDeptService")
public class SysUserDeptServiceImpl extends ServiceImpl<SysUserDeptDao, SysUserDeptEntity> implements SysUserDeptService {

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

        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> deptIdList) {
        //先删除用户与部门关系
        deleteBatch(new Long[]{userId});

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

        //保存用户与菜单关系
        for(Long deptId : deptIdList){
            SysUserDeptEntity sysUserDeptEntity = new SysUserDeptEntity();
            sysUserDeptEntity.setDeptId(deptId);
            sysUserDeptEntity.setUserId(userId);

            this.save(sysUserDeptEntity);
        }
    }

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

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