<?php

namespace Main\Src;

use Dever;
use Main\Lib\Core;

class Content extends Core
{
    /*
    public function del()
    {
        $uid = Dever::input('uid');
        Dever::db('passport/user')->delete($uid);
        $info = Dever::db('passport/wechat')->state(array('uid' => $uid));
        if ($info) {
            foreach ($info as $k => $v) {
                Dever::db('passport/wechat')->delete($v['id']);
            }
        }

        return 'ok';
    }
    */

    public function score_code_actact()
    {
        $where['status'] = 2;
        $where['product_id'] = 15;
        $where['type'] = 3;
        $where['buy_id'] = Dever::input('buy_id', 48);
        $uid = Dever::input('uid');
        $test = Dever::input('test');
        if ($uid > 0) {
            $where['uid'] = $uid;
        }
        $id = array
        (
            48 => 10,
            49 => 100,
            50 => 300,
            51 => 600
        );
        $buy = Dever::db('journal/buy_num')->one($where['buy_id']);
        $info = Dever::db('journal/info')->one($where['product_id']);
        $data = Dever::db('journal/order')->state($where);
        $time = Dever::maketime('2019-01-29 14:44:30');
        if ($data) {
            if ($test == 1) {
                print_r($data);die;
            }
            foreach ($data as $k => $v) {
                if ($v['cdate'] < $time && isset($id[$v['buy_id']]) && $id[$v['buy_id']]) {
                    if ($v['type'] == 3) {

                        $w['code'] = $v['code'];
                        $o = Dever::db('code/info')->one($w);
                        print_r($buy);
                        if ($o) {
                            $up['product_num'] = $buy['num'];
                            $up['where_id'] = $o['id'];
                            Dever::db('code/info')->update($up);
                        }

                        print_r($o);
                        print_r($v);
                    }


                }
            }
        }
    }

    # 跑积分
    public function score_actact()
    {
        $where['status'] = 2;
        $where['product_id'] = 15;
        $where['type'] = Dever::input('type', 1);
        $where['buy_id'] = Dever::input('buy_id', 48);
        $uid = Dever::input('uid');
        $test = Dever::input('test');
        if ($uid > 0) {
            $where['uid'] = $uid;
        }
        $id = array
        (
            48 => 10,
            49 => 100,
            50 => 300,
            51 => 600
        );
        $info = Dever::db('journal/info')->one($where['product_id']);
        $data = Dever::db('journal/order')->state($where);
        $time = Dever::maketime('2019-01-29 14:44:30');
        if ($data) {
            print_r($data);die;
            if ($test == 1) {
                print_r($data);die;
            }
            foreach ($data as $k => $v) {
                if ($v['uid'] != 25371 && $v['uid'] != 25555 && $v['cdate'] < $time && isset($id[$v['buy_id']]) && $id[$v['buy_id']]) {
                    $num = $id[$v['buy_id']] - 1;
                    $score = $num * $info['score'];

                    $user = Dever::db('score/user')->one(array('uid' => $v['uid']));
                    if ($user) {
                        $update['where_id'] = $user['id'];
                        $update['score'] = $user['score'] + $score;

                        
                        //$user_log = Dever::db('score/user_log')->one(array('uid' => $v['uid'], 'num' => $score, 'status' => 1));
                        $user_log = false;

                        if (!$user_log) {
                            if ($v['type'] == 1) {
                                Dever::score($v['uid'], 'buy_journal', '购买小刊', 'act/lib/score.submit?method=pay&type=4&id=' . $v['product_id'], $score, false);
                            } elseif ($v['type'] == 3) {

                                $w['type'] = 2;
                                $w['code'] = $v['code'];
                                $w['status'] = 2;
                                $o = Dever::db('journal/order')->one($w);

                                if ($o) {
                                    Dever::score($o['uid'], 'dh_journal', '兑换小刊', 'act/lib/score.submit?method=code&type=4&id=' . $o['product_id'], $score, false);
                                }
                            }
                        }
                        print_r($v);
                    }
                }
            }
        }

        return 'ok';
    }

    public function test_redis()
    {
        # 测试缓存
        $redis = new \Redis;

        $expire = 3600;

        $key = 'test_';

        $redis->connect('192.168.0.220', '6379');

        for($i=0; $i<= 100;$i++) {
            $redis->set($key . $i, 'key_' . $i . '_value_' . rand(0,10000), $expire);
        }

        for($i=0; $i<= 100;$i++) {
            $data[] = $redis->get($key . $i);
        }

        echo 'ok';die;
    }

    public function test_memcache()
    {
        # 测试缓存
        $cache = new \Memcached;

        $expire = 3600;

        $key = 'test_';

        $cache->addServer('192.168.0.220', '11211', 100);

        for($i=0; $i<= 100;$i++) {
            $cache->set($key . $i, 'key_' . $i . '_value_' . rand(0,10000), $expire);
        }

        for($i=0; $i<= 100;$i++) {
            $data[] = $cache->get($key . $i);
        }

        echo 'ok';die;
    }

