<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Uapp\Controller;
class LittleShopController extends BaseController {
	public function _initialize(){
        parent::_initialize ();
        // $user_info=$this->checkuserauth();
        // if(empty($user_info) || $user_info['is_littleshop']==0){
        //     $return['status'] = 3;
        //     $return['msg'] = '登录信息或小店信息有误';
        //     $this->ajaxReturn($return);exit();
        // }
       
    }
    public function index(){
        $return = array('status'=>3,'msg'=>'数据获取失败');
        $user_info=$this->checkuserauth();
        if(empty($user_info) || $user_info['is_littleshop']==0){
            $return['status'] = 3;
            $return['msg'] = '登录信息或小店信息有误';
            $this->ajaxReturn($return);exit();
        }
        $postdata = I('param.');
        $postdata['shop_id']=$postdata['lshop_id'];
        if($postdata['shop_id']){
            $shop_id=$postdata['shop_id'];
            if($postdata['eid']){
                if($user_info['id'])$duid=$user_info['id'];else $duid=0;
                if($duid==$postdata['eid']){
                    $is_show=1;
                }else{
                    $is_show=2;
                }
                $return['data']['is_show'] = $is_show;
            }
        }else{
            $shop_id=intval(idtoname($user_info['id'],'LittleShop','id','uid',false));
        }
        // if(!$shop_id){
        //     //还没有开通小店 跳转 指定区域商品页面
        //     $return['data']['shop_url'] = $shop_id;
        //     $this->redirect('Mobile/LittleShop/no_shop');
        // }
        $where=array();
        $where['id'] = $shop_id;
        $info=M('LittleShop')->where($where)->field(true)->find();
        if($info){
            if(empty($info['remark'])) $info['remark'] = '比较懒，什么也没留下！';
            $info['logo'] = format_url($info['logo']);
            $info['surface_plot'] = format_url($info['surface_plot']);
            //获取我的下级中的所有注册会员
            $ranks = array();
            $ranks['rank_id'] = 1;
            $uids=get_all_child_id($user_info['id'],$ranks);
            array_push($uids,$user_info['id']);
            //总营业额
            $map=array();
            $map['pay_status']=3;
            $map['is_delete']=0;
            $map['status']=3;
            $map['type']=14;//外卖订单
            $map['uid']=array('in',$uids);
            $order_info = M('OrderInfo')->where($map)->field('sum(goods_amount) as sellmoney,count(id) as order_num')->select();
            $info['sellmoney']=format_price($order_info[0]['sellmoney']);
            //总订单数
            $info['order_num']=$order_info[0]['order_num'];
             //商品数
            $g_map['lg.shop_id']=$shop_id;
            $g_map['lg.is_putaway']=0;
            $g_map['g.status']=1;
            $g_map['g.publish_wait']=1;
            $info['goods_num']=intval(M('LittleShopGoods')->alias('lg')->where($g_map)->join('LEFT JOIN __GOODS__ g ON g.id=lg.goods_id')->count());

            $return['status'] = 1;
            $return['data']['info'] = $info;

        }

       
        $return['data']['goods_ids'] = $goods_ids;
        $return['data']['shop_id'] = $shop_id;

        $before_title=$title?$title:'我的小店';
        $return['data']['title'] = $before_title;
        $this->ajaxReturn($return);
    }
    /*
    * 小店精选
    */
    public function choice(){
        $return = array('status'=>3,'msg'=>'数据获取失败');
        $postdata = I('param.');
        $lshop_id = $postdata['lshop_id'];
        // $lshop_id = cookie('lshop_id');
        // $user_info=$this->checkuserauth();
        $uid=$postdata['uid'];
        // if(!$uid){
        //     $return['status'] = 3;
        //     $return['msg'] = '请先登录';    
        //     $this->ajaxReturn($return);exit();
        // }
        // $user_shop_id=intval(idtoname($uid,'LittleShop','id','uid',false));
        // 获取用户自己小店或者推荐人小店信息
        $user_shop_id=intval(D('Wxsmall/Login')->get_lshop_id($uid));
        if($lshop_id){
            $shop_id=$lshop_id;
        }else{
            $shop_id=$user_shop_id;
        }
        if($lshop_id == $user_shop_id){
            $return['data']['is_lshop']=1;
        }else{
            $return['data']['is_lshop']=0;
        }
        if($shop_id=='false' || !$shop_id || empty($shop_id)){
            $return['status'] = 1;
            $return['url'] = U('Mobile/ProLevel/index');
            $return['msg'] = '去晋升';
            // -----晋升权益-------
            $member_rank=$info=array();
            if($uid){
                $u_map['id']=$uid;
                // $u_map['id']=29;
                $info=M('Member')->where($u_map)->field(true)->find();
                $info['rank_name']=idtoname($info['rank_id'],'MemberRank','name');
                $member_rank = M('MemberRank')->where('status=1')->select();
                foreach ($member_rank as $key => $value) {
                    $level_pro = unserialize($value['level_pro']);
                    $member_rank[$key]['config']=$level_pro;
                    if($level_pro['pro_type']==1)$new_array[]=$value['id'];
                    else{
                        unset($member_rank[$key]);
                    }
                }
            }
                
            // print_r($member_rank);
            $map = array();
            $map['name'] = 'PRO_GOODS';
            $res = M('Config')->field('value')->where($map)->find();

            $thisinfo=unserialize($res['value']);
            $goods_ids=implode(',',unserialize($thisinfo['temp']));
            $map['id'] = array('in',$goods_ids);
            $goods_list = M('Goods')->where($map)->field('id,img,goods_name,goods_sn,shop_price,market_price')->select();
            if($goods_list){
                foreach ($goods_list as $key => $value) {
                    $goods_list[$key]['img']=thumb($value['img'],300,300);
                    $goods_list[$key]['shop_price']=format_price($value['shop_price']);
                    $goods_list[$key]['market_price']=format_price($value['market_price']);
                }
            }
            $return['data']['info']=$info;
            $return['data']['rank_list']=$member_rank;
            $return['data']['goods_list']=$goods_list;
            // --------------end-------------
            $this->ajaxReturn($return);exit();
        }
        $info=M('LittleShop')->field(true)->find($shop_id);
        if($info){
            $info['surface_plot']=format_url($info['surface_plot']);
            // if($info['logo']){
                $info['logo']=format_url($info['logo']);
            // }
            // else{
            //     $info['logo']=format_url($user_info['header_img']);
            // }            
        }
        $map=array();
        $map['shop_id']=$shop_id;
        $map['is_putaway']=0;
        $map['is_show']=1;
        $goods_ids_show=M('LittleShopGoods')->where($map)->getField('goods_id',true);
        $map['is_show']=0;
        $goods_ids_none=M('LittleShopGoods')->where($map)->getField('goods_id',true);
        // print_r($goods_ids);die;
        if($goods_ids_show){
            $goods_ids=implode(',', $goods_ids_show);
            $where=array();$where['id']='';if($goods_ids)$where['id']=array('in',$goods_ids);
            $goods_list_show=array();
            $goods_list_show=M('Goods')->where($where)->field('id,goods_name,img,sell_count,shop_price,market_price,best_img')->select();
            if($goods_list_show){
                foreach ($goods_list_show as $key => $value) {
                    if($value['best_img']){
                        $goods_list_show[$key]['img']=format_url($value['best_img']);
                    }else{
                        $goods_list_show[$key]['img']=format_url($value['img']);
                    }
                    $goods_list_show[$key]['shop_price']=format_price($value['shop_price']);
                    $goods_list_show[$key]['market_price']=format_price($value['market_price']);
                }
            }                
        }
        if($goods_ids_none){
            $goods_ids=implode(',', $goods_ids_none);
            $where=array();$where['id']='';if($goods_ids)$where['id']=array('in',$goods_ids);
            $goods_list_none=array();
            $goods_list_none=M('Goods')->where($where)->field('id,goods_name,img,sell_count,shop_price,market_price,best_img')->select();
            if($goods_list_none){
                foreach ($goods_list_none as $key => $value) {
                    $goods_list_none[$key]['img']=format_url($value['img']);
                    $goods_list_none[$key]['shop_price']=format_price($value['shop_price']);
                    $goods_list_none[$key]['market_price']=format_price($value['market_price']);
                }
            }                
        }
        $return['status']=1;
        $return['data']['user_shop_id']=$user_shop_id;
        $return['data']['is_littleshop']=$user_info['is_littleshop'];
        $return['data']['shop_info']=$info;
        $return['data']['shop_id']=$shop_id;
        $return['data']['goods_list_show']=$goods_list_show;
        $return['data']['goods_list_none']=$goods_list_none;
        $before_title=$title?$title:'店铺精选';
        $return['data']['title'] = $before_title;
        $this->ajaxReturn($return);
    }
    /*
    * 等级晋升
    */
    public function pro_level(){
        $return = array('status'=>3,'msg'=>'数据获取失败');
        $postdata = I('param.');
        $lshop_id = $postdata['lshop_id'];
        // $lshop_id = cookie('lshop_id');
        // $user_info=$this->checkuserauth();
        // $uid=$user_info['id'];
        // if(!$uid){
        //     $return['status'] = 3;
        //     $return['msg'] = '请先登录';    
        //     $this->ajaxReturn($return);exit();
        // }
        $uid=$postdata['uid'];
        // $user_shop_id=intval(idtoname($uid,'LittleShop','id','uid',false));
        // 获取用户自己小店或者推荐人小店信息
        $user_shop_id=intval(D('Wxsmall/Login')->get_lshop_id($uid));
        if($lshop_id){
            $shop_id=$lshop_id;
        }else{
            $shop_id=$user_shop_id;
        }
        if($lshop_id == $user_shop_id){
            $return['data']['is_lshop']=1;
        }else{
            $return['data']['is_lshop']=0;
        }
        $return['status'] = 1;
        $return['url'] = U('Mobile/ProLevel/index');
        $return['msg'] = '去晋升';
        // -----晋升权益-------
        $u_map['id']=$uid;
        // $u_map['id']=29;
        $info=M('Member')->where($u_map)->field(true)->find();
        $info['rank_name']=idtoname($info['rank_id'],'MemberRank','name');
        $member_rank = M('MemberRank')->where('status=1')->select();
        foreach ($member_rank as $key => $value) {
            $level_pro = unserialize($value['level_pro']);
            $member_rank[$key]['config']=$level_pro;
            if($level_pro['pro_type']==1)$new_array[]=$value['id'];
            else{
                unset($member_rank[$key]);
            }
        }
        // print_r($member_rank);
        $map = array();
        $map['name'] = 'PRO_GOODS';
        $res = M('Config')->field('value')->where($map)->find();

        $thisinfo=unserialize($res['value']);
        $goods_ids=implode(',',unserialize($thisinfo['temp']));
        $map['id'] = array('in',$goods_ids);
        $goods_list = M('Goods')->where($map)->field(true)->select();
        if($goods_list){
            foreach ($goods_list as $key => $value) {
                $goods_list[$key]['format_spec'] = D("Common/GoodsAttr")->getattr($value['id']);
                $goods_list[$key]['img']=thumb($value['img'],300,300);
                $goods_list[$key]['shop_price']=format_price($value['shop_price']);
                $goods_list[$key]['market_price']=format_price($value['market_price']);
                $cart_num=0;
                if($uid){
                    $c_map = array();
                    $c_map['is_valid'] = 1;
                    $c_map['user_id'] = $uid;
                    $c_map['goods_id'] = $value['id'];
                    $c_map['shop_id'] = $postdata['shop_id'];
                    $cart_num=M('Cart')->where($c_map)->getField('goods_num');
                }
                $goods_list[$key]['cart_num']=$cart_num;
            }
        }
        $cart_info=array();
        if($uid && $uid!=false)$cart_info=D("Wxsmall/Takeout")->get_cart_total_info($uid);
        $return['data']['cart_info']=$cart_info;
        $return['data']['info']=$info;
        $return['data']['rank_list']=$member_rank;
        $return['data']['goods_list']=$goods_list;
        // --------------end-------------
        $this->ajaxReturn($return);exit();
    }

