<?php

namespace Act\Lib;

use Dever;

class Share
{
    # 获取某个用户在某个图文的分享回流数
    public function getRefluxNum($uid, $id, $type)
    {
        $where['source_uid'] = $uid;
        $where['type'] = $type;
        $where['data_id'] = $id;
        return Dever::db('act/share_reflux')->total($where);
    }

    # 提交分享
    public function submit($uid, $id, $type)
    {
        $where['uid'] = $uid;
        $where['data_id'] = $id;
        $where['type'] = $type;
        $info = Dever::db('act/share')->one($where);
        if (!$info) {
            $where['num'] = 1;
            Dever::db('act/share')->insert($where);
        } else {
            $where['num'] = $info['num'] + 1;
            $where['where_id'] = $info['id'];
            Dever::db('act/share')->update($where);
        }

        Dever::score($uid, 'share_friend', '邀请好友');

        return true;
    }

    # 回流
    public function submit_reflux($source_uid, $uid, $id, $type, $system = 1)
    {
        if ($source_uid == $uid) {
            return false;
        }
        $where['uid'] = $source_uid;
        //$where['uid'] = $uid;
        $where['data_id'] = $id;
        $where['type'] = $type;

        $share = Dever::db('act/share')->one($where);
        if ($share) {
            $where = array();
            $user = Dever::db('passport/user')->one($uid);

            $where['user_type'] = 3;

            //if ($user['temp'] == 2 || $user['bind'] == 1) {
            if ($user) {
                # 必须是已经绑定手机号或者授权用户才可以增加积分

                # 新用户 增加邀请关系
                if (Dever::project('invite')) {
                    $parent = Dever::load('invite/api')->getParent($uid, $level = 1);
                    if (!$parent) {
                        # 新用户
                        $where['user_type'] = 1;
                        Dever::load('invite/api')->setRelation($uid, $source_uid);
                    } else {
                        # 老用户
                        $where['user_type'] = 2;
                        
                    }
                }
                if ($type <= 4) {
                    $method = '';
                    $name = Dever::config('base')->type[$type];
                    if ($type == 1) {
                        $method = 'article';
                    }
                    if ($type == 2) {
                        $method = 'vod';
                    }
                    if ($type == 3) {
                        $method = 'live';
                    }
                    if ($type == 4) {
                        $method = 'journal';
                    }

                    # 增加积分
                    if ($where['user_type'] == 1) {
                        $score = 0;
                        if ($type == 4) {
                            $active = Dever::db('journal/active')->one(array('id' => $id));
                            /*
                            if ($active && $active['status'] == 1 && time() <= $active['end']) {
                                $score = $active['invite_score'];
                            }
                            */
                            if ($active) {
                                $score = $active['invite_score'];
                            }
                        }
                        Dever::score($source_uid, 'share_'.$method.'_new_reflux', '通过'.$name.'邀请到新用户', 'act/lib/score.submit?method=share&type='.$type.'&id=' . $id, $score);
                        # 插入到邀请列表里
                        Dever::load('act/lib/invite')->submit($source_uid, $uid, $id, $type);

                        # 小刊订阅
                        if ($type == 4) {
                            if (isset($active) && $active) {
                                $num = $active['invite_num'];

                                $invite_num = Dever::load('act/lib/invite')->getTotal($source_uid, $id, $type);

                                if ($invite_num >= $num) {

                                    # 发消息
                                    if ($invite_num == $num) {
                                        $journal = Dever::db('journal/info')->one($id);
                                        if (Dever::project('message')) {
                                            Dever::load('message/lib/data')->push(-1, $source_uid, '活动提醒', '邀请人数已达'.$invite_num.'人，您获得了 '.$journal['name'].' 的阅读资格！', 11, $journal['cate_id'], 1, Dever::load('act/lib/note')->push(4, $journal['id'], $journal['name']));
                                        }

                                        # 发送模板消息 这里没有formid 暂时先不发送模板消息，后续应该通过前端收集formid就好了

                                        # 发短信
                                        /*
                                        if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms') && $system == 1) {
                                            $send = array();
                                            $send['num'] = $invite_num;
                                            $send['name'] = $journal['name'];
                                            Dever::load('sms/api.send', 'share_journal', $user['mobile'], $send);
                                        }
                                        */
                                    }
                                    
                                    Dever::load('act/lib/subscribe')->submit($source_uid, $id, 3);
                                }
                            }
                        }
                        
                    } elseif ($where['user_type'] == 2) {
                        Dever::score($source_uid, 'share_'.$method.'_reflux', '通过'.$name.'邀请到老用户');
                    }
                }
            }

            
            $where['source_uid'] = $source_uid;
            $where['uid'] = $uid;
            $where['data_id'] = $id;
            $where['type'] = $type;
            $where['share_id'] = $share['id'];
            $info = Dever::db('act/share_reflux')->one($where);
            if (!$info) {
                Dever::db('act/share_reflux')->insert($where);
                return true;
            }
        }

        return false;
    }
}