<?php

namespace Main\Lib;

use Dever;

class Cron
{
    public function dao_order1_api()
    {
        # 根据订单的buy_id来确定生成的本数
        $order = Dever::db('journal/order')->getNew();
        $test = Dever::input('test');
        if ($test == 1) {
            print_r($order);die;
        }
        if ($order) {
            foreach ($order as $k => $v) {
                $info = Dever::db('act/order')->one(array('order_id' => $v['order_id']));
                if (!$info) {
                    unset($v['id']);
                    $v['status'] = 7;
                    Dever::db('act/order')->insert($v);
                }
            }
        }

        return 'ok';
    }

    public function dao_order2_api()
    {
        # 根据订单的buy_id来确定生成的本数
        $id = Dever::input('product_id', 26);
        $where['product_id'] = $id;
        $where['status'] = 7;
        //$where['score_status'] = 1;
        $order_id = Dever::input('order_id');
        if ($order_id) {
            $where['order_id'] = $order_id;
        }
        $uid = Dever::input('uid');
        if ($uid) {
            $where['uid'] = $uid;
        }
        $order = Dever::db('act/order')->state($where);
        $test = Dever::input('test');
        if ($test == 1) {
            print_r($order);die;
        }
        if ($order) {
            foreach ($order as $k => $v) {
                $this->getpays($v['order_id']);
            }
        }

        return 'ok';
    }

    public function buy_code_api()
    {
        # 根据订单的buy_id来确定生成的本数
        $id = Dever::input('product_id', 26);
        $where['product_id'] = $id;
        $where['status'] = 2;
        //$where['score_status'] = 1;
        $order_id = Dever::input('order_id');
        if ($order_id) {
            $where['order_id'] = $order_id;
        }
        $uid = Dever::input('uid');
        if ($uid) {
            $where['uid'] = $uid;
        }
        $order = Dever::db('act/order')->state($where);
        $test = Dever::input('test');
        if ($test == 1) {
            print_r($order);die;
        }
        if ($order) {
            $db = Dever::load('code/lib/core');
            foreach ($order as $k => $v) {
                if ($v['type'] == 1 || $v['type'] == 3) {
                    $v['code'] = '';
                    $v['type'] = 3;
                    $db->createCodeByOrder($v);
                }
            }
        }

        return 'ok';
    }

    public function buy_num_api()
    {
        # 根据订单的buy_id来确定生成的本数
        $id = Dever::input('product_id', 26);
        $where['product_id'] = $id;
        $where['status'] = 2;
        //$where['score_status'] = 1;
        $order_id = Dever::input('order_id');
        if ($order_id) {
            $where['order_id'] = $order_id;
        }
        $uid = Dever::input('uid');
        if ($uid) {
            $where['uid'] = $uid;
        }
        $order = Dever::db('act/order')->state($where);
        $test = Dever::input('test');
        if ($test == 1) {
            print_r($order);die;
        }
        if ($order) {
            $db = Dever::load('act/lib/num');
            $user = array();
            foreach ($order as $k => $v) {
                if ($v['type'] == 1 || $v['type'] == 3) {
                    if ($v['buy_id'] > 0) {
                        $num = Dever::db('journal/buy_num')->one($v['buy_id']);
                        if ($num) {
                            if (isset($user[$v['uid']]) && $user[$v['uid']]) {
                                $user[$v['uid']]['num'] += $num['num'];
                            } else {
                                $user[$v['uid']] = array();
                                $user[$v['uid']]['uid'] = $v['uid'];
                                $user[$v['uid']]['id'] = $v['product_id'];
                                $user[$v['uid']]['num'] = $num['num'];
                            }
                        }
                    }
                }
            }

            foreach ($user as $k => $v) {
                $db->submit($v['id'], $v['uid'], $v['num'], false);
            }
        }

        return 'ok';
    }
    # 增加积分
    public function scores2_api()
    {
        //$where['start'] = Dever::maketime('2019-04-24 23:12:40');
        $where['start'] = Dever::maketime('2019-04-25 00:34:55');
        $where['end'] = Dever::maketime('2019-04-25 14:47:16');
        $where['score_type'] = 1;
        $where['action_id'] = Dever::input('action_id');
        $data = Dever::db('score/action_log')->getDataByDate($where);

        $test = Dever::input('test');
        if ($test == 1) {
            print_r($data);die;
        }
        if ($data) {
            foreach ($data as $k => $v) {
                Dever::load('score/lib/core.oper?log_id='.$v['id']);
            }
        }
        return $data;

        # 购买小刊
        /*
        $where['product_id'] = Dever::input('journal_id', 28);
        $where['type'] = 1;
        $where['status'] = 2;
        $data = Dever::db('journal/order')->state($where);
        $score = 20;
        if ($data) {
            foreach ($data as $k => $v) {
                # 计算该订单能加的积分数
                if ($v['buy_id'] && $v['buy_id'] > 0) {
                    $buy = Dever::db('journal/buy_num')->one($data['buy_id']);
                    if ($buy && $buy['num'] > 0) {
                        $update_score = $score*$buy['num'];
                    }
                }
            }
        }

        # 兑换小刊
        $where['product_id'] = Dever::input('journal_id', 28);
        $where['type'] = 2;
        $where['status'] = 2;
        $data = Dever::db('journal/order')->state($where);
        */
    }

