package com.mcwl.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mcwl.common.constant.CacheConstants;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.system.domain.SysUserThirdAccount;
import com.mcwl.system.mapper.SysUserThirdAccountMapper;
import com.mcwl.system.service.ISysUserService;
import com.mcwl.system.service.ISysUserThirdAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 第三方登录表 业务层处理
 * @author DaiZibo
 * @date 2024/12/30
 * @apiNote
 */

@Slf4j
@Service
public class SysUserThirdAccountServiceImpl implements ISysUserThirdAccountService {


    @Resource
    private ISysUserService sysUserService;

    @Resource
    private SysUserThirdAccountMapper sysUserThirdAccountMapper;

    @Override
    public SysUser selectUserByOpenId(String openid) {

        //根据openid查询第三方登录表数据
        LambdaQueryWrapper<SysUserThirdAccount> sysUserThirdAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserThirdAccountLambdaQueryWrapper.eq(SysUserThirdAccount::getBindType, CacheConstants.WE_CHAT);
        sysUserThirdAccountLambdaQueryWrapper.eq(SysUserThirdAccount::getOpenid,openid);
        sysUserThirdAccountLambdaQueryWrapper.eq(SysUserThirdAccount::getDelFlag,0);
        SysUserThirdAccount sysUserThirdAccount = sysUserThirdAccountMapper.selectOne(sysUserThirdAccountLambdaQueryWrapper);

        //未查询到返回空对象
        if (sysUserThirdAccount == null){

            log.info("获取到的数据:{}",sysUserThirdAccount);
            return new SysUser();
        }

        //根据信息查登录人信息
        return sysUserService.selectUserById(sysUserThirdAccount.getUserId());
    }

    @Override
    public void add(SysUserThirdAccount sysUserThirdAccount) {

        //添加第三方登录权限
        sysUserThirdAccountMapper.insert(sysUserThirdAccount);
    }

}