    /**
     *我的商品在售
     */
    public function my_goods_up(){
        $return=array('status'=>2,'msg'=>'已加载全部数据');
        $postdata = I('param.');
        // $return = array('status'=>1,'msg'=>'数据获取失败');
        $user_info=$this->checkuserauth();
        if(empty($user_info) || $user_info['is_littleshop']==0){
            $return['status'] = 3;
            $return['msg'] = '登录信息或小店信息有误';
            $this->ajaxReturn($return);exit();
        }
        $map=array();
        
        if($postdata['desc']){
            // $map['shop_id']=$postdata['lshop_id'];//小店id
            $order=$postdata['desc'].' desc';
            $return['data']['desc'] = $postdata['desc'];
        }else{
            $order = 'id desc';
        }
        $map['shop_id']=intval(idtoname($user_info['id'],'LittleShop','id','uid',false));
        $shop_info = M('LittleShop')->where('id='.$map['shop_id'])->field(true)->find();
        if($shop_info) $return['data']['shop_info'] = $shop_info;        
        // 小店名称
        $shop_name=idtoname($user_info['id'],'LittleShop','name','uid',false);
        if($postdata['type']==0 || empty($postdata['type'])) $map['is_putaway']=0; //上架
        if($postdata['type'] && $postdata['type']==2) $map['is_putaway']=1; //下架
        
        $upgoods_ids=M('LittleShopGoods')->where($map)->getField('goods_id',true);

        
         if(!empty($upgoods_ids)){
            $upstrids=implode(',',$upgoods_ids);
            $where['id']=array('in',$upstrids);
            $field='id,img,goods_name,sell_count,addtime,shop_price';
            $upgoods_list = page_list('Goods',$where,$order,$field,3);

            // var_dump($upgoods_list); echo '==='; die;
            // $upgoods_list=M('Goods')->where($where)->field('id,img,goods_name,sell_count,addtime,shop_price')->order($order)->select();
            $return['data']['totalpage'] = $upgoods_list['totalPages'];
            if($upgoods_list['list']){
                foreach($upgoods_list['list'] as $ks=>$vs){
                    $upgoods_list['list'][$ks]['img'] = format_url($vs['img']);
                    $upgoods_list['list'][$ks]['addtime'] = time_format($vs['addtime'],'Y-m-d');
                    $wh=array();
                    $wh['shop_id']=$shop_info['id'];
                    $wh['goods_id']=$vs['id'];
                    $is_show=M('LittleShopGoods')->where($wh)->getField('is_show');
                    $upgoods_list['list'][$ks]['is_show'] = $is_show;
                    $is_putaway = M('LittleShopGoods')->where($wh)->getField('is_putaway');
                    if($is_putaway==0){
                        $upgoods_list['list'][$ks]['is_putaway'] = '下架';
                        $upgoods_list['list'][$ks]['is_put'] = 0;
                    }
                    if($is_putaway==1){
                        $upgoods_list['list'][$ks]['is_putaway'] = '上架';
                        $upgoods_list['list'][$ks]['is_put'] = 1;
                    }

                    // if($is_show==1) $upgoods_list[$ks]['is_show'] = '已推荐';
                    // else $upgoods_list[$ks]['is_show'] = '推荐';
                    
                }
                $return['status'] = 1;
                

            }else{
                $return['status'] = 2;
                $return['msg'] = '已加载全部数据';
            }
            
        }

        $put_arr=array();
        $put_arr['shop_id'] = $map['shop_id'];
        $put_arr['is_putaway'] =1;
        // $map['is_putaway']=1; //下架
        $down_num=M('LittleShopGoods')->where($put_arr)->count();
        unset($put_arr['is_putaway']);
        $put_arr['is_putaway']=0; //上架
        $up_num=M('LittleShopGoods')->where($put_arr)->count();

        $return['data']['down_num'] = intval($down_num);
        $return['data']['up_num'] = intval($up_num);
        $return['data']['upgoods_list'] = $upgoods_list['list'];
        $return['data']['shop_id'] = $map['shop_id'];
        $before_title=$title?$title:'我的商品';
        $return['data']['page_title'] = $before_title;
        $this->ajaxReturn($return);
    }


