<?php

namespace Combine\Lib;

use Dever;

class Core
{

    # 合并用户数据
    public function handle($uid, $drop)
    {
        $table = array
        (
            'passport/app',
            'passport/wechat',
            'act/feedback',
            'act/comment',
            'act/form_id',
            'act/invite',
            'act/like',
            'act/live_comment',
            'act/live_note',
            //'act/score',//积分相关的要加上，不能直接修改
            'act/share',
            'act/share_reflux',
            'act/subscribe',
            'act/watch',
            //'code/info',
            'source/user',
            'score/action_log',
            //'score/user',//积分相关的要加上，不能直接修改
            //'score/user_level',//等级，不做合并，保留即可
            'score/user_log',//积分日志
            'message/inbox',
            'invite/relation',
            //'pay/order',//支付系统单独建立，如果通过接口会有安全问题，因为数据是独立的，暂时不做合并
            'journal/order',
            'act/order',
        );
        if (is_string($drop)) {
            $drop = explode(',', $drop);
        }
        foreach ($drop as $k => $v) {
            if ($v == $uid) {
                continue;
            }
            foreach ($table as $k1 => $v1) {
                Dever::db($v1)->updates(array('set_uid' => $uid, 'option_uid' => $v));
            }

            # 处理一些特殊的情况
            $this->other($uid, $v);

            # 积分相关 要把积分加上，再删掉之前的
            $this->score($uid, $v, 'score/user');
            $this->score($uid, $v, 'act/score');
        }
    }

    private function other($uid, $drop_uid)
    {
        Dever::db('code/info')->updates(array('set_uid' => $uid, 'option_uid' => $drop_uid));
        Dever::db('code/info')->updates(array('set_create_uid' => $uid, 'option_create_uid' => $drop_uid));

        $journal = Dever::db('journal/info')->state();
        if ($journal) {
            foreach ($journal as $k => $v) {
                Dever::setInput('set_product_id', $v['id']);
                Dever::db('code/info')->updates(array('set_uid' => $uid, 'option_uid' => $drop_uid));
                Dever::db('code/info')->updates(array('set_create_uid' => $uid, 'option_create_uid' => $drop_uid));
            }
        }
        
        Dever::db('invite/relation')->updates(array('set_to_uid' => $uid, 'option_to_uid' => $drop_uid));
    }

    private function score($uid, $drop_uid, $table, $col = 'score')
    {
        $db = Dever::db($table);
        $user_score = $db->one(array('uid' => $uid));
        $user_drop_score = $db->one(array('uid' => $drop_uid));
        if ($user_score && $user_drop_score) {
            $score = $user_score[$col] + $user_drop_score[$col];

            $update['where_id'] = $user_score['id'];
            $update['score'] = $score;
            $db->update($update);
            
            $db->delete($user_drop_score['id']);
        } elseif (!$user_score && $user_drop_score) {

            $score = $user_drop_score[$col];

            $update['where_id'] = $user_drop_score['id'];
            $update['uid'] = $uid;
            $update['score'] = $score;
            $db->update($update);
        }
    }
}