    # 去重
    public function quchong_api()
    {
        # 购买小刊
        $id = Dever::input('product_id', 28);
        $where['product_id'] = $id;
        $where['status'] = 2;
        $order_id = Dever::input('order_id');
        if ($order_id) {
            $where['order_id'] = $order_id;
        }
        $uid = Dever::input('uid');
        if ($uid) {
            $where['uid'] = $uid;
        }
        $where['type'] = 2;
        //$where['score_status'] = 1;
        $order = Dever::db('journal/order')->state($where);

        if($order) {
            $user = array();
            foreach ($order as $k => $v) {
                if ($v['type'] == 2) {
                    $key = $v['uid'] . '_' . $v['code'];
                    if (!isset($user[$key])) {
                        $user[$key] = $v;
                    } else {
                        Dever::db('journal/order')->update(array('where_id' => $v['id'], 'state' => 2));
                    }
                }
            }
        }

        return 'ok';
    }

    # 重新计算排行榜
    public function pai4_api()
    {
        $id = Dever::input('product_id', 28);

        # 购买小刊
        $where['product_id'] = $id;
        $where['status'] = 2;
        $order_id = Dever::input('order_id');
        if ($order_id) {
            $where['order_id'] = $order_id;
        }
        $uid = Dever::input('uid');
        if ($uid) {
            $where['uid'] = $uid;
        }
        $where['type'] = array(1,3);
        //$where['score_status'] = 1;
        $order = Dever::db('journal/order')->getBuy($where);

        if ($order) {
            $user = array();
            foreach ($order as $k => $v) {
                $this->paiOrder1($v, $user);
                
            }
        }

        $test = Dever::input('test');
        if ($test == 1) {
            print_r($user);die;
        }

        # 重建积分排行
        foreach ($user as $k => $v) {
            print_r($v);
            $info = Dever::db('act/score')->one(array('uid' => $v['uid'], 'type' => 4, 'data_id' => $id));
            if (!$info) {
                $insert['uid'] = $v['uid'];
                $insert['type'] = 4;
                $insert['data_id'] = $id;
                $insert['score'] = $v['score'];
                Dever::db('act/score')->insert($insert);
            } else {
                $update['where_id'] = $info['id'];
                $update['uid'] = $v['uid'];
                $update['type'] = 4;
                $update['data_id'] = $id;
                $update['score'] = $v['score'] + $info['score'];
                Dever::db('act/score')->update($update);
            }
        }

        return 'ok';

        
    }

    public function paiOrder1($order, &$user)
    {
        if (!isset($user[$order['uid']])) {
            $user[$order['uid']] = array();
            $user[$order['uid']]['uid'] = $order['uid'];
            $user[$order['uid']]['score'] = 0;
        }

        $score = 0;
        if ($order['cash'] == 6) {
            $score = 20;
        }

        if ($order['cash'] == 60) {
            $score = 20*10;
        }

        if ($order['cash'] == 588) {
            $score = 20*100;
        }

        if ($order['cash'] == 1728) {
            $score = 20*300;
        }

        if ($order['cash'] == 3348) {
            $score = 20*600;
        }

        $user[$order['uid']]['score'] += $score;
    }

