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

namespace Common\Model;
use Think\Model;
class PromotExtModel extends Model
{
    /*
     * 获取拼团商品列表
     */
    public function get_fight_list($map=array(),$join='',$order='',$p=1,$pagesize=0,$goods_type=1,$rand=''){
        $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
        $p=empty($p)?1:$p;
        $now_time=NOW_TIME;
        $map['a.pt_id'] = array('neq',0);
        $map['c.is_pt'] = 1;
        /**
         *  5-28 fk         $member_info = session('member_info');
         **/
        if($rand !=''){
            $rank_id=$rand;
        }
        if(session('member_info')){
            $member_info = session('member_info');
            $rank_id=$member_info['rank_id'];
        }
        $map['c.user_rank']=array('elt',$rank_id);
        $map['c.start_time'] = array('elt',$now_time);
        $map['c.end_time'] = array('gt',$now_time);
        $catid = $map['cat_id'];unset($map['cat_id']);
        if (!empty($catid)) {
			if($goods_type == 5){
				GLOBAL $PurchaseCategory;
				$catinfo = pcat_list($catid,1);
			}else{
				GLOBAL $GoodsCategory;
				$catinfo = $GoodsCategory[$catid];
                $catinfo=$catinfo['allson'];
			}
            $extension_goods_id = get_extension_goods($catinfo, 1);
            $where=array();
            if (!empty($catinfo)) $where['a.cat_id'] = array('in', $catinfo);
            if (!empty($extension_goods_id)) $where['a.id'] = array('in', $extension_goods_id);
            if (!empty($where)) {
                $where['_logic'] = 'or';
                $map['_complex'] = $where;
            } else {
                $map['a.cat_id'] = $catid;
            }
            
        }
        $field = 'a.*,c.end_time,c.id as cid,c.group_number,a.id as aid';
        $join = ' JOIN __PROMOT_ACT__ c on c.id=a.pt_id';
        $order = empty($order) ? 'sell_count desc,avg_point desc,count desc,updatetime desc' : $order;
        $goodslist = page_list('Goods', $map, $order, $field, $pagesize, $join, $p);
        $goodslist['map']=$map;
        $goodslist['catinfo']=$catinfo;
        foreach ($goodslist['list'] as $key => $val) {
            $where = array();$where['style_ext_id'] = $val['id'];$where['style'] = 4;
            $price = M('PromotExt')->where($where)->order('id desc')->field('remarks,act_id')->find();
            $goodslist['list'][$key]['remarks'] = unserialize($price['remarks']);
			
			$og_count = 0;
			$map = array();$map['pt_id'] = $price['act_id'];
			$map['pay_status'] = 3;$map['status'] = array('neq',4);
            $oi_ids = M('OrderInfo')->where($map)->getField('id',true);
			if($oi_ids){
				$map = array();$map['order_id'] = array('in',$oi_ids);
				$og_count = M('OrderGoods')->where($map)->sum('goods_number');
				if(!$og_count){$og_count = 0;}
			}
            $goodslist['list'][$key]['number'] = floatval($og_count);
        }
        // $now_time=NOW_TIME;
        // $where['a.style'] = 4;
        // $where['c.is_pt'] = 1;
        // $where['c.end_time'] = array('gt',$now_time);
        // $where['group'] = 'a.style_ext_id';
        // $field='max(a.id) as aid';
        // $join.=' LEFT JOIN __PROMOT_ACT__ c on a.act_id = c.id ';
        // $order=empty($order)?'a.id desc':$order;
        // $goodslist_id = page_list('PromotExt', $where, $order, $field, $pagesize, $join, $p);

        // if(!empty($goodslist_id)){
        //     foreach ($goodslist_id['list'] as $key => $value) {
        //         $arr[] = $value['aid'];
        //     }
        //     $str_ids = implode(',', $arr);
        // }
        
        // $map['a.id'] = array('in',$str_ids);
        // $fields = 'a.id as aid,a.style_ext_id,a.remarks,c.id as cid';
        // $goodslist = page_list('PromotExt', $map, $order, $fields, $pagesize, $join, $p);
        // if(!empty($goodslist)){
        //     foreach ($goodslist['list'] as $k => $val) {
        //         $goodslist['list'][$k]['group_price'] = unserialize($val['remarks']);
        //         $goodslist['list'][$k]['goods_mes'] = idtoname($val['style_ext_id'],'Goods','shop_price,img,goods_name');
        //     }
        // }
        
        return $goodslist;
    }

    public function get_fight($id){
        $return=array();
        $return['status']=0;
        if(empty($id)){
            $return['msg']='参数错误';
            return $return;
        }
        $field='g.*,a.*,g.id as goodsid,c.*,c.id as cid,a.id as aid';
        $join=' LEFT JOIN __GOODS__ g on a.style_ext_id = g.id ';
        $join.=' LEFT JOIN __PROMOT_ACT__ c on a.act_id = c.id ';
        $now_time=NOW_TIME;
        $map = array();$map['a.style_ext_id'] = $id;
        $map['a.style'] = 4;$map['c.end_time'] = array('gt',$now_time);
        $data=M('PromotExt')->alias('a')->join($join)->field($field)->where($map)->order('a.id desc')->find();
        if(empty($data)){
            $return['msg']='未找到该商品';
            return $return;
        }
		$og_count = 0;
		$map = array();$map['pt_id'] = $data['cid'];
		$map['pay_status'] = 3;$map['status'] = array('neq',4);
		$oi_ids = M('OrderInfo')->where($map)->getField('id',true);
		if($oi_ids){
			$map = array();$map['order_id'] = array('in',$oi_ids);
			$og_count = M('OrderGoods')->where($map)->sum('goods_number');
			if(!$og_count){$og_count = 0;}
		}
		$data['og_count'] = floatval($og_count);
        $return['status']=1;
        $return['data']=$this->format_fight_item($data);
        return $return;
    }

    public function format_fight_item($data=array()){
        $result=array();
        $result=$data;
        $result['remarks']=unserialize($data['remarks']);
        return $result;
    }

}
?>