<?php

/**

 * 发卡小程序模块小程序支付

 *

 * @author huichuang

 * @url 

 */

define('IN_MOBILE', true);

require '../../framework/bootstrap.inc.php';

//$postStr = $GLOBALS['HTTP_RAW_POST_DATA']; // 这里拿到微信返回的数据结果
$postStr = file_get_contents('php://input');
libxml_disable_entity_loader(true);

$xmlstring = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$getData = json_decode(json_encode($xmlstring),true);

if ($getData['total_fee'] && $getData['result_code'] == 'SUCCESS') {

    $osn = trim($getData['out_trade_no']);
    $res = pdo_get('hcgroup_order',array('orderno'=>$osn));
    if ($res && $res['status'] == 1)
    {
        $group = pdo_get('hcgroup_group', array('id'=>$res['groupid']));
        if($group['neednum']-$group['buynum']==1){
            $data['status'] = 2;
        }
        $data['buynum'] = $group['buynum']+1;
        //$data['endtime'] = time();
        //更新团购数量
        pdo_update('hcgroup_group', $data, array('id'=>$res['groupid']));
        //更新订单状态
    	pdo_update('hcgroup_order', array('status'=>2,'isshare'=>1,'paytime'=>time()), array('orderno'=>$osn));
        //更新销量
        $goods_info = pdo_get('hcgroup_goods',array('goods_id'=>$res['goods_id']),array('sales_num'));
        pdo_update('hcgroup_goods', array('sales_num'=>$goods_info['sales_num']+$res['number']),array('goods_id'=>$res['goods_id']));
        //判断是否成团
        $groupid = $res['groupid'];
        if($group['neednum']-$group['buynum']==1){
            pdo_update('hcgroup_order', array('isshare'=>2), array('groupid'=>$groupid));

            //更新一分抽奖活动参与信息
            if(!empty($res['actno'])){
                $group_userid = pdo_getall('hcgroup_order', array('groupid'=>$res['groupid']));
                //
                foreach ($group_userid as $key => $val) {
                    if($key==0) $userids = $val['user_id'];
                    else $userids .= ','.$val['user_id'];
                }
                $activ = pdo_get('hcgroup_activ',array('actno'=>$res['actno']));
                if(empty($activ['user_ids'])){
                    $act['user_ids']  = $userids;
                }else{
                    $act['user_ids'] = $activ['user_ids'].','.$userids;
                }
                $act['currnum'] = $activ['currnum'] + count($group_userid);
                pdo_update('hcgroup_activ', $act ,array('uniacid'=>$res['uniacid'],'actno'=>$res['actno']));
            }


            $account = pdo_get('account_wxapp',array('uniacid'=>$res['uniacid']),array('key','secret'));
            $token = $_COOKIE["access_token"];
            if (!$token) {
                $ress = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$account['key'].'&secret='.$account['secret']);
                $ress = json_decode($ress, true);
                $token = $ress['access_token'];
                setcookie("access_token", $token, time()+7200);

            }
            $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$token;

            $group = pdo_get('hcgroup_group',array('id'=>$groupid));
            $starttime = date('Y-m-d H:i:s',$group['starttime']);
            $endtime = date('Y-m-d H:i:s',$group['endtime']);
            $title = pdo_getcolumn('hcgroup_goods',array('goods_id'=>$group['goods_id']),array('title'));

            $info = pdo_fetchall("SELECT fact_amount,orderno,package,openid FROM ".tablename('hcgroup_order')." as o left join ".tablename('hcgroup_member')." as m ON o.user_id=m.id WHERE o.groupid = :groupid AND o.uniacid=:uniacid" , array(':groupid'=>$groupid,':uniacid' => $res['uniacid']));

            foreach ($info as $key => $val) {
                $data['touser']=$val['openid'];
                $tpl_msg = pdo_getcolumn('hcgroup_set',array('uniacid'=>$res['uniacid']),array('tpl_msg'));
                $tpl_msg = json_decode($tpl_msg,true);
                $data['template_id'] = $tpl_msg['group_success'];

                $data['form_id']=$val['package'];
              
                $data['data']['keyword1']['value']=$title;
                $data['data']['keyword1']['color']='#173177';
              
                $data['data']['keyword2']['value']=$val['fact_amount'];
                $data['data']['keyword2']['color']='#173177';
              
                $data['data']['keyword3']['value']=$starttime;
                $data['data']['keyword3']['color']='#173177';
              
                $data['data']['keyword4']['value']=$endtime;
                $data['data']['keyword4']['color']='#173177';
              
                $data['data']['keyword5']['value']='如果未按承诺时间发货，系统将按照规则进行赔偿。';
                $data['data']['keyword5']['color']='#173177';

                $data['page'] = 'hc_groups/pages/order/list/list?cid=WAIT_SEND';
                $json = json_encode($data);
                //执行
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
                $output = curl_exec($ch);
                curl_close($ch);
                unset($json);
                //file_put_contents(dirname(__FILE__)."/cert/1.txt",$output);
            }
        }
    }
    echo 'success';
    return ;
}
