/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.MapUtils;
import io.renren.modules.sys.dao.SysUserPlantDao;
import io.renren.modules.sys.entity.SysUserPlantEntity;
import io.renren.modules.sys.service.SysUserPlantService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 用户与角色对应关系
 *
 * @author Mark 735032128@qq.com
 */
@Service("sysUserPlantService")
public class SysUserPlantServiceImpl extends ServiceImpl<SysUserPlantDao, SysUserPlantEntity> implements SysUserPlantService {

    @Override
    public void saveOrUpdate(Long userId, List<Long> plantIdList) {
        //先删除用户与角色关系
        this.removeByMap(new MapUtils().put("user_id", userId));

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

        //保存用户与角色关系
        for (Long plantId : plantIdList) {
            SysUserPlantEntity sysUserPlantEntity = new SysUserPlantEntity();
            sysUserPlantEntity.setUserId(userId);
            sysUserPlantEntity.setPlantId(plantId);

            this.save(sysUserPlantEntity);
        }
    }

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

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

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