<?php
/**
 * 会员中心
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class LittleShopModel extends Model{
    /**
       *小店商品加载更多
       *@return array
       */
    public function goods_list($type = 0, $map = array(), $join = '', $order = '', $p = 1, $pagesize = 0){
        $pagesize = empty($pagesize) ? C('WEB_GOODS_PAGE') : $pagesize;
        $p = empty($p) ? 1 : $p;
        $map['status'] = 1;
        $map['publish_wait'] = 1;
        $map['is_delete'] = 0;
        switch ($type) {
            case 1:
                $map['is_best'] = 1;
                break;
            case 2:
                $map['is_new'] = 1;
                break;
            case 3:
                $map['is_hot'] = 1;
                break;
        }
        $catid = $map['cat_id'];
        if (!empty($catid)) {
            GLOBAL $GoodsCategory;
            $catinfo = $GoodsCategory[$catid];
            $extension_goods_id = get_extension_goods($catinfo['allson'], 1);
            if (!empty($catinfo['allson'])) $where['cat_id'] = array('in', $catinfo['allson']);
            if (!empty($extension_goods_id)) $where['id'] = array('in', $extension_goods_id);
            if (!empty($where)) {
                $where['_logic'] = 'or';
                $map['_complex'] = $where;
                unset($map['cat_id']);
            } else {
                $map['cat_id'] = $catid;
            }
        }
        $order = empty($order) ? 'sell_count desc,avg_point desc,count desc,updatetime desc' : $order;
        $goodslist = page_list('Goods', $map, $order, true, $pagesize, $join, $p);
        foreach ($goodslist['list'] as $key => $val) {
            $goodslist['list'][$key] = $this->format_goodslist_item($val);
            $goodslist['list'][$key]['end_time'] = 0;
            $goodslist['list'][$key]['endtime'] = 0;
            if($val['pre_id']>0){
                $ext_map['act_id'] = $val['pre_id'];
                $style = M('PromotExt')->where($ext_map)->getField('style');
                $goodslist['list'][$key]['type'] = $style;
                $end = idtoname($val['pre_id'],'PromotAct','end_time');
                if($end > NOW_TIME){
                    $remarks = M('PromotExt')->where($ext_map)->getField('remarks');
                    $remark = unserialize($remarks);
                    $goodslist['list'][$key]['shop_price'] = format_price($remark['group_price']);
                    $goodslist['list'][$key]['end_time'] = 1;
                }else{
                    $goodslist['list'][$key]['end_time'] = 2;
                    $save['pre_id'] = 0;
                    $gid['id'] = $val['id'];
                    $update = M('Goods')->where($gid)->save($save);//活动结束，将商品的活动id改为0
                }
            }
           if($val['pt_id']>0){
                $end = idtoname($val['pt_id'],'PromotAct','end_time');
                if($end > NOW_TIME){
                    $ext_map['act_id'] = $val['pt_id'];
                    $remarks = M('PromotExt')->where($ext_map)->getField('remarks');
                    $remark = unserialize($remarks);
                    $goodslist['list'][$key]['shop_price'] = format_price($remark);
                    $goodslist['list'][$key]['endtime'] = 1;
                }else{
                    $goodslist['list'][$key]['endtime'] = 2;
                    $save['pt_id'] = 0;
                    $gid['id'] = $val['id'];
                    $update = M('Goods')->where($gid)->save($save); //活动结束，将商品的活动id改为0
                }
            }
        }
        return $goodslist;
    }
    /**
       *修改小店信息
       *@return array
       */
    public function lshop_infor($data=array(),$user_info=''){
        $return['status']=0;
        $map['id']=$data['id'];
        // if($data) $regular= new \Common\Vendor\Regular();
        
        // if(!$regular->isEmpty($data['name'])){
        //     $return['msg']='小店名称不能为空';
        //     return $return;
        // }
        if($data['name']){
            $where['id']=array('neq',$data['id']);
            $where['name']=$data['name'];
            $count=M('LittleShop')->where($where)->count('id');
            if($count>0){
                $return['msg']='小店名称已存在，请更换小店名称';
                return $return;
            }
        }

        $savedata=$data;
        unset($savedata['id']);
        $result=M('LittleShop')->where($map)->save($savedata);
        if($result) {
            $return['url']=U('Mobile/LittleShop/index',array('eid'=>$user_info['id'],'shop_id'=>$data['id']));
            $return['msg']='修改成功';
            $return['status']=1;
        } else {
            $return['msg']='修改失败';
        }
        return $return;
    }

    /**
       *小店首页商品信息
       *@return array
       */
    public function get_goods_index($param=array()){
        $root=array();
        $map=array();
        $join='';
        if(empty($param['page']))$param['page']=1;
        $p=$param['page'];
        if(!empty($param['keyword']))$map['goods_name']=array('like','%'.$param['keyword'].'%');
        if(!empty($param['cat_id']))$map['cat_id']=$param['cat_id'];
        if(!empty($param['brand_id']))$map['brand_id']=$param['brand_id'];
        //是否为自营
        if(!empty($param['is_self'])&& $param['is_self'] == 1)$map['shop_id']=0;
        //是否是商家包邮
        if(!empty($param['is_shipping'])&& $param['is_shipping'] == 1)$map['is_shipping']=1;
        //仅显示有货
        if(!empty($param['is_have'])&& $param['is_have'] == 1)$map['goods_number']=array('gt',0);
        /*排序开始*/
        $orders=D('Common/Goods')->cate_order($param);
        /*排序结束*/
        /*价格分级开始*/
        $price_list = get_price_map($catid,$param['price']);
        $pricelist=empty($price_list['pricelist'])?array():$price_list['pricelist'];
        if(!empty($price_list['shop_price']))$map['shop_price']=$price_list['shop_price'];
        /*价格分级结束*/
        /*属性筛选开始*/
        $type_id=get_cate_type($param['cat_id']);//类型ID
        $attr_list=D('Common/GoodsAttr')->get_attr($param,$type_id);
        if(!empty($attr_list)){
            $map['_string']=$attr_list['_string'];
        }
        /*属性筛选结束*/
        /*标签筛选开始*/
        $labels=$param['labelarray'];
        if($labels){
            $labels=explode(',',$labels);
        }
        // $labels=$param['label'];if($labels)$labels=explode(',',urldecode($labels));
        $labels_map=D('Common/Goods')->get_label_map($labels);
        if(!empty($labels_map['map']))$map['id']=$labels_map['map']['id'];
        /*标签筛选结束*/
        /* 排序 */
        $order_type=$param['sort'];
        if(!empty($param['order_name']) && $order_type)$order=$param['order_name'].' '.$order_type;
        
        /*拼团*/
        if($param['is_pt'] == 1){
            $map['pt_id'] = array('gt',0);
        }
        /*预售*/
        if($param['is_pre'] == 1){
            $map['pre_id'] = array('gt',0);
        }
        
        $goods_list=D('Common/Goods')->get_goods_list(0,$map,$join,$order,$p);
        $cate_name=empty($param['cat_id'])?'分类':idtoname($param['cat_id'],'GoodsCategory');
        $root['cate_name']=$cate_name;
        $root['orders']=$orders;
        $root['pricelist']=$pricelist;
        $root['type_id']=$type_id;
        $root['labels']=$labels_map['labels'];
        $root['goods_list']=$goods_list;
        return $root;
    }
    /**
       *小店添加banner、商品
       *@return array
       */
    public function add_sthing($data=array()){
        $return['status']=0;
        if($data['type']==1){ //商品加入我的小店、
            if($data['goods_id']){
                $where['shop_id']=array('eq',$data['id']);
                $where['goods_id']=$data['goods_id'];
                $count=M('LittleShopGoods')->where($where)->count('id');
                if($count>0){
                    $return['msg']='该商品已存在，看看其他商品吧';
                    return $return;
                }
            }
            $savedata['shop_id']=$data['id'];
            $savedata['goods_id']=$data['goods_id'];
            $savedata['uid']=$data['uid'];

            $result=M('LittleShopGoods')->add($savedata);

        }else{ //轮播图加入我的小店、
            $map['id']=$data['id']; //小店id
            $mybanner=M('LittleShop')->where($map)->field('id,banner')->find();
            if(!$mybanner){
                $return['msg']='小店不存在';
                return $return;
            }
            $arrbanner=explode(',',$mybanner['banner']);
            if(!in_array($data['banner'],$arrbanner)){
                if(!empty($mybanner['banner'])){
                    $savedata['banner']=$mybanner['banner'].",".$data['banner'];
                }else{
                    $savedata['banner']=$data['banner'];
                }
                
            }else{
                $return['msg']='图片已经存在';
                return $return;
            }
            $result=M('LittleShop')->where($map)->save($savedata);
        }
        if($result!=false){
            $return['url']=U('Mobile/LittleShop/index',array('eid'=>MID,'shop_id'=>$data['id']));
            $return['status']=1;
            $return['msg']='添加成功';
        }else {
            $return['msg']='添加失败';
        }
        return $return;
    }
    /**
       *小店删除banner
       *@return array
       */
    public function del_banner($data=array()){
        $return['status']=0;
        $map['id']=$data['shop_id']; //小店id
        $mybanner=M('LittleShop')->where($map)->field('id,banner')->find();
        if(!$mybanner){
            $return['msg']='小店不存在';
            return $return;
        }
        $arrbanner=explode(',',$mybanner['banner']);
        if(in_array($data['id'],$arrbanner)){
            foreach ($arrbanner as $key=>$value){
                if($value==$data['id']){
                    unset($arrbanner[$key]);
                }
            }
            $savedata['banner']=implode(',',$arrbanner);
        }else{
            $return['msg']='图片不存在';
            return $return;
        }
        $result=M('LittleShop')->where($map)->save($savedata);
        if($result!=false){
            $return['url']=U('Mobile/LittleShop/index',array('eid'=>MID,'shop_id'=>$data['shop_id']));
            $return['status']=1;
            $return['msg']='删除成功';
        }else {
            $return['msg']='删除失败';
        }
        return $return;
    }

    /**
       *小店编辑商品
       *@return array
       */
    public function update_goods($data=array(),$user=''){
        $return['status']=0;
        if(empty($data)){
            $return['msg']='参数错误';
            return $return;
        }
        $map['shop_id']=$data['shop_id']; //小店id
        $map['goods_id']=$data['goods_id']; //商品id
        $goodsinfo=M('LittleShopGoods')->where($map)->field(true)->find();
        if(!$goodsinfo){
            $adddata['shop_id']=$data['shop_id'];
            $adddata['goods_id']=$data['goods_id'];
            $adddata['uid']=$user['id'];
            $result=M('LittleShopGoods')->add($adddata);
        }else{
            if($data['type']<=1){//上下架
                if($goodsinfo['is_putaway']==1 && $data['type']==0){
                    $savedata['is_putaway']=0;
                }else{
                    $savedata['is_putaway']=1;
                }
            }else{
                if($goodsinfo['is_show']==1){//推荐首页
                    $return['data']['is_show']=$savedata['is_show']=0;
                }else{
                    $return['data']['is_show']=$savedata['is_show']=1;
                }
            }
            $result=M('LittleShopGoods')->where($map)->save($savedata);
        }
        
        if($result!=false){
            unset($map['goods_id']);
            $map['is_putaway']=1; //下架
            $return['data']['down_num']=M('LittleShopGoods')->where($map)->count();
            unset($map['is_putaway']);
            $map['is_putaway']=0; //上架
            $return['data']['up_num']=M('LittleShopGoods')->where($map)->count();
            $return['url']=U('Mobile/LittleShop/index',array('eid'=>$user['id'],'shop_id'=>$data['shop_id']));
            $return['status']=1;
            $return['msg']='操作成功';
        }else {
            $return['msg']='操作失败';
        }
        return $return;
    }
    
     
}
?>