package com.yida.data.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.constant.StringConstant;
import com.yida.data.common.core.entity.notice.EduNoticeSystem;
import com.yida.data.common.core.entity.notice.EduNoticeSystemUser;
import com.yida.data.common.core.entity.system.Role;
import com.yida.data.common.core.entity.system.SystemUser;
import com.yida.data.common.core.entity.system.UserRole;
import com.yida.data.school.feign.news.RemoteNoticeSystemService;
import com.yida.data.school.feign.news.RemoteNoticeSystemUserService;
import com.yida.data.system.mapper.RoleMapper;
import com.yida.data.system.mapper.UserMapper;
import com.yida.data.system.mapper.UserRoleMapper;
import com.yida.data.system.service.IUserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author MrBird
 */
@Service("userRoleService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RemoteNoticeSystemUserService remoteNoticeSystemUserService;
    @Resource
    RemoteNoticeSystemService remoteNoticeSystemService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserRolesByRoleId(String[] roleIds) {
        List<String> list = Arrays.asList(roleIds);
        this.baseMapper.delete(new LambdaQueryWrapper<UserRole>().in(UserRole::getRoleId, list));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserRolesByUserId(String[] userIds) {
        List<String> list = Arrays.asList(userIds);
        this.baseMapper.delete(new LambdaQueryWrapper<UserRole>().in(UserRole::getUserId, list));
    }

    @Override
    public List<String> findUserIdsByRoleId(String[] roleIds) {
        List<UserRole> list = baseMapper.selectList(new LambdaQueryWrapper<UserRole>().in(UserRole::getRoleId, String.join(StringConstant.COMMA, roleIds)));
        return list.stream().map(userRole -> String.valueOf(userRole.getUserId())).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindRoleByRoleName(UserRole[] userRoles) {
        EduNoticeSystem eduNoticeSystem = new EduNoticeSystem();
        EduNoticeSystemUser eduNoticeSystemUser = new EduNoticeSystemUser();
        for (UserRole userRole : userRoles) {
            Role role = roleMapper.selectOne(Wrappers.lambdaQuery(new Role()).eq(Role::getRoleName,
                    userRole.getRoleName()));
            if (count(Wrappers.lambdaQuery(new UserRole()).eq(UserRole::getRoleId, role.getRoleId()).eq(UserRole
                    ::getUserId, userRole.getUserId())) == 0) {
                userRole.setRoleId(role.getRoleId());
                save(userRole);


                //eduNoticeSystem.setContent("管理员已将您添加为" + role.getRoleName());
                //Long noticeId = eduNoticeSystemService.insertNotice(eduNoticeSystem);
                //eduNoticeSystemUser.setUserId(userRole.getUserId());
                //eduNoticeSystemUser.setNoticeId(noticeId);
                //eduNoticeSystemUserService.insertNotice(eduNoticeSystemUser);
            }
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindRoleByMobile(String mobile, String roleName) {
        SystemUser systemUser =
                userMapper.selectOne(Wrappers.lambdaQuery(new SystemUser()).eq(SystemUser::getMobile, mobile));
        Role role = roleMapper.selectOne(Wrappers.lambdaQuery(new Role()).eq(Role::getRoleName, roleName));
        UserRole userRole = new UserRole();
        if (count(Wrappers.lambdaQuery(new UserRole()).eq(UserRole::getRoleId, role.getRoleId()).eq(UserRole
                ::getUserId, systemUser.getUserId())) == 0) {
            userRole.setUserId(systemUser.getUserId());
            userRole.setRoleId(role.getRoleId());
            save(userRole);
        }

//        EduNoticeSystem eduNotice = new EduNoticeSystem();
//        eduNotice.setContent("管理员已经将您添加为" + roleName);
//        Long noticeId = eduNoticeSystemService.insertNotice(eduNotice);
//        EduNoticeSystemUser eduNoticeSystemUser = new EduNoticeSystemUser();
//        eduNoticeSystemUser.setUserId(systemUser.getUserId());
//        eduNoticeSystemUser.setNoticeId(noticeId);
//        eduNoticeSystemUserService.insertNotice(eduNoticeSystemUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bindRoleByUser(Long sysUserId, String roleName) {
        Role role = roleMapper.selectOne(Wrappers.lambdaQuery(new Role()).eq(Role::getRoleName, roleName));
        if (role == null) {
            role = new Role();
            role.setRoleName(roleName);
            roleMapper.insert(role);
        }
        UserRole userRole = new UserRole();
        if (count(Wrappers.lambdaQuery(new UserRole()).eq(UserRole::getRoleId, role.getRoleId()).eq(UserRole
                ::getUserId, sysUserId)) == 0) {
            userRole.setUserId(sysUserId);
            userRole.setRoleId(role.getRoleId());
            save(userRole);
        }
        //SystemUser systemUser = userMapper.selectById(sysUserId);
        //EduNoticeSystem eduNotice = new EduNoticeSystem();
        //eduNotice.setContent("管理员已经将您添加为" + roleName);
        //Long noticeId = eduNoticeSystemService.insertNotice(eduNotice);
        //EduNoticeSystemUser eduNoticeSystemUser = new EduNoticeSystemUser();
        //eduNoticeSystemUser.setNoticeId(noticeId);
        //eduNoticeSystemUser.setUserId(systemUser.getUserId());
        //eduNoticeSystemUserService.insertNotice(eduNoticeSystemUser);
    }
}
