<?php

if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}




if(!$_G['uid']){
    echo json_encode(array('code' => 0, 'msg' => 'no login'));
    exit;
}


if($_GET['action'] == 'lottery'){

    file_put_contents('1.txt', '11');

    $gid = intval($_GET['gid']);
    if($gid < 1){
        echo json_encode(array('code' => 0, 'msg' => 'no goods'));
        exit;
    }
    $sql = "SELECT * FROM mocuz_card_score_goods WHERE id = $gid";
    $goods = DB::fetch_first($sql);

    if($goods['num']<=0)
    {
        echo json_encode(array('code' => 2, 'msg' => 'num shortage'));
        exit; 
    }


    if($goods['type'] != 2){
        echo json_encode(array('code' => 0, 'msg' => 'not lottery'));
        exit;
    }

    //限制兑换次数
    $sql = "SELECT * FROM mocuz_card_score_exchange WHERE uid = {$_G['uid']} AND gid=$gid ";
    $query = DB::query($sql);
    $exchangeNum = intval(DB::num_rows($query));

    if($exchangeNum >= $goods['exchange_num'])
    {
        echo json_encode(array('code' => 5, 'msg' => 'over exchange nums'));
        exit;
    }

    $sql = "SELECT * FROM mocuz_card_score_exchange WHERE uid = {$_G['uid']} AND gid=$gid AND status=2";

    $info = DB::fetch_first($sql);
    if(!empty($info)){
        echo json_encode(array('code' => 4, 'eid' => $info['id'], 'gid' => $info['gid']));
        exit;
    }


    $status = 1;
    if($goods['cate'] == 2){
        $status = 2;
    }

    $sql = "SELECT type FROM mocuz_card_score_shopinfo";
    $shopinfo = DB::fetch_first($sql);
    $count = DB::fetch_first("SELECT * FROM ".DB::table('common_member_count')." WHERE uid={$_G['uid']}");
    $extcredits = 'extcredits'.$shopinfo['type'];
    $now_count = $count[$extcredits];

    if($now_count - $goods['score'] < 0){
        echo json_encode(array('code' => 3, 'msg' => 'no score'));
        exit;
    }

    updatemembercount($_G['uid'], array($extcredits => '-'.$goods['score']), true, '', 0, '');
    credit_log($_G['uid'],'moc',0,array($extcredits => '-'.$goods['score']), 'score');




    $num_range = $goods['chance'] * 10000;
    $total = 10000;
    $number = mt_rand(1, $total);

    if($number <= $num_range){


        $time = time();
        $sql = "INSERT INTO mocuz_card_score_exchange 
                SET `gid`='$gid' , 
                    `uid`='{$_G['uid']}' , 
                    `score`='{$goods['score']}' , 
                    `exchange_time`='{$time}' , 
                    `status`='$status' , `extra`='' , `cate`='{$goods['cate']}' , `type`='{$goods['type']}'";
        DB::query($sql);
        $id = mysql_insert_id();
        if($goods['num'] - 1 == 0){
            $sql = "UPDATE mocuz_card_score_goods SET num=num-1,status=0 WHERE id=$gid";
        }else{
            $sql = "UPDATE mocuz_card_score_goods SET num=num-1 WHERE id=$gid";
        }
       
        DB::query($sql);
        echo json_encode(array('code' => 1, 'data' => $id, 'status' =>$status));


        exit;
    }else{
        echo json_encode(array('code' => 1, 'data' => 0));
        exit;
    }
}

if($_GET['action'] == 'exchange'){



    unset($_SESSION['hash']);

    $extra = isset($_GET['extra']) ? trim($_GET['extra']) : '';
    $rextra = isset($_GET['extra']) ? trim($_GET['rextra']) : '';



    if(!empty($extra) && $extra != $rextra){
        echo 'extra error';
        exit;
    }






    $gid = intval($_GET['gid']);
    $sql = "SELECT * FROM mocuz_card_score_goods WHERE id = $gid";
    $goods = DB::fetch_first($sql);


    if($goods['num']<=0)
    {
        echo json_encode(array('code' => 2, 'msg' => 'num shortage'));
        exit;
    }

    if($goods['cate'] == 2 && empty($extra)){
        echo 'extra empty';
        exit;
    }

    if($goods['type'] != 1){
        echo 'type error';
        exit;
    }

    //限制兑换次数
    $sql = "SELECT * FROM mocuz_card_score_exchange WHERE uid = {$_G['uid']} AND gid=$gid ";
    $query = DB::query($sql);

    $exchangeNum = intval(DB::num_rows($query));

    if($exchangeNum >= $goods['exchange_num'])
    {
        echo json_encode(array('code' => 5, 'msg' => 'over exchange nums'));
        exit;
    }


    $sql = "SELECT type FROM mocuz_card_score_shopinfo";
    $shopinfo = DB::fetch_first($sql);
    $count = DB::fetch_first("SELECT * FROM ".DB::table('common_member_count')." WHERE uid={$_G['uid']}");

    $extcredits = 'extcredits'.$shopinfo['type'];
    $now_count = $count[$extcredits];

    if($now_count - $goods['score'] < 0){
        echo json_encode(array('code' => 3, 'msg' => 'no score'));
        exit;
    }






    updatemembercount($_G['uid'], array($extcredits => '-'.$goods['score']), true, '', 0, '');
    credit_log($_G['uid'],'moc',0,array($extcredits => '-'.$goods['score']), 'score');




    $time = time();
    $sql = "INSERT INTO mocuz_card_score_exchange 
                SET `gid`='$gid' , 
                    `uid`='{$_G['uid']}' , 
                    `score`='{$goods['score']}' , 
                    `exchange_time`='{$time}' , 
                    `status`='1' , `extra`='{$extra}' , `cate`='{$goods['cate']}' , `type`='{$goods['type']}'";
    DB::query($sql);
    $id = mysql_insert_id();
    if($goods['num'] - 1 == 0){
        $sql = "UPDATE mocuz_card_score_goods SET num=num-1,status=0 WHERE id=$gid";
    }else{
        $sql = "UPDATE mocuz_card_score_goods SET num=num-1 WHERE id=$gid";
    }
    DB::query($sql);
    
    file_put_contents('2.txt', $id.'-'.$sql."\r\n", FILE_APPEND );
    echo json_encode(array('code' => 1, 'data' => $id, 'gid' => $gid));
    exit;
}


?>