<?php
/**
 * 会员等级管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoli <1094186793@sina.cn>
 */

namespace Admin\Controller;
class MemberRankController extends BaseController{
	public function _initialize()
    {
        parent::_initialize();
		$sortData = array('1'=>'等级1(最低)','2'=>'等级2','3'=>'等级3','4'=>'等级4','5'=>'等级5','1'=>'等级1(最低)','6'=>'等级6','7'=>'等级7','8'=>'等级8','9'=>'等级9','10'=>'等级10(最高)');
		$this->assign('sortData',$sortData);
		$this->assign('check',C('GRADE_CHOICES'));
    }
     public function _before_edit(){
    	$level=M('MemberRank')->where('status=1')->select();
        foreach ($level as $key => $value) {
            $new[$value['id']]=$value['name'];
        }
        $this->assign('level', $new);
        $postdata = I('param.');
		
        // $temp = M('MemberRank')->where('id='.$postdata['id'])->getField('level_pro');
        // $info = unserialize($temp);
		//jxr start 2019.05.22
        $temp = M('MemberRank')->where('id='.$postdata['id'])->field('level_pro,level_pro_plus')->find($postdata['id']);
        $info = unserialize($temp['level_pro']);
		$info_plus = unserialize($temp['level_pro_plus']);
		//jxr end 2019.05.22
		$postdata['level_pro_plus']=serialize($temp_plus);//jxr 2019.05.22
		
        $this->assign($info);
		$this->assign($info_plus);
         // print_r($info);
    }
    public function _before_add(){
    	$level=M('MemberRank')->where('status=1')->select();
        foreach ($level as $key => $value) {
            $new[$value['id']]=$value['name'];
        }
        $this->assign('level', $new);
         // print_r($info);
    }
    //添加会员等级
    public function insert(){
		$postdata=I('post.');
		$this->judge($postdata);
		$temp = array(
			'pro_type' => $postdata['pro_type'], //晋升方式
            'type_buy' => $postdata['type_buy'],      //购买指定商品
            'buy_money' => $postdata['buy_money'],        //购买指定商品满足金额
            'one_buy' => $postdata['one_buy'],   //自己消费
            'one_buy_money' => $postdata['one_buy_money'],    //自己消费满足金额
            'zhi_buy' => $postdata['zhi_buy'],    //直推消费
            'zhi_buy_money' => $postdata['zhi_buy_money'],    //直推消费满足金额
            'jian_buy' => $postdata['jian_buy'], //间推消费
            'jian_buy_money' => $postdata['jian_buy_money'],   //间推消费满足金额
            'zhi_tui' => $postdata['zhi_tui'],  //直推会员晋升
            'zhi_level'=>$postdata['zhi_level'],//直推会员等级要求
            'zhi_level_num' => $postdata['zhi_level_num'],  //直推对应等级会员人数
            'jian_tui' => $postdata['jian_tui'],   //间推推会员晋升
            'jian_level' => $postdata['jian_level'],  //间推会员等级要求
            'jian_level_num' => $postdata['jian_level_num'],   //间推对应等级会员人数
            'all_tui'=>$postdata['all_tui'],//全部下级会员晋升
            'all_level' => $postdata['all_level'],  //全部下级会员等级要求
            'all_level_num'=>$postdata['all_level_num'],//全部下级对应等级会员人数
        );
		//jxr start 2019.05.22
		$temp_plus = array(
			
            'zhi_tui_plus' => $postdata['zhi_tui_plus'],  //直推会员晋升
            'zhi_level_plus'=>$postdata['zhi_level_plus'],//直推会员等级要求
            'zhi_level_num_plus' => $postdata['zhi_level_num_plus'],  //直推对应等级会员人数
        );
		//jxr end 2019.05.22
		$postdata['level_pro']=serialize($temp);
		$postdata['level_pro_plus']=serialize($temp_plus);//jxr 2019.05.22
		$add = array();
		$add['name']=$postdata['name'];
		$add['goods_price']=$postdata['goods_price'];
		$add['member_weight']=$postdata['member_weight'];
		$add['member_level']=$postdata['member_level'];
		$add['rank_img']=$postdata['rank_img'];
		$add['sort']=$postdata['sort'];
		$add['shop_qrcode']=$postdata['shop_qrcode'];
		$add['level_pro']=serialize($temp);
		$add['level_pro_plus']=serialize($temp_plus);//jxr 2019.05.22
		$map['id']=$postdata['id'];
		$res = M('MemberRank')->where($map)->add($add);
		$jumpUrl = I('forward') ? I('forward') : U(CONTROLLER_NAME.'/index');
		if($res) $this->success('添加成功',$jumpUrl);
    	else $this->error('添加失败',$jumpUrl);
    }
	//编辑会员等级
    public function update(){
		$postdata=I('post.');
		// print_r($postdata);exit;
		$this->judge($postdata);
		$temp = array(
			'pro_type' => $postdata['pro_type'], //晋升方式
            'type_buy' => $postdata['type_buy'],      //购买指定商品
            'buy_money' => $postdata['buy_money'],        //购买指定商品满足金额
            'one_buy' => $postdata['one_buy'],   //自己消费
            'one_buy_money' => $postdata['one_buy_money'],    //自己消费满足金额
            'zhi_buy' => $postdata['zhi_buy'],    //直推消费
            'zhi_buy_money' => $postdata['zhi_buy_money'],    //直推消费满足金额
            'jian_buy' => $postdata['jian_buy'], //间推消费
            'jian_buy_money' => $postdata['jian_buy_money'],   //间推消费满足金额
            'zhi_tui' => $postdata['zhi_tui'],  //直推会员晋升
            'zhi_level'=>$postdata['zhi_level'],//直推会员等级要求
            'zhi_level_num' => $postdata['zhi_level_num'],  //直推对应等级会员人数
            'jian_tui' => $postdata['jian_tui'],   //间推推会员晋升
            'jian_level' => $postdata['jian_level'],  //间推会员等级要求
            'jian_level_num' => $postdata['jian_level_num'],   //间推对应等级会员人数
            'all_tui'=>$postdata['all_tui'],//全部下级会员晋升
            'all_level' => $postdata['all_level'],  //全部下级会员等级要求
            'all_level_num'=>$postdata['all_level_num'],//全部下级对应等级会员人数百分比
        );
		//jxr start 2019.05.22
		if(empty($postdata['zhi_tui_plus']))$postdata['zhi_tui_plus']=0;
		$temp_plus = array(
			'zhi_tui_plus' => $postdata['zhi_tui_plus'],  //直推会员晋升
			'zhi_level_plus'=>$postdata['zhi_level_plus'],//直推会员等级要求
			'zhi_level_num_plus' => $postdata['zhi_level_num_plus'],  //直推对应等级会员人数
		);
		//jxr end 2019.05.22
		$postdata['level_pro']=serialize($temp);
		$postdata['level_pro_plus']=serialize($temp_plus);//jxr 2019.05.22
		$save = array();
		$save['name']=$postdata['name'];
		$save['goods_price']=$postdata['goods_price'];
		$save['member_weight']=$postdata['member_weight'];
		$save['member_level']=$postdata['member_level'];
		$save['rank_img']=$postdata['rank_img'];
		$save['sort']=$postdata['sort'];
		$save['shop_qrcode']=$postdata['shop_qrcode'];
		$save['level_pro']=serialize($temp);
		if($temp_plus)$save['level_pro_plus']=serialize($temp_plus);//jxr 2019.05.22
		//jxr start 2019.05.22
		$save['trank_num']=$postdata['trank_num'];
		$save['js_money']=$postdata['js_money'];
		$save['fg_money']=$postdata['fg_money'];
		//jxr end 2019.05.22
		$map['id']=$postdata['id'];
		$res = M('MemberRank')->where($map)->save($save);
		$jumpUrl = I('forward') ? I('forward') : U(CONTROLLER_NAME.'/index');
		if($res) $this->success('修改成功',$jumpUrl);
    	else $this->error('修改失败',$jumpUrl);
		
    }
	//添加会员等级
  //   public function _before_insert(){
		// $postdata=I('post.');
		// $this->judge($postdata);
  //   }
	//编辑会员等级
  //   public function _before_update(){
		// $postdata=I('post.');
		// $this->judge($postdata);
  //   }
	//删除会员等级
	public function _before_delete(){
		$id=I('id');
		if(isset($id)&&is_array($id)){
			foreach($id as $k=>$v){
				$result=check_delete($v,1,'rank');
				if(!$result['status'])unset($id[$k]);
			}
			if(empty($id)){$this->error('会员已绑定等级,不可删除');exit;}
		}else{
			$result=check_delete($id,1,'rank');
			if(!$result['status']){$this->error($result['info']);exit;}
		}
		$this->id=$id;
	} 
	//对等级积分进行判断
	private function judge($postdata){
		$name=$postdata['name'];
		$nmap['name']=$name;
		$nmap['id']=array('neq',$postdata['id']);
		$ncount=M('MemberRank')->where($nmap)->count();
		if($ncount>0)$this->error('会员等级名称已存在！');
		if(!$postdata['sort'])$this->error('请选择级别！');
        $map['id']=array('neq',$postdata['id']);
        $map['sort']=$postdata['sort'];
        $result = M('MemberRank')->where($map)->count();
        if ($result>0){$this->error('该等级已经存在！');}
	}
	public function update_change(){
		$postdata = I('param.');
		$postdata=I('post.');
		unset($postdata['__hash__']);
		R('Admin/Config/save',array($postdata));
		$this->success('保存成功！');
	}
	public function search_goods(){
    	$param=I('param.');
    	$map = array();
    	$map['goods_name']=array('like',"%".$param['keyword']."%");
        $map['status']=1;
    	$map['is_delete']=0;
        $map['is_offline']=1;//线上商品
    	$g_list = M('Goods')->where($map)->select();
    	// print_r($g_list);exit;
    	foreach($g_list as $k=>$v){
            $result.="<option value='".$v['id']."'>".$v['goods_name']."</option>"; 
        }
        echo $result;exit;
    }
    public function add_goods(){
    	$param=I('param.');
    	// print_r($param);exit;
    	$goods_map['id'] = $param['id'];
    	$res = M('Goods')->where($goods_map)->find();
    	$this->assign($res);
    	$ress = $this->fetch('goods_list');
    	$return['data']=$ress;
    	$return['status']=1;
    	$this->ajaxReturn($return);
    }
    public function update_goods(){
    	$param=I('param.');
    	$ids = array_unique($param['goods_id']);
        $goods_name = $param['goods_name'];
        foreach ($ids as $key => $value) {
            $where=array();
            $where['goods_name']=$goods_name[$key];
			$where['id']=$value;
			$where['is_delete']=0;
            $goods['is_jinsheng']=1;
            M('Goods')->where($where)->save($goods);
        }
        $new_ids = array();
        $map=array();$map['is_jinsheng']=1;$map['is_delete']=0;
		$map['id']=array('in',$ids);
        $goods_ids = M('Goods')->where($map)->field('id')->select();
        foreach ($goods_ids as $key => $value) {
            $new_ids[]=$value['id'];
        }
    	$save['config']=$param['pro_type']; //升级方式 
    	$save['temp']= serialize($new_ids);
    	$save = serialize($save);
    	$data['value']=$save;
    	$where['name'] = 'PRO_GOODS';
    	$saves = M('Config')->data($data)->where("name='PRO_GOODS'")->save();
    	if($save) $this->success('修改成功');
    	else $this->error('修改失败');
    }
    //gyc 会员等级晋升指定商品
    public function member_promote(){
    	$param=I('param.');
    	$where['name'] = 'PRO_GOODS';
    	$res = M('Config')->field('value')->where($where)->find();

    	$info=unserialize($res['value']);
		if(!empty($info['temp'])){
			$goods_ids=implode(',',unserialize($info['temp']));
			$map['id'] = array('in',$goods_ids);
			// $map['is_offline']=0;
			// $map['is_delete']=0;
			$goods_list = M('Goods')->where($map)->field('id,img,goods_name,goods_sn,shop_price,rank_jinsheng')->order('sort asc,id desc')->select();
			// print_r($goods_list);
		}
    	$this->assign('show_list',$goods_list);
    	$this->assign('pro_type',$info['config']);
    	$this->display();
    }
    //积分清零设置
    public function rank_info(){
        $postdata = I('param.');
        $where['name'] = 'PRO_INFO';
        $res = M('Config')->field('value')->where($where)->find();
        if(IS_POST){
            $postdata=I('post.');
            $data['value']=$postdata['content'];
            $saves = M('Config')->data($data)->where("name='PRO_INFO'")->save();
            $this->success('设置成功');return;
        }else{
             // print_r($res);
            $this->assign('content',$res['value']);
            $this->display();
        }
    }
}
?>