<?php
/**
 * 积分商品模型
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <1094186793@qq.com>
 */

namespace Common\Model;
use Think\Model;
class GoodsIntegralModel extends Model
{
    /**
     * 获取有效积分商品列表
     * @param array $map
     * @param string $join
     * @param string $order
     * @param int $p
     * @param int $pagesize
     * @return array
     */
    public function get_integral_list($order_arr = array()){
        $return=$t_map=$where=array();
        $return['status'] = 1;
        if($order_arr){
            if($order_arr['childcatid']){
                $t_map['g.cat_id'] = $order_arr['childcatid'];
            }else{
                if (!empty($order_arr['catid'])) {
                    GLOBAL $PurchaseCategory;
                    $catinfo = $PurchaseCategory[$order_arr['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';
                        $t_map['_complex'] = $where;
                        unset($t_map['cat_id']);
                    } else {
                        $t_map['g.cat_id'] = $order_arr['catid'];
                    }
                }
            }

            if($order_arr['order'] == 'sell_count'){
                $order = 'g.sell_count desc';
            }else if($order_arr['order'] == 'money_desc'){
                $order = 'g.shop_price desc';
            }else if($order_arr['order'] == 'money_asc'){
                $order = 'g.shop_price asc';
            }
            $order_arr['screen_order'] = array_unique(explode(',',$order_arr['screen_order']));
            if(in_array('1',$order_arr['screen_order'])){
                $t_map['g.is_hot'] = 1;
            }else if(in_array('2',$order_arr['screen_order'])){
                $t_map['g.is_new'] = 1;
            }else if(in_array('3',$order_arr['screen_order'])){
                $t_map['g.is_promote'] = 1;
                $t_map['g.promote_id'] = array('gt',0);
                $t_map['g.promote_start_time'] = array('elt',NOW_TIME);
                $t_map['g.promote_end_time'] = array('egt',NOW_TIME);
            }else if(in_array('4',$order_arr['screen_order'])){
                $where = array();
                $where['id'] = array('gt',0);
                $where['start_time'] = array('elt',NOW_TIME);
                $where['end_time'] = array('egt',NOW_TIME);
                $cat_ids = M('PromotAct')->where($where)->getField('id',true);
                if($cat_ids){
                    $p_map = array();
                    $p_map['act_id'] = array('in',$cat_ids);
                    $p_map['style'] = 3;
                    $goods_ids = M('PromotExt')->where($p_map)->getField('style_ext_id',true);
                    $t_map['g.id'] = array('in',$goods_ids);
                }else{
                    $t_map['g.id'] = array('in','');
                }
            }else if(in_array('5',$order_arr['screen_order'])){
                $map = array();
                $map['goods_id'] = array('gt',0);
                $goods_ids = M('MemberPrice')->where($map)->getField('goods_id',true);
                $t_map['g.id'] = array('in',$goods_ids);
            }

            if($order_arr['brand_order']){
                $brand_order=array_filter(explode(',',$order_arr['brand_order']));
                if($brand_order)$t_map['g.brand_id'] =count($brand_order)==1?implode($brand_order): array('in',$brand_order);
            }
            if($order_arr['keyword']){
                $t_map['g.goods_name'] = array('like','%'.$order_arr['keyword'].'%');
            }
        }else{
            $order = 'g.sort asc';
        }
        $t_map['g.status']=1;
        $t_map['g.publish_wait'] = 1;
        $t_map['g.is_delete']=0;
        $t_map['g.is_internet']=1;
        //$pagesize=2;
        $pagesize = empty($pagesize) ? C('WEB_GOODS_PAGE') : $pagesize;
        $p = $order_arr['p'];
        $p = empty($p) ? 1 : $p;
        $field='g.*,a.*,g.id as goodsid';
        $join=' LEFT JOIN __GOODS__ g ON a.goods_id=g.id ';
        $order=empty($order)?'g.sell_count desc,g.avg_point desc,g.count desc,g.updatetime desc':$order;
        $goods_list=page_list("GoodsIntegral",$t_map,$order,$field,$pagesize,$join,$p);
        // $goods_list=M('Goods')->where($t_map)->field(true)->order($order)->select();
        if(empty($goods_list)){
            $return['status']=0;
            $return['g_status']=0;
            return $return;
        }
        if($goods_list['list']){
            $return['g_status']=1;
           /* foreach($goods_list['list'] as $k=>$v){
                $goods_list['list'][$k] = $this->format_takeout_item($v);
            }*/
        }
        $return['goods_list']=$goods_list;
        $return['page']=$p;
        return $return;
    }
    /**
     * 根据积分商品id获取
     * @param $id   积分的id
     * @return array
     */
    public function get_integral($id,$shop_id){
        $return=array();
        $return['status']=0;
        if(empty($id)){
            $return['msg']='参数错误';
            return $return;
        }
        $map['a.id']=$id;
        $map['a.status']=1;
        $map['g.status']=1;
        $map['g.publish_wait']=1;
        $map['g.is_delete']=0;
        $field='g.*,a.*,g.id as goodsid,gc.content';
        $join='LEFT JOIN __GOODS__ g ON a.goods_id=g.id';
        $join.=' LEFT JOIN __GOODS_CONTENT__ gc ON a.goods_id=gc.goods_id';
        $data=M('GoodsIntegral')->alias('a')->join($join)->field($field)->where($map)->find();
        if(empty($data)){
            $return['msg']='未找到该商品';
            return $return;
        }
        $sg_map=array();
        $sg_map['goods_id']=$id;
        $sg_map['shop_id']=$shop_id;
        $sg_info=M('ShopsGoods')->where($sg_map)->getField('goods_id');
        if(empty($sg_info)){$data['is_add'] = 0;}
        else{$data['is_add'] = 1;}
        $return['status']=1;
        $return['data']=$this->format_integral_item($data);
        return $return;
    }
    public function format_integral_item($data=array()){
        $result=array();
        $result=$data;
        //$result['contract_list']=D('Common/Goods')->get_goods_contract($data['shop_id']);
        return $result;
    }
    public function get_cartIntegral($postdata,$addre_id)
    {
        $return = array('status'=>0,'msg'=>'');
       $goods_info=D("Common/OrderInfo")->buy_now($postdata);
        if($goods_info['status']<=0){
            $return['msg']=$goods_info['msg'];
            return $return;
        }

        $list=$goods_info['data'];
        if($list){
                foreach ($list as $key => $val) {
                    $list[$key]['shop_info'] = D("Common/Cart")->format_goods_shop($val['shop_id'],$addre_id);
                    $list[$key]['goods_attr_format'] = unserialize($val['goods_attr']);
                    $list[$key]['all_price'] = ($val['shop_price'] + ($val['cart_integral'] * C('WEB_INTEGRAL_RATIO') / 100)) * $val['goods_num'];
                    $list[$key]['all_price_show'] =format_price($val['shop_price']* $val['goods_num'])." + ". $val['cart_integral']*$val['goods_num']."分";
                    if ($val['shop_price'] > 0) {
                        $list[$key]['all_goods_price'] =format_price($val['shop_price'] * $val['goods_num']) . "+ ";
                    }else{
                        $list[$key]['all_goods_price']=format_price(0)." + ";
                    }
                    if ($val['cart_integral'] > 0) {
                        $list[$key]['all_goods_price'] .= $val['cart_integral'] * $val['goods_num'] . "积分";
                    }
                    $list[$key]['shipping_fee'] = D("Common/Cart")->shipping_fee($list[$key]['all_price'], $val, $list[$key]['shop_info']['configure']);
                    $list[$key]['shop_total_price'] = $list[$key]['shipping_fee'] + $list[$key]['all_price'];//店铺总价格
                    $list[$key]['shop_total_price_show'] =format_price(($val['shop_price']* $val['goods_num'])+$list[$key]['shipping_fee'])." + ". $val['cart_integral']*$val['goods_num']."分";//店铺总价格
                    $list[$key]['shop_total_weight'] = $val['goods_weight'] * $val['goods_num'];//店铺商品总重量
                }
                $return['status']=1;
                $return['list']=$list;
            }else{
                $return['msg']='没有此积分商品';
            }

        return $return;
    }
    public function get_cartIntegrals($cart_ids)
    {
        $return = array();
        $return['status'] = 0;
        $c_where['c.id'] = array('in', $cart_ids);
        $list = M('Cart')->alias('c')->JOIN('LEFT JOIN __GOODS__ g ON g.id=c.goods_id')->JOIN('LEFT JOIN __GOODS_INTEGRAL__ gi ON gi.goods_id=c.goods_id')->where($c_where)->field('g.*,g.id as goods_id,c.id as id,c.user_id,c.cookie_id,c.goods_attr,c.goods_num,c.is_addcart,c.cart_integral as cart_integral,c.shop_price as shop_price')->order('g.shop_id asc')->select();
        if($list) {
            foreach ($list as $key => $val) {
                $list[$key]['shop_info'] = D("Common/Cart")->format_goods_shop($val['shop_id']);
                $list[$key]['goods_attr_format'] = unserialize($val['goods_attr']);
                $list[$key]['all_price'] = ($val['shop_price'] + ($val['cart_integral'] * C('WEB_INTEGRAL_RATIO') / 100)) * $val['goods_num'];
                if ($val['shop_price'] > 0) {
                    $list[$key]['all_goods_price'] = format_price($val['shop_price'] * $val['goods_num']);
                }
                if ($val['cart_integral'] > 0) {
                    $list[$key]['all_goods_price'] .= '+' . $val['cart_integral'] * $val['goods_num'] . "积分";
                }
                $list[$key]['shipping_fee'] = D("Common/Cart")->shipping_fee($list[$key]['all_price'], $val, $list[$key]['shop_info']['configure']);
                $list[$key]['shop_total_price'] = $list[$key]['shipping_fee'] + $list[$key]['all_price'];//店铺总价格
                $list[$key]['shop_total_weight'] = $val['goods_weight'] * $val['goods_num'];//店铺商品总重量
            }
            $return['status']=1;
            $return['list']=$list;
        }else{
            $return['msg']='没有此积分商品';
        }
        return $return;
    }
}
?>