package com.app.service.platform.auth;

import com.app.constant.AccountConstant;
import com.app.entity.platform.system.AccountAuths;
import com.app.mapper.platform.AccountAuthsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class AccountAuthsService {

    @Autowired
    private AccountAuthsMapper mapper;

    public Optional<AccountAuths> findById(String id) {
        return Optional.ofNullable(mapper.selectById(id));
    }

    public List<AccountAuths> findByAccountId(String accountId) {
        return mapper.selectList(new QueryWrapper<AccountAuths>().lambda()
                .eq(AccountAuths::getAccountId, accountId)
        );
    }

    public Optional<AccountAuths> findByUsername(String username) {
        return Optional.ofNullable(mapper.selectOne(new QueryWrapper<AccountAuths>().lambda()
                .eq(AccountAuths::getIdentityType, AccountConstant.IdentityType.USERNAME)
                .eq(AccountAuths::getIdentifier, username)
        ));
    }

    public Optional<AccountAuths> findByPhone(String accountId) {
        return Optional.ofNullable(mapper.selectOne(new QueryWrapper<AccountAuths>().lambda()
                .eq(AccountAuths::getIdentityType, AccountConstant.IdentityType.PHONE)
                .eq(AccountAuths::getIdentifier, accountId)
        ));
    }

    public Optional<AccountAuths> findWeixinMiniProgramByAccountId(String accountId) {
        return Optional.ofNullable(mapper.selectOne(new QueryWrapper<AccountAuths>().lambda()
                .eq(AccountAuths::getIdentityType, AccountConstant.IdentityType.WEIXIN_MINI_PROGRAM)
                .eq(AccountAuths::getIdentifier, accountId)
        ));
    }

    public Optional<AccountAuths> findWeixinMiniProgramByOpenId(String openId) {
        return Optional.ofNullable(mapper.selectOne(new QueryWrapper<AccountAuths>().lambda()
                .eq(AccountAuths::getIdentityType, AccountConstant.IdentityType.WEIXIN_MINI_PROGRAM)
                .eq(AccountAuths::getIdentifier, openId)
        ));
    }

    public AccountAuths saveOrUpdate(AccountAuths accountAuths) {
        AccountAuths _auths;
        if (StringUtils.isEmpty(accountAuths.getId())) {
            _auths = new AccountAuths();
            _auths.setAccountId(accountAuths.getAccountId());
            _auths.setIdentityType(accountAuths.getIdentityType());
            _auths.setIdentifier(accountAuths.getIdentifier());
            _auths.setCredential(accountAuths.getCredential());
            _auths.setSalt(accountAuths.getSalt());
            mapper.insert(_auths);
        } else {
            _auths = findById(accountAuths.getId()).get();
            _auths.setIdentityType(accountAuths.getIdentityType());
            _auths.setIdentifier(accountAuths.getIdentifier());
            _auths.setCredential(accountAuths.getCredential());
            _auths.setSalt(accountAuths.getSalt());
            mapper.updateById(_auths);
        }

        return _auths;
    }

    public void updatePhone(String accountId, String phoneNumber) {
        Optional<AccountAuths> optional = findByPhone(accountId);
        if (optional.isPresent()) {
            AccountAuths auths = optional.get();
            auths.setIdentifier(phoneNumber);
            mapper.updateById(auths);
        }
    }

}
