<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\repository\channel;

use app\model\UserChannelAccount;
use app\repository\BaseRepository;


/**
 * User Channel Account Repository
 * @package app\repository\channel
 */
class UserChannelAccountRepository extends BaseRepository
{

    /**
     * 查询用户所有渠道绑定账号
     * @param int $userId 用户ID
     * @return array
     */
    public function findAllByUser(int $userId): array
    {
        if ($userId <= 0) {
            return [];
        }

        $rows = UserChannelAccount::where('user_id', $userId)
            ->order('id', 'asc')
            ->select()
            ->toArray();

        return is_array($rows) ? $rows : [];
    }


    /**
     * 按OpenID查找渠道账号
     * @param string $channel
     * @param string $openid
     * @return UserChannelAccount|null
     */
    public function findByOpenid(string $channel, string $openid): ?UserChannelAccount
    {
        $row = UserChannelAccount::where([
            'channel' => $channel,
            'openid' => $openid,
        ])->find();

        return $row instanceof UserChannelAccount ? $row : null;
    }


    /**
     * 按UnionID查找渠道绑定账号
     * @param string $unionid 微信UnionID
     * @return UserChannelAccount|null
     */
    public function findByUnionid(string $unionid): ?UserChannelAccount
    {
        $row = UserChannelAccount::where('unionid', $unionid)->find();

        return $row instanceof UserChannelAccount ? $row : null;
    }


    /**
     * 按渠道和提供商用户ID查找绑定
     * @param string $channel 渠道标识
     * @param string $providerUid 提供商用户ID
     * @return UserChannelAccount|null
     */
    public function findByProviderUid(string $channel, string $providerUid): ?UserChannelAccount
    {
        $row = UserChannelAccount::where([
            'channel' => $channel,
            'provider_uid' => $providerUid,
        ])->find();

        return $row instanceof UserChannelAccount ? $row : null;
    }


    /**
     * 按用户和渠道查找绑定
     * @param int $userId 用户ID
     * @param string $channel 渠道标识
     * @return UserChannelAccount|null
     */
    public function findByUserAndChannel(int $userId, string $channel): ?UserChannelAccount
    {
        $row = UserChannelAccount::where([
            'user_id' => $userId,
            'channel' => $channel,
        ])->find();

        return $row instanceof UserChannelAccount ? $row : null;
    }


    /**
     * 按用户和多个渠道查找绑定
     * @param int $userId 用户ID
     * @param array $channels 渠道标识列表
     * @return UserChannelAccount|null
     */
    public function findByUserAndChannels(int $userId, array $channels): ?UserChannelAccount
    {
        if ($userId <= 0 || $channels === []) {
            return null;
        }

        $row = UserChannelAccount::where('user_id', $userId)
            ->whereIn('channel', $channels)
            ->order('id', 'asc')
            ->find();

        return $row instanceof UserChannelAccount ? $row : null;
    }


    /**
     * 保存渠道账号绑定信息
     * @param array $data 绑定数据
     * @return UserChannelAccount
     */
    public function saveBinding(array $data): UserChannelAccount
    {
        $row = UserChannelAccount::where([
            'channel' => (string)($data['channel'] ?? ''),
            'provider_uid' => (string)($data['provider_uid'] ?? ''),
        ])->find();

        if (!$row) {
            $row = new UserChannelAccount();
        }

        $row->save($data);
        return $row;
    }
}