       //全部订单
    public function order_info(){
        $return['status']=3;
        $postdata=I('param.');
        $user_info=$this->checkuserauth();
        //获取我的下级中的所有注册会员
        $where['rank_id']=1;
        $uids=get_all_child_id($user_info['id'],$where);
        array_push($uids,$user_info['id']);
        $map['pay_status']=3;
        $map['is_delete']=0;
        $map['status']=3;
        $map['type']=14;//外卖订单
        $map['uid']=array('in',$uids);
       
        
        // $page=empty($postdata['page'])?1:$postdata['page'];
        $all_order = page_list('OrderInfo',$map,'id desc',true);
        //格式化订单数据
        if($all_order['list']){
            foreach($all_order['list'] as $k=>$v){
                $goods_info['all_num']=$goods_info['num']=0;
                // $all_order['list'][$k]=$v;
                if($v['type']==3){
                    $all_order['list'][$k]['goods_amount']=$v['goods_amount']-$v['used_score_money'];
                }else{
                    $all_order['list'][$k]['goods_amount']=$v['goods_amount'];
                }
                if($v['shop_id']){
                    $all_order['list'][$k]['shop_name']=idtoname($v['shop_id'],'Shops','name');
                }
                $order_operation=D('Wxsmall/Member')->order_operation($v);
                $all_order['list'][$k]['button_list']=$order_operation['button_list'];
                $all_order['list'][$k]['order_status']=$order_operation['order_status'];
                $goods_map['order_id']=$v['id'];
                $goods_info['num']=M('OrderGoods')->where($goods_map)->count();
                if($goods_info['num']) {
                    $goods_info['list']=M('OrderGoods')->where($goods_map)->getField('id,goods_id,goods_name,goods_attr,goods_price,goods_number,status,goods_integral',true);
                    foreach ($goods_info['list'] as $ke => $vo) {
                        $attr = unserialize($vo['goods_attr']);
                        $goods_info['list'][$ke] = $vo;
                        $goods_info['all_num'] += $vo['goods_number'];
                        $goods_info['list'][$ke]['goods_price'] = $vo['goods_price'];
                        $goods_info['list'][$ke]['img'] = idtoname($vo['goods_id'], 'Goods', 'img');
                        $goods_info['list'][$ke]['attr_name'] = $attr['name'];
                        $goods_info['list'][$ke]['status'] = $vo['status'];
                        $goods_info['list'][$ke]['goods_number']=intval($vo['goods_number']);
                        $goods_info['list'][$ke]['img']=thumb($goods_info['list'][$ke]['img'],430,430);
                    }
                    $all_order['list'][$k]['goods']=$goods_info;
                }else{
                    unset($all_order['list'][$k]);
                }

            }
            foreach ($all_order['list'] as $key => $value) {
                $all_order['list'][$key]['all_num_desc']='';
                if($value['goods']['all_num']>0){$all_order['list'][$key]['all_num_desc']="共".$value['goods']['all_num']."件商品  ";}
            }
            
            // if($all_order['count'] > $page_num*$page){
            //     $all_order['show'] = 1;
            // }else{
            //     $all_order['show'] = 0;
            // }

            $return['data']=$all_order;
            $return['status']=1;
        }else{
            $return['status'] = 2;
            $return['msg']='已加载全部数据';
        }
        $this->ajaxReturn($return);
    }
	