    # 重新计算排行榜
    public function pai3_api()
    {
        $id = Dever::input('product_id', 28);

        # 购买小刊
        $where['product_id'] = $id;
        $where['status'] = 2;
        $order_id = Dever::input('order_id');
        if ($order_id) {
            $where['order_id'] = $order_id;
        }
        $uid = Dever::input('uid');
        if ($uid) {
            $where['uid'] = $uid;
        }
        $where['type'] = 2;
        //$where['score_status'] = 1;
        $order = Dever::db('journal/order')->getBuy($where);

        $user = array();
        if ($order) {
            foreach ($order as $k1 => $v1) {
                $this->paiOrder($v1, $user);
                //Dever::db('journal/order')->update(array('where_id' => $v1['id'], 'score_status' => 2));
            }
        }

        $test = Dever::input('test');
        if ($test == 1) {
            print_r($user);die;
        }

        # 重建积分排行
        foreach ($user as $k => $v) {
            print_r($v);
            $info = Dever::db('act/score')->one(array('uid' => $v['uid'], 'type' => 4, 'data_id' => $id));
            if (!$info) {
                $insert['uid'] = $v['uid'];
                $insert['type'] = 4;
                $insert['data_id'] = $id;
                $insert['score'] = $v['score'];
                Dever::db('act/score')->insert($insert);
            } else {
                $update['where_id'] = $info['id'];
                $update['uid'] = $v['uid'];
                $update['type'] = 4;
                $update['data_id'] = $id;
                $update['score'] = $v['score'] + $info['score'];
                Dever::db('act/score')->update($update);
            }
        }

        return 'ok';
        
    }

    private function paiOrder($order, &$user)
    {
        if (!isset($user[$order['uid']])) {
            $user[$order['uid']] = array();
            $user[$order['uid']]['uid'] = $order['uid'];
            $user[$order['uid']]['score'] = 0;
        }
        if ($order['type'] <= 2) {
            # 购买小刊、兑换小刊增加积分
            if ($order['type'] == 1 && $order['buy_id'] > 0) {
                $buy = Dever::db('journal/buy_num')->one($order['buy_id']);
                if ($buy) {
                    $num = $buy['num'];
                    $user[$order['uid']]['score'] += $num*20;
                }
            } elseif ($order['type'] == 2 && $order['code']) {
                $code = Dever::db('code/info')->one(array('code' => $order['code'], 'state' => 1, 'type' => 3));
                if ($code) {
                    if ($code['order_id']) {
                        $info = Dever::db('journal/order')->one(array('order_id' => $code['order_id'], 'status' => 2, 'type' => 3));
                        if ($info) {
                            $num = $code['product_num'];
                            $user[$order['uid']]['score'] += $num*20;
                        }
                    } elseif ($code['product_num'] > 0) {
                        $num = $code['product_num'];
                        $user[$order['uid']]['score'] += $num*20;
                    }
                }
            }
        }
    }


    # 收回多余的兑换码
    public function deleteCode_api()
    {
        $order_id = Dever::input('order_id');
        if ($order_id) {
            $where['order_id'] = $order_id;
        }
        $where['product_id'] = 28;
        //$where['refund_status'] = 1;
        $where['type'] = 3;
        $data = Dever::db('journal/order')->state($where);

        $test = Dever::input('test');
        if ($test == 1) {
            print_r($data);
        }

        if ($data) {
            foreach ($data as $k => $v) {
                $this->deleteCodes($v);
            }
        }

        return 'ok';
    }

    private function deleteCodes($data)
    {
        # 获取该订单所能拥有的兑换码数
        $code_num = 1;
        $uid = $data['uid'];
        if ($data['buy_id'] > 0) {
            $buy = Dever::db('journal/buy_num')->one($data['buy_id']);
            $code_num = $buy['code'];
        }

        # 这个订单下所有码
        $code_where['order_id'] = $data['order_id'];
        $code_where['type'] = 4;
        $code = Dever::db('code/info')->getData($code_where);

        # code_num = 1
        # code = 21
        $test = Dever::input('test');
        if ($test == 1) {
            echo $code_num;
            print_r($code);die;
        }
        if ($code) {
            $i = 1;
            foreach ($code as $k => $v) {
                if ($i > $code_num) {
                    Dever::db('code/info')->update(array('where_id' => $v['id'], 'state' => 2));
                }
                $i++;
            }
        }
    }

