package com.sptg.permissions.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.util.CommonUtil;
import com.sptg.permissions.api.dto.AccountDto;
import com.sptg.permissions.api.dto.AccountOauthDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.AccountOauth;
import com.sptg.permissions.biz.mapper.AccountOauthMapper;
import com.sptg.permissions.biz.service.AccountOauthServiceInterface;
import com.sptg.permissions.biz.service.AccountServiceInterface;
import com.sptg.permissions.biz.service.OrgServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * title: AccountOauthServiceImpl
 * package: com.sptg.permissions.biz.security.impl
 * description: 账号第三方授权-服务层接口实现类
 *
 *                  
 *                 
 */
@Service
@RequiredArgsConstructor
public class AccountOauthServiceImpl extends ServiceImpl<AccountOauthMapper, AccountOauth> implements AccountOauthServiceInterface {

    private final AccountServiceInterface accountServiceInterface;

    @Override
    public AccountOauth selectByUuIdAndSource(String uuId, String source) {
        Wrapper<AccountOauth> wrapper = new EntityWrapper<>();
        wrapper.eq("s_source", source);
        wrapper.eq("s_uuId", uuId);
        AccountOauth oauth = this.selectOne(wrapper);
        return oauth;
    }

    @Override
    public Page<AccountOauth> pagingQuery(PageBean<AccountOauth,AccountOauthDto> pageBean) {
        AccountOauthDto dto = pageBean.getDto();
        //  获取用户的关联列表
        Wrapper<AccountOauth> wrapper = new EntityWrapper<>();
        wrapper.eq("s_source", dto.getSource());
        wrapper.eq(StringUtils.hasText(dto.getUserName()), "s_user_name", dto.getUserName());
        //  分页查询关联信息
        return this.selectPage(pageBean, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean bindUser(AccountOauthDto dto) {
        Wrapper<AccountOauth> wrapper = new EntityWrapper<>();
        wrapper.eq("s_source", dto.getSource());
        wrapper.eq("s_uuId", dto.getUuId());
        AccountOauth oauth = this.selectOne(wrapper);
        if(oauth == null){
            AccountOauth accountOauth = createAccountOauth(dto);
            return (this.insert(accountOauth));
        }else{
            AccountOauth updateOauth = updateAccountOauth(oauth, dto);
            boolean update = this.updateById(updateOauth);
            return update;
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean untieUser(AccountOauthDto dto) {
        Wrapper<AccountOauth> wrapper = new EntityWrapper<>();
        wrapper.eq("s_source", dto.getSource());
        wrapper.eq("s_uuId", dto.getUuId());
        AccountOauth oauth = this.selectOne(wrapper);
        oauth.setAccountId(null);
        return this.updateById(oauth);
    }

    //新增授权用户
    private AccountOauth createAccountOauth(AccountOauthDto dto){
        AccountOauth oauth = BeanUtil.copyProperties(dto, AccountOauth.class);
        Date date = new Date();
        oauth.setCreateDate(date);
        oauth.setUpdateDate(date);
        return oauth;
    }

    //新增授权用户
    private AccountOauth updateAccountOauth(AccountOauth old, AccountOauthDto _new){
        old.setNickName(_new.getNickName());
        old.setEmail(_new.getEmail());
        old.setAvatar(_new.getAvatar());
        if(_new.getAccountId() != null){
            old.setAccountId(_new.getAccountId());
        }
        old.setUpdateDate(new Date());
        return old;
    }


}
