<?php
namespace Journal\Lib;

use Dever;

class Pay
{
    private $key = 'jmss_2018';

    
    /*
    public function go_api()
    {
        $where['code'] = 'null';
        $where['type'] = 3;
        $where['status'] = 2;
        $data = Dever::db('act/order')->getUnCode($where);
        echo Dever::sql();
        if ($data) {
            print_r($data);die;
            foreach ($data as $k => $v) {
                # 购买兑换码
                $order = $v;
                $product_num = 1;
                $code_num = 1;
                $uid = $order['uid'];
                if ($order['buy_id'] > 0) {
                    $buy = Dever::db('journal/buy_num')->one($order['buy_id']);
                    $product_num = $buy['num'];
                    $code_num = $buy['code'];
                }
                
                if ($code_num > 1) {
                    $product_num = intval($product_num/$code_num);
                    for ($i = 0; $i < $code_num; $i++) {
                        $update['code'] = Dever::load('code/lib/core')->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $uid, $order['order_id']);
                    }
                } else {
                    $update['code'] = Dever::load('code/lib/core')->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $uid, $order['order_id']);
                }

                $update['where_id'] = $order['id'];
                Dever::db('act/order')->update($update);
            }
        }
    }


    public function test_api()
    {
        $data['status'] = 1;
        $this->order(1, 'test', $data);
    }
    */

    # 退款操作
    public function order($id, $name, $data)
    {
        $status = Dever::param('status', $data);

        if ($status == 5 && $id > 0) {
            $send = array();
            $info = Dever::db('act/order')->one($id);
            $send['pay_project_id'] = 1;
            $send['pay_uid'] = $info['uid'];
            $send['pay_order_id'] = $info['order_id'];
            $send['pay_tk_pic'] = Dever::param('tk_pic', $data);
            $send['pay_tk_time'] = Dever::param('tk_time', $data);
            $send['pay_tk_desc'] = Dever::param('tk_desc', $data);
            $send['pay_status'] = $status;
            $send['dever_token'] = $this->key;

            # 减少积分

            Dever::load('pay/lib/set.updateStatus', $send);
        }
    }

	/**
	 * 支付成功后,调取的接口 这里的安全以后再升级吧,升级成和pay/lib/set.updateStatus一样的
	 *
	 * @return mixed
	 */
	public function act_api($param = array())
	{
        $send = Dever::preInput('pay_');
        $key = md5($this->key);
        ksort($send);
        $send['signature'] = md5($key . '&' . http_build_query($send));
        $signature = Dever::input('signature');
        if ($send['signature'] == $signature) {
            $product_id = $send['pay_product_id'];
            $uid = $send['pay_uid'];
            $cash = $send['pay_cash'];
            $order_id = $send['pay_order_id'];
            $status = $send['pay_status'];
            $msg = $send['pay_msg'];

            $order = Dever::db('act/order')->one(array('order_id' => $order_id, 'uid' => $uid));

            if ($send['pay_status'] == 2 && $order) {
                
                $update = array();
                $update['where_id'] = $order['id'];
                $update['status'] = 2;
                if ($order['type'] == 3 && !$order['code']) {
                    $code = Dever::load('code/lib/core')->createCodeByOrder($order);
                    if ($code) {
                        $update['code'] = $code;
                    }
                }

                /*
                if ($order['type'] == 3 && !$order['code']) {
                    # 购买兑换码
                    $product_num = 1;
                    $code_num = 1;
                    if ($order['buy_id'] > 0) {
                        $buy = Dever::db('journal/buy_num')->one($order['buy_id']);
                        $product_num = $buy['num'];
                        $code_num = $buy['code'];
                    }
                    
                    if ($code_num > 1) {
                        $product_num = intval($product_num/$code_num);
                        for ($i = 0; $i < $code_num; $i++) {
                            $update['code'] = Dever::load('code/lib/core')->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $uid, $order['order_id']);
                        }
                    } else {
                        $update['code'] = Dever::load('code/lib/core')->createCode($order['cate_id'] . ',' . $order['product_id'], $order['cate_id'], $order['product_id'], $order['seller_id'], $product_num, $uid, $order['order_id']);
                    }
                }
                */

                Dever::db('act/order')->update($update);

                if ($order['type'] != 1) {
                    return;
                }

                # 订阅
                Dever::load('act/lib/subscribe')->submit($uid, $order['product_id'], 1);

                $score = false;
                $num = false;
                if ($order['buy_id'] > 0) {
                    $info = Dever::db('journal/info')->one($order['product_id']);
                    $buy = Dever::db('journal/buy_num')->one($order['buy_id']);
                    if ($info && $buy && $info['score'] > 0) {
                        $score = $buy['num'] * $info['score'];
                    } elseif ($info && $buy) {
                        $num = $buy['num'];
                    }
                }
                Dever::score($uid, 'buy_journal', '购买小刊', 'act/lib/score.submit?method=pay&type=4&id=' . $order['product_id'], $score, $num);

                # 发消息
                $journal = Dever::db('journal/info')->one($order['product_id']);

                if (Dever::project('message')) {
                    Dever::load('message/lib/data')->push(-1, $uid, '购买提醒', '购买成功,您获得了 '.$journal['name'].' 的阅读资格!', 11, $order['cate_id'], 1, Dever::load('act/lib/note')->push(4, $journal['id'], $journal['name']));
                }

                $user = Dever::db('passport/user')->one($uid);

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

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

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

            } else {
                Dever::db('act/order')->update(array('where_id' => $order['id'], 'status' => 3));
            }
        }
	}

    public function send_api()
    {
        $uid = Dever::input('uid', 8);
        # 发模板消息
        $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid));
        if ($wechat && Dever::project('wechat_applet')) {
            $send['key'] = 'buy_journal';
            $send['project_id'] = 2;
            $send['touser'] = $wechat['openid'];
            $send['page'] = Dever::config('base')->applet_index . '?scene=' . Dever::login($uid) . ',' . '4,' . $order['product_id'];
            $send['data'] = array
            (
                'keyword1' => array('value' => date('Y年m月d日 H:i', $order['cdate'])),
                'keyword2' => array('value' => '购买成功,您获得了 '.$journal['name'].' 的阅读资格!'),
            );
            $send['data'] = json_encode($send['data']);
            $send['form_id'] = Dever::load('act/lib/form')->get($uid, 2, $order['cate_id']);

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