    //是否推荐
    public function go_isshow(){
        $return = array('status'=>1,'msg'=>'');
        $user_info=$this->checkuserauth();
        $postdata=I('param.');
        $garr=array();
        $garr['goods_id'] = $postdata['goodsid'];
        $garr['is_show'] = $postdata['is_show'];
        $garr['shop_id'] = idtoname($user_info['id'],'LittleShop','id','uid',false);
        $count = M('LittleShopGoods')->where($garr)->count();
       
        if($count==0){
            $return['status'] = 3;
            $return['msg'] = '数据获取错误';
            $this->ajaxReturn($return);exit();
        }
        $is_show = M('LittleShopGoods')->where($garr)->getField('is_show');
        if($is_show==1) $save_arr=array('is_show'=>0);
        else  $save_arr=array('is_show'=>1);
        $save_id = M('LittleShopGoods')->where($garr)->save($save_arr);
        if($save_id) $this->ajaxReturn($return);
        
    }
    //商品下架
    public function update_myshop(){
        $postdata = I('param.');
        $user_info=$this->checkuserauth();
        $data=array();
        $data['shop_id'] = idtoname($user_info['id'],'LittleShop','id','uid',false);
        $data['type'] = $postdata['ptype'];
        $data['goods_id'] = $postdata['goods_id'];
        $data = D('Wxsmall/LittleShop')->update_goods($data,$user_info);
        $this->ajaxReturn($data);
    }
    public function sub_shop(){
        $postdata = I('param.');
        $user_info=$this->checkuserauth();
        $postdata['surface_plot'] = $postdata['index_img'];
        $postdata['name'] = $postdata['shop_name'];
        $postdata['logo'] = $postdata['logo'];
        $postdata['id'] = idtoname($user_info['id'],'LittleShop','id','uid',false);
 
        $data = D('Wxsmall/LittleShop')->lshop_infor($postdata,$user_info);
        if($data['status']!=1)$data['status']=3;
        $this->ajaxReturn($data);
    }
    public function info() {
        $return['status']=3;
        $where['name'] = 'PRO_INFO';
        $res = M('Config')->field('value')->where($where)->find();
        $content=$res['value'];
        if($content){
            $return['data']['content']=h($content);
            $return['status']=1;
        }else{
            $return['msg']="未获取到数据";
        }
        $this->ajaxReturn($return);
    }
    public function check_lshop(){
        $return['status']=3;
        $param = I('param.');
        if(!$param['uid'] || !$param['token']){
            $return['msg']="未获取到数据";
            $this->ajaxReturn($return);
        }
        $where['id'] = $param['uid'];
        $res = M('Member')->field('is_littleshop')->where($where)->find();
        $is_littleshop=$res['is_littleshop'];
        $return['is_littleshop']=$is_littleshop;
        $return['lshop_id']=intval(D('Wxsmall/Login')->get_lshop_id($param['uid']));
        $return['status']=1;
        $this->ajaxReturn($return);
    }
}