<?php
namespace Base\Service;

use Base\Facade\Request;

class PaymentMerchantService {

    const WAY_ALIPAY = 1;
    const WAY_WEIXIN = 2;
    const WAY_EXPRESS = 4;

    const STATUS_ACTIVE = 1;
    const STATUS_INACTIVE = 0;

    public static $statusList = [
        0 => '禁用',
        1 => '启用',
    ];

    public static $channels = [
        1 => '支付宝',
        2 => '微信支付',
        3 => '易宝支付',
        4 => '双乾支付',
        5 => '汇付宝支付',
    ];

    public static $ways = [
        self::WAY_ALIPAY => '支付宝',
        self::WAY_WEIXIN => '微信',
        self::WAY_EXPRESS => '快捷支付',
    ];

    public function getStatusText($status)
    {
        return self::$statusList[$status] ?? '未知';
    }

    public function getChannelText($channel)
    {
        return self::$channels[$channel] ?? '未知';
    }

    public function getMerchantsByIds(array $ids, $fields = '*')
    {
        if (count($ids) == 0) {
            return [];
        }
        $rules = M('payment_merchant', 'tab_')->field($fields)->where(['in' => ['in', $ids]])->select();
        return index_by_column('id', $rules);
    }

    public function getMerchantsByWay($way)
    {
        $conditions = [];
        $conditions['_string'] = 'ways&' . $way . '=' . $way;
        $conditions['status'] = self::STATUS_ACTIVE;
        return M('payment_merchant', 'tab_')->where($conditions)->select();
    }

    public function getWaysValue($ways)
    {
        $waysValue = 0;
        foreach ($ways as $way) {
            $waysValue = $waysValue | $way;
        }
        return $waysValue;
    }

    public function getWaysRow($waysValue)
    {
        $row = [];
        foreach (self::$ways as $key => $name) {
            if (($waysValue & $key) == $key) {
                $row[] = $key;
            }
        }
        return $row;
    }

    public function getWaysName($waysValue)
    {
        $nameList = [];
        $keys = $this->getWaysRow($waysValue);
        foreach ($keys as $key) {
            $nameList[] = self::$ways[$key] ?? '未知';
        }
        return $nameList;
    }

    public function getIdentifierByConfig($config, $channel)
    {
        $identifier = '';
        switch($channel) {
            case 1:
                $identifier = $config['app_id'] ?? '';
                break;
            case 2:
                $identifier = $config['partner'] ?? '';
                break;
            case 3:
                $identifier = $config['partner'] ?? '';
                break;
            case 4:
                $identifier = $config['merno'] ?? '';
                break;
            case 5:
                $identifier = $config['merno'] ?? '';
                break;
        }
        return $identifier;
    }

    public function setDefault($way, $merchantId)
    {
        $merchantBefore = $this->getDefault($way);
        if ($merchantBefore && $merchantBefore['id'] == $merchantId) {
            return;
        }
        $merchantAfter = M('payment_merchant', 'tab_')->where(['id' => $merchantId])->find();
        if ($merchantAfter) {
            $afterData = ['is_default' => $merchantAfter['is_default'] | $way];
            M('payment_merchant', 'tab_')->where(['id' => $merchantId])->save($afterData);
        }
        if ($merchantBefore) {
            $beforeData = ['is_default' => $merchantBefore['is_default'] ^ $way];
            M('payment_merchant', 'tab_')->where(['id' => $merchantBefore['id']])->save($beforeData);
        }
    }

    public function getDefault($way)
    {
        return M('payment_merchant', 'tab_')->where(['_string' => 'ways&' . $way . '=' . $way . ' and is_default&' . $way . '=' . $way])->find();
    }
}