package cn.iocoder.yudao.module.system.service.auth;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.system.dal.dataobject.auth.AdminOauthUserOtherInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.auth.AdminOauthUserOtherInfoMapper;
import cn.iocoder.yudao.module.system.service.auth.dto.AdminOauthUserOtherInfoDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 用户三方登录信息 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class AdminOauthUserOtherInfoServiceImpl implements AdminOauthUserOtherInfoService {

    @Resource
    private AdminOauthUserOtherInfoMapper adminOauthUserOtherInfoMapper;

    @Override
    public void checkInsert(Long userId, String appId, String openId, Integer socialType) {
        AdminOauthUserOtherInfoDO adminOauthUserOtherInfoDOS = this.getByCondition(userId, socialType);
        if (adminOauthUserOtherInfoDOS == null) {
            adminOauthUserOtherInfoMapper.insert(new AdminOauthUserOtherInfoDO()
                    .setUserId(userId)
                    .setAppId(appId)
                    .setOpenId(openId)
                    .setSocialType(socialType));
        } else {
            if (!adminOauthUserOtherInfoDOS.getOpenId().equals(openId)) {
                adminOauthUserOtherInfoMapper.updateById(adminOauthUserOtherInfoDOS.setOpenId(openId));
            }
        }
    }

    @Override
    public AdminOauthUserOtherInfoDO getByCondition(Long userId, Integer socialType) {
        return adminOauthUserOtherInfoMapper.selectOne(new LambdaQueryWrapper<AdminOauthUserOtherInfoDO>()
                .eq(AdminOauthUserOtherInfoDO::getUserId, userId)
                .eq(AdminOauthUserOtherInfoDO::getSocialType, socialType));
    }


    @Override
    public List<AdminOauthUserOtherInfoDO> getOpenIdByCondition(AdminOauthUserOtherInfoDTO dto) {
        return adminOauthUserOtherInfoMapper.getOpenIdByCondition(dto);
    }

    @Override
    public void delByCondition(Long userId, String appId, String openId, Integer socialType) {
        adminOauthUserOtherInfoMapper.delete(new LambdaQueryWrapper<AdminOauthUserOtherInfoDO>()
                .eq(AdminOauthUserOtherInfoDO::getUserId, userId)
                .eq(AdminOauthUserOtherInfoDO::getAppId, appId)
                .eq(AdminOauthUserOtherInfoDO::getOpenId, openId)
                .eq(AdminOauthUserOtherInfoDO::getSocialType, socialType));
    }

}