    public function refund_api()
    {
        $order_id = Dever::input('order_id');
        if ($order_id) {
            $where['order_id'] = $order_id;
        }
        $where['product_id'] = 28;
        $where['status'] = 5;
        $where['refund_status'] = 1;
        $data = Dever::db('journal/order')->state($where);

        $test = Dever::input('test');
        if ($test == 1) {
            print_r($data);die;
        }
        if ($data) {
            foreach ($data as $k => $v) {
                $this->refundScore($v);
            }
        }
    }

    private function refundScore($data)
    {
        $score = 0;
        if ($data['cash'] == 6) {
            $score = 20;
        }

        if ($data['cash'] == 60) {
            $score = 20*10;
        }

        if ($data['cash'] == 588) {
            $score = 20*100;
        }

        if ($data['cash'] == 1728) {
            $score = 20*300;
        }

        if ($data['cash'] == 3348) {
            $score = 20*600;
        }

        if ($score > 0) {

            if ($data['type'] == 1) {
                $where['uid'] = $data['uid'];
                $user = Dever::db('score/user')->one($where);
                if ($user) {
                    $user_score = $user['score'] - $score;
                    if ($user_score < 0) {
                        $user_score = 0;
                    }
                    Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
                }

                $where['type'] = 4;
                $where['data_id'] = 28;
                $user = Dever::db('act/score')->one($where);
                if ($user) {
                    $user_score = $user['score'] - $score;
                    if ($user_score < 0) {
                        $user_score = 0;
                    }
                    Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
                }
            } elseif ($data['type'] == 3) {
                $code_where['order_id'] = $data['order_id'];
                //$code_where['type'] = 1;
                $code_where['create_uid'] = $data['uid'];
                $code = Dever::db('code/info')->state($code_where);
                if ($code) {
                    print_r($code);
                    foreach ($code as $k => $v) {
                        if ($v['type'] == 1) {
                            Dever::db('code/info')->update(array('where_id' => $v['id'], 'type' => 4));
                        } elseif ($v['type'] == 3) {
                            $this->refundScoreCode($v);
                        }
                    }
                }
            }
            

            Dever::db('journal/order')->update(array('where_id' => $data['id'], 'refund_status' => 2));
        }

        print_r($data);

        print_r($score);

        echo "\r\n";

        return $score;
    }

    private function refundScoreCode($data)
    {
        if (isset($data['score']) && $data['score'] > 0) {
            $score = $data['score'];
        } elseif (isset($data['product_num']) && $data['product_num'] > 0) {
            $score = $data['product_num'] * 20;
        }

        if ($score > 0) {
            $where['uid'] = $data['uid'];
            $user = Dever::db('score/user')->one($where);
            if ($user) {
                $user_score = $user['score'] - $score;
                if ($user_score < 0) {
                    $user_score = 0;
                }
                Dever::db('score/user')->update(array('where_id' => $user['id'], 'score' => $user_score));
            }

            $where['type'] = 4;
            $where['data_id'] = 28;
            $user = Dever::db('act/score')->one($where);
            if ($user) {
                $user_score = $user['score'] - $score;
                if ($user_score < 0) {
                    $user_score = 0;
                }
                Dever::db('act/score')->update(array('where_id' => $user['id'], 'score' => $user_score));
            }
        }
    }

    public function pays_api()
    {
        $where['product_id'] = 28;
        $where['type'] = 3;
        $where['status'] = 1;
        $where['cron_status'] = 1;
        $data = Dever::db('journal/order')->state($where);
        if ($data) {
            foreach ($data as $k => $v) {
                $this->getpays($v['order_id']);
                Dever::db('journal/order')->update(array('where_id' => $v['id'], 'cron_status' => 2));
            }
        }
    }

