<?php
/**
 * 管理员管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class ActivityModel extends Model{
    protected $tableName = 'promot_act';
    public function get_index($param){
        $return=array('status'=>1,'msg'=>'');
        $id = $param['id'];
        $cart_id = $param['cart_id'];
        if(empty($id)){
            $return['status']=0;
            $return['msg']='参数错误';
            return $return;
        }
        $map=array();
        $map['id']=$id;
        $Actinfo = M('PromotAct')->where($map)->field('id,end_time,act_type,min_amount,act_type_ext,big_img,act_name')->find();
        if ($Actinfo['act_type'] == 1) {
            $Actinfo['title_name'] = '满' . $Actinfo['min_amount'] . '减' . $Actinfo['act_type_ext'];
        } elseif ($Actinfo['act_type'] == 2) {
            $Actinfo['title_name'] = '满' . $Actinfo['min_amount'] . '享' . ($Actinfo['act_type_ext'] / 10) . '折';
        } else {
            $Actinfo['title_name'] = $Actinfo['act_name'];
        }
        //获取赠品
        $return['gift_list']=array();
        if($Actinfo['act_type']==3 || $Actinfo['act_type']==4) {
            $list = D("Common/Promot")->get_gift_goods($id);
            if ($list['status'] == 1 && $list['gift_list']) {
                $gift_list = $list['gift_list'];
            }
            if($gift_list){
                $return['gift_list']=$gift_list;
            }
        }
        //获取购物车的赠品
        if($cart_id){
            $map=array();
            $map['id']=$cart_id;
            $cart_info=M("Cart")->where($map)->field('sum(shop_price*goods_num) as total_price,actgoodsid')->find();
            if($cart_info['actgoodsid']){
                if(stripos($cart_info['actgoodsid'],'-')){
                    $act_id=explode('-',$cart_info['actgoodsid']);
                    $gift_id=$act_id[0];
                }else{
                    $gift_id=$cart_info['actgoodsid'];
                }
            }
        }
        $return['gift_id']=$gift_id;
        $Actinfo['big_img']=thumb($Actinfo['big_img']);
        $map=array();
        $map['user_id']=$param['uid'];
        $cart_num_list=M("Cart")->where($map)->field('goods_num')->select();
        $cart_num = 0;
        foreach($cart_num_list as $k=>$v){
            $cart_num += $v['goods_num'];
        }
        $return['init_list'] = $this->get_cart($param);//默认购物车中已加入的商品
        $return['actinfo']=$Actinfo;
        $return['cart_num']=$cart_num;
        //活动下的商品
        $data = D('Common/Promot')->get_active_goods($param);
        if($data['goods_info']['list']){
            foreach($data['goods_info']['list'] as $key=>$val){
                $data['goods_info']['list'][$key]['img']=thumb($val['img']);
                $price_info=D('Common/Takeout')->get_min_price($param['shop_id'],$val['id']);
                $data['goods_info']['list'][$key]['shop_price']=format_price($price_info['goods_price']);
                $data['goods_info']['list'][$key]['stock'] = $this->format_stock($param['shop_id'],$val);//根据当前定位的门店信息取出对门店商品库存
            }
            $return['goods_info']=$data['goods_info'];
        }else{
            $return['status']=2;
            $return['msg']='暂无数据';
            return $return;
        }
        return $return;
    }
    public function format_stock($shop_id=0,$data=array()){
        $return = array();
        $map = array();
        $map['goods_id'] = $data['id'];
        $map['shop_id'] = $shop_id;
        $map['status'] = 1;
        if($data['format_spec']['newarr']){//此商品有规格属性
            if(!empty($shop_id)){
                $specarr = json_decode($data['format_spec']['combination']);
                $specstrarr = array();
                foreach($specarr as $v){
                    if(empty($v))continue;
                    sort($v);$specstrarr[] = implode('_',$v);
                }
                if($specstrarr){
                    $format = array();
                    foreach($specstrarr as $v){
                        $format[$v] = 0;//初始化商品规格库存
                    }
                    $map['goods_stock'] = array('IN',$specstrarr);
                    $result = M('ShopsGoodsStock')->where($map)->field('goods_stock,goods_number')->select();
                    if($result){
                        foreach($result as $k=>$v){
                            $format[$v['goods_stock']] = $v['goods_number'];
                        }
                    }
                    $return['goods_stock_number'] = $format;
                }
            }else{
                $return['goods_stock_number'] = 0;
            }
        }else{
            if($data['sale_type']==1){//组合商品
                $goods_number = $this->get_group_goods_stock($data['id'],$shop_id);
                $return['goods_number'] = $goods_number?$goods_number:0;
            }else{
                $format = M('ShopsGoodsStock')->where($map)->getField('goods_number');
                $return['goods_number'] = $format?$format:0;
            }
        }
        return $return;
    }
    //获取组合商品的库存
    public function get_group_goods_stock($goodsid,$shop_id){
        $map=array();
        $map['goodsid']=$goodsid;
        $list = M('GroupGoods')->where($map)->field('linkid,attr_key,ratio')->select();
        foreach($list as $k=>$v){
            $map=array();
            $map['goods_id'] = $v['linkid'];
            $map['shop_id'] = $shop_id;
            $map['status'] = 1;
            $goods_number=M('ShopsGoodsStock')->where($map)->getField('goods_number');
            if($goods_number==false){
                $goodsnum[]=0;
            }else{
                $goods_number=floatval($goods_number);
                $goodsnum[]=intval($goods_number/$v['ratio']);
            }
        }
        return min($goodsnum);
    }
    /**
     * 获取购物车已经存在的商品
     * @param array $param 用户信息
     * @return array 返回信息
     */
    public function get_cart($param){
        $return = $map = array();
        if($param['uid']&&$param['shop_id']){
            $map['user_id'] = $param['uid'];
            $map['shop_id'] = $param['shop_id'];
            $result = M('Cart')->where($map)->field(true)->select();
            if($result){
                foreach($result as $k=>$v){
                    if(empty($v['goods_attr'])){
                        $key = $v['goods_id'];
                    }else{
                        $list = unserialize($v['goods_attr']);
                        $key = $list['key'];
                        $return[$key]['spec_format'] = $list;
                    }
                    $return[$key]['id'] = $v['goods_id'];
                    $return[$key]['num'] = $v['goods_num'];
                    $return[$key]['price'] = $v['shop_price'];
                    $return[$key]['actgoodsid'] = $v['actgoodsid'];
                }
            }
        }
        return $return;
    }
}