<?php
/**
 * 推荐设置控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Admin\Controller;
class LittleShopController extends BaseController
{
	public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 小店列表
     */
    public function index(){
        $postdata=I('param.');
        $keyword = $postdata['keyword'];
        $map=array();
        if(!empty($keyword)){
            $whereor['id']=array('like','%'.$keyword.'%');
            $whereor['name']=array('like','%'.$keyword.'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
            $seotitle=$keyword.'_';
        }
        $list = $this->lists('LittleShop',$map);
        
        foreach ($list as $key => $value) {
            $list[$key]['username']=idtoname($value['uid'],'Member','username','id',false);
            
            //获取我的下级中的所有注册会员
            $where=array();
            $where['rank_id']=1;
            $uids=get_all_child_id($value['uid'],$where);
            array_push($uids,$value['uid']);
            //总营业额
            $map=array();
            $map['pay_status']=3;
            $map['is_delete']=0;
            $map['status']=3;
            $map['uid']=array('in',$uids);
            $order_info = M('OrderInfo')->where($map)->field('sum(goods_amount) as sellmoney,count(id) as order_num')->select();
            $list[$key]['sellmoney']=format_decimal($order_info[0]['sellmoney']);

            //总订单数
            $list[$key]['order_num']=$order_info[0]['order_num'];
            //商品数
            $g_map['lg.shop_id']=$value['id'];
            $g_map['lg.is_putaway']=0;
            $g_map['g.status']=1;
            $g_map['g.publish_wait']=1;
            $list[$key]['goods_num']=intval(M('LittleShopGoods')->alias('lg')->where($g_map)->join('LEFT JOIN __GOODS__ g ON g.id=lg.goods_id')->count());
        }
        $this->assign('list', $list);
        $this->assign($postdata);
        $this->display();
    }
    /**
     * 修改小店信息
     */
    public function updateshop(){
        $postdata = I('post.');
        $map=$data=array();
        $data['name']= $postdata['name'];
        $data['remark']= $postdata['remark'];
        $data['logo']= $postdata['logo'];
        $data['surface_plot']= $postdata['surface_plot'];
        //轮播图
		$map['id']=$postdata['id'];
        $g_info =M('LittleShop')->where($map)->save($data);
        if (!empty($g_info)) {
            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(CONTROLLER_NAME . '/index');
            $this->assign('jumpUrl', $jumpUrl);
            $this->success('编辑成功！');
        }else {
            $this->error('编辑失败！');
        }
    }
   
}
?>