<?php
/**
 * 三方平台测试
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */
namespace Home\Controller;
class Test3Controller extends BaseController {
    
        
    /**
      * 推广海报
      * @param $id 会员 $is_check 是否检测 1不检测 2检测
      * @return mixed
      */
    public function tgercode(){
        $id=I('get.id','1','intval');
        $result=tgercode($id,2);
        if($result['status']){
            echo '<img src="'.$result['url'].'"><br>'.$result['url'];
        }else{
            echo $result['msg'];
        }
    }    
    public function index(){        
        // 推送模板消息 领取微信优惠券
            $uid=34;
            $id=6;
            $map['id']=$uid;
            $member_info=M("Member")->where($map)->field('mobile,username,nickname')->find();
            $add_data['to']=$uid;//$member_info['mobile'];
            if($member_info['username']){
                $add_data['user_name']=$member_info['username'];
            }elseif($member_info['nickname']){
                $add_data['user_name']=$member_info['nickname'];
            }else{
                $add_data['user_name']=$member_info['mobile'];
            }
            $add_data['first'] = '领取卡券';
            $where['id']=$id;
            $add_data['type']='优惠券';
            $add_data['card_sn']=M("Coupons")->where($where)->getField('title');
            // $add_data['goods_name']=M("Coupons")->where($where)->getField('title');
            $add_data['send_time'] = date('Y-m-d H:i:s',NOW_TIME);
            $add_data['remark'] = "尊敬的会员，您获得一张卡券，请及时领取";
            $add_data['type'] = 4;
            $add_data['content'] = "请领取卡券";
            $url=U("Mobile/Index/coupons_give",array('id'=>$id,'uid'=>$uid),true,true);
            $data['list']=sendbytheme($add_data,"MEMBER_CARE_NEW",$url);
            $data['status']=1;
            echo "<pre/>";print_r($data);exit();
    }
    public function xcx_matter_code(){
        $uid = 1;$goods_id = 12;
        $invite_key = base64_encode($uid);
        $param=array();$param['width']=120;
        $param['type']=1;
        $param['path'] = "pages/takeout/cont/cont?invite_key=".$invite_key."&id=".$goods_id;//小程序参数
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $result=$wxsmall->get_location_small_code($param);
        preg_match('/(.*)base64,(.*)/', $result, $matches);
        $base64 = $matches['2'];
        $img = base64_decode($base64);
        $old_cache_f= ROOT_PATH."/uploads/".date("YmdHis")."wx_img.jpg";
        if(!empty($img)){@file_put_contents($old_cache_f,$img);}
        print_r($old_cache_f);exit;
    }  
    public function model_default(){
        $shop7='[{"content":{"showType":"1","space":"2","height":0,"nav_list":[{"link":{"is_param":"0","findname":"\u5546\u54c1\u8be6\u60c5","url_navtype":"7","url_modulename":"13","sonfindname":"\u5170\u598d\u7f8e\u81fb\u7f8e\u65f6\u5149\u6d01\u9762\u6ce1\u6ce1"},"is_url":false,"url":"url:7:13:","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190318\/5c8f4acd57595.jpg"},{"link":{"is_param":"0","findname":"\u5546\u54c1\u8be6\u60c5","url_navtype":"7","url_modulename":"14","sonfindname":"\u5170\u598d\u7f8e\u81fb\u7f8e\u65f6\u5149\u6c34"},"is_url":false,"url":"url:7:14:","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190318\/5c8f4ad1d30d4.png"}]},"data_type":7,"id":"2018317104113318"},{"content":{"nav_list":[{"link":{"is_param":"0","findname":"\u9886\u5238\u4e2d\u5fc3","url_navtype":"5"},"is_url":false,"url":"url:5::","is_param":false,"url_param":"","title":"\u9886\u5238\u4e2d\u5fc3","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190126\/5c4bad6b5a56f.png"},{"link":{"is_param":"0","findname":"\u9650\u65f6\u4fc3\u9500","url_navtype":"19"},"is_url":false,"url":"url:19::","is_param":false,"url_param":"","title":"\u9650\u65f6\u4fc3\u9500","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190126\/5c4bad71caa4b.png"},{"link":{"is_param":"1","findname":"\u6211\u7684\u8ba2\u5355","url_navtype":"2"},"is_url":false,"url":"url:2::","is_param":true,"url_param":"","title":"\u6211\u7684\u8ba2\u5355","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190126\/5c4bad8a91266.png"},{"link":{"is_param":"0","findname":"\u62fc\u56e2\u5217\u8868","url_navtype":"21"},"is_url":false,"url":"url:21::","is_param":false,"url_param":"","title":"\u62fc\u56e2\u8d2d","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190126\/5c4bada46268c.png"}]},"data_type":6,"id":"2018317112354319"},{"content":{"nav_list":[{"link":{"is_param":"0","findname":"\u65b0\u54c1","url_navtype":"30"},"is_url":false,"url":"url:30::","is_param":false,"url_param":"","title":"\u65b0\u54c1\u4e0a\u5e02","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190318\/5c8f4b07e7835.png"},{"link":{"is_param":"0","findname":"\u4f18\u60e0\u6d3b\u52a8","url_navtype":"17"},"is_url":false,"url":"url:17::","is_param":false,"url_param":"","title":"\u4f18\u60e0\u6d3b\u52a8","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190318\/5c8f4b1b41d92.png"},{"link":{"is_param":"0","findname":"\u4f1a\u5458\u4e2d\u5fc3","url_navtype":"20"},"is_url":false,"url":"url:20::","is_param":false,"url_param":"","title":"\u4f1a\u5458\u4e2d\u5fc3","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190318\/5c8f4b2d03041.png"},{"link":{"is_param":"0","findname":"\u6587\u7ae0\u5217\u8868","url_navtype":"27","url_modulename":"1","sonfindname":"\u5934\u6761"},"is_url":false,"url":"url:27:1:","is_param":false,"url_param":"","title":"\u7f8e\u5986\u5b66\u9662","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190318\/5c8f4b35bd43f.jpg"}]},"data_type":6,"id":"2019361852620"},{"content":{"height":20},"data_type":16,"id":"201912684939203"},{"content":{"showType":"2","space":"2","height":0,"nav_list":[{"link":"#","is_url":false,"url":"","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190125\/5c4ab5a9a1cf1.png"}]},"data_type":7,"id":"20191259841452"},{"content":{"layout":"2","showPrice":true,"showIco":true,"showName":true,"goodsNum":"18","firstPriority":1,"secondPriority":0,"thirdPriority":0,"is_timelimit":1,"goodslist":[{"goodsItem_id":"1","link":"#","img":"\/Public\/drag\/images\/goodsItem1.jpg","price":"\u00a5100.00","del_price":"\u00a5200.00","title":"\u7b2c\u4e00\u4e2a\u5546\u54c1"},{"goodsItem_id":"2","link":"#","img":"\/Public\/drag\/images\/goodsItem2.jpg","price":"\u00a5200.00","del_price":"\u00a5300.00","title":"\u7b2c\u4e8c\u4e2a\u5546\u54c1"},{"goodsItem_id":"3","link":"#","img":"\/Public\/drag\/images\/goodsItem3.jpg","price":"\u00a5300.00","del_price":"\u00a5400.00","title":"\u7b2c\u4e09\u4e2a\u5546\u54c1"},{"goodsItem_id":"4","link":"#","img":"\/Public\/drag\/images\/goodsItem4.jpg","price":"\u00a5400.00","del_price":"\u00a5500.00","title":"\u7b2c\u56db\u4e2a\u5546\u54c1"}]},"data_type":4,"id":"201912318355208"},{"content":{"showType":"2","space":"2","height":0,"nav_list":[{"link":"#","is_url":false,"url":"","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190125\/5c4a61bba975e.png"}]},"data_type":7,"id":"20191259923941"}]';
        echo "<pre/>"; print_r(json_encode($shop7,true));
        $shop8='[{"content":{"showType":"1","space":"2","height":0,"nav_list":[{"link":"#","is_url":false,"url":"","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190126\/5c4bbe9331260.jpg"},{"link":{"is_param":"0","findname":"\u5546\u54c1\u8be6\u60c5","url_navtype":"7","url_modulename":"53","sonfindname":"\u53e4\u9a70\uff08GUCCI\uff09\u7eda\u4e3d\u6800\u5b50\u6de1\u9999\u6c34 30ml"},"is_url":false,"url":"url:7:53:","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190307\/5c80bbe7064cb.png"},{"link":{"is_param":"0","findname":"\u5546\u54c1\u8be6\u60c5","url_navtype":"7","url_modulename":"54","sonfindname":"\u8fea\u5965\uff08Dior\uff09 \u3010\u9999\u6e2f\u76f4\u90ae\u3011\u8fea\u5965Dior\u771f\u6211\u5973\u58eb\u9999\u6c34 \u82b1\u6f3e\u751c\u5fc3\u5973\u58eb\u9999\u6c34"},"is_url":false,"url":"url:7:54:","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190307\/5c80bbfed24a0.png"},{"link":{"is_param":"0","findname":"\u5546\u54c1\u8be6\u60c5","url_navtype":"7","url_modulename":"18","sonfindname":"\u5c0f\u96c0\u5e78\u8865\u6c34\u7d27\u81f4\u9762\u819c30\u7247\u88c5"},"is_url":false,"url":"url:7:18:","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190307\/5c80bc29c91fb.png"}]},"data_type":7,"id":"2018317104113318"},{"content":{"nav_list":[{"link":{"is_param":"0","findname":"\u9886\u5238\u4e2d\u5fc3","url_navtype":"5"},"is_url":false,"url":"url:5::","is_param":false,"url_param":"","title":"\u9886\u5238\u4e2d\u5fc3","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190228\/5c77bf634e93e.png"},{"link":{"is_param":"0","findname":"\u9650\u65f6\u4fc3\u9500","url_navtype":"19","url_modulename":"10","sonfindname":"\u95f9\u5143\u5bb5"},"is_url":false,"url":"url:19:10:","is_param":false,"url_param":"","title":"\u9650\u65f6\u4fc3\u9500","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190228\/5c77bf6c71321.png"},{"link":{"is_param":"0","findname":"\u56e2\u957f\u63a8\u8350","url_navtype":"35"},"is_url":false,"url":"url:35::","is_param":false,"url_param":"","title":"\u56e2\u957f\u63a8\u8350","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190228\/5c77bf75a9791.png"},{"link":{"is_param":"0","findname":"\u62fc\u56e2\u5217\u8868","url_navtype":"21"},"is_url":false,"url":"url:21::","is_param":false,"url_param":"","title":"\u62fc\u56e2\u8d2d","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190228\/5c77bf821ee06.png"},{"link":{"is_param":"0","findname":"\u54c1\u724c\u9986","url_navtype":"33"},"is_url":false,"url":"url:33::","is_param":false,"url_param":"","title":"\u54c1\u724c\u9986","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190228\/5c77bf529199e.png"},{"link":{"is_param":"0","findname":"\u79ef\u5206\u5546\u57ce","url_navtype":"34"},"is_url":false,"url":"url:34::","is_param":false,"url_param":"","title":"\u79ef\u5206\u5546\u54c1","showtitle":"\u5bfc\u822a\u540d\u79f0","img":"\/uploads\/image\/20190314\/5c89f6924edb5.png"}]},"data_type":6,"id":"2018317112354319"},{"content":{"height":20},"data_type":16,"id":"201912684939203"},{"content":{"showType":"2","space":"2","height":0,"nav_list":[{"link":"#","is_url":false,"url":"","is_param":false,"url_param":"","title":"\u6807\u9898","showtitle":"\u6807\u9898","img":"\/uploads\/image\/20190125\/5c4ab5a9a1cf1.png"}]},"data_type":7,"id":"20191259841452"},{"content":{"layout":"4","showPrice":true,"showIco":true,"showName":true,"goodsNum":"18","firstPriority":1,"secondPriority":0,"thirdPriority":0,"is_timelimit":1,"goodslist":[{"goodsItem_id":"1","link":"#","img":"\/Public\/drag\/images\/goodsItem1.jpg","price":"\u00a5100.00","del_price":"\u00a5200.00","title":"\u7b2c\u4e00\u4e2a\u5546\u54c1"},{"goodsItem_id":"2","link":"#","img":"\/Public\/drag\/images\/goodsItem2.jpg","price":"\u00a5200.00","del_price":"\u00a5300.00","title":"\u7b2c\u4e8c\u4e2a\u5546\u54c1"},{"goodsItem_id":"3","link":"#","img":"\/Public\/drag\/images\/goodsItem3.jpg","price":"\u00a5300.00","del_price":"\u00a5400.00","title":"\u7b2c\u4e09\u4e2a\u5546\u54c1"},{"goodsItem_id":"4","link":"#","img":"\/Public\/drag\/images\/goodsItem4.jpg","price":"\u00a5400.00","del_price":"\u00a5500.00","title":"\u7b2c\u56db\u4e2a\u5546\u54c1"}]},"data_type":4,"id":"201912318355208"}]';
        echo "<pre/>"; print_r(json_encode($shop8,true));
        exit();
    }
	
	
	//gyc 2019.1.22
	function affiliate($order_id=20){
		$map = array();
		$map['id'] = $order_id;
		$order_info = M('OrderInfo')->where($map)->field('uid,lshop_id')->find();
		//1113
		$all_money=$all_profit=$all_commission=$fixed=0;//商品金额百分比//利润//固定金额百分比分佣//固定积分或金额时
		$goods_ids = unserialize(C('PRO_GOODS'));
		$g_ids = unserialize($goods_ids['temp']);
		$map = array();
		$map['order_id']=$order_id;
		$map['goods_id']=array('not in',$g_ids);
		$order_goods=M('OrderGoods')->alias('o')
			->where($map)
			->join('__GOODS__ as g on o.goods_id=g.id and g.is_referral>0')
			->field('sum(o.goods_price*o.goods_number) as all_money,sum((o.goods_price-o.supplier_price)*o.goods_number) as all_profit,sum(o.commission*o.goods_number) as all_commission,sum(o.goods_number) as fixed,o.is_referral')
			->group('o.is_referral')
			->select();
		$all_moneys = $all_profits = $fixeds = array();
		foreach ($order_goods as $k => $v) {
			if($v['is_referral'] == 0){//不分佣
				$return['status'] = 1;
				return $return;
			}elseif($v['is_referral'] == 1){//按系统设定
				$all_moneys[$k] = $v['all_money'];
				$all_profits[$k] = $v['all_profit'];
			}elseif($v['is_referral'] == 2){//按固定金额
				$all_moneys[$k] = $all_profits[$k] = $v['all_commission'];
			}
			$fixeds[$k] = $v['fixed']; //数量
		}
		$all_money = array_sum($all_moneys);
		$all_profit = array_sum($all_profits);
		$fixed = array_sum($fixeds);
		$affiliate = unserialize(C('AFFILIATE'));
		$aff_setting= unserialize(C('AFF_SETTING')); //jxr 2019.10.11 
		$pro_level = M('Config')->field('value')->where("name='AFFILIATE_THREE'")->find();
		$pro_level = unserialize($pro_level['value']);
		//判断分销内购
		if($affiliate['config']['is_ngfx']==1){
			$rank_info = ex_level($order_info['uid']);//内购三级
		}else $rank_info = ex_ranks($order_info['uid']);
		if(!empty($rank_info)){
			$pid = $rank_info[1]['id'];
			$ppid = $rank_info[2]['id'];
			$pppid = $rank_info[3]['id'];
		}
		foreach($pro_level['info'] as $k=>$value){
			if($pid && ($rank_info[1]['rank_id']==$value['zhi_level'] || $value['zhi_level']==0)){//一级
				if(($rank_info[2]['rank_id']==$value['jian_level'] || $value['jian_level']==0)){//二级判断
					if(($rank_info[3]['rank_id']==$value['t_level'] || $value['t_level']==0)){//三级判断
						//一级分的钱
						if($pid && ($rank_info[1]['rank_id'] >= $affiliate['config']['ex_level'])){
							if($affiliate['config']['fx_type']==1){//分现金
								if($aff_setting['config']['fx_money_type']==1){//订单金额
									$pid_money = format_decimal($all_money*$value['fen_money']/100);
								}else{//订单利润
									$pid_money = format_decimal($all_profit*$value['fen_money']/100);
								}
								if($pid_money>0){
									$log='被推荐人('.idtoname($order_info['uid'],'Member','username;nickname').')下单，将赠1级('.idtoname($pid,'Member','username;nickname').')推荐人'.' 余额：'.$pid_money;
								}
							}else{//分积分 
								if($aff_setting['config']['fx_money_type']==1){//订单金额
									$pid_point  = intval($all_money*$value['fen_money']/100);
								}else{//订单利润
									$pid_point  = intval($all_profit*$value['fen_money']/100);
								}
								if($pid_point>0){
									$log='被推荐人('.idtoname($order_info['uid'],'Member','username;nickname').')下单，将赠1级('.idtoname($pid,'Member','username;nickname').')推荐人'.' 积分：'.$pid_point;
								}
							}
							//有导购
							if($affiliate['config']['is_dg']==1){//开启导购分佣
								$dg_num = $affiliate['config']['dg_num'];//导购分佣占一级分佣百分比
								$dg_id =idtoname($order_info['lshop_id'],'LittleShop','uid');
								if($dg_id){
									$dg_log='推荐('.idtoname($order_info['uid'],'Member','username;nickname').')下单，将赠导购人('.idtoname($dg_id,'Member','username;nickname').')';
									if($pid_money>0){
										$dg_moeny =format_decimal($pid_money*$dg_num/100);
										$dg_log.=' 余额：'.$dg_moeny;
										$pid_money=format_decimal($pid_money-$dg_moeny);
									}
									if($pid_point>0){
										$dg_point = intval($pid_point*$dg_num/100);
										$dg_log.=' 积分：'.$dg_point;
										$pid_point=intval($pid_point-$dg_point);
									}
									if($dg_moeny>0 || $dg_point>0)affiliate_log($order_id,1,$dg_id,$dg_moeny,$dg_point,$dg_log);
								}
							}
							//一级分佣
							affiliate_log($order_id,1,$pid,$pid_money,$pid_point,$log);
						}
						
								
						if($aff_setting['config']['money_type']==1){//订单金额分佣
							if($affiliate['config']['fx_type']==1){//分现金
								if($aff_setting['config']['fx_money_type']==1){//订单金额
									if($ppid && ($rank_info[2]['rank_id'] >= $affiliate['config']['ex_level'])) $ppid_money = format_decimal($all_money*$value['sfen_money']/100);
									if($pppid && ($rank_info[3]['rank_id'] >= $affiliate['config']['ex_level'])) $pppid_money = format_decimal($all_money*$value['tfen_money']/100);
								}else{//订单利润
									if($ppid && ($rank_info[2]['rank_id'] >= $affiliate['config']['ex_level'])) $ppid_money = format_decimal($all_profit*$value['sfen_money']/100);
									if($pppid && ($rank_info[3]['rank_id'] >= $affiliate['config']['ex_level'])) $pppid_money = format_decimal($all_profit*$value['tfen_money']/100);
								}
							}else{//分积分 
								if($aff_setting['config']['fx_money_type']==1){//订单金额
									if($ppid && ($rank_info[2]['rank_id'] >= $affiliate['config']['ex_level'])) $ppid_point  = intval($all_money*$value['sfen_money']/100);
									if($pppid && ($rank_info[3]['rank_id'] >= $affiliate['config']['ex_level'])) $pppid_point  = intval($all_money*$value['tfen_money']/100);
								}else{//订单利润
									if($ppid && ($rank_info[2]['rank_id'] >= $affiliate['config']['ex_level'])) $ppid_point  = intval($all_profit*$value['sfen_money']/100);
									if($pppid && ($rank_info[3]['rank_id'] >= $affiliate['config']['ex_level'])) $pppid_point  = intval($all_profit*$value['tfen_money']/100);
								}
							}
						}else{//上级所获分佣*比例
							/* print_r(22); */
							if($affiliate['config']['fx_type']==1){//分现金
								if($ppid && ($rank_info[2]['rank_id'] >= $affiliate['config']['ex_level'])) $ppid_money = format_decimal($pid_money*$value['sfen_money']/100);
								if($pppid && ($rank_info[3]['rank_id'] >= $affiliate['config']['ex_level'])) $pppid_money = format_decimal($ppid_money*$value['tfen_money']/100);
							}else{
								if($ppid && ($rank_info[2]['rank_id'] >= $affiliate['config']['ex_level'])) $ppid_point  = intval($pid_point*$value['sfen_money']/100);
								if($pppid && ($rank_info[3]['rank_id'] >= $affiliate['config']['ex_level'])) $pppid_point  = intval($ppid_point*$value['tfen_money']/100);
							}
						}

						// $array = array();
						// $array['all_money'] = $all_money;
						// $array['all_profit'] = $all_profit;
						// $array['pid_money'] = $pid_money;
						// $array['ppid_money'] = $ppid_money;
						// $array['pppid_money'] = $pppid_money;
						// print_r($array);exit;
						if($affiliate['config']['fx_type']==1){//分现金
							if($ppid_money>0){
								$ppid_log='被推荐人('.idtoname($order_info['uid'],'Member','username;nickname').')下单，将赠2级('.idtoname($ppid,'Member','username;nickname').')推荐人'.' 余额：'.$ppid_money;
								affiliate_log($order_id,1,$ppid,$ppid_money,0,$ppid_log);
							}
							if($pppid_money>0){
								$pppid_log='被推荐人('.idtoname($order_info['uid'],'Member','username;nickname').')下单，将赠3级('.idtoname($pppid,'Member','username;nickname').')推荐人'.' 余额：'.$pppid_money;
								affiliate_log($order_id,1,$pppid,$pppid_money,0,$pppid_log);
							}
						}else{//分积分
							if($ppid_point>0){
								$ppid_log='被推荐人('.idtoname($order_info['uid'],'Member','username;nickname').')下单，将赠2级('.idtoname($ppid,'Member','username;nickname').')推荐人'.' 积分：'.$ppid_point;
								affiliate_log($order_id,1,$ppid,0,$ppid_point,$ppid_log);  
							}
							if($pppid_point>0){
								$pppid_log='被推荐人('.idtoname($order_info['uid'],'Member','username;nickname').')下单，将赠3级('.idtoname($pppid,'Member','username;nickname').')推荐人'.' 积分：'.$pppid_point;
								affiliate_log($order_id,1,$pppid,0,$pppid_point,$pppid_log);  
							}
						}
						$return['status'] = 1;
						return $return;
					}
				}
			} 
		}die;
	}
	
	
	function member_upgrade($id=58,$orderids_arr='43,16,44,42'){
        if(empty($id))return false;
        $member_info = M('Member')->where('id='.$id)->find();
        if(empty($member_info))return false;
        $map = array();
        $map['status']=1;
        $map['sort'] = array('gt',$member_info['rank_id']);
        $member_ranks = M('MemberRank')->where($map)->order('sort asc')->select();
        /* //购买指定商品金额
        if(!empty($orderids_arr)){
            $o_ids = $orderids_arr;
            $goods_ids = unserialize(C('PRO_GOODS'));
            $g_ids = unserialize($goods_ids['temp']);
            $map = array();
            $map['order_id']=array('in',$o_ids);
            $map['goods_id']=array('in',$g_ids);
            $buy_money = M('OrderGoods')->where($map)->sum('goods_price*goods_number');
        } */
		//jxr start 修改
		$ranks=array(2,3,4);
		$rank_num=array();
		//推荐会员晋升人数
		$map = array();
		$map['uid']=$id;
		$member_link = M('MemberRankLink')->where($map)->select();
		$new_array = array();
		if(!empty($member_link)){
			foreach ($member_link as $key => $value) {
				$new_array[$value['rank_id']]=$value;
				foreach($ranks as $k => $val){
					if(empty($rank_num[$val]))$rank_num[$val]=array('zhi_num'=>0,'jian_num'=>0,'all_num'=>0);
					if($val <= $value['rank_id']){
						$rank_num[$val]['zhi_num'] +=$value['zhi_num'];
						$rank_num[$val]['jian_num'] +=$value['jian_num'];
						
						$rank_num[$val]['all_num'] +=$value['all_num'];
					}
				}
			}
		}
		//jxr end 修改
        if(!empty($member_ranks)){
            foreach ($member_ranks as $key => $value) {
                $level_pro=unserialize($value['level_pro']);
                 // print_r($level_pro);exit;
                // 购买指定商品晋升
                if($level_pro['pro_type']==1){
                    if($level_pro['type_buy']==1){
						//购买指定商品金额
						if(!empty($orderids_arr)){
							$o_ids = $orderids_arr;
							$goods_ids = unserialize(C('PRO_GOODS'));
							$g_ids = unserialize($goods_ids['temp']);
							//该等级的晋升商品	jxr 2019.10.12  start 
							$map = array();
							$map['rank_jinsheng']=array('in','0,'.$value['id']);
							$map['id']=array('in',$g_ids);
							$gids=M('Goods')->where($map)->getField('id',true);
							//该等级的晋升商品	jxr 2019.10.12  end
							$map = array();
							$map['order_id']=array('in',$o_ids);
							$map['goods_id']=array('in',$gids);
							$buy_money = M('OrderGoods')->where($map)->sum('goods_price*goods_number');
						}
                        if($buy_money>=$level_pro['buy_money']) $return['pro_status']=1;
                        else $return['pro_status']=0;
                    }
                }
                //消费满额晋升
                if($level_pro['pro_type']==2){
                    if($level_pro['one_buy']==1){
                        $one_buy_money = $member_info['one_buy_count'];
                        if($one_buy_money>=$level_pro['one_buy_money']) $res['one_buy']=1;
                        else $res['one_buy']=0;
                    }else{
                        $res['one_buy']=1;
                    }
                    if($level_pro['zhi_buy']==1){
                        $zhi_buy_money = $member_info['zhi_buy_count'];
                        if($zhi_buy_money>=$level_pro['zhi_buy_money']) $res['zhi_buy']=1;
                        else $res['zhi_buy']=0;
                    }else{
                        $res['zhi_buy']=1;
                    }
                    if($level_pro['jian_buy']==1){
                        $jian_buy_money = $member_info['jian_buy_count'];
                        if($jian_buy_money>=$level_pro['jian_buy_money']) $res['jian_buy']=1;
                        else $res['jian_buy']=0;
                    }else{
                        $res['jian_buy']=1;
                    }
                    if($res['one_buy']==1 && $res['zhi_buy']==1 && $res['jian_buy']==1){
                        $return['pro_status']=1;
                    }else{
                        $return['pro_status']=0;
                    }
                }
                //推荐会员晋升
                if($level_pro['pro_type']==3){
                    
                    if($level_pro['zhi_tui']==1){
                        $link_info = $rank_num[$level_pro['zhi_level']];
                        if($link_info['zhi_num']>=$level_pro['zhi_level_num']) $res['zhi_tui']=1;
                        else $res['zhi_tui']=0;
                        
                    }else{
                        $res['zhi_tui']=1;
                    }
                    if($level_pro['jian_tui']==1){
                        $link_info = $rank_num[$level_pro['jian_level']];
                        if($link_info['jian_num']>=$level_pro['jian_level_num']) $res['jian_tui']=1;
                        else $res['jian_tui']=0;
                    }else{
                        $res['jian_tui']=1;
                    }
                    if($level_pro['all_tui']==1){
						$link_info = $rank_num[$level_pro['all_level']];
                        if($link_info['all_num']>=$level_pro['all_level_num']) $res['all_tui']=1;
                        else $res['all_tui']=0;
                    }else{
                        $res['all_tui']=1;
                    }
                    if($res['zhi_tui']==1 && $res['jian_tui']==1){
                        $return['pro_status']=1;
                    }else{
                        $return['pro_status']=0;
                    }
                } 
				// 或者条件 jxr
				if(!empty($level_pro_plus)){
					if($level_pro_plus['zhi_tui_plus']==1){
						$link_info_plus = $rank_num[$level_pro_plus['zhi_level_plus']];
						if($link_info_plus['zhi_num']>=$level_pro_plus['zhi_level_num_plus']) $res['zhi_tui_plus']=1;
						else $res['zhi_tui_plus']=0;
					}
					if($res['zhi_tui_plus']==1){
						$return['pro_status_plus']=1;
					}else{
						$return['pro_status_plus']=0;
					}
				}
				
				if($return['pro_status']==1 || $return['pro_status_plus']==1){
					$return['rank_id'][]=$value['id'];break;
				}
            }
              $inf =array_filter($return['rank_id']);
              if(!empty($inf)){
                $rrr = change_rank_link($id,$inf[0],$member_info['rank_id']);
                $rest = pro_affiliate($id,$inf[0]);
                // print_r($rrr);exit;
                $save['rank_id'] = $inf[0];
                $save['is_littleshop']=1;
				if($inf[0]==2){
					$save['js_vip_time']=NOW_TIME;//jxr end 2019.05.31
				}
                $map = array();
                $map['id'] = $id;
                $result=M('Member')->where($map)->save($save);

                $map = array();
                $map['uid']=$id;
                $link_save['u_rank']=$inf[0];
                M('MemberRankLink')->where($map)->save($link_save);

                $map = array();
                $map['uid']=$id;
                $little_count = M('LittleShop')->where($map)->count();
                if($little_count==0){
                    $little_shop['uid']=$id;
                    $little_shop['addtime']=NOW_TIME;
                    $little_shop['status']=1;
                    M('LittleShop')->add($little_shop);
                }
                $pid = idtoname($id,'Member','parent_id');
				member_upgrade($pid);
                /* $ppid = idtoname($pid,'Member','parent_id');
                if(!empty($pid))$parent_ids[]=$pid;
                if(!empty($ppid))$parent_ids[]=$ppid;
                foreach ($parent_ids as $key => $va) {
                    member_upgrade($va);
                } */
                
              }
        }
    }
	
	
	
	
	//gyc 2019.1.27
    function pro_affiliate($uid=58,$rank_id=2){
        if(empty($uid) || empty($rank_id))return false;
        $affiliate = unserialize(C('AFFILIATE'));
		$pro_setting= unserialize(C('PRO_SETTING')); //jxr 2019.10.11 
        $pro_level = M('Config')->field('value')->where("name='PRO_LEVEL'")->find();
        $pro_level = unserialize($pro_level['value']);
        // print_r($pro_level);exit;
        $member_rank=idtoname($rank_id,'MemberRank','*');//自己等级信息
        $level_pro = unserialize($member_rank['level_pro']);
		// print_r($level_pro);die;
        $pid = idtoname($uid,"Member",'parent_id');
        $ppid = idtoname($pid,"Member",'parent_id');
        $pppid = idtoname($ppid,"Member",'parent_id');
        $pid_rank = idtoname($pid,"Member",'rank_id');
        $ppid_rank = idtoname($ppid,"Member",'rank_id');
        $pppid_rank = idtoname($pppid,"Member",'rank_id');
        if($level_pro['pro_type']==1){//晋升奖励 百分比  或者固定值
            foreach($pro_level['info'] as $k=>$value){
                // print_r($value);exit;
				if($rank_id==$value['one_level'] || $value['one_level']==0){
					if( ($pid_rank==$value['zhi_level']) || $value['zhi_level']==0 ){
						if( ($ppid_rank==$value['jian_level']) || $value['jian_level']==0 ){
							if( ($pppid_rank==$value['san_level']) || $value['san_level']==0 ){
								if($pid && $pro_setting['config']['fx_money_type']==3) $arr['pid_money'] = format_decimal($value['fen_money']); //固定值
								if($pid && $pro_setting['config']['fx_money_type']==1) $arr['pid_money'] = format_decimal($level_pro['buy_money']*$value['fen_money']/100); //百分比
								if($ppid && $pro_setting['config']['fx_money_type']==3) $arr['ppid_money'] = format_decimal($value['sfen_money']); //固定值
								if($ppid && $pro_setting['config']['fx_money_type']==1) $arr['ppid_money'] = format_decimal($level_pro['buy_money']*$value['sfen_money']/100);//百分比
								if($pppid && $pro_setting['config']['fx_money_type']==3) $arr['pppid_money'] = format_decimal($value['ssfen_money']); //固定值
								if($pppid && $pro_setting['config']['fx_money_type']==1) $arr['pppid_money'] = format_decimal($level_pro['buy_money']*$value['ssfen_money']/100);//百分比
							}
						}
					}
				}
               /*  if( ($pid_rank==$value['zhi_level']) && ($value['jian_level']!=0  && $ppid_rank==$value['jian_level']) ){
                    if($pid && $pro_setting['fx_money_type']==3) $arr['pid_money'] = format_decimal($value['fen_money']); //固定值
					if($pid && $pro_setting['fx_money_type']==1) $arr['pid_money'] = format_decimal($level_pro['buy_money']*$value['fen_money']/100); //百分比
                    if($ppid && $pro_setting['fx_money_type']==3) $arr['ppid_money'] = format_decimal($value['sfen_money']); //固定值
					if($ppid && $pro_setting['fx_money_type']==1) $arr['ppid_money'] = format_decimal($level_pro['buy_money']*$value['sfen_money']/100);//百分比
					if($pppid && $pro_setting['fx_money_type']==3) $arr['ppid_money'] = format_decimal($value['ssfen_money']); //固定值
					if($pppid && $pro_setting['fx_money_type']==1) $arr['ppid_money'] = format_decimal($level_pro['buy_money']*$value['ssfen_money']/100);//百分比
                }
                if( ($pid_rank==$value['zhi_level']) &&  ($value['jian_level']==0) ){
                    if($pid) $arr['pid_money'] = format_decimal($level_pro['buy_money']*$value['fen_money']/100);
                } */
            }
        }else{//分上一级 固定值
            if($member_rank['sort']>=$pro_level['item']['zi_one_level']){
                $pid_sort = idtoname($pid_rank,'MemberRank','sort');
                if($pid && ($pid_sort>=$pro_level['item']['zi_jian_level'])  ){  
                    $arr['pid_money'] = $pro_level['item']['zi_fen_money'];
                    if($pro_level['item']['fen_type']==2){
                        $arr['point']=1; 
                    }
                }
            }
        }
        if($pid && $arr['pid_money']){
            /* if($arr['point']==1){//分积分
                $point = $arr['pid_money'];
            }else{//分现金 */
                $money  = $arr['pid_money'];
            /* } */
            $log='被推荐人('.idtoname($uid,'Member','username;nickname').')等级晋升，将赠1级推荐人('.idtoname($pid,'Member','username;nickname').')';
            if($money>0){
              $log.=' 余额：'.$money;
              affiliate_log('',1,$pid,$money,0,$log,1); 
              account_change($pid,$money,8,15,'被推荐人'.idtoname($uid,'Member','username;nickname').'等级晋升，1级推荐人得到佣金分成');
            }
            /* if($point>0){
              $log.=' 积分：'.$point;
              affiliate_log('',1,$pid,$money,$point,$log,1); 
              account_change($v['uid'],$v['point'],4,15,'被推荐人'.idtoname($uid,'Member','username;nickname').'等级晋升，1级推荐人得到佣金分成');
            } */
            $return['status'] = 1;
        }
        if($ppid && $arr['ppid_money']){  
            $money = $arr['ppid_money'];
            $log='被推荐人('.idtoname($uid,'Member','username;nickname').')等级晋升，将赠2级推荐人('.idtoname($ppid,'Member','username;nickname').')';
            if($money>0){
              $log.=' 余额：'.$money;
              affiliate_log('',1,$ppid,$money,0,$log,1); 
              account_change($ppid,$money,8,15,'被推荐人'.idtoname($uid,'Member','username;nickname').'等级晋升，2级推荐人得到佣金分成');
            }
            
            $return['status'] = 1;
        }
		if($pppid && $arr['pppid_money']){  
            $money = $arr['pppid_money'];
            $log='被推荐人('.idtoname($uid,'Member','username;nickname').')等级晋升，将赠3级推荐人('.idtoname($pppid,'Member','username;nickname').')';
            if($money>0){
              $log.=' 余额：'.$money;
              affiliate_log('',1,$pppid,$money,0,$log,1); 
              account_change($pppid,$money,8,15,'被推荐人'.idtoname($uid,'Member','username;nickname').'等级晋升，3级推荐人得到佣金分成');
            }
            $return['status'] = 1;
        }
        // print_r($return);exit;
    }

	
}