<?php 
namespace Admin\Controller;
class ExtensionListController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        $separate=$this->separate=array('0'=>'未分成','1'=>'已分成','2'=>'取消分成');
        $this->assign('separate',$separate);
        $separate_type=$this->separate_type=array('0'=>'拓客奖励','1'=>'推荐订单分成');
        $this->assign('separate_types',$separate_type);
        $is_auth=check_use_auth("NationalCustomers");
        if($is_auth['status']==0){
            $this->error('该版本没有此权限',$is_auth['url']);return;
        }
    }
    public function index()
    {
        $post = I('param.');
        $map=array();
        if($post) {
            if ($post['reg_from'] && !$post['reg_to']) $map['addtime'] = array(array('gt', strtotime($post['reg_from'])));
            if ($post['reg_to'] && !$post['reg_from']) $map['addtime'] = array(array('lt', strtotime($post['reg_to'])));
            if ($post['reg_from'] && $post['reg_to']) $map['addtime'] = array(array('gt', strtotime($post['reg_from'])), array('lt', strtotime($post['reg_to'])));
            if (intval($post['is_separate']) >= 0 && !empty($post['is_separate'])) {
                if ($post['is_separate'] == 1) {
                    $map['is_separate'] = 1;
                } elseif (is_numeric($post['is_separate']) == 0) {
                    $map['is_separate'] = 0;
                } elseif ($post['is_separate'] == 2) {
                    $map['is_separate'] = 2;
                }
            }
            if (intval($post['separate_type']) >= 0 && !empty($post['separate_type'])) {
                if (is_numeric($post['separate_type']) == 0) {
                    $map['separate_type'] = 0;
                } elseif ($post['separate_type'] == 1) {
                    $map['separate_type'] = 1;
                }
            }
            if($post['keyword']){
                $goods_map=array();
                if (is_numeric($post['keyword'])) {
                    $goods_map['id|username'] = array(intval($post['keyword']), array('like', '%' . $post['keyword'] . '%'), '_multi' => true);
                } else if (!empty($post['keyword'])) {
                    $goods_map['username'] = array('like', '%' . (string)$post['keyword'] . '%');
                }
                if (!empty($goods_map)) {
                    $goodsids = M('Member')->field('id')->where($goods_map)->select();
                    $goods_ids = array();
                    foreach ($goodsids as $k => $v) {
                        $goods_ids[$k] = $v['id'];
                    }
                    if (!empty($goods_ids)) {
                        $map['uid'] = array('in', $goods_ids);
                    }
                }
            }
            if($post['order_id']){
                $goods_map=array();
                $goods_map['order_sn'] =$post['order_id'];
                $goodsids = M('OrderInfo')->field('id,order_sn')->where($goods_map)->select();
                $goods_ids = array();
                foreach ($goodsids as $k => $v) {
                    $goods_ids[$k] = $v['id'];
                }
                if (!empty($goods_ids)) {
                    $map['order_id'] = array('in', $goods_ids);
                }
            }
        }
        $this->assign('post',$post);
        $list = $this->lists('AffiliateLog', $map, 'id desc');
        foreach($list as $key=>$val){
            $list[$key]['username']=$this->get_user_name($val['uid']);
            $data=array(
                'total_money'=>array_sum(array_map(function($val){return $val['money'];},$list)),
                'total_points'=>array_sum(array_map(function($val){return $val['point'];},$list)),
            );
        }
        $this->assign('list', $list);
        $this->assign('data', $data);
        $this->assign('is_separate', isset($post['is_separate'])?$post['is_separate']:'');
        $this->display();
    }
    public function add(){
        $id=I('id');
        $map['id']=$id;
       $desc = M("AccountLog")->where($map)->getField('change_desc');
        $this->ajaxReturn($desc);
    }

    public function del()
    {
        $postdata = I('post.');
        $id = I('get.id');
        if (empty($postdata['id']) && !empty($id)) $postdata['id'][] = $id;
        if (empty($postdata['id'])) {
            $this->error('请选择要取消分成的项');
        }
        $data['is_separate'] = 2;
        foreach ($postdata['id'] as $key => $value) {
            $map['id'] = $value;
            $red = M('AffiliateLog')->where($map)->save($data);
        }
        if ($red > 0) {
            $this->success('推荐人的分成取消成功！');
        } else {
            $this->error('推荐人的分成取消失败！');
        }
    }
    /**
     * 导出批发商品
     */
    public function export_pur()
    {
        $post = I('param.');
        if($post) {
            if ($post['reg_from'] && !$post['reg_to']) $map['addtime'] = array(array('gt', strtotime($post['reg_from'])));
            if ($post['reg_to'] && !$post['reg_from']) $map['addtime'] = array(array('lt', strtotime($post['reg_to'])));
            if ($post['reg_from'] && $post['reg_to']) $map['addtime'] = array(array('gt', strtotime($post['reg_from'])), array('lt', strtotime($post['reg_to'])));
            if (intval($post['is_separate']) >= 0) {
                if ($post['is_separate'] == 1) {
                    $map['is_separate'] = 1;
                } elseif ($post['is_separate'] == 0) {
                    $map['is_separate'] = 0;
                } elseif ($post['is_separate'] == 2) {
                    $map['is_separate'] = 2;
                }
            }
            if (intval($post['separate_type']) >= 0) {
                if ($post['separate_type'] == 0) {
                    $map['separate_type'] = 0;
                } elseif ($post['separate_type'] == 1) {
                    $map['separate_type'] = 1;
                }
            }
            if($post['keyword']){
                $goods_map=array();
                if (is_numeric($post['keyword'])) {
                    $goods_map['id|username'] = array(intval($post['keyword']), array('like', '%' . $post['keyword'] . '%'), '_multi' => true);
                } else if (!empty($post['keyword'])) {
                    $goods_map['username'] = array('like', '%' . (string)$post['keyword'] . '%');
                }
                if (!empty($goods_map)) {
                    $goodsids = M('Member')->field('id')->where($goods_map)->select();
                    $goods_ids = array();
                    foreach ($goodsids as $k => $v) {
                        $goods_ids[$k] = $v['id'];
                    }
                    if (!empty($goods_ids)) {
                        $map['uid'] = array('in', $goods_ids);
                    }
                }
            }
            if($post['order_id']){
                $goods_map=array();
                $goods_map['order_sn'] =$post['order_id'];
                $goodsids = M('OrderInfo')->field('id,order_sn')->where($goods_map)->select();
                $goods_ids = array();
                foreach ($goodsids as $k => $v) {
                    $goods_ids[$k] = $v['id'];
                }
                if (!empty($goods_ids)) {
                    $map['order_id'] = array('in', $goods_ids);
                }
            }
        }
        $list = M('AffiliateLog')->where($map)->field(true)->select();
        $info=array();
        foreach($list as$key=>$val){
            $info=array(
                'tr'=>'-',
                'tr'=>'-',
                'tr'=>'-',
                'tr'=>'-',
                'tr'=>'-',
                'total_money'=>array_sum(array_map(function($val){return $val['money'];},$list)),
                'total_points'=>array_sum(array_map(function($val){return $val['point'];},$list)),
            );
        }
        $title = array('序号', '推荐人', '盈利金额', '分佣积分','分成详情','订单号','分成类型', '分成状态' ,'变更时间');
        // echo "<pre>";
        // print_r($postdata);exit;
        $data = array();
        foreach ($list as $k => $v) {
            $data[$k][] = $v['id'];
            $data[$k][] = $this->get_user_name($v['uid']);
            $data[$k][] = $v['money'];
            $data[$k][] = $v['point'];
            $data[$k][] = $v['desc'];
            $data[$k][] = idtoname($v['order_id'],"OrderInfo",'order_sn',false);
            $data[$k][] = $this->separate_type[$v['separate_type']];
            $data[$k][] = $this->separate[$v['is_separate']];
            $data[$k][] = date('Y-m-d H:i:s',($v['addtime']));

        }
       $data[]=array(
            '总计：',
           $info['tr'],
           $info['total_money'],
           $info['total_points'],
           $info['tr'],
           $info['tr'],
           $info['tr'],
           $info['tr'],
           $info['tr'],
        );
        export_xls($title, $data, 'utf8');
        unset($title, $data);
        exit;
    }
    private function get_user_name($uid){
        if(empty($uid))return false;
        $map=array();
        $map['id']=$uid;
        $member_info=M("Member")->where($map)->field('username,nickname,mobile')->find();
        if($member_info['username']){
            $username=$member_info['username'];
        }elseif($member_info['nickname']){
            $username=$member_info['nickname'];
        }else{
            $username=$member_info['mobile'];
        }
        return $username;
    }
}
?>