package cn.ey.biz.module.system.service.permission;

import cn.ey.biz.module.system.controller.admin.permission.vo.userRole.UserRoleExcelVO;
import cn.ey.biz.module.system.dal.dataobject.permission.RoleDO;
import cn.ey.biz.module.system.dal.dataobject.permission.UserRoleDO;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.dal.mapper.permission.RoleMapper;
import cn.ey.biz.module.system.dal.mapper.permission.UserRoleMapper;
import cn.ey.biz.module.system.dal.mapper.user.AdminUserMapper;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户角色关联表实现
 */
@Service
public class UserRoleServiceImpl  implements UserRoleService{

    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private AdminUserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<UserRoleExcelVO> getListUserRole() {
        List<UserRoleDO> userRoleList=userRoleMapper.selectList();
        List<AdminUserDO>  usersList=userMapper.selectList();
        List<RoleDO> roleList=roleMapper.selectList();

        List<UserRoleExcelVO> userRoleExcelVOList=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(userRoleList)){
            for(UserRoleDO ur:userRoleList){
                List<AdminUserDO> adminUserList=usersList.stream().filter(x->x.getId().equals(ur.getUserId())).collect(Collectors.toList());
                List<RoleDO> roleLists=roleList.stream().filter(x->x.getId().equals(ur.getRoleId())).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(adminUserList) && CollectionUtil.isNotEmpty(roleLists)){
                    UserRoleExcelVO userRoleExcelVO =new UserRoleExcelVO();
                    userRoleExcelVO.setGpn(adminUserList.get(0).getGpn());
                    userRoleExcelVO.setRoleCode(roleLists.get(0).getCode());
                    userRoleExcelVOList.add(userRoleExcelVO);
                }
            }
        }
        return userRoleExcelVOList;
    }


    @Override
    public List<UserRoleExcelVO> getListUserRoleSql() {
        return  userRoleMapper.getUserRoleGpnRoCode();
    }

    @Override
    @Transactional
    public void importUserRoleList(List<UserRoleExcelVO> userRoleExceList) {
        List<UserRoleDO> userRoleDOList=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(userRoleExceList)){
            List<String> gpnList = userRoleExceList.stream().map(x -> x.getGpn()).collect(Collectors.toList());
            List<AdminUserDO>  usersList=userMapper.selectList(new LambdaUpdateWrapper<AdminUserDO>().in(AdminUserDO::getGpn,gpnList));
            List<RoleDO> roleList=roleMapper.selectList();

            for (UserRoleExcelVO urLi:userRoleExceList){
                List<AdminUserDO> adminUserList=usersList.stream().filter(x->x.getGpn().equals(urLi.getGpn())).collect(Collectors.toList());
                List<RoleDO> roleLists=roleList.stream().filter(x->x.getCode().equals(urLi.getRoleCode())).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(adminUserList)&& CollectionUtil.isNotEmpty(roleLists)){
                    UserRoleDO userRoleDO=new UserRoleDO();
                    userRoleDO.setUserId(adminUserList.get(0).getId());
                    userRoleDO.setRoleId(roleLists.get(0).getId());
                    userRoleDOList.add(userRoleDO);
                }
            }
            userRoleMapper.insertBatch(userRoleDOList);
        }
    }

}
