<?php

namespace Agent\Lib;

use Dever;

class Member
{
    public function getOne($id, $table = 'agent/member', $area = false)
    {
        if ($id < 0) {
            return '无';
        }
        $member = Dever::db($table)->find($id);
        if ($member) {
            if (isset($member['mid'])) {
                $member['id'] = $member['mid'];  
            }

            $member['role'] = Dever::db('setting/role')->find($member['role']);
            $url = Dever::url('project/database/list?project=agent&table=member_area&mid='.$member['id'].'&page_type=1&[refer]', 'manage');
            
            $member['name'] = '<a href="'.$url.'">' . $member['name'] . '</a>';
            $code = Dever::load("invite/api.code", $member['id']);
            $string = '';
            if ($code) {
                $string = '[' . $code . '] <br />';
            }
            $prize_type = '';
            if($member['role']['id'] == 8){
                if($member['prize_type'] && $member['prize_type'] == 1){
                    $prize_type = '(名额外)';
                }elseif($member['prize_type'] && $member['prize_type'] == 2){
                    $prize_type = '(名额内)';
                }
               
            }
            $string .= $member['name'] . '（'.$member['id'].'）' .'<br />' . $member['mobile'] . '<br />' . $member['role']['name'].$prize_type;
            
            if (isset($member['level_id']) && $member['level_id'] && $member['level_id'] > 0) {
                $member['level'] = Dever::db('setting/level')->find($member['level_id']);
                $string .= '（'.$member['level']['name'].'）';
            }

            if (isset($member['shop_id']) && $member['shop_id'] > 0) {
                $string .= ' ' . Dever::load("shop/info-one#name", $member['shop_id']);
            }

            if ($area) {
                $area = Dever::load("area/api.string", $member['area']);
                if ($area) {
                    $string .= '<br />' . $area;
                }
            }

            $member['mobile'] = strip_tags($member['mobile']);

            $url = Dever::url('project/database/list?project=agent&table=member&search_api_parent_mid='.$member['mobile'].'&[refer]', 'manage');

            $string .= '<br /><a href="'.$url.'">直推：'. Dever::load("invite/api")->getChildNum($member['id'], 1) . '</a>';

            if ($member['source_id'] > 0) {
                $source = Dever::db('setting/source')->one($member['source_id']);
                if ($source) {
                    $string .= '<br />来源：' . $source['name'];
                }
            }
            
            return $string;
            
        } else {
            return '';
        }
    }

    public function getInfoString($member)
    {
        $member = $this->getInfo($member);

        $info = '代理商信息：'.$member['name'].' '.$member['mobile'].' ' . $member['role']['name'];
        if ($member && isset($member['level']) && $member['level']) {
            $info .= '（'.$member['level']['name'].'）';
        }

        return $info;
    }

    public function getInfo($member)
    {
        Dever::setInput('getInfo', 1);
        if (is_numeric($member)) {
            $member = Dever::db('agent/member')->find($member);
        }
        $member['area_string'] = Dever::load("area/api.string", $member['area'], '/', true, '');
        $member['role'] = Dever::db('setting/role')->find($member['role']);
        $member['level'] = Dever::db('setting/level')->find($member['level_id']);

        $member['shop'] = array();
        if ($member['shop_id'] && $member['shop_id'] > 0) {
            $member['shop'] = Dever::db('shop/info')->find($member['shop_id']);
        }

        $data = Dever::load('passport/reg')->getSign($member['id']);
        if ($data['signature']) {
            $member['signature'] = $data['signature'];
        }

        $member['birthday'] = date('Y-m-d', $member['birthday']);

        return $member;
    }

    /**
     * 检测邀请码
     *
     * @return mixed
     */
    public function checkCode()
    {
        $code = Dever::input('code');
        if (!$code) {
        	Dever::alert('请输入邀请码');
        }
        $uid = Dever::load('invite/api')->getUid($code);
        if (!$uid) {
        	Dever::alert('请输入正确的邀请码');
        }

        $user = Dever::db('agent/member')->find($uid);
        if (!$user) {
        	Dever::alert('请输入正确的邀请码');
        }

        if ($user['status'] <= 2) {
        	return $user;
        }

        Dever::alert('邀请人状态不正常');
    }

