package org.linlinjava.jz.db.service;

import com.google.common.collect.Lists;
import org.linlinjava.jz.db.dao.JzDmLabelProfileMapper;
import org.linlinjava.jz.db.dao.JzSkillGroupUserMapper;
import org.linlinjava.jz.db.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 技能组成员管理
 */
@Service
public class JzSkillGroupUserService {

    @Resource
    private JzSkillGroupUserMapper skillGroupUserMapper;

    public int add(JzSkillGroupUser skillGroupUser) {
        skillGroupUser.setCreateTime(LocalDateTime.now());
        skillGroupUser.setUpdateTime(LocalDateTime.now());
        return skillGroupUserMapper.insertSelective(skillGroupUser);
    }

    public int deleteById(JzSkillGroupUser skillGroupUser) {
        skillGroupUser.setUpdateTime(LocalDateTime.now());
        skillGroupUser.setDeleted(true);
        return skillGroupUserMapper.updateByPrimaryKeySelective(skillGroupUser);
    }

    public int updateById(JzSkillGroupUser skillGroupUser) {
        skillGroupUser.setUpdateTime(LocalDateTime.now());
        return skillGroupUserMapper.updateByPrimaryKeySelective(skillGroupUser);
    }

    public List<JzSkillGroupUser> findByGroupId(Integer groupid) {
        JzSkillGroupUserExample example = new JzSkillGroupUserExample();
        example.or().andSkillGroupIdEqualTo(groupid).andDeletedEqualTo(false);
        return skillGroupUserMapper.selectByExample(example);
    }

    public List<JzSkillGroupUser> findByGroupId(Integer[] groupids) {
        JzSkillGroupUserExample example = new JzSkillGroupUserExample();
        example.or().andSkillGroupIdIn(Arrays.asList(groupids)).andDeletedEqualTo(false);
        return skillGroupUserMapper.selectByExample(example);
    }

    public List<Integer> findUserIdByGroupId(Integer[] groupids){
        JzSkillGroupUserExample example = new JzSkillGroupUserExample();
        example.or().andSkillGroupIdIn(Arrays.asList(groupids)).andDeletedEqualTo(false);
        List<JzSkillGroupUser> users= skillGroupUserMapper.selectByExample(example);

        List<Integer> result=Lists.newArrayList();
        if(users!=null&&users.size()>0){
            for(JzSkillGroupUser user:users){
                result.add(user.getUserId());
            }
        }
        return  result;
    }

    @Resource
    private JzDmLabelProfileMapper profileMapper;

    public int findSkillGroupIdbyUserId(int userId) {

        JzSkillGroupUserExample example = new JzSkillGroupUserExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        JzSkillGroupUser skillGroupUser = skillGroupUserMapper.selectOneByExample(example);

        if (skillGroupUser == null) return 0;
        return skillGroupUser.getSkillGroupId();
    }

    /**
     * 查询用户归属技能组，一个用户可能归属多个技能组
     * @param userId
     * @return
     */
    public List<Integer> findSkillGroupIdsbyUserId(int userId) {
        JzSkillGroupUserExample example = new JzSkillGroupUserExample();
        example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<JzSkillGroupUser> jzSkillGroupUsers = skillGroupUserMapper.selectByExample(example);
        List<Integer> lists = Lists.newArrayList();
        jzSkillGroupUsers.forEach(item -> {
            lists.add(item.getSkillGroupId());
        });
        return lists;
    }

    /**
     * 冻结技能组下 所有用户
     *
     * @param groupid
     * @param admin
     */
    public void deletedAll(Integer groupid, Integer admin) {
        JzSkillGroupUserExample example = new JzSkillGroupUserExample();
        example.or().andSkillGroupIdEqualTo(groupid);
        skillGroupUserMapper.logicalDeleteByExample(example);
    }

}