    public function test()
    {
        $journal = Dever::db('journal/info')->one(8);
        # 发模板消息
        $uid = Dever::input('uid');
        $user = Dever::db('passport/user')->one($uid);
        $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid));

        print_r($wechat);die;
        if ($wechat && Dever::project('wechat_applet')) {
            $send['key'] = 'buy_journal';
            $send['project_id'] = 1;
            $send['touser'] = $wechat['openid'];
            $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($uid) . ',' . '4,' . $order['product_id'];
            $send['data'] = json_encode(array
            (
                'keyword1' => array('value' => date('Y年m月d日 H:i', $order['cdate'])),
                'keyword2' => array('value' => '购买成功，您获得了 '.$journal['name'].' 的阅读资格！'),
            ));
            $send['form_id'] = Dever::load('act/lib/form')->get($uid, 2);

            if ($send['form_id']) {
                Dever::load('wechat_applet/msg.send', $send);
            }
        }

        # 发短信
        if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms')) {
            $send = array();
            $send['name'] = $journal['name'];
            Dever::load('sms/api.send', 'buy_journal', $user['mobile'], $send);
        }
    }

    # 开屏广告
    public function ad()
    {
        $this->data['push_screen_ad'] = Dever::load('push/lib/data')->get('app_screen_ad', 'APP开屏广告', 1);

        return $this->data;
    }
    
    # 首页精选
    public function home()
    {
        //MWY2M2MtZThRSG1iR2NHTjV6SjlHUDZvSHNlVURQVkRiT1dtREQxVVA4SThSOEc5bFhnUW43UExo
        //echo Dever::login(12);die;
        //1 NDY3MmVlOGVsdUxxa2pzNHczckR2TEl1NGFmVmVTd2ItWWhDcDB1T04wdXBDaUpDMl9rRmU4VHM=
        //2 ZTdjNjBHUTY4QUpoZk16QVlZRlg0d3locDFRcll5a3FQcWhFdEo1M1QzaFhmNGhrUlAyTTdGVWc= 
        //9 ZTBiZWN5dW9ublpnSzR2dmlXUUc5MWJJQWk5d3pFQmx0TEp6a0VJVkU4NG5HVTYtcjhFNTdfdnc=
        # 栏目
        //$this->data['push_menu'] = Dever::load('push/lib/data')->get('menu', '头部导航');

        # 小程序去掉视频栏目
        $cate = array();
        //$cate['id_no'] = 2;
        $this->data['menu'] = Dever::db('content/cate')->getAll($cate);

        if ($this->app) {
            # 焦点图
            $this->data['push_focus'] = Dever::load('push/lib/data')->get('app_focus', 'APP精选焦点图');

            # 广告
            $this->data['push_ad'] = Dever::load('push/lib/data')->get('app_ad', 'APP精选广告位');

            # 广告弹窗
            $this->data['push_open_ad'] = Dever::load('push/lib/data')->get('app_open_ad', 'APP精选弹窗广告位');
        } else {
            # 焦点图
            $this->data['push_focus'] = Dever::load('push/lib/data')->get('focus', '精选焦点图');

            # 广告
            $this->data['push_ad'] = Dever::load('push/lib/data')->get('ad', '精选广告位');

            # 广告弹窗
            $this->data['push_open_ad'] = Dever::load('push/lib/data')->get('open_ad', '精选弹窗广告位');
        }
        
        # 开屏广告
        $this->data['push_screen_ad'] = Dever::load('push/lib/data')->get('app_screen_ad', 'APP开屏广告', 1);

        # 推荐位
        $this->data['push_link'] = Dever::load('push/lib/data')->get('link', '精选分类');

        # 精选推荐
        $this->data['push_up'] = Dever::load('push/lib/data')->get('up', '精选推荐');

        $this->news();

        return $this->data;
    }

    # 精选推荐更多
    public function up()
    {
    	$this->data['push_up'] = Dever::load('push/lib/data')->get('up', '精选推荐', 10, true);

    	return $this->data;
    }

    # 下拉更多
    public function news()
    {
    	# 直接从已审核的列表中读取
    	$this->data['news'] = array();

        $cate = Dever::input('menu');
        $where = array();
        if ($cate > 0) {
            $where['cate_id'] = $cate;
        }

        $type = Dever::input('type');
        if ($type > 0) {
            $where['type'] = $type;
        }

        if (checkVersion()->video == 2) {
            $where['type_no'] = 2;
        }

    	$data = Dever::db('audit/data')->getAll($where);
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k] = $this->content($v);
            }
            $this->data['news'] = $data;
        } else {
            $this->data['news'] = array();
        }

        return $this->data;
    }
}