	/**
     * 验证手机号
     * @return mixed
     */
    public function checkMobile($mobile)
    {
        $rule = Dever::rule('mobile');

        if (!$mobile) {
            Dever::alert('手机号不能为空');
        }
        if (!preg_match($rule, $mobile)) {
            Dever::alert('手机号码格式错误');
        }

        return $mobile;
    }

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

    /**
     * 更新余额和业绩
     * @return mixed
     */
    public function up($child, $mid, $order_id, $role, $type, $cash, $sell, $level_id = false, $desc = '', $group_num = 0)
    {
        $member = Dever::db('agent/member')->find(array('id' => $mid, 'clear' => true));
        $data['where_id'] = $mid;
        if ($cash) {
            Dever::load('bill/lib/cash')->up($mid, $type, $cash, $role, $order_id, $desc, 2);
            $data['cash'] = $cash;
        } else {
            $data['cash'] = 0;
        }

        Dever::load('bill/lib/sell')->up($child, $mid, $type, $sell, $role, $order_id, $desc);
        
        $data['upcol'] = $sell;
        if ($level_id) {
            $data['level_id'] = $level_id;
        }

        //$method = 'upGroupSell';
        if ($type == 1) {
            $method = 'upSell';
        } else {
            $method = 'upGroupSell';
        }
        $data['clear'] = true;
        Dever::db('agent/member')->$method($data);

        if ($level_id && $level_id != $member['level_id']) {
            
            Dever::load('agent/lib/level_log')->up($mid, $order_id, $member['level_id'], $level_id, $sell, $member['group_sell'] + $sell, $group_num);
        }
    }

    /**
     * 查看详情
     * @return mixed
     */
    public function show()
    {
        $mid = Dever::input('mid');
        $member = Dever::db('agent/member')->one($mid);

        $config_type = Dever::db('agent/member')->config['config_type'];
        $config_status = Dever::db('agent/member')->config['config_status'];

        $result['代理商信息'] = $this->getShowInfo($member);

        if ($member['parent_mid'] && $member['parent_mid'] > 0) {
            $parent = Dever::db('agent/member')->one($member['parent_mid']);
            if ($parent) {
                $result['上级信息'] = $this->getShowInfo($parent);
            }
        }

        $idcard_pic = '<a href="'.$member['idcard_front'].'" target="_blank"><img src="'.$member['idcard_front'].'" width="100" /></a>';
        if ($member['idcard_back']) {
            $idcard_pic .= '&nbsp;&nbsp;<a href="'.$member['idcard_back'].'" target="_blank"><img src="'.$member['idcard_back'].'" width="100" /></a>';
        }
        $member_name = $member['name'];
        $member_number = $member['idcard'];

        $result['认证信息'] = array
        (
            'type' => 'user',
            'content' => array
            (
                array('类型', $config_type[$member['type']]),
                array('姓名', $member_name),
                array('身份证号', $member_number),
            ),
        );

        if ($member['type'] == 2) {
            $company_pic = '<a href="'.$member['company_pic'].'" target="_blank"><img src="'.$member['company_pic'].'" width="100" /></a>';
            $member_name = $member['company_name'];
            $member_number = $member['company_number'];

            $result['认证信息']['content'][] = array('企业名称', $member_name);
            $result['认证信息']['content'][] = array('营业执照', $member_number);
            $result['认证信息']['content'][] = array('营业执照照片', $company_pic);
        }

        $result['认证信息']['content'][] = array('身份证照片', $idcard_pic);

        $button = array();
        if (Dever::load('manage/auth')->checkFunc('agent.member', 'edit', '修改基础资料')) {
            $button[0]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=name,mobile,email,area,role,level_id,is_shop,is_area', 'manage'),
                'name' => '修改基础资料',
            );
        }

