<?php
namespace Base\Service;

use Base\Model\PromoteModel;
use Base\Model\ApplyModel;

class PromoteCoinRecordService {

    public static $types = [
        1 => '收入',
        2 => '支出',
    ];

    public static $targetTypes = [
        1 => '推广员',
        2 => '玩家',
        3 => '管理员',
    ];

    public static $subTypes = [
        1 => '平台充值',
        2 => '线下转账',
        3 => '推广转账',
        4 => '玩家转账',
        5 => '后台扣除',
        6 => '迁移回收',
        7 => '迁移扣除',
        8 => '后台发放',
    ];

    public function __construct()
    {

    }

    public function getTypeText($type)
    {
        return self::$types[$type] ?? '未知';
    }

    public function getSubTypeText($subType)
    {
        return self::$subTypes[$subType] ?? '未知';
    }

    public function getTargetTypeText($targetType, $targetLevel)
    {
        if ($targetType == 1) {
            return PromoteService::$levels[$targetLevel] ?? '未知';
        } else {
            return self::$targetTypes[$targetType] ?? '未知'; 
        }
    }

    public function getTargetTypeLevels()
    {
        $items = [];
        foreach (self::$targetTypes as $key => $name) {
            if ($key == 1) {
                foreach (PromoteService::$levels as $level => $levelName) {
                    $items[$key . '_' . $level] = $levelName . '[推广]';
                }
            } else {
                $items[$key] = $name;
            }
        }
        return $items;
    }

    public function addRecord($params) {
        $data = $this->createRecord($params);
        return M('PromoteCoinRecord', 'tab_')->add($data);
    }

    public function createRecord($params) {
        $sn = date('YmdHis').strtoupper(substr(md5(json_encode($params) . 'PromoteCoinRecord' . rand(0, 99999)), 8, 16));
        $data = [
            'sn' => $sn,
            'type' => $params['type'],
            'sub_type' => $params['sub_type'],
            'ref_id' => $params['ref_id'],
            'promote_id' => $params['promote_id'],
            'target_id' => $params['target_id'],
            'target_type' => $params['target_type'],
            'target_level' => $params['target_level'] ?? 0,
            'coin' => $params['coin'],
            'balance_coin' => $params['balance_coin'],
            'game_id' => $params['game_id'] ?? 0,
            'create_time' => isset($params['create_time']) ? $params['create_time'] : time(),
            'description' => $params['description'],
            'remark' => isset($params['remark']) ? $params['remark'] : '',
        ];
        return $data;
    }

    public function getRef($record)
    {
        $ref = null;
        if ($record['sub_type'] == 1) {
            $ref = M('coin_pay_order', 'tab_')->where(['id' => $record['id']])->find();
        } else if ($record['sub_type'] == 2) {
            $ref = M('coin_pay_order', 'tab_')->where(['id' => $record['id']])->find();
        } else if ($record['sub_type'] == 3) {
            $ref = M('promote_coin_transfer_log', 'tab_')->where(['id' => $record['id']])->find();
        } else if ($record['sub_type'] == 4) {
            $ref = M('promote_coin_transfer_log', 'tab_')->where(['id' => $record['id']])->find();
        } else if ($record['sub_type'] == 5) {
            //
        } else if ($record['sub_type'] == 6) {
            // 
        } else if ($record['sub_type'] == 7) {
            // 
        } else if ($record['sub_type'] == 8) {
            // 
        }
        return $ref;
    }

    public function getRefDetail($record)
    {
        $ref = $this->getRef($record);
        if (!$ref) {
            return null;
        }

        $refTitle = '';
        $items = [];
        if (in_array($record['sub_type'], [1, 2])) {
            $refTitle = '充值订单信息';
            $payWays = [1 => '支付宝', 2 => '微信', 3 => '银联转账'];
            $payTypes = [1 => '线上充值', 2 => '线下充值'];
            $items = [
                ['name' => '充值订单号', 'value' => $ref['order_number']],
                ['name' => '付款时间', 'value' => date('Y-m-d H:i:s', $ref['pay_time'])],
                ['name' => '充值类型', 'value' => $payWays[$ref['pay_type']]],
                ['name' => '支付方式', 'value' => $payWays[$ref['pay_way']]],
                ['name' => '支付金额', 'value' => $ref['pay_amount']],
                ['name' => '平台币数量', 'value' => $ref['coin_num']],
            ];
        } elseif ($record['sub_type'] == 3) {
            $promote = M('promote', 'tab_')->field(['id', 'account'])->where(['id' => $ref['target_id']])->find();
            $preWord = $record['type'] == 1 ? '来源' : '目标';
            $refTitle = '推广员转账信息';
            $items = [
                ['name' => '转账订单号', 'value' => $ref['sn']],
                ['name' => '转账时间', 'value' => date('Y-m-d H:i:s', $ref['create_time'])],
                ['name' => '平台币数量', 'value' => $ref['num']],
                ['name' => '适用游戏', 'value' => $game ? $game['game_name'] : '所有游戏'],
                ['name' => '转账描述', 'value' => $ref['description']],
                ['name' => '转账备注', 'value' => $ref['remark']],
                ['name' => $preWord . '推广账号', 'value' => $promote['account']],
                ['name' => $preWord . '推广账号类型', 'value' => PromoteService::$levels[$ref['target_level']]],
            ];
        } elseif ($record['sub_type'] == 4) {
            $refTitle = '玩家转账信息';
            $game = null;
            if ($ref['game_id'] > 0) {
                $game = M('game', 'tab_')->field(['id', 'game_name'])->where(['id' => $ref['game_id']])->find();
            }
            $user = M('user', 'tab_')->field(['id', 'account'])->where(['id' => $ref['target_id']])->find();
            $items = [
                ['name' => '转账订单号', 'value' => $ref['sn']],
                ['name' => '转账时间', 'value' => date('Y-m-d H:i:s', $ref['create_time'])],
                ['name' => '平台币数量', 'value' => $ref['num']],
                ['name' => '适用游戏', 'value' => $game ? $game['game_name'] : '所有游戏'],
                ['name' => '转账描述', 'value' => $ref['description']],
                ['name' => '转账备注', 'value' => $ref['remark']],
                ['name' => '玩家账号', 'value' => $user['account']],
            ];
        }

        return [
            'refTitle' => $refTitle,
            'items' => $items,
        ];
    }
}