<?php

namespace Active\Lib;

use Dever;
Class Manage 
{
    public function upCode_api($id)
    {
        $id = Dever::input('id');
        if ($id) {
            $data = Dever::db('active/code')->find($id);
            if ($data && $data['status'] == 1) {
                $where['where_id'] = $id;
                $where['status'] = 2;
                $admin = Dever::load('manage/auth.info');
                $where['audit_admin'] = $admin['id'];
                Dever::db('active/code')->update($where);
                // echo Dever::sql();die;
            }
        }
        return 'reload';
    }
	#全额退款
	public function refund_api($id)
    {
		$data = Dever::db('active/order')->find($id);
        // if ($data['mid'] && $data['mid'] > 0) {
        //     $user = Dever::db('active/user')->find(array('mid'=>$data['mid']));
        //     if ($user && $user['id']) {
        //         $uid = $user['id'];
        //     }
        // } else {
        //     $uid = $data['uid'];
        // }
        $this->common($data,1);
  		return 'reload';
	}

    public function insertInfoRefund ($id,$name,$data)
    {
        $num = Dever::param('num',$data);
        $price = Dever::param('price',$data);
        $order_id = Dever::param('order_id',$data);
        $data = Dever::db('active/order')->find($order_id);
        if ($num < 1) {
            Dever::alert('退款数量不正确');
        }
        if($num > $data['num']) {
            Dever::alert('退款数量大于购买数量');
        }
        $info = Dever::db('active/info')->find($data['active_id']);
        // $refund_price = $num*$info['price'];
        if ($price > $info['price']) {
            Dever::alert('退款金额大于购买金额');
        }
        $code = Dever::db('active/code')->getCode(array('order_id' => $order_id,'status'=>'1,2'));
        $use_code = count($code);#已领取的所有数量
        if (!$data['refund_num']) {
            $data['refund_num'] = 0;
        }
        $no_code = $data['num'] - $use_code - $data['refund_num'];#未使用的数量
        if ($num > $no_code){
            Dever::alert('退款数量不正确');
        }
        // if ($data['refund_num'] > 0){
        //     $info = Dever::db('active/info_refund')->state(array('order_id'=>$order_id,'status'=>2));
        //     $count = $data['num'] - count($info);
        //     if ($num > $count) {
        //         Dever::alert('退款数量大于购买数量');
        //     }
        // }
        
    }

    public function updateInfoRefund ($id,$name,$data)
    {
        $num = Dever::param('num',$data);
        $price = Dever::param('price',$data);
        $desc = Dever::param('desc',$data);
        $order_id = Dever::param('order_id',$data);
        $code_id = Dever::param('code_id',$data);
        $data = Dever::db('active/order')->find($order_id);
        // $info = Dever::db('active/info_refund')->state(array('order_id'=>$order_id));
        // $active = Dever::db('active/info')->find($data['active_id']);
       $this->common($data,2,$num,$id,$price,$desc);
        return 'reload';
    }
    
    public function show($id)
    {
        $data = Dever::db('active/order')->find($id);
        $refund_admin = '-';
        $rdate = '-';
        if ($data && isset($data['refund_admin']) && $data['refund_admin'] && $data['refund_admin']>0 && $data['rdate']) {
            $admin = Dever::load('manage/admin-find',$data['refund_admin']);
            $refund_admin = $admin['username'];
            $rdate = date('Y-m-d H:i',$data['rdate']);
        }
        $html = $refund_admin.'<br/>'.$rdate;
        return $html;
    }

    public function active($id)
    {
        $data = Dever::db('active/info')->find($id);
        $html = array();
        if (isset($data) && $data) {
            $html['active_name'] = $data['name'];
        }
        
        $info = Dever::db('active/code')->find($id);
        if ($info && isset($info['audit_admin']) && $info['audit_admin']  && $info['audit_admin'] > 0) {
            $aduit = Dever::load('manage/admin-find',$info['audit_admin']);
            $html['admin_name'] = $aduit['username'];
        } elseif ($info && $info['top_admin']) {
            $off = Dever::db('active/info_off')->find($info['top_admin']);
            $html['admin_name'] = $off['name'];
        }
        return $html;
    }
    public function user($id)
    {
        $html = '';
        $data = Dever::db('active/user')->find($id);
        if ($data) {
            $member = Dever::load('active/lib/user')->agent($data);
            if ($member) {
                $user = '<br/>(代理商)';
            } else {
                $user = '';
            }
            $html = $data['name'].'<br/>'.$data['mobile'].$user;
        }
        
        return $html;
    }
    public function dOff($id)
    {
        $data = Dever::db('active/code')->state(array('active_id' => $id,'status'=>1));
        $html = count($data);
        return $html;
    }
    public function insertInfoCode($id, $name, $data)
    {
        $code = Dever::param('code',$data);
        if ($code) {
            $info = Dever::db('active/info_code')->find(array('code'=>$code));
            if ($info['code']) {
                Dever::alert('核销码已核销');
            }
        }
    }
    public function updateInfoCode($id, $name, $data)
    {
        $code = Dever::param('code',$data);
        $data = Dever::db('active/code')->find(array('write_code'=>$code));
        if ($data) {
            $where['where_id'] = $data['id'];
            $where['status'] = 2;
            $where['offdate'] = time();
            $admin = Dever::load('manage/auth.info');
            $where['audit_admin'] = $admin['id'];
            Dever::db('active/code')->update($where);
        }

    }
    public function get()
    {
        $data[0]['name'] = '头衔';
        $data[0]['child'] = Dever::db('setting/title')->select();
        $data['state'] = 1;
        return $data;
    }
    public function search_api()
    {
        $value = Dever::input('value');
        if (!$value) {
            return '核销码或手机号不存在';
        }
        $active_id = Dever::input('search_option_active_id');
        if ($value) {
            $data = Dever::db('active/code')->one(array('write_code' => $value,'state' =>1));
            if (!$data) {
                if (strlen($value) != 11){
                    return '核销码或手机号不正确';
                } else {
                    $tell = preg_match("/^1[3456789]\d{9}$/",$value);
                    if ($tell) {
                        $user = Dever::db('active/user')->find(array('mobile'=>$value));
                        if ($user) {
                            $data = Dever::db('active/code')->find(array('join_uid'=>$user['id'],'active_id'=>1,'status' => 1));
                            if (!$data) {
                                return '核销码或手机号不正确或已核销';
                            }
                        } else {
                            return '核销码或手机号不正确';
                        }
                    }
                }
            }
            $table = array();
            if ($data) {
                $user = Dever::db('active/user')->find(array('id'=>$data['join_uid']));
                $parent = Dever::db('active/user')->find(array('id'=>$data['buy_uid']));
                $table = array();
                $table['head'] = array('姓名', '手机号','邀请人', '邀请人手机号');
                $table['body'] = array();
                $table['body'] = array($user['name'], $user['mobile'],$parent['name'], $parent['mobile']);
                
                $result[] = array
                (
                    'type' => 'table',
                    'content' => $table,
                );

                $html = Dever::show('', $result, false, false);
                return $html;
            }else{
                return '核销码不存在';
            }
        } else {
            return '';
        }
    }

    public function checkMcode()
    {
        $mobile = Dever::input('mobile');
        $code = Dever::input('mcode');
        if (!$code) {
            Dever::alert('请输入验证码');
        }
        $code = Dever::load('passport/reg')->mcode($mobile, $code, 2);
        if (!$code) {
            Dever::alert('验证码输入错误');
        }
        return 'ok';
    }


    public function list()
    {
        $id = Dever::input('id');
        $info = Dever::db('active/order')->find($id);
        $active = Dever::db('active/info')->find($info['active_id']);
        $config = Dever::db('active/order')->config['config_status'];
        $status = Dever::status($config,$info['status']);
        if ( !$info['uid']) {
            $user = Dever::db('active/user')->find($info['mid']);
            $info['uid'] = $user['id'];
        }
        $code = Dever::db('active/code')->getCode(array('order_id' => $id,'status'=>'1,2,3'));
        $use_code = count($code);#已领取的所有数量
        // print_R($use_code);die;
        if (!$info['refund_num']) {
            $info['refund_num'] = 0;
        }
        $tcode = Dever::db('active/code')->getCode(array('order_id' => $id,'status'=>'1,2'));
        $tuse = count($tcode);
         // $tui_code = $info['num'] - $info['refund_num'];#未使用的数量
        $tui_code = $info['num'] - $tuse - $info['refund_num'];#未使用的数量
        $no_code = $info['num'] - $use_code;#未使用的数量
        if ($info['uid'] && $info['uid'] > 0) {
            $user = Dever::db('active/user')->find($info['uid']);
            $user_name = $user['name'];
            $user_mobile = $user['mobile'];
            $user_idcard = $user['idcard'];
            $member = Dever::load('active/lib/user')->agent($user);
            if ($member) { 
                $role = Dever::db('setting/role')->find($member['role']);
                if (isset($role) && $role && isset($role['name']) && $role['name']) {
                    $role_name = '<br/>'.$role['name'];
                } else {
                    $role_name = '';
                }
                $title = Dever::db('setting/title')->find($member['title_id']);
                if (isset($title) && $title && isset($title['name']) && $title['name']) {
                    $title_name = '('.$title['name'].')';
                } else {
                    $title_name = '';
                }
                $level = Dever::db('setting/level')->find($member['level_id']);
                if (isset($level) && $level && isset($level['name']) && $level['name']) {
                    $level_name = '('.$level['name'].')';
                } else {
                    $level_name = '';
                }
                $user_name = $member['name'].$role_name.$title_name.$level_name;
                $user_mobile = $member['mobile'];
                $user_idcard = $member['idcard'];
            }
        }
        // print_R($id);die;
        $refund = Dever::db('active/info_refund')->getSearch(array('order_id'=>$id));
        $refund_path = array();
        $refund_desc = '';
        if ($refund) {
            foreach ($refund as $k1 => $v1) {
                 $refund_log = Dever::db('active/info_refund_log')->getSearch(array('order_id'=>$v1['id']));
                 $refund_desc = $v1['desc'];
                 if ($refund_log) {
                    if ($refund_log['desc'] != $v1['desc']) {
                        $refund_desc = $refund_log['desc'];
                    }
                 }
                 if (!$v1['num']) {
                    $v1['num'] = 0;
                 }
                 if (!$v1['price']) {
                    $v1['price'] = 0;
                 }
                $refund_path[] =  array('退款数量',$v1['num']);
                $refund_path[] = array('退款金额',$v1['price']);
                $refund_path[] = array('退款备注',$refund_desc);
            }
        }

        $path = array();
        $path_data = array();
        if ($info['path']) {
            $path = Dever::json_decode($info['path']);
            foreach ($path as $k => $v) {
                $field = Dever::db('active/info_field')->find($v['id']);
                if ($field['type'] == 1) {
                    $path_data[] = array($field['name'], $v['value']);
                } elseif ($field['type'] == 2) {
                    $value = Dever::db('active/info_field_value')->find($v['value']);
                    if ($value) {
                        $path_data[] = array($field['name'], $value['name']);
                    }
                }
            }
        }
        $result['订单信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('订单单号', $info['order_num']),
                    array('活动名称', $active['name']),
                    array('订单状态', $status),
                ),
                array
                (
                    array('购买数量', $info['num']),
                    array('支付金额', $info['price']),
                    array('备注',$info['remark']),
                ),
                array
                (
                    array('已使用', $use_code),
                    array('未使用', $no_code),
                    // array('退款数量', $info['refund_num']),
                ),
                array
                (
                    // array('退款数量', $info['refund_num']),
                    // array('退款总金额', $refund_money),
                    // array('退款备注', $refund_desc),
                    
                ),
            ),
        );
        if ($refund_path && $refund) {
            $result['退款信息'] = array
            (
                'type' => 'info',
                'content' => array
                (
                    $refund_path,
                ),
            );
        }
        
        
        $result['用户信息'] = array
        (
            'type' => 'info',
            'content' => array
            (
                array
                (
                    array('用户姓名', $user_name),
                    array('用户电话', $user_mobile),
                    array('用户身份证号', $user_idcard),
                ),
            ),
        );
        if ($path) {
            $result['报名信息'] = array
            (
                'type' => 'info',
                'content' => array
                (
                    $path_data,
                ),
            );
        }
        $button = array();
        if (Dever::load('manage/auth')->checkFunc('active.order', 'edit', '备注')) {
            $purl = Dever::url('project/database/update?project=active&table=order&where_id='.$info['id'].'&col=remark', 'manage');
            $button[] = array
            (
                'type' => 'edit',
                'link' => $purl,
                'name' => '备注',
            );
        }
        if ($info['status'] == 2 && $info['code_num'] < 1 && time() < $active['act_start'] && $active['refund_type'] == 2 ) {
            
            if (Dever::load('manage/auth')->checkFunc('active.order', 'editrefund', '全额退款')) {
                $purl = Dever::url('active/lib/manage.refund&id='.$id);
                $button[] = array
                (
                    'type' => 'action',
                    'link' => $purl,
                    'name' => '全额退款',
                );
            }
        }
        if ($info['status'] > 1 && $tui_code > 0 && $info['status'] != 3  && $active['refund_type'] == 2) {
            // && time()<$active['act_start']
            if (Dever::load('manage/auth')->checkFunc('active.order', 'editrefund1', '未使用退款')) {
                $purl = Dever::url('project/database/update?project=active&table=info_refund&search_option_price='.$active['price'].'&search_option_order_id='.$info['id'].'&col=num,order_id,price,desc', 'manage');
                $button[] = array
                (
                    'type' => 'edit',
                    'link' => $purl,
                    'name' => '未使用退款',
                );
            }
        }
        
        if ($use_code >= 0) {
           $result +=  $this->table($id);
            
        }
        $head = array
        (
            'name' => '基本资料',
            'btn' => $button,
        );
        $html = Dever::show($head, $result);
        return $html;
    }

    private function table ($id) 
    {
        $data = Dever::db('active/code')->state(array('order_id' => $id));
        if ($data) {
            $head = array('参加人信息', '核销码', '核销状态', '领取时间', '核销人', '管理');
             // '核销时间',
            $body = array();
            foreach ($data as $k => $v) {
                $user = Dever::db('active/user')->find($v['join_uid']);
                $join_name = '';
                if ($user) {
                    $join_name = $user['name'].'<br/>'.$user['mobile'];
                }
                $admin_name = '';
                if ($v['status'] == 1) {
                    $status = '待核销';
                } elseif($v['status'] == 2) {
                    $status = '已核销';
                   if ($v['audit_admin'] && $v['audit_admin'] >0) {
                       $admin = Dever::load('manage/admin-find',$v['audit_admin']);
                       if ($admin) {
                            $admin_name = $admin['username'];
                       } 
                   } else {
                        if ($v['top_admin'] && $v['top_admin'] > 0) {
                            $admin = Dever::db('active/info_off')->find($v['top_admin']);
                            if ($admin) {
                                $admin_name = $admin['name'];
                            }
                        }
                   }
                } else {
                    $status = '已作废';
                }
                $cdate = '';
                if ($v['cdate']) {
                    $cdate = date('Y-m-d H:i',$v['cdate']);
                }
                // $offdate = '';
                // if ($v['offdate']) {
                //     $offdate = date('Y-m-d H:i',$v['offdate']);
                // }
                $active = Dever::db('active/info')->find($v['active_id']);
                $m = '';
                $url = $name = '';
                if ($active['refund_type'] == 2 ) {
                    // && time()<$active['act_start'] 
                    if ($v['status'] == 1) {
                        $url = Dever::url('project/database/update?project=active&table=info_refund_log&search_option_price='.$active['price'].'&search_option_code_id='.$v['id'].'&search_option_order_id='.$id.'&col=price,order_id,code_id,desc', 'manage');
                        $name = '退款';
                    } elseif ($v['status'] == 2) {
                        if ($active['off_type'] == 2) {
                            $url = Dever::url('project/database/update?project=active&table=info_refund_log&search_option_price='.$active['price'].'&search_option_code_id='.$v['id'].'&search_option_order_id='.$id.'&col=price,order_id,code_id,desc', 'manage');
                                $name = '退款';
                        }
                    } elseif ($v['status'] == 3) {
                        if ($active['refundagin_type'] == 2) {
                            $log = Dever::db('active/info_refund_log')->getTui(array('order_id'=>$id,'code_id'=>$v['id']));
                            if ($log && $log['price'] < $active['price']) {
                                $url = Dever::url('project/database/update?project=active&table=info_refund_log&search_option_price='.$active['price'].'&search_option_code_id='.$v['id'].'&search_option_order_id='.$id.'&col=price,order_id,code_id,desc', 'manage');
                                $name = '再次退款';
                        }
                    }
                }
                   
                    if (Dever::load('manage/auth')->checkFunc('active.order', 'editorder', '退款')) {
                        if (isset($url) && $url) {
                            $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\')" class="layui-btn">'.$name.'</a>';
                        }
                        
                    }
                    // $url = Dever::url('lib/manage.setOrder&order_id=' . $v['order_id'].'&code_id='.$v['id'].'&col=price', 'active');
                }
                
                $d = array
                (
                    $join_name,
                    $v['write_code'],
                    $status,
                    $cdate,
                    // $offdate,
                    $admin_name,
                    $m,
                );
                $body[] = $d;
            }
            $result['核销码使用列表'] = array
            (
                'type' => 'table',
                'content' => array
                (
                    'head' => $head,
                    'body' => $body,
                )
            );
           return $result;
        } else {
            return array();
        }
    }
    public function insertInfoRefundLog($id,$name,$data)
    {
        $price = Dever::param('price',$data);
        $order_id = Dever::param('order_id',$data);
        $code_id = Dever::param('code_id',$data);
        $order = Dever::db('active/order')->find($order_id);
        $active = Dever::db('active/info')->find($order['active_id']);
        if (!$price) {
            Dever::alert('退款金额不能为空');
        }
        if ($price > $active['price']) {
            Dever::alert('退款金额大于购买金额');
        }
        $data = Dever::db('active/info_refund_log')->find(Array('order_id'=>$order_id,'code_id'=>$code_id,'status'=>2));
        if($data) {
            Dever::setInput('noNum', 1);
            if (isset($data['price']) && $data['price']) {
                $tprice = $price+$data['price'];
                 if ($tprice>$active['price']) {
                    Dever::alert('退款金额大于购买金额');
                }
            }
        }
    }
    public function updateInfoRefundLog($id,$name,$data)
    {
        $price = Dever::param('price',$data);
        $order_id = Dever::param('order_id',$data);
        $code_id = Dever::param('code_id',$data);
        $desc = Dever::param('desc',$data);
        $order = Dever::db('active/order')->find($order_id);
        $where['order_id'] = $order_id;
        $where['code_id'] = $code_id;
        $where['price'] = trim($price);
        $where['desc'] = $desc;
        $data = Dever::db('active/order')->find($order_id);
        $ids = $this->common($data,3,false,false,$price,$desc);
        if ($ids) {
            $where['status'] = 2;
            $where['id'] = $id;
            Dever::db('active/info_refund_log')->update($where);
            
            $admin = Dever::load('manage/auth.info');
            $w['where_id'] = $code_id;
            $w['status'] = 3;
            $w['offdate'] = time();
            $w['audit_admin'] = $admin['id'];
            Dever::db('active/code')->update($w);
        }
        return 'reload';
    }
    public function setOrder_api($order_id,$code_id){
        $data = Dever::db('active/order')->find($order_id);
        $ids = $this->common($data,3);
        if ($ids) {
            $admin = Dever::load('manage/auth.info');
            $w['where_id'] = $code_id;
            $w['status'] = 3;
            $w['offdate'] = time();
            $w['audit_admin'] = $admin['id'];
            Dever::db('active/code')->update($w);
        }
        return 'reload';
    }

    public function common($data,$type,$num=false,$refund_id=false,$refund_price=false,$refund_desc=false){
        Dever::config('base')->hook = true;
        $active = Dever::db('active/info')->find($data['active_id']);
        $info = Dever::db('active/info_refund')->state(array('order_id'=>$data['id'], 'status' => 2));
        if (time() > $active['act_start']) {
            // Dever::alert('活动已开始，不能退款');
        }
        
        if ($type == 1) {
            $code = Dever::db('active/code')->find(array('order_id'=>$data['id']));
            if ($data['code_num'] > 0 || (isset($code) && $code)) {
                Dever::alert('此状态下不可退款');
            }
            if ($data['status'] != 2) {
                Dever::alert('此状态下不可退款');
            }
            $refund_num = $data['num'];
            $price = $data['price'];
        }
        if ($type == 2) {
            if ($refund_price) {
                $price = $num * $refund_price;
            } else {
                $price = $num * $active['price'];
            }
            
            $refund_num = $num;
        }
        if ($type == 3) {
            $refund_num = 1;
            if ($refund_price) {
                $price = $refund_price;
            } else {
                $price = $active['price'];
            }
            
        }
        // if ($refund_desc) {
        //     $where['desc'] = $refund_desc;
        // }
        $param = array
        (
            'project_id' => 6,
            'channel_id' => 1,
            'system_source' => 1,
            'account_id' => 8,
            'order_id' => $data['order_num'],
            'refund_cash' => $price,
            //'refund_order_id' => '',
        );
        if ($type != 1) {
            $param['refund_order_id'] = count($info) + 1;
            if ($data['order_num'] == 'D202207114441013576950807') {
                $param['refund_order_id'] = $param['refund_order_id'] + 1;
            }
        }
        
        if ($data['price'] > 0) {
            $result = Dever::load('pay/api.refund', $param);

            if (!$result) {
                # 退款失败，抛出错误
                //throw new \Exception('退款失败');
                Dever::alert('退款失败，请联系管理员');
            } else { 
                $admin = Dever::load('manage/auth.info');
                $time = time();
                $where['where_id'] = $data['id'];
                $where['status'] = 6;
                $where['refund_admin'] = $admin['id'];
                $where['rdate'] = $time;
                if (!$data['refund_num']) {
                    $data['refund_num'] = 0;
                }
                $noNum = Dever::input('noNum');
                if ($noNum == 1) {
                    $where['refund_num'] = $data['refund_num'];
                } else {
                    $where['refund_num'] = $refund_num + $data['refund_num'];
                }
                

                if ($type == 1 || ($price == $data['price']) || ($data['num'] == $where['refund_num'])) {
                    $ids = Dever::db('active/order')->update($where);
                } else {
                    // $where['status'] = 2;
                    $where['status'] = 7;
                    $ids = Dever::db('active/order')->update($where);
                }
                if ($type == 2) {
                    $insert['status'] = 2;
                    $insert['where_id'] = $refund_id;
                    $insert['audit_admin'] = $admin['id'];
                    if ($refund_desc) {
                        $insert['desc'] = $refund_desc;
                    }
                    $insert['price'] = $price;
                    Dever::db('active/info_refund')->update($insert);
                } elseif ($type == 3) {
                    $add['status'] = 2;
                    $add['order_id'] = $data['id'];
                    $add['num'] = 1;
                    $add['price'] = $price;
                    if ($refund_desc) {
                        $add['desc'] = $refund_desc;
                    }
                    Dever::db('active/info_refund')->insert($add);
                }
                if (isset($ids) && $ids){
                    return $ids;
                }
            }
        }
    }

    

    #扫码核销活动
    public function code_api(){
        $active_id = Dever::input('id');
        if (!$active_id) {
             return '活动id不存在';
        }
        $active = Dever::db('active/info')->find($active_id);
        $config = Dever::db('active/info')->config['config_act_status'];
        
        $code = Dever::input('code');
        if (!$code) {
            $data = array();
            $data['active_name'] = $active['name'];
            $data['status'] = Dever::status($config,$active['act_status']);
            $data['act_time'] = date('Y.m.d H:i:s',$active['act_start']).'~'.date('Y.m.d H:i:s',$active['act_end']);
            $data['active_id'] = $active_id;
            $one = Dever::db('active/code')->find(array('code'=>$code));
            $data['seat_num'] = $one['seat_num'];
            return Dever::render('off_code', $data);
        }
       
        // print_R($code);die;
        $data = Dever::db('active/code')->find(array('write_code'=>$code,'active_id'=>$active_id,'status'=>1));
        if ($data) {
            $data['active_name'] = $active['name'];
            $data['status'] = Dever::status($config,$active['act_status']);
            $data['act_time'] = date('Y.m.d H:i:s',$active['act_start']).'~'.date('Y.m.d H:i:s',$active['act_end']);
            $join_user = Dever::db('active/user')->find($data['join_uid']);
            $data['join_name'] = $join_user['name'];
            $data['join_mobile'] = $join_user['mobile'];
            $con = Dever::db('deploy/base-one');
            if ($join_user['avatar']) {
                $data['join_avatar'] = $join_user['avatar'];
            } else {
                $data['join_avatar'] = $con['avatar'];
            }
             
             $data['buy_name'] = '';
                $data['buy_mobile'] = '';
                $data['buy_avatar'] = '';
            $buy_user = Dever::db('active/user')->find($data['buy_uid']);
            if ($buy_user) {
                $data['buy_name'] = $buy_user['name'];
                $data['buy_mobile'] = $buy_user['mobile'];
                $data['buy_avatar'] = '';
                if ($buy_user['avatar']) {
                    $data['buy_avatar'] = $buy_user['avatar'];
                } 
            }
            
            $member = Dever::load('active/lib/user')->agent($buy_user);
            $data['role_name'] ='';
            $data['title_name'] = '';
            if ($member) {
                $role = Dever::db('setting/role')->find($member['role']);
                $title = Dever::db('setting/title')->find($member['title_id']);
                $level = Dever::db('setting/level')->find($member['level_id']);
                if ($role) {
                    if (isset($level) && $level && isset($level['name']) && $level['name']) {
                        $data['role_name'] = $role['name'] .'('.$level['name'].')';
                    } else {
                        $data['role_name'] = $role['name'];
                    }
                }
                if ($title  && $title['name']) {
                    $data['title_name'] = $title['name'];
                }

                if (!$data['buy_avatar']) {
                    $data['buy_avatar'] = $member['avatar'];
                } else {
                    $data['buy_avatar'] = $con['avatar'];
                }
            }
        } else {
            return '核销码不存在或已核销';
        }
        $type = Dever::input('type');
        if ($type == 1) {
            return Dever::render('off_code', $data);
        }else {
            return $data;
        }
        
    }
}