        if (Dever::load('manage/auth')->checkFunc('agent.member', 'editTitle', '修改头衔')) {
            $button[0]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=title_id,state', 'manage'),
                'name' => '修改头衔',
            );
        }

        if (Dever::load('manage/auth')->checkFunc('agent.member', 'editPassport', '修改密码')) {
            $button[0]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=password', 'manage'),
                'name' => '修改密码',
            );
        }

        if (Dever::load('manage/auth')->checkFunc('agent.member','sport','修改认证资料')) {
            $button[0]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=type,idcard,idcard_front,idcard_back,company_name,company_pic,company_number', 'manage'),
                'name' => '修改认证资料',
            );
        }

        if (Dever::load('manage/auth')->checkFunc('agent.member', 'editShop', '修改门店')) {
            $button[0]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=shop_id', 'manage'),
                'name' => '修改门店',
            );
        }

        if (Dever::load('manage/auth')->checkFunc('agent.member', 'editMobile', '修改手机号')) {
            $button[0]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=mobile', 'manage'),
                'name' => '修改手机号',
            );
        }

        /*
        $button[] = array
        (
            'type' => 'edit',
            'link' => Dever::url('project/database/update?project=agent&table=member&where_id='.$member['id'].'&status=1&col=shop_id', 'manage'),
            'name' => '修改归属门店',
        );
        */
        $url = '';
        if (Dever::load('manage/auth')->checkFunc('agent.member', 'bun', '禁用账号')) {
            if ($member['status'] == 5) {
                $button[0]['btn'][] = array
                (
                    'type' => 'action',
                    'link' => Dever::url('lib/member.setStatus?id=' . $member['id'] . '&value=2', 'agent'),
                    'name' => '恢复账号',
                );
            } elseif ($member['status'] == 2) {
                $button[0]['btn'][] = array
                (
                    'type' => 'action',
                    'link' => Dever::url('lib/member.setStatus?id=' . $member['id'] . '&value=5', 'agent'),
                    'name' => '禁用账号',
                );
            }
        }
        

        $button[0]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=order&search_api_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '代理订单',
        );

        $button[0]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=order&search_api_parent_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '直推代理订单',
        );

        $button[0]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member_level_log&search_api_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '升级日志',
        );

       if (Dever::load('manage/auth')->checkFunc('agent.member', 'edit1', '发放资金')) {
            $button[1]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=bill&table=push_cash&search_option_mid='.$member['id'], 'manage'),
                'name' => '发放资金',
            );
        } 

       if (Dever::load('manage/auth')->checkFunc('agent.member', 'edit2', '发放业绩')) {
            $button[1]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=bill&table=push_sell&search_option_mid='.$member['id'], 'manage'),
                'name' => '发放业绩',
            );
        }
        

        $button[1]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=bill&table=cash&search_api_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '资金账户',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=bill&table=sell&search_api_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '业绩流水',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member_goods&search_option_state=1&search_api_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '代理商权益',
        );

        $button[1]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=dh_order&search_option_state=1&search_api_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '权益订单',
        );

        $button[2]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member_agreement&search_option_state=1&search_api_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '代理商合同',
        );

        $button[2]['btn'][] = array
        (
            'type' => 'link',
            'link' => Dever::url('project/database/list?project=agent&table=member&search_option_state=1&search_api_parent_mid='.$member['mobile'].'&[refer]', 'manage'),
            'name' => '查看直推团队',
        );

        
        /*
        $button[2]['btn'][] = array
        (
            'type' => 'edit',
            'link' => Dever::url('lib/member.setStatus?id=' . $member['id'] . '&value=5', 'agent'),
            'name' => '查看邀请关系',
        );
        */

        $furl = '';
        if (Dever::load('manage/auth')->checkFunc('agent.member','set','设置直推上级')) {
            $button[2]['btn'][] = array
            (
                'type' => 'edit',
                'link' => Dever::url('project/database/update?project=agent&table=member_parent&mid='.$member['id'], 'manage'),
                'name' => '设置直推上级',
            );
        }
        if (Dever::load('manage/auth')->checkFunc('agent.member','setmessage','发送短信')) {
            $table = 'template';
            $res = Dever::load('sms/api')->getTable($table);
            $button[2]['btn'][] = array
            (
                'type' => 'edit',
                
                'link' => Dever::url($res.'&search_option_state=1&search_option_type=daili_reg_yes,daili_reg_no,daili_goods_no,daili_tixian_yes,daili_tixian_no,daili_hetong_no&search_option_name='.$member['name'].'&search_option_mobile='.$member['mobile'], 'manage'),
                'name' => '发送短信',
            );
        }


        /*
        if (Dever::load('manage/auth')->checkFunc('agent.member', 'edit6', '查看关系图谱')) {
            $button[2]['btn'][] = array
            (
                'type' => 'link',
                'link' => Dever::url('lib/manage.relation?mid='.$member['id'].'&[refer].manage', 'agent'),
                'name' => '查看关系图谱',
            );
        }
        */
        $head_btn[] = array
        (
            'type' => 'link',
            'link' => 'refer',
            'name' => '返回上一页',
        );
        $head = array
        (
            'name' => '基本信息',
            'btn' => $head_btn,
        );
        $foot = $button;
        $html = Dever::show($head, $result, $foot, false, 'dls-user-box');

        return $html;
    }

    public function getShowInfo($member)
    {
        $role = Dever::db('setting/role')->one($member['role']);
        $level = Dever::db('setting/level')->one($member['level_id']);
        $area = Dever::load("area/api.string", $member['area'], '-', true, '');
        $code = Dever::load("invite/api.code", $member['id']);
        $title = Dever::db('setting/title')->getDataByIds(array('id' => $member['title_id']));
        $title_name = array();
        if ($title) {
            foreach ($title as $k => $v) {
                $title_name[] = $v['name'];
            }
        }
        $title_name = implode('、', $title_name);
        if ($member['shop_id']) {
            $shop = Dever::db('shop/info')->one($member['shop_id']);
        } else {
            $shop['id'] = -1;
            $shop['name'] = '无';
        }

        $result = array();

        if ($level) {
            $role['name'] .= '（' . $level['name'] . '）';
        }

        if (!$member['cash']) {
            $member['cash'] = 0;
        }

        if (!$member['sell']) {
            $member['sell'] = 0;
        }

        if (!$member['group_sell']) {
            $member['group_sell'] = 0;
        }

        if (!$member['avatar']) {
            $default = Dever::db('passport/avatar')->one(1);
            $member['avatar'] = $default['avatar'];
        }

        $result = array
        (
            'type' => 'user',
            'pic' => $member['avatar'],
            'content' => array
            (
                $member['name'],
                array('手机号', $member['mobile']),
                array('邀请码', $code),
                array('代理商头衔', $title_name),
                array('代理角色', $role['name']),
                array('代理区域', $area),
                array('所属店铺', $shop['name']),
                array('资金余额', '<span style="color:blue">¥' . $member['cash'] . '元</span>'),
                array('直推业绩', '<span style="color:blue">¥' . $member['sell'] . '元</span>'),
                array('团队业绩', '<span style="color:blue">¥' . $member['group_sell'] . '元</span>'),
            ),
        );

        return $result;
    }

    public function setStatus_api()
    {
        $where['where_id'] = Dever::input('id');
        $where['status'] = Dever::input('value');

        Dever::db('agent/member')->update($where);

        return 'reload';
    }

    public function create($data)
    {
        $update['mobile'] = $data['mobile'];
        $info = Dever::db('agent/member')->find(array('mobile' => $data['mobile']));

        $update['name'] = $data['name'];
        $update['idcard'] = $data['idcard'];
        $update['idcard_front'] = $data['idcard_front'];
        $update['idcard_back'] = $data['idcard_back'];
        $update['company_name'] = $data['company_name'];
        $update['company_pic'] = $data['company_pic'];
        $update['company_number'] = $data['company_number'];
        $update['parent_mid'] = $data['parent_mid'];
        $update['source_id'] = $data['source_id'];
        $update['type'] = $data['type'];

        if (!$info) {
            $update['role'] = 1;
            $update['level_id'] = -1;
            
            $mid = Dever::db('agent/member')->insert($update);
            /*
            if ($mid && $data['parent_mid'] && $data['parent_mid'] > 0) {
                Dever::load('invite/api')->setRelation($mid, $data['parent_mid']);
            }
            */
            
            return $mid;
        } else {
            $update['where_id'] = $info['id'];
            Dever::db('agent/member')->update($update);
            return $info['id'];
        }
    }
    #认证时间
    public function set_rdate($id){
        $info=Dever::db('agent/member')->one(array('id'=>$id,'state'=>1));
        if($info['cdate']){
            $data['注册时间']=date('Y-m-d H:i',$info['cdate']);
        }else{
            $data['注册时间']='-';
        }
        if($info['status']==2){
            if($info['rdate']){
                $data['认证时间']=date('Y-m-d H:i',$info['rdate']);
            }else{
                $data['认证时间']='-';
            }
        }else{
            $data['认证时间']='-';
        }
        // $html = Dever::table($data);
        return '注册时间：'.$data['注册时间'].'<br/>认证时间：'.$data['认证时间'];
    
    }
    #更改收件人信息
    public function set_mobile($id){
        $info=Dever::db('agent/dh_order')->one(array('id'=>$id,'state'=>1));
        $data=array();
        if($info && $info['name']=='兑换商品'){
            $address=Dever::db('agent/member_address')->one(array('id'=>$info['address_id'],'state'=>1));
            $data['联系人']=$address['contact'];
            $data['联系电话']=$address['mobile'];
            $res=Dever::load('agent/lib/address')->getOne($address['mid'],$address['id']);
            $data['详细地址']=$res['address_info'].$address['house_number'];
            // $data['门牌号']=$address['house_number'];
            return $data['联系人'].'<br/>'.$data['联系电话'].'<br/>'.$data['详细地址'];
        }else{
            return '';
        }
        // $html=Dever::table($data);
        // return $html;
    }
    public function daili($id){
        $info=Dever::db('agent/member')->one(array('id'=>$id,'state'=>1));
        $member['role'] = Dever::db('setting/role')->find($info['role']);
        $name ='<a href='.Dever::url('project/database/list&project=bill&table=sell&menu=bill&search_option_state=1&function=&method=search&search_api_mid='.$info['mobile'].'&search_fulltext_order_num=&search_option_start_cdate=&search_option_end_cdate=&excel=2&page_type=2', 'manage').'>'.$info['name'].'</a>';
        $mobile = $info['mobile'];
        $role = $member['role']['name'];
        if (isset($info['level_id']) && $info['level_id'] && $info['level_id'] > 0) {
                $info['level'] = Dever::db('setting/level')->find($info['level_id']);
                $role .= '（'.$info['level']['name'].'）';
        }
        $html = $name.'<br/>'.$mobile.'<br/>'.$role;
        return $html;
    }
    #上级信息
    public function sdaili($id){
        $res=Dever::db('agent/member')->one(array('id'=>$id,'state'=>1));
        if($res['parent_mid']>0){
            $info=Dever::db('agent/member')->one(array('id'=>$res['parent_mid'],'state'=>1));
            $member['role'] = Dever::db('setting/role')->find($info['role']);
            $data['姓名']='<a href='.Dever::url('project/database/list&project=bill&table=sell&menu=bill&search_option_state=1&function=&method=search&search_api_mid='.$info['mobile'].'&search_fulltext_order_num=&search_option_start_cdate=&search_option_end_cdate=&excel=2&page_type=2', 'manage').'>'.$info['name'].'</a>';
            $data['电话']=$info['mobile'];
            $data['角色']=$member['role']['name'];
            if (isset($info['level_id']) && $info['level_id'] && $info['level_id'] > 0) {
                    $info['level'] = Dever::db('setting/level')->find($info['level_id']);
                    $data['角色'] .= '（'.$info['level']['name'].'）';
            }                
            $data['团队业绩']=$info['group_sell'];
            $html = $data['姓名'].'<br/>'.$data['电话'].'<br/>'.$data['角色'].'<br/>'.$data['团队业绩'];
            return $html;
        }elseif($res['source_id'] && $res['source_id'] >=1){
            $source=Dever::db('setting/source')->one(array('id'=>$res['source_id'],'state'=>1));
            $data['来源']=$source['name'];
            return $html =$data['来源'];
        }else{
            return '无';
        }        
    }

    public function search_api()
    {
        $value = Dever::input('value');
        if ($value) {
            $parent_mid = Dever::load('invite/api')->getUid($value);
            $table = array();
            if ($parent_mid) {
                $member = Dever::db('agent/member')->find($parent_mid);
                if (!$member) {
                    return '暂无账号';
                }
                $table = array();
                $table['head'] = array('姓名', '手机号');
                $table['body'] = array();
                $table['body'][] = array($member['name'], $member['mobile']);

                $result[] = array
                (
                    'type' => 'table',
                    'content' => $table,
                );

                $html = Dever::show('', $result, false, false);
                return $html;
            }

            return '暂无账号';
        } else {
            return '';
        }
    }
    #新增业绩（去掉直推最大一组市场，其他小组市场新增业绩之和）
    public function new_money($id){
        $start = Dever::input('search_option_start_dever_time');
        $end = Dever::input('search_option_end_dever_time');
        $where = array();
        if($start){
            $where['start'] = $start;
        }
        if($end){
            $where['end'] = $end;
        }
        $where['mid'] = $id;
        $where['state'] = 1;
        $where['type'] = 2;
        $where['stype'] = 1;
        $info = Dever::db('bill/sell')->getNewChildParent($where);
        $temp = array();
        $sell = 0;
        $max = count($info);
        if ($max > 0) {
            foreach ($info as $key => $val) {
                $temp[] = $val['total']; // 用一个空数组来承接字段
                $sell += $val['total'];
            }
            if($temp){
                $total = sprintf ('%01.2f',$sell - max($temp));
            }else{
                $total = sprintf ('%01.2f',$sell);
            }
        } else {
            $total = 0;
            foreach ($info as $key => $val) {
                $total += $val['total'];
            }
            $total = sprintf ('%01.2f',$total);
        }
        return $total;
    }
    #新增正常业绩
    public function new_zmoney($id){
        $start = Dever::input('search_option_start_dever_time');
        $end = Dever::input('search_option_end_dever_time');
        $where = array();
        if($start){
            $where['start'] = $start;
        }
        if($end){
            $where['end'] = $end;
        }
        $where['mid'] = $id;
        $where['state'] = 1;
        $where['type'] = 2;
        $where['stype'] = 1;
        $info = Dever::db('bill/sell')->getNewParent($where);

        $total = sprintf ('%01.2f',$info['total']);
        return $total;
    }
    #头衔统计数据导出
    public function out_member($data){
        $header = array('姓名', '手机号', '代理角色', '代理头衔', '直推业绩', '团队业绩', '新增业绩', '新增正常业绩');
        $body = array();
        foreach($data as $k => $v){
            $role = Dever::db('setting/role')->find($v['role']);
            $title = Dever::db('setting/title')->find($v['title_id']);
            $new_sell = $this->new_money($v['id']);
            $new_zsell = $this->new_zmoney($v['id']);
            $d = array
            (
                $v['name'],
                $v['mobile'],
                $role['name'],
                $title['name'],
                $v['sell'],
                $v['group_sell'],
                $new_sell,
                $new_zsell,
            );
            $body[] = $d;
        }
        $file = Dever::input('excel_name');
        return Dever::excelExport($body, $header, $file);
    }
}