    private function getpays($order_id)
    {
        $url = 'http://mapi.jstyle.cn/pay/pay/?api.search&order_id='.$order_id.'&account_id=1';

        Dever::curl($url);
    }
	public function unpay_api()
	{}
	public function activeEnd_api()
	{}
	#用户未支付电子刊情况下，未支付订单超过2小时后，发送一次唤回支付提醒
    public function unpay()
    {
    	$time = 7200;
    	$where['cdate'] = time() + $time;
    	$where['note'] = 1;
    	$where['type'] = '1,3';
        $data = Dever::db('journal/order')->getDataByTime($where);

        if ($data) {
            foreach ($data as $k => $v) {
                $user = Dever::db('passport/user')->one($v['uid']);
                if (!$user) {
                    continue;
                }
                Dever::db('journal/order')->update(array('where_id' => $v['id'], 'note' => 2));

                $journal = Dever::db('journal/info')->one($v['product_id']);

                if (Dever::project('message')) {
                    Dever::load('message/lib/data')->push(-1, $v['uid'], '支付提醒', '您订阅的'.$journal['name'].'尚未支付！', 11, $v['cate_id'], 1, Dever::load('act/lib/note')->push(4, $journal['id'], $journal['name']));
                }
                
                # 发送短消息
                if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms')) {
                    $send = array();
                    $send['name'] = $journal['name'];
                    //Dever::load('sms/api.send', 'note_live', $user['mobile'], $send);
                }

                # 发模板消息
                $wechat = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'type' => 1, 'system_id' => $v['cate_id']));
                if ($wechat && Dever::project('wechat_applet')) {
                    $send['key'] = 'unbuy_journal';
                    $send['project_id'] = $v['cate_id'];
                    $send['touser'] = $wechat['openid'];
                    $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($v['uid']) . ',' . '4,' . $v['product_id'];
                    $send['data'] = array
                    (
                        'keyword1' => array('value' => $journal['name']),
                        'keyword2' => array('value' => $v['cash']),
                        'keyword3' => array('value' => '您订阅的'.$journal['name'].'尚未支付！'),
                    );
                    $send['data'] = json_encode($send['data']);
                    $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 2, $v['cate_id']);

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

        return 'ok';
    }

    #电子杂志活动结束前5个时，向己有电子刊阅读权限的用户发送活动结束提醒
    public function activeEnd()
    {
    	$time = 5*3600;
    	$where['ends'] = time();
        $where['end'] = $where['ends'] + $time;
        $data = Dever::db('journal/active')->getDataByTime($where);
        //echo Dever::sql();die;

        $test = Dever::input('test');
        if ($test == 1) {
            print_r($data);die;
        }
        if ($data) {
            foreach ($data as $ks => $vs) {

            	$journal = Dever::db('journal/info')->one($vs['info_id']);

            	$subscribe = Dever::db('act/subscribe')->state(array('note' => 1, 'type' => 4, 'data_id' => $vs['info_id']));

            	if ($subscribe) {
            		foreach ($subscribe as $k => $v) {
            			$user = Dever::db('passport/user')->one($v['uid']);
		                if ($user) {
		                    Dever::db('act/subscribe')->update(array('where_id' => $v['id'], 'note' => 2));

                            if (Dever::project('message')) {
                                Dever::load('message/lib/data')->push(-1, $v['uid'], '活动结束提醒', '您参与的'.$journal['name'].'积分排行榜活动还有5小时就要结束了，想要冲榜赢奖品的要抓紧机会呀〜', 11, $journal['cate_id'], 1, Dever::load('act/lib/note')->push(4, $journal['id'], $journal['name']));
                            }
                            
                            # 发送短消息
                            if (isset($user['mobile']) && $user['mobile'] && Dever::project('sms')) {
                                $send = array();
                                $send['name'] = $journal['name'];
                                //Dever::load('sms/api.send', 'note_live', $user['mobile'], $send);
                            }

                            # 发模板消息
                            $wechat = Dever::db('passport/wechat')->one(array('uid' => $v['uid'], 'type' => 1, 'system_id' => $journal['cate_id']));
                            if ($wechat && Dever::project('wechat_applet')) {
                                $send['key'] = 'service_end_note';
                                $send['project_id'] = $journal['cate_id'];
                                $send['touser'] = $wechat['openid'];
                                $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($v['uid']) . ',' . '4,' . $journal['id'];
                                $send['data'] = array
                                (
                                    'keyword1' => array('value' => $journal['name']),
                                    'keyword2' => array('value' => date('Y-m-d H:i', $vs['end'])),
                                    'keyword3' => array('value' => '您参与的'.$journal['name'].'积分排行榜活动还有5小时就要结束了，想要冲榜赢奖品的要抓紧机会呀~'),
                                );
                                $send['data'] = json_encode($send['data']);
                                $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 1, $journal['cate_id']);

                                if (!$send['form_id']) {
                                    $send['form_id'] = Dever::load('act/lib/form')->get($v['uid'], 2, $journal['cate_id']);
                                }

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

        return 'ok';
    }
}