package com.muyu.system.saas.service.impl;


import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.muyu.common.core.constant.UserConstants;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.system.domain.SysUser;
import com.muyu.common.system.domain.SysUserPost;
import com.muyu.common.system.domain.SysUserRole;
import com.muyu.common.system.remote.RemoteUserService;
import com.muyu.system.saas.mapper.SysUserPostMapper;
import com.muyu.system.saas.mapper.SysUserRoleMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.system.saas.mapper.SysUserMapper;
import com.muyu.system.saas.service.SysUserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 胡杨
 * @Name: SysUser
 * @Description:
 * @CreatedDate: 2024/9/23 下午8:55
 * @FilePath: com.muyu.system.saas.service.impl
 */

@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {
    @Resource
    private RemoteUserService remoteUserService;
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private SysUserPostMapper userPostMapper;
    @Resource
    private SysUserRoleMapper userRoleMapper;

    @Override
    public SysUser selectUserByUserName(String userName) {
        return userMapper.selectUserByUserName(userName);
    }

    /**
     * 校验用户名称/手机号/邮箱是否唯一
     *
     * @param user 用户信息
     *
     * @return 结果
     */
    @Override
    public SysUser checkUserNameUnique (SysUser user) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(
                    StringUtils.isNotNull(user.getUserId()),
                    SysUser::getUserId, user.getUserId()
                )
                .eq(SysUser::getUserName, user.getUserName())
                .or()
                .eq(SysUser::getPhonenumber, user.getPhonenumber())
                .or()
                .eq(SysUser::getEmail, user.getEmail())
                .last("LIMIT 1");
        return this.getOne(queryWrapper);
    }

    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    @Override
    public void insertUserPost (SysUser user) {
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotEmpty(posts)) {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>();
            for (Long postId : posts) {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            userPostMapper.batchUserPost(list);
        }
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    @Override
    public void insertUserRole (SysUser user) {
        this.insertUserRole(user.getUserId(), user.getRoleIds());
    }

    @Override
    public List<SysUser> selectUserList(SysUser user) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(
                        StringUtils.isNotEmpty(user.getUserName()),
                        SysUser::getUserName, user.getUserName())
                .eq(
                        StringUtils.isNotEmpty(user.getStatus()),
                        SysUser::getStatus, user.getStatus())
                .eq(
                        StringUtils.isNotEmpty(user.getFirmCode()),
                        SysUser::getFirmCode, user.getFirmCode())
                .eq(
                        ObjectUtils.isNotEmpty(user.getDeptId()),
                        SysUser::getDeptId, user.getDeptId())
                .like(
                        StringUtils.isNotEmpty(user.getNickName()),
                        SysUser::getNickName, user.getNickName())
                .like(
                        StringUtils.isNotEmpty(user.getEmail()),
                        SysUser::getEmail, user.getEmail())
                .like(
                        StringUtils.isNotEmpty(user.getPhonenumber()),
                        SysUser::getPhonenumber, user.getPhonenumber());
        return this.list(queryWrapper);
    }

    /**
     * 新增用户角色信息
     *
     * @param userId  用户ID
     * @param roleIds 角色组
     */
    public void insertUserRole (Long userId, Long[] roleIds) {
        if (StringUtils.isNotEmpty(roleIds)) {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roleIds) {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }
            userRoleMapper.batchUserRole(list);
        }
    }





}
