wangtengyu
2018-12-07 f459412e0dac4ed94106da043b4c6f8576bfe496
commit | author | age
19351a 1 <?php
B 2
3 /**
4  * 商品相关函数库
5 */
6
7 if (!defined('IN_ECS'))
8 {
9     die('Hacking attempt');
10 }
11
12 /**
13  * 商品推荐usort用自定义排序行数
14  */
15 function goods_sort($goods_a, $goods_b)
16 {
17     if ($goods_a['sort_order'] == $goods_b['sort_order']) {
18         return 0;
19     }
20     return ($goods_a['sort_order'] < $goods_b['sort_order']) ? -1 : 1;
21
22 }
23
24 /**
25  * 获得指定分类同级的所有分类以及该分类下的子分类
26  *
27  * @access  public
28  * @param   integer     $cat_id     分类编号
29  * @return  array
30  */
31 function get_categories_tree($cat_id = 0)
32 {
33     if ($cat_id > 0)
34     {
35         $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
36         $parent_id = $GLOBALS['db']->getOne($sql);
37     }
38     else
39     {
40         $parent_id = 0;
41     }
42
43     /*
44      判断当前分类中全是是否是底级分类,
45      如果是取出底级分类上级分类,
46      如果不是取当前分类及其下的子分类
47     */
48     $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$parent_id' AND is_show = 1 ";
49     if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
50     {
51         /* 获取当前分类及其子分类 */
52         $sql = 'SELECT cat_id,cat_name ,parent_id,is_show ' .
53                 'FROM ' . $GLOBALS['ecs']->table('category') .
54                 "WHERE parent_id = '$parent_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
55
56         $res = $GLOBALS['db']->getAll($sql);
57
58         foreach ($res AS $row)
59         {
60             if ($row['is_show'])
61             {
62                 $cat_arr[$row['cat_id']]['id']   = $row['cat_id'];
63                 $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
64                 $cat_arr[$row['cat_id']]['url']  = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
65
66                 if (isset($row['cat_id']) != NULL)
67                 {
68                     $cat_arr[$row['cat_id']]['cat_id'] = get_child_tree($row['cat_id']);
69                 }
70             }
71         }
72     }
73     if(isset($cat_arr))
74     {
75         return $cat_arr;
76     }
77 }
78
79 function get_child_tree($tree_id = 0)
80 {
81     $three_arr = array();
82     $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$tree_id' AND is_show = 1 ";
83     if ($GLOBALS['db']->getOne($sql) || $tree_id == 0)
84     {
85         $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
86                 'FROM ' . $GLOBALS['ecs']->table('category') .
87                 "WHERE parent_id = '$tree_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
88         $res = $GLOBALS['db']->getAll($child_sql);
89         foreach ($res AS $row)
90         {
91             if ($row['is_show'])
92
93                $three_arr[$row['cat_id']]['id']   = $row['cat_id'];
94                $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
95                $three_arr[$row['cat_id']]['url']  = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
96
97                if (isset($row['cat_id']) != NULL)
98                    {
99                        $three_arr[$row['cat_id']]['cat_id'] = get_child_tree($row['cat_id']);
100
101             }
102         }
103     }
104     return $three_arr;
105 }
106
107 /**
108  * 调用当前分类的销售排行榜
109  *
110  * @access  public
111  * @param   string  $cats   查询的分类
112  * @return  array
113  */
114 function get_top10($cats = '')
115 {
116     $cats = get_children($cats);
117     $where = !empty($cats) ? "AND ($cats OR " . get_extension_goods($cats) . ") " : '';
118
119     /* 排行统计的时间 */
120     switch ($GLOBALS['_CFG']['top10_time'])
121     {
122         case 1: // 一年
123             $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 365 * 86400) . "'";
124         break;
125         case 2: // 半年
126             $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 180 * 86400) . "'";
127         break;
128         case 3: // 三个月
129             $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 90 * 86400) . "'";
130         break;
131         case 4: // 一个月
132             $top10_time = "AND o.order_sn >= '" . date('Ymd', gmtime() - 30 * 86400) . "'";
133         break;
134         default:
135             $top10_time = '';
136     }
137
138     $sql = 'SELECT g.goods_id, g.goods_name, g.shop_price, g.goods_thumb, SUM(og.goods_number) as goods_number ' .
139            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g, ' .
140                 $GLOBALS['ecs']->table('order_info') . ' AS o, ' .
141                 $GLOBALS['ecs']->table('order_goods') . ' AS og ' .
142            "WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 $where $top10_time " ;
143     //判断是否启用库存,库存数量是否大于0
144     if ($GLOBALS['_CFG']['use_storage'] == 1)
145     {
146         $sql .= " AND g.goods_number > 0 ";
147     }
148     $sql .= ' AND og.order_id = o.order_id AND og.goods_id = g.goods_id ' .
149            "AND (o.order_status = '" . OS_CONFIRMED .  "' OR o.order_status = '" . OS_SPLITED . "') " .
150            "AND (o.pay_status = '" . PS_PAYED . "' OR o.pay_status = '" . PS_PAYING . "') " .
151            "AND (o.shipping_status = '" . SS_SHIPPED . "' OR o.shipping_status = '" . SS_RECEIVED . "') " .
152            'GROUP BY g.goods_id ORDER BY goods_number DESC, g.goods_id DESC LIMIT ' . $GLOBALS['_CFG']['top_number'];
153            
154     $arr = $GLOBALS['db']->getAll($sql);
155
156     for ($i = 0, $count = count($arr); $i < $count; $i++)
157     {
158         $arr[$i]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
159                                     sub_str($arr[$i]['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $arr[$i]['goods_name'];
160         $arr[$i]['url']        = build_uri('goods', array('gid' => $arr[$i]['goods_id']), $arr[$i]['goods_name']);
161         $arr[$i]['thumb'] = get_image_path($arr[$i]['goods_id'], $arr[$i]['goods_thumb'],true);
162         $arr[$i]['price'] = price_format($arr[$i]['shop_price']);
163     }
164
165     return $arr;
166 }
167
168 /**
169  * 获得推荐商品
170  *
171  * @access  public
172  * @param   string      $type       推荐类型,可以是 best, new, hot
173  * @return  array
174  */
175 function get_recommend_goods($type = '', $cats = '')
176 {
177     if (!in_array($type, array('best', 'new', 'hot')))
178     {
179         return array();
180     }
181
182     //取不同推荐对应的商品
183     static $type_goods = array();
184     if (empty($type_goods[$type]))
185     {
186         //初始化数据
187         $type_goods['best'] = array();
188         $type_goods['new'] = array();
189         $type_goods['hot'] = array();
190         $data = read_static_cache('recommend_goods');
191         if ($data === false)
192         {
193             $sql = 'SELECT g.goods_id, g.is_best, g.is_new,g.goods_brief, g.is_hot, g.is_promote, b.brand_name,g.sort_order ' .
194                ' FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
195                ' LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
196                ' WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND (g.is_best = 1 OR g.is_new =1 OR g.is_hot = 1)'.
197                ' ORDER BY g.sort_order, g.last_update DESC';
198             $goods_res = $GLOBALS['db']->getAll($sql);
199             //定义推荐,最新,热门,促销商品
200             $goods_data['best'] = array();
201             $goods_data['new'] = array();
202             $goods_data['hot'] = array();
203             $goods_data['brand'] = array();
204             if (!empty($goods_res))
205             {
206                 foreach($goods_res as $data)
207                 {
208                     if ($data['is_best'] == 1)
209                     {
210                         $goods_data['best'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
211                     }
212                     if ($data['is_new'] == 1)
213                     {
214                         $goods_data['new'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
215                     }
216                     if ($data['is_hot'] == 1)
217                     {
218                         $goods_data['hot'][] = array('goods_id' => $data['goods_id'], 'sort_order' => $data['sort_order']);
219                     }
220                     if ($data['brand_name'] != '')
221                     {
222                         $goods_data['brand'][$data['goods_id']] = $data['brand_name'];
223                     }
224                 }
225             }
226             write_static_cache('recommend_goods', $goods_data);
227         }
228         else
229         {
230             $goods_data = $data;
231         }
232
233         $time = gmtime();
234         $order_type = $GLOBALS['_CFG']['recommend_order'];
235
236         //按推荐数量及排序取每一项推荐显示的商品 order_type可以根据后台设定进行各种条件显示
237         static $type_array = array();
238         $type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot');
239         if (empty($type_array))
240         {
241             foreach($type2lib as $key => $data)
242             {
243                 if (!empty($goods_data[$key]))
244                 {
245                     $num = get_library_number($data);
246                     $data_count = count($goods_data[$key]);
247                     $num = $data_count > $num  ? $num : $data_count;
248                     if ($order_type == 0)
249                     {
250                         //usort($goods_data[$key], 'goods_sort');
251                         $rand_key = array_slice($goods_data[$key], 0, $num);
252                         foreach($rand_key as $key_data)
253                         {
254                             $type_array[$key][] = $key_data['goods_id'];
255                         }
256                     }
257                     else
258                     {
259                         $rand_key = array_rand($goods_data[$key], $num);
260                         if ($num == 1)
261                         {
262                             $type_array[$key][] = $goods_data[$key][$rand_key]['goods_id'];
263                         }
264                         else
265                         {
266                             foreach($rand_key as $key_data)
267                             {
268                                 $type_array[$key][] = $goods_data[$key][$key_data]['goods_id'];
269                             }
270                         }
271                     }
272                 }
273                 else
274                 {
275                     $type_array[$key] = array();
276                 }
277             }
278         }
279
280         //取出所有符合条件的商品数据,并将结果存入对应的推荐类型数组中
281         $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style,g.goods_brief , g.market_price, g.shop_price AS org_price, g.promote_price, ' .
282                 "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
283                 "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img, RAND() AS rnd " .
284                 'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
285                 "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
286                 "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ";
287         $type_merge = array_merge($type_array['new'], $type_array['best'], $type_array['hot']);
288         $type_merge = array_unique($type_merge);
289         $sql .= ' WHERE g.goods_id ' . db_create_in($type_merge);
290         $sql .= ' ORDER BY g.sort_order, g.last_update DESC';
291
292         $result = $GLOBALS['db']->getAll($sql);
293         foreach ($result AS $idx => $row)
294         {
295             if ($row['promote_price'] > 0)
296             {
297                 $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
298                 $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
299             }
300             else
301             {
302                 $goods[$idx]['promote_price'] = '';
303             }
304
305             $goods[$idx]['id']           = $row['goods_id'];
306             $goods[$idx]['goods_brief']  = $row['goods_brief'];
307             $goods[$idx]['name']         = $row['goods_name'];
308             $goods[$idx]['brief']        = $row['goods_brief'];
309             $goods[$idx]['brand_name']   = isset($goods_data['brand'][$row['goods_id']]) ? $goods_data['brand'][$row['goods_id']] : '';
310             $goods[$idx]['goods_style_name']   = add_style($row['goods_name'],$row['goods_name_style']);
311
312             $goods[$idx]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
313                                                sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
314             $goods[$idx]['short_style_name']   = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
315             $goods[$idx]['market_price'] = price_format($row['market_price']);
316             $goods[$idx]['shop_price']   = price_format($row['shop_price']);
317             $goods[$idx]['thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);
318             $goods[$idx]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
319             $goods[$idx]['url']          = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
320             if (in_array($row['goods_id'], $type_array['best']))
321             {
322                 $type_goods['best'][] = $goods[$idx];
323             }
324             if (in_array($row['goods_id'], $type_array['new']))
325             {
326                 $type_goods['new'][] = $goods[$idx];
327             }
328             if (in_array($row['goods_id'], $type_array['hot']))
329             {
330                 $type_goods['hot'][] = $goods[$idx];
331             }
332         }
333     }
334     return $type_goods[$type];
335 }
336
337 /**
338  * 获得促销商品
339  *
340  * @access  public
341  * @return  array
342  */
343 function get_promote_goods($cats = '')
344 {
345     $time = gmtime();
346     $order_type = $GLOBALS['_CFG']['recommend_order'];
347
348     /* 取得促销lbi的数量限制 */
349     $num = get_library_number("recommend_promotion");
350     $sql = 'SELECT g.goods_id, g.goods_name,g.goods_brief, g.goods_name_style, g.market_price, g.shop_price AS org_price, g.promote_price, ' .
351                 "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
352                 "promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, b.brand_name, " .
353                 "g.is_best, g.is_new, g.is_hot, g.is_promote, RAND() AS rnd " .
354             'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
355             'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
356             "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
357                 "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
358             'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' .
359             " AND g.is_promote = 1 AND promote_start_date <= '$time' AND promote_end_date >= '$time' ";
360     $sql .= $order_type == 0 ? ' ORDER BY g.sort_order, g.last_update DESC' : ' ORDER BY rnd';
361     $sql .= " LIMIT $num ";
362     $result = $GLOBALS['db']->getAll($sql);
363
364     $goods = array();
365     foreach ($result AS $idx => $row)
366     {
367         if ($row['promote_price'] > 0)
368         {
369             if(intval($row['shop_price']) != '0' || intval($row['shop_price']) != '')
370         {
371             $goods[$idx]['zhekou'] = (number_format(number_format($row['promote_price'],2)/number_format($row['shop_price'],2),2))*10;        }
372         else
373         {
374         $goods[$idx]['zhekou']          = '0';
375             }
376             $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
377             $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
378         }
379         else
380         {
381             $goods[$idx]['promote_price'] = '';
382         }
383
384         $goods[$idx]['id']           = $row['goods_id'];
385         $goods[$idx]['name']         = $row['goods_name'];
386         $time = gmtime();
387         if ($time >= $row['promote_start_date'] && $time <= $row['promote_end_date'])
388         {
389              $goods[$idx]['gmt_end_time'] = local_date('M d, Y H:i:s',$row['promote_end_date']);
390         }
391         else
392         {
393             $goods[$idx]['gmt_end_time'] = 0;
394         }
395         $goods[$idx]['lefttime']     = $row['promote_end_date'] - gmtime();
396         $goods[$idx]['brief']        = $row['goods_brief'];
397         $goods[$idx]['brand_name']   = $row['brand_name'];
398         $goods[$idx]['goods_style_name']   = add_style($row['goods_name'],$row['goods_name_style']);
399         $goods[$idx]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
400         $goods[$idx]['short_style_name']   = add_style($goods[$idx]['short_name'],$row['goods_name_style']);
401         $goods[$idx]['market_price'] = price_format($row['market_price']);
402         $goods[$idx]['shop_price']   = price_format($row['shop_price']);
403         $goods[$idx]['thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);
404         $goods[$idx]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
405         $goods[$idx]['url']          = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
406         
407     }
408     return $goods;
409 }
410
411 /**
412  * 获得指定分类下的推荐商品
413  *
414  * @access  public
415  * @param   string      $type       推荐类型,可以是 best, new, hot, promote
416  * @param   string      $cats       分类的ID
417  * @param   integer     $brand      品牌的ID
418  * @param   integer     $min        商品价格下限
419  * @param   integer     $max        商品价格上限
420  * @param   string      $ext        商品扩展查询
421  * @return  array
422  */
423 function get_category_recommend_goods($type = '', $cats = '', $brand = 0, $min =0,  $max = 0, $ext='')
424 {
425     $brand_where = ($brand > 0) ? " AND g.brand_id = '$brand'" : '';
426
427     $price_where = ($min > 0) ? " AND g.shop_price >= $min " : '';
428     $price_where .= ($max > 0) ? " AND g.shop_price <= $max " : '';
429
430     $sql =  'SELECT g.goods_id, g.goods_name, g.goods_name_style,g.goods_brief, g.market_price,g.is_best,g.is_new,g.is_hot,g.shop_price AS org_price, g.promote_price, ' .
431                 "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
432                 'promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, goods_img, b.brand_name ' .
433             'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
434             'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON b.brand_id = g.brand_id ' .
435             "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
436                     "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
437             'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ' . $brand_where . $price_where . $ext;
438     $num = 0;
439     $type2lib = array('best'=>'recommend_best', 'new'=>'recommend_new', 'hot'=>'recommend_hot', 'promote'=>'recommend_promotion');
440     $num = get_library_number($type2lib[$type]);
441
442     switch ($type)
443     {
444         case 'best':
445             $sql .= ' AND is_best = 1';
446             break;
447         case 'new':
448             $sql .= ' AND is_new = 1';
449             break;
450         case 'hot':
451             $sql .= ' AND is_hot = 1';
452             break;
453         case 'promote':
454             $time = gmtime();
455             $sql .= " AND is_promote = 1 AND promote_start_date <= '$time' AND promote_end_date >= '$time'";
456             break;
457     }
458
459     if (!empty($cats))
460     {
461         $sql .= " AND (" . $cats . " OR " . get_extension_goods($cats) .")";
462     }
463
464     $order_type = $GLOBALS['_CFG']['recommend_order'];
465     $sql .= ($order_type == 0) ? ' ORDER BY g.sort_order, g.last_update DESC' : ' ORDER BY RAND()';
466     $res = $GLOBALS['db']->selectLimit($sql, $num);
467
468     $idx = 0;
469     $goods = array();
470     while ($row = $GLOBALS['db']->fetchRow($res))
471     {
472         if ($row['promote_price'] > 0)
473         {
474             $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
475             $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
476         }
477         else
478         {
479             $goods[$idx]['promote_price'] = '';
480         }
481
482         $goods[$idx]['id']           = $row['goods_id'];
483          $goods[$idx]['goods_brief']           = $row['goods_brief'];
484         
485         $goods[$idx]['name']         = $row['goods_name'];
486         $goods[$idx]['brief']        = $row['goods_brief'];
487         $goods[$idx]['brand_name']   = $row['brand_name'];
488         $goods[$idx]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
489                                        sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
490         $goods[$idx]['market_price'] = price_format($row['market_price']);
491         $goods[$idx]['shop_price']   = price_format($row['shop_price']);
492         $goods[$idx]['thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);
493         $goods[$idx]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
494         $goods[$idx]['url']          = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
495
496         $goods[$idx]['short_style_name'] = add_style($goods[$idx]['short_name'], $row['goods_name_style']);
497         $idx++;
498     }
499
500     return $goods;
501 }
502
503 /**
504  * 获得商品的详细信息
505  *
506  * @access  public
507  * @param   integer     $goods_id
508  * @return  void
509  */
510 function get_goods_info($goods_id)
511 {
512     $time = gmtime();
513     $sql = 'SELECT g.*, c.measure_unit, b.brand_id, b.brand_name AS goods_brand, m.type_money AS bonus_money, ' .
514                 'IFNULL(AVG(r.comment_rank), 0) AS comment_rank, ' .
515                 "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS rank_price " .
516             'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
517             'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS c ON g.cat_id = c.cat_id ' .
518             'LEFT JOIN ' . $GLOBALS['ecs']->table('brand') . ' AS b ON g.brand_id = b.brand_id ' .
519             'LEFT JOIN ' . $GLOBALS['ecs']->table('comment') . ' AS r '.
520                 'ON r.id_value = g.goods_id AND comment_type = 0 AND r.parent_id = 0 AND r.status = 1 ' .
521             'LEFT JOIN ' . $GLOBALS['ecs']->table('bonus_type') . ' AS m ' .
522                 "ON g.bonus_type_id = m.type_id AND m.send_start_date <= '$time' AND m.send_end_date >= '$time'" .
523             " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
524                     "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
525             "WHERE g.goods_id = '$goods_id' AND g.is_delete = 0 " .
526             "GROUP BY g.goods_id";
527             
528     $row = $GLOBALS['db']->getRow($sql);
529
530     if ($row !== false)
531     {
532         /* 用户评论级别取整 */
533         $row['comment_rank']  = ceil($row['comment_rank']) == 0 ? 5 : ceil($row['comment_rank']);
534
535         /* 获得商品的销售价格 */
536         $row['market_price']        = price_format($row['market_price']);
537         $row['shop_price_formated'] = price_format($row['shop_price']);
538
539         /* 修正促销价格 */
540         if ($row['promote_price'] > 0)
541         {
542             $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
543         }
544         else
545         {
546             $promote_price = 0;
547         }
548
549         /* 处理商品水印图片 */
550         $watermark_img = '';
551
552         if ($promote_price != 0)
553         {
554             $watermark_img = "watermark_promote";
555         }
556         elseif ($row['is_new'] != 0)
557         {
558             $watermark_img = "watermark_new";
559         }
560         elseif ($row['is_best'] != 0)
561         {
562             $watermark_img = "watermark_best";
563         }
564         elseif ($row['is_hot'] != 0)
565         {
566             $watermark_img = 'watermark_hot';
567         }
568
569         if ($watermark_img != '')
570         {
571             $row['watermark_img'] =  $watermark_img;
572         }
573
574         $row['promote_price_org'] =  $promote_price;
575         $row['promote_price'] =  price_format($promote_price);
576
577         /* 修正重量显示 */
578         $row['goods_weight']  = (intval($row['goods_weight']) > 0) ?
579             $row['goods_weight'] . $GLOBALS['_LANG']['kilogram'] :
580             ($row['goods_weight'] * 1000) . $GLOBALS['_LANG']['gram'];
581
582         /* 修正上架时间显示 */
583         $row['add_time']      = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
584
585         /* 促销时间倒计时 */
586         $time = gmtime();
587         if ($time >= $row['promote_start_date'] && $time <= $row['promote_end_date'])
588         {
589              $row['gmt_end_time']  = $row['promote_end_date'];
590         }
591         else
592         {
593             $row['gmt_end_time'] = 0;
594         }
595
596         /* 是否显示商品库存数量 */
597         $row['goods_number']  = ($GLOBALS['_CFG']['use_storage'] == 1) ? $row['goods_number'] : '';
598
599         /* 修正积分:转换为可使用多少积分(原来是可以使用多少钱的积分) */
600         $row['integral']      = $GLOBALS['_CFG']['integral_scale'] ? round($row['integral'] * 100 / $GLOBALS['_CFG']['integral_scale']) : 0;
601
602         /* 修正优惠券 */
603         $row['bonus_money']   = ($row['bonus_money'] == 0) ? 0 : price_format($row['bonus_money'], false);
604
605         /* 修正商品图片 */
606         $row['goods_img']   = get_image_path($goods_id, $row['goods_img']);
607         $row['goods_thumb'] = get_image_path($goods_id, $row['goods_thumb'], true);
608
609         return $row;
610     }
611     else
612     {
613         return false;
614     }
615 }
616
617 /**
618  * 获得商品的属性和规格
619  *
620  * @access  public
621  * @param   integer $goods_id
622  * @return  array
623  */
624 function get_goods_properties($goods_id)
625 {
626     /* 对属性进行重新排序和分组 */
627     $sql = "SELECT attr_group ".
628             "FROM " . $GLOBALS['ecs']->table('goods_type') . " AS gt, " . $GLOBALS['ecs']->table('goods') . " AS g ".
629             "WHERE g.goods_id='$goods_id' AND gt.cat_id=g.goods_type";
630     $grp = $GLOBALS['db']->getOne($sql);
631
632     if (!empty($grp))
633     {
634         $groups = explode("\n", strtr($grp, "\r", ''));
635     }
636
637     /* 获得商品的规格 */
638     $sql = "SELECT a.attr_id, a.attr_name, a.attr_group, a.is_linked, a.attr_type, ".
639                 "g.goods_attr_id, g.attr_value, g.attr_price " .
640             'FROM ' . $GLOBALS['ecs']->table('goods_attr') . ' AS g ' .
641             'LEFT JOIN ' . $GLOBALS['ecs']->table('attribute') . ' AS a ON a.attr_id = g.attr_id ' .
642             "WHERE g.goods_id = '$goods_id' " .
643             'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id';
644     $res = $GLOBALS['db']->getAll($sql);
645
646     $arr['pro'] = array();     // 属性
647     $arr['spe'] = array();     // 规格
648     $arr['lnk'] = array();     // 关联的属性
649
650     foreach ($res AS $row)
651     {
652         $row['attr_value'] = str_replace("\n", '<br />', $row['attr_value']);
653
654         if ($row['attr_type'] == 0)
655         {
656             $group = (isset($groups[$row['attr_group']])) ? $groups[$row['attr_group']] : $GLOBALS['_LANG']['goods_attr'];
657
658             $arr['pro'][$group][$row['attr_id']]['name']  = $row['attr_name'];
659             $arr['pro'][$group][$row['attr_id']]['value'] = $row['attr_value'];
660         }
661         else
662         {
663             // 获取商品属性的缩略图
664             $goods_attr_id = $row['goods_attr_id'];
665             $sql_1 = "select thumb_url from ". $GLOBALS['ecs']->table('goods_gallery'). " where goods_id='$goods_id' and goods_attr_id='$goods_attr_id' and is_attr_image='1' ";
666             $goods_attr_thumb = $GLOBALS['db']->getOne($sql_1);
667             
668             $arr['spe'][$row['attr_id']]['attr_id'] = $row['attr_id'];
669             $arr['spe'][$row['attr_id']]['attr_type'] = $row['attr_type'];
670             $arr['spe'][$row['attr_id']]['name']     = $row['attr_name'];
671             $arr['spe'][$row['attr_id']]['values'][] = array(
672                                                         'label'        => $row['attr_value'],
673                                                         'price'        => $row['attr_price'],
674                                                         'goods_attr_thumb'    => $goods_attr_thumb,
675                                                         'format_price' => price_format(abs($row['attr_price']), false),
676                                                         'id'           => $row['goods_attr_id']);
677         }
678
679         if ($row['is_linked'] == 1)
680         {
681             /* 如果该属性需要关联,先保存下来 */
682             $arr['lnk'][$row['attr_id']]['name']  = $row['attr_name'];
683             $arr['lnk'][$row['attr_id']]['value'] = $row['attr_value'];
684         }
685     }
686
687     return $arr;
688 }
689
690 /**
691  * 获得属性相同的商品
692  *
693  * @access  public
694  * @param   array   $attr   // 包含了属性名称,ID的数组
695  * @return  array
696  */
697 function get_same_attribute_goods($attr)
698 {
699     $lnk = array();
700
701     if (!empty($attr))
702     {
703         foreach ($attr['lnk'] AS $key => $val)
704         {
705             $lnk[$key]['title'] = sprintf($GLOBALS['_LANG']['same_attrbiute_goods'], $val['name'], $val['value']);
706
707             /* 查找符合条件的商品 */
708             $sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price AS org_price, ' .
709                         "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
710                         'g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date ' .
711                     'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
712                     'LEFT JOIN ' . $GLOBALS['ecs']->table('goods_attr') . ' as a ON g.goods_id = a.goods_id ' .
713                     "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
714                         "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
715                     "WHERE a.attr_id = '$key' AND g.is_on_sale=1 AND a.attr_value = '$val[value]' AND g.goods_id <> '$_REQUEST[id]' " .
716                     'LIMIT ' . $GLOBALS['_CFG']['attr_related_number'];
717             $res = $GLOBALS['db']->getAll($sql);
718
719             foreach ($res AS $row)
720             {
721                 $lnk[$key]['goods'][$row['goods_id']]['goods_id']      = $row['goods_id'];
722                 $lnk[$key]['goods'][$row['goods_id']]['goods_name']    = $row['goods_name'];
723                 $lnk[$key]['goods'][$row['goods_id']]['short_name']    = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
724                     sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
725                 $lnk[$key]['goods'][$row['goods_id']]['goods_thumb']     = (empty($row['goods_thumb'])) ? $GLOBALS['_CFG']['no_picture'] : $row['goods_thumb'];
726                 $lnk[$key]['goods'][$row['goods_id']]['market_price']  = price_format($row['market_price']);
727                 $lnk[$key]['goods'][$row['goods_id']]['shop_price']    = price_format($row['shop_price']);
728                 $lnk[$key]['goods'][$row['goods_id']]['promote_price'] = bargain_price($row['promote_price'],
729                     $row['promote_start_date'], $row['promote_end_date']);
730                 $lnk[$key]['goods'][$row['goods_id']]['url']           = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
731             }
732         }
733     }
734
735     return $lnk;
736 }
737
738 /**
739  * 获得指定商品的相册
740  *
741  * @access  public
742  * @param   integer     $goods_id
743  * @return  array
744  */
745 function get_goods_gallery($goods_id)
746 {
747     $sql = 'SELECT img_id, img_url, thumb_url, img_desc' .
748         ' FROM ' . $GLOBALS['ecs']->table('goods_gallery') .
749         " WHERE goods_id = '$goods_id' LIMIT " . $GLOBALS['_CFG']['goods_gallery_number'];
750     $row = $GLOBALS['db']->getAll($sql);
751     /* 格式化相册图片路径 */
752     foreach($row as $key => $gallery_img)
753     {
754         $row[$key]['img_url'] = get_image_path($goods_id, $gallery_img['img_url'], false, 'gallery');
755         $row[$key]['thumb_url'] = get_image_path($goods_id, $gallery_img['thumb_url'], true, 'gallery');
756     }
757     return $row;
758 }
759
760 /**
761  * 获得指定分类下的商品
762  *
763  * @access  public
764  * @param   integer     $cat_id     分类ID
765  * @param   integer     $num        数量
766  * @param   string      $from       来自web/wap的调用
767  * @param   string      $order_rule 指定商品排序规则
768  * @return  array
769  */
770 function assign_cat_goods($cat_id, $num = 0, $from = 'web', $order_rule = '')
771 {
772     $children = get_children($cat_id);
773
774     $sql = 'SELECT g.goods_id, g.goods_name, g.market_price, g.shop_price AS org_price, ' .
775                 "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
776                'g.promote_price, promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img ' .
777             "FROM " . $GLOBALS['ecs']->table('goods') . ' AS g '.
778             "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
779                     "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
780             'WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND '.
781                 'g.is_delete = 0 AND (' . $children . 'OR ' . get_extension_goods($children) . ') ';
782
783     $order_rule = empty($order_rule) ? 'ORDER BY g.sort_order, g.goods_id DESC' : $order_rule;
784     $sql .= $order_rule;
785     if ($num > 0)
786     {
787         $sql .= ' LIMIT ' . $num;
788     }
789     $res = $GLOBALS['db']->getAll($sql);
790
791     $goods = array();
792     foreach ($res AS $idx => $row)
793     {
794         if ($row['promote_price'] > 0)
795         {
796             $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
797             $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
798         }
799         else
800         {
801             $goods[$idx]['promote_price'] = '';
802         }
803
804         $goods[$idx]['id']           = $row['goods_id'];
805         $goods[$idx]['name']         = $row['goods_name'];
806         $goods[$idx]['brief']        = $row['goods_brief'];
807         $goods[$idx]['market_price'] = price_format($row['market_price']);
808         $goods[$idx]['short_name']   = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
809                                         sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
810         $goods[$idx]['shop_price']   = price_format($row['shop_price']);
811         $goods[$idx]['thumb']        = get_image_path($row['goods_id'], $row['goods_thumb'], true);
812         $goods[$idx]['goods_img']    = get_image_path($row['goods_id'], $row['goods_img']);
813         $goods[$idx]['url']          = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
814     }
815
816     if ($from == 'web')
817     {
818         $GLOBALS['smarty']->assign('cat_goods_' . $cat_id, $goods);
819     }
820     elseif ($from == 'wap')
821     {
822         $cat['goods'] = $goods;
823     }
824
825     /* 分类信息 */
826     $sql = 'SELECT cat_name FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
827     $cat['name'] = $GLOBALS['db']->getOne($sql);
828     $cat['url']  = build_uri('category', array('cid' => $cat_id), $cat['name']);
829     $cat['id']   = $cat_id;
830
831     return $cat;
832 }
833
834 /**
835  * 获得指定的品牌下的商品
836  *
837  * @access  public
838  * @param   integer     $brand_id       品牌的ID
839  * @param   integer     $num            数量
840  * @param   integer     $cat_id         分类编号
841  * @param   string      $order_rule     指定商品排序规则
842  * @return  void
843  */
844 function assign_brand_goods($brand_id, $num = 0, $cat_id = 0,$order_rule = '')
845 {
846     $sql =  'SELECT g.goods_id, g.goods_name, g.market_price, g.shop_price AS org_price, ' .
847                 "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ".
848                 'g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img ' .
849             'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' .
850             "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
851                     "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
852             "WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.brand_id = '$brand_id'";
853
854     if ($cat_id > 0)
855     {
856         $sql .= get_children($cat_id);
857     }
858
859     $order_rule = empty($order_rule) ? ' ORDER BY g.sort_order, g.goods_id DESC' : $order_rule;
860     $sql .= $order_rule;
861     if ($num > 0)
862     {
863         $res = $GLOBALS['db']->selectLimit($sql, $num);
864     }
865     else
866     {
867         $res = $GLOBALS['db']->query($sql);
868     }
869
870     $idx = 0;
871     $goods = array();
872     while ($row = $GLOBALS['db']->fetchRow($res))
873     {
874         if ($row['promote_price'] > 0)
875         {
876             $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
877         }
878         else
879         {
880             $promote_price = 0;
881         }
882
883         $goods[$idx]['id']            = $row['goods_id'];
884         $goods[$idx]['name']          = $row['goods_name'];
885         $goods[$idx]['short_name']    = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
886             sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
887         $goods[$idx]['market_price']  = price_format($row['market_price']);
888         $goods[$idx]['shop_price']    = price_format($row['shop_price']);
889         $goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : '';
890         $goods[$idx]['brief']         = $row['goods_brief'];
891         $goods[$idx]['thumb']         = get_image_path($row['goods_id'], $row['goods_thumb'], true);
892         $goods[$idx]['goods_img']     = get_image_path($row['goods_id'], $row['goods_img']);
893         $goods[$idx]['url']           = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
894
895         $idx++;
896     }
897
898     /* 分类信息 */
899     $sql = 'SELECT brand_name FROM ' . $GLOBALS['ecs']->table('brand') . " WHERE brand_id = '$brand_id'";
900
901     $brand['id']   = $brand_id;
902     $brand['name'] = $GLOBALS['db']->getOne($sql);
903     $brand['url']  = build_uri('brand', array('bid' => $brand_id), $brand['name']);
904
905     $brand_goods = array('brand' => $brand, 'goods' => $goods);
906
907     return $brand_goods;
908 }
909
910 /**
911  * 获得所有扩展分类属于指定分类的所有商品ID
912  *
913  * @access  public
914  * @param   string $cat_id     分类查询字符串
915  * @return  string
916  */
917 function get_extension_goods($cats)
918 {
919     $extension_goods_array = '';
920     $sql = 'SELECT goods_id FROM ' . $GLOBALS['ecs']->table('goods_cat') . " AS g WHERE $cats";
921     $extension_goods_array = $GLOBALS['db']->getCol($sql);
922     return db_create_in($extension_goods_array, 'g.goods_id');
923 }
924
925 /**
926  * 判断某个商品是否正在特价促销期
927  *
928  * @access  public
929  * @param   float   $price      促销价格
930  * @param   string  $start      促销开始日期
931  * @param   string  $end        促销结束日期
932  * @return  float   如果还在促销期则返回促销价,否则返回0
933  */
934 function bargain_price($price, $start, $end)
935 {
936     if ($price == 0)
937     {
938         return 0;
939     }
940     else
941     {
942         $time = gmtime();
943         if ($time >= $start && $time <= $end)
944         {
945             return $price;
946         }
947         else
948         {
949             return 0;
950         }
951     }
952 }
953
954 /**
955  * 获得指定的规格的价格
956  *
957  * @access  public
958  * @param   mix     $spec   规格ID的数组或者逗号分隔的字符串
959  * @return  void
960  */
961 function spec_price($spec)
962 {
963     if (!empty($spec))
964     {
965         if(is_array($spec))
966         {
967             foreach($spec as $key=>$val)
968             {
969                 $spec[$key]=addslashes($val);
970             }
971         }
972         else
973         {
974             $spec=addslashes($spec);
975         }
976
977         $where = db_create_in($spec, 'goods_attr_id');
978
979         $sql = 'SELECT SUM(attr_price) AS attr_price FROM ' . $GLOBALS['ecs']->table('goods_attr') . " WHERE $where";
980         $price = floatval($GLOBALS['db']->getOne($sql));
981     }
982     else
983     {
984         $price = 0;
985     }
986
987     return $price;
988 }
989
990 /**
991  * 取得团购活动信息
992  * @param   int     $group_buy_id   团购活动id
993  * @param   int     $current_num    本次购买数量(计算当前价时要加上的数量)
994  * @return  array
995  *                  status          状态:
996  */
997 function group_buy_info($group_buy_id, $current_num = 0)
998 {
999     /* 取得团购活动信息 */
1000     
1001     //dqy add start 2011-8-24
1002     $group_buy_id = intval($group_buy_id);
1003     $sql = "SELECT b.*,g.*, b.act_id AS group_buy_id, b.act_desc AS group_buy_desc, b.start_time AS start_date, b.end_time AS end_date " .
1004             "FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS b " .
1005             "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON b.goods_id = g.goods_id " .
1006             "WHERE act_id = '$group_buy_id' " .
1007             "AND act_type = '" . GAT_GROUP_BUY . "'";
1008             
1009             //dqy add end 2011-8-24
1010     $group_buy = $GLOBALS['db']->getRow($sql);
1011
1012     /* 如果为空,返回空数组 */
1013     if (empty($group_buy))
1014     {
1015         return array();
1016     }
1017
1018     $ext_info = unserialize($group_buy['ext_info']);
1019     $group_buy = array_merge($group_buy, $ext_info);
1020
1021     /* 格式化时间 */
1022     $group_buy['formated_start_date'] = local_date('Y-m-d H:i', $group_buy['start_time']);
1023     $group_buy['formated_end_date'] = local_date('Y-m-d H:i', $group_buy['end_time']);
1024
1025     /* 格式化保证金 */
1026     $group_buy['formated_deposit'] = price_format($group_buy['deposit'], false);
1027
1028     /* 处理价格阶梯 */
1029     $price_ladder = $group_buy['price_ladder'];
1030     if (!is_array($price_ladder) || empty($price_ladder))
1031     {
1032         $price_ladder = array(array('amount' => 0, 'price' => 0));
1033     }
1034     else
1035     {
1036         foreach ($price_ladder as $key => $amount_price)
1037         {
1038             $price_ladder[$key]['formated_price'] = price_format($amount_price['price'], false);
1039         }
1040     }
1041     $group_buy['price_ladder'] = $price_ladder;
1042
1043     /* 统计信息 */
1044     $stat = group_buy_stat($group_buy_id, $group_buy['deposit']);
1045     $group_buy = array_merge($group_buy, $stat);
1046
1047     /* 计算当前价 */
1048     $cur_price  = $price_ladder[0]['price']; // 初始化
1049     $cur_amount = $stat['valid_goods'] + $current_num; // 当前数量
1050     foreach ($price_ladder as $amount_price)
1051     {
1052         if ($cur_amount >= $amount_price['amount'])
1053         {
1054             $cur_price = $amount_price['price'];
1055         }
1056         else
1057         {
1058             break;
1059         }
1060     }
1061     //yyy start
1062 $group_buy['goods_desc'] = $GLOBALS['db']->getOne("select goods_desc from ".$GLOBALS['ecs']->table('goods')." where goods_id=".$group_buy['goods_id']);
1063 //yyy end
1064
1065     $group_buy['cur_price'] = $cur_price;
1066     $group_buy['formated_cur_price'] = price_format($cur_price, false);
1067
1068     //dqy add start 2011-8-24
1069     if ($group_buy['shop_price'] == 0)
1070     {
1071             $group_buy['zhekou'] = 0;
1072     }else{
1073         
1074         $group_buy['zhekou'] = number_format(intval($group_buy['cur_price'])/intval($group_buy['shop_price']),2) * 100;
1075     }
1076         
1077     $group_buy['jiesheng'] = $group_buy['shop_price'] - $group_buy['cur_price'];
1078     //dqy add end 2011-8-24
1079     
1080     /* 最终价 */
1081     $group_buy['trans_price'] = $group_buy['cur_price'];
1082     $group_buy['formated_trans_price'] = $group_buy['formated_cur_price'];
1083     $group_buy['trans_amount'] = $group_buy['valid_goods'];
1084
1085     /* 状态 */
1086     $group_buy['status'] = group_buy_status($group_buy);
1087     if (isset($GLOBALS['_LANG']['gbs'][$group_buy['status']]))
1088     {
1089         $group_buy['status_desc'] = $GLOBALS['_LANG']['gbs'][$group_buy['status']];
1090     }
1091
1092     $group_buy['start_time'] = $group_buy['formated_start_date'];
1093     $group_buy['end_time'] = $group_buy['formated_end_date'];
1094
1095     return $group_buy;
1096 }
1097
1098
1099 /*
1100  * 取得某团购活动统计信息
1101  * @param   int     $group_buy_id   团购活动id
1102  * @param   float   $deposit        保证金
1103  * @return  array   统计信息
1104  *                  total_order     总订单数
1105  *                  total_goods     总商品数
1106  *                  valid_order     有效订单数
1107  *                  valid_goods     有效商品数
1108  */
1109 function group_buy_stat($group_buy_id, $deposit)
1110 {
1111     $group_buy_id = intval($group_buy_id);
1112
1113     /* 取得团购活动商品ID */
1114     $sql = "SELECT goods_id " .
1115            "FROM " . $GLOBALS['ecs']->table('goods_activity') .
1116            "WHERE act_id = '$group_buy_id' " .
1117            "AND act_type = '" . GAT_GROUP_BUY . "'";
1118     $group_buy_goods_id = $GLOBALS['db']->getOne($sql);
1119
1120     /* 取得总订单数和总商品数 */
1121     $sql = "SELECT COUNT(*) AS total_order, SUM(g.goods_number) AS total_goods " .
1122             "FROM " . $GLOBALS['ecs']->table('order_info') . " AS o, " .
1123                 $GLOBALS['ecs']->table('order_goods') . " AS g " .
1124             " WHERE o.order_id = g.order_id " .
1125             "AND o.extension_code = 'group_buy' " .
1126             "AND o.extension_id = '$group_buy_id' " .
1127             "AND g.goods_id = '$group_buy_goods_id' " .
1128             "AND (order_status = '" . OS_CONFIRMED . "' OR order_status = '" . OS_UNCONFIRMED . "')";
1129             
1130     $stat = $GLOBALS['db']->getRow($sql);
1131     if ($stat['total_order'] == 0)
1132     {
1133         $stat['total_goods'] = 0;
1134     }
1135
1136     /* 取得有效订单数和有效商品数 */
1137     $deposit = floatval($deposit);
1138     if ($deposit > 0 && $stat['total_order'] > 0)
1139     {
1140         $sql .= " AND (o.money_paid + o.surplus) >= '$deposit'";
1141         $row = $GLOBALS['db']->getRow($sql);
1142         $stat['valid_order'] = $row['total_order'];
1143         if ($stat['valid_order'] == 0)
1144         {
1145             $stat['valid_goods'] = 0;
1146         }
1147         else
1148         {
1149             $stat['valid_goods'] = $row['total_goods'];
1150         }
1151     }
1152     else
1153     {
1154         $stat['valid_order'] = $stat['total_order'];
1155         $stat['valid_goods'] = $stat['total_goods'];
1156     }
1157
1158     return $stat;
1159 }
1160
1161 /**
1162  * 获得团购的状态
1163  *
1164  * @access  public
1165  * @param   array
1166  * @return  integer
1167  */
1168 function group_buy_status($group_buy)
1169 {
1170     $now = gmtime();
1171     if ($group_buy['is_finished'] == 0)
1172     {
1173         /* 未处理 */
1174         if ($now < $group_buy['start_time'])
1175         {
1176             $status = GBS_PRE_START;
1177         }
1178         elseif ($now > $group_buy['end_time'])
1179         {
1180             $status = GBS_FINISHED;
1181         }
1182         else
1183         {
1184             if ($group_buy['restrict_amount'] == 0 || $group_buy['valid_goods'] < $group_buy['restrict_amount'])
1185             {
1186                 $status = GBS_UNDER_WAY;
1187             }
1188             else
1189             {
1190                 $status = GBS_FINISHED;
1191             }
1192         }
1193     }
1194     elseif ($group_buy['is_finished'] == GBS_SUCCEED)
1195     {
1196         /* 已处理,团购成功 */
1197         $status = GBS_SUCCEED;
1198     }
1199     elseif ($group_buy['is_finished'] == GBS_FAIL)
1200     {
1201         /* 已处理,团购失败 */
1202         $status = GBS_FAIL;
1203     }
1204
1205     return $status;
1206 }
1207
1208 /**
1209  * 取得拍卖活动信息
1210  * @param   int     $act_id     活动id
1211  * @return  array
1212  */
1213 function auction_info($act_id, $config = false)
1214 {
1215     $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('goods_activity') . " WHERE act_id = '$act_id'";
1216     $auction = $GLOBALS['db']->getRow($sql);
1217     if ($auction['act_type'] != GAT_AUCTION)
1218     {
1219         return array();
1220     }
1221     $auction['status_no'] = auction_status($auction);
1222     if ($config == true)
1223     {
1224
1225         $auction['start_time'] = local_date('Y-m-d H:i', $auction['start_time']);
1226         $auction['end_time'] = local_date('Y-m-d H:i', $auction['end_time']);
1227     }
1228     else
1229     {
1230         $auction['start_time'] = local_date($GLOBALS['_CFG']['time_format'], $auction['start_time']);
1231         $auction['end_time'] = local_date($GLOBALS['_CFG']['time_format'], $auction['end_time']);
1232     }
1233     $ext_info = unserialize($auction['ext_info']);
1234     $auction = array_merge($auction, $ext_info);
1235     $auction['formated_start_price'] = price_format($auction['start_price']);
1236     $auction['formated_end_price'] = price_format($auction['end_price']);
1237     $auction['formated_amplitude'] = price_format($auction['amplitude']);
1238     $auction['formated_deposit'] = price_format($auction['deposit']);
1239
1240     /* 查询出价用户数和最后出价 */
1241     $sql = "SELECT COUNT(DISTINCT bid_user) FROM " . $GLOBALS['ecs']->table('auction_log') .
1242             " WHERE act_id = '$act_id'";
1243     $auction['bid_user_count'] = $GLOBALS['db']->getOne($sql);
1244     if ($auction['bid_user_count'] > 0)
1245     {
1246         $sql = "SELECT a.*, u.user_name " .
1247                 "FROM " . $GLOBALS['ecs']->table('auction_log') . " AS a, " .
1248                         $GLOBALS['ecs']->table('users') . " AS u " .
1249                 "WHERE a.bid_user = u.user_id " .
1250                 "AND act_id = '$act_id' " .
1251                 "ORDER BY a.log_id DESC";
1252         $row = $GLOBALS['db']->getRow($sql);
1253         $row['formated_bid_price'] = price_format($row['bid_price'], false);
1254         $row['bid_time'] = local_date($GLOBALS['_CFG']['time_format'], $row['bid_time']);
1255         $auction['last_bid'] = $row;
1256     }
1257
1258     /* 查询已确认订单数 */
1259     if ($auction['status_no'] > 1)
1260     {
1261         $sql = "SELECT COUNT(*)" .
1262                 " FROM " . $GLOBALS['ecs']->table('order_info') .
1263                 " WHERE extension_code = 'auction'" .
1264                 " AND extension_id = '$act_id'" .
1265                 " AND order_status " . db_create_in(array(OS_CONFIRMED, OS_UNCONFIRMED));
1266         $auction['order_count'] = $GLOBALS['db']->getOne($sql);
1267     }
1268     else
1269     {
1270         $auction['order_count'] = 0;
1271     }
1272
1273     /* 当前价 */
1274     $auction['current_price'] = isset($auction['last_bid']) ? $auction['last_bid']['bid_price'] : $auction['start_price'];
1275     $auction['formated_current_price'] = price_format($auction['current_price'], false);
1276
1277     return $auction;
1278 }
1279
1280 /**
1281  * 取得拍卖活动出价记录
1282  * @param   int     $act_id     活动id
1283  * @return  array
1284  */
1285 function auction_log($act_id)
1286 {
1287     $log = array();
1288     $sql = "SELECT a.*, u.user_name " .
1289             "FROM " . $GLOBALS['ecs']->table('auction_log') . " AS a," .
1290                       $GLOBALS['ecs']->table('users') . " AS u " .
1291             "WHERE a.bid_user = u.user_id " .
1292             "AND act_id = '$act_id' " .
1293             "ORDER BY a.log_id DESC";
1294     $res = $GLOBALS['db']->query($sql);
1295     while ($row = $GLOBALS['db']->fetchRow($res))
1296     {
1297         $row['bid_time'] = local_date($GLOBALS['_CFG']['time_format'], $row['bid_time']);
1298         $row['formated_bid_price'] = price_format($row['bid_price'], false);
1299         $log[] = $row;
1300     }
1301
1302     return $log;
1303 }
1304
1305 function auction_log_count($act_id)
1306 {
1307      $sql = "SELECT count(*) " .
1308             "FROM " . $GLOBALS['ecs']->table('auction_log') . " AS a," .
1309                       $GLOBALS['ecs']->table('users') . " AS u " .
1310             "WHERE a.bid_user = u.user_id " .
1311             "AND act_id = '$act_id' ";
1312     return $GLOBALS['db']->getOne($sql);
1313 }
1314
1315 /**
1316  * 计算拍卖活动状态(注意参数一定是原始信息)
1317  * @param   array   $auction    拍卖活动原始信息
1318  * @return  int
1319  */
1320 function auction_status($auction)
1321 {
1322     $now = gmtime();
1323     if ($auction['is_finished'] == 0)
1324     {
1325         if ($now < $auction['start_time'])
1326         {
1327             return PRE_START; // 未开始
1328         }
1329         elseif ($now > $auction['end_time'])
1330         {
1331             return FINISHED; // 已结束,未处理
1332         }
1333         else
1334         {
1335             return UNDER_WAY; // 进行中
1336         }
1337     }
1338     elseif ($auction['is_finished'] == 1)
1339     {
1340         return FINISHED; // 已结束,未处理
1341     }
1342     else
1343     {
1344         return SETTLED; // 已结束,已处理
1345     }
1346 }
1347
1348 /**
1349  * 取得商品信息
1350  * @param   int     $goods_id   商品id
1351  * @return  array
1352  */
1353 function goods_info($goods_id)
1354 {
1355     $sql = "SELECT g.*, b.brand_name " .
1356             "FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
1357                 "LEFT JOIN " . $GLOBALS['ecs']->table('brand') . " AS b ON g.brand_id = b.brand_id " .
1358             "WHERE g.goods_id = '$goods_id'";
1359     $row = $GLOBALS['db']->getRow($sql);
1360     if (!empty($row))
1361     {
1362         /* 修正重量显示 */
1363         $row['goods_weight'] = (intval($row['goods_weight']) > 0) ?
1364             $row['goods_weight'] . $GLOBALS['_LANG']['kilogram'] :
1365             ($row['goods_weight'] * 1000) . $GLOBALS['_LANG']['gram'];
1366
1367         /* 修正图片 */
1368         $row['goods_img'] = get_image_path($goods_id, $row['goods_img']);
1369     }
1370
1371     return $row;
1372 }
1373
1374 /**
1375  * 取得优惠活动信息
1376  * @param   int     $act_id     活动id
1377  * @return  array
1378  */
1379 function favourable_info($act_id)
1380 {
1381     $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('favourable_activity') .
1382             " WHERE act_id = '$act_id'";
1383     $row = $GLOBALS['db']->getRow($sql);
1384     if (!empty($row))
1385     {
1386         $row['start_time'] = local_date($GLOBALS['_CFG']['time_format'], $row['start_time']);
1387         $row['end_time'] = local_date($GLOBALS['_CFG']['time_format'], $row['end_time']);
1388         $row['formated_min_amount'] = price_format($row['min_amount']);
1389         $row['formated_max_amount'] = price_format($row['max_amount']);
1390         $row['gift'] = unserialize($row['gift']);
1391         if ($row['act_type'] == FAT_GOODS)
1392         {
1393             $row['act_type_ext'] = round($row['act_type_ext']);
1394         }
1395     }
1396
1397     return $row;
1398 }
1399
1400 /**
1401  * 批发信息
1402  * @param   int     $act_id     活动id
1403  * @return  array
1404  */
1405 function wholesale_info($act_id)
1406 {
1407     $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('wholesale') .
1408             " WHERE act_id = '$act_id'";
1409     $row = $GLOBALS['db']->getRow($sql);
1410     if (!empty($row))
1411     {
1412         $row['price_list'] = unserialize($row['prices']);
1413     }
1414
1415     return $row;
1416 }
1417
1418 /**
1419  * 添加商品名样式
1420  * @param   string     $goods_name     商品名称
1421  * @param   string     $style          样式参数
1422  * @return  string
1423  */
1424 function add_style($goods_name, $style)
1425 {
1426     $goods_style_name = $goods_name;
1427
1428     $arr   = explode('+', $style);
1429
1430     $font_color     = !empty($arr[0]) ? $arr[0] : '';
1431     $font_style = !empty($arr[1]) ? $arr[1] : '';
1432
1433     if ($font_color!='')
1434     {
1435         $goods_style_name = '<font color=' . $font_color . '>' . $goods_style_name . '</font>';
1436     }
1437     if ($font_style != '')
1438     {
1439         $goods_style_name = '<' . $font_style .'>' . $goods_style_name . '</' . $font_style . '>';
1440     }
1441     return $goods_style_name;
1442 }
1443
1444 /**
1445  * 取得商品属性
1446  * @param   int     $goods_id   商品id
1447  * @return  array
1448  */
1449 function get_goods_attr($goods_id)
1450 {
1451     $attr_list = array();
1452     $sql = "SELECT a.attr_id, a.attr_name " .
1453             "FROM " . $GLOBALS['ecs']->table('goods') . " AS g, " . $GLOBALS['ecs']->table('attribute') . " AS a " .
1454             "WHERE g.goods_id = '$goods_id' " .
1455             "AND g.goods_type = a.cat_id " .
1456             "AND a.attr_type = 1";
1457     $attr_id_list = $GLOBALS['db']->getCol($sql);
1458     $res = $GLOBALS['db']->query($sql);
1459     while ($attr = $GLOBALS['db']->fetchRow($res))
1460     {
1461         if (defined('ECS_ADMIN'))
1462         {
1463             $attr['goods_attr_list'] = array(0 => $GLOBALS['_LANG']['select_please']);
1464         }
1465         else
1466         {
1467             $attr['goods_attr_list'] = array();
1468         }
1469         $attr_list[$attr['attr_id']] = $attr;
1470     }
1471
1472     $sql = "SELECT attr_id, goods_attr_id, attr_value, attr_price " .
1473             "FROM " . $GLOBALS['ecs']->table('goods_attr') .
1474             " WHERE goods_id = '$goods_id' " .
1475             "AND attr_id " . db_create_in($attr_id_list);
1476     $res = $GLOBALS['db']->query($sql);
1477     while ($goods_attr = $GLOBALS['db']->fetchRow($res))
1478     {
1479         $attr_list[$goods_attr['attr_id']]['goods_attr_list'][$goods_attr['goods_attr_id']] = $goods_attr['attr_value'];
1480         $attr_list[$goods_attr['attr_id']]['goods_attr_price'][$goods_attr['goods_attr_id']] = $goods_attr['attr_price'];
1481     }
1482
1483     return $attr_list;
1484 }
1485
1486 /**
1487  * 获得购物车中商品的配件
1488  *
1489  * @access  public
1490  * @param   array     $goods_list
1491  * @return  array
1492  */
1493 function get_goods_fittings($goods_list = array())
1494 {
1495     $temp_index = 0;
1496     $arr        = array();
1497
1498     $sql = 'SELECT gg.parent_id, ggg.goods_name AS parent_name, gg.goods_id, gg.goods_price, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price AS org_price, ' .
1499                 "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price ".
1500             'FROM ' . $GLOBALS['ecs']->table('group_goods') . ' AS gg ' .
1501             'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . 'AS g ON g.goods_id = gg.goods_id ' .
1502             "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ".
1503                     "ON mp.goods_id = gg.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ".
1504             "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS ggg ON ggg.goods_id = gg.parent_id ".
1505             "WHERE gg.parent_id " . db_create_in($goods_list) . " AND g.is_delete = 0 AND g.is_on_sale = 1 ".
1506             "ORDER BY gg.parent_id, gg.goods_id";
1507
1508     $res = $GLOBALS['db']->query($sql);
1509
1510     while ($row = $GLOBALS['db']->fetchRow($res))
1511     {
1512         $arr[$temp_index]['parent_id']         = $row['parent_id'];//配件的基本件ID
1513         $arr[$temp_index]['parent_name']       = $row['parent_name'];//配件的基本件的名称
1514         $arr[$temp_index]['parent_short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
1515             sub_str($row['parent_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['parent_name'];//配件的基本件显示的名称
1516         $arr[$temp_index]['goods_id']          = $row['goods_id'];//配件的商品ID
1517         $arr[$temp_index]['goods_name']        = $row['goods_name'];//配件的名称
1518         $arr[$temp_index]['short_name']        = $GLOBALS['_CFG']['goods_name_length'] > 0 ?
1519             sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];//配件显示的名称
1520         $arr[$temp_index]['fittings_price']    = price_format($row['goods_price']);//配件价格
1521         $arr[$temp_index]['shop_price']        = price_format($row['shop_price']);//配件原价格
1522         $arr[$temp_index]['goods_thumb']       = get_image_path($row['goods_id'], $row['goods_thumb'], true);
1523         $arr[$temp_index]['goods_img']         = get_image_path($row['goods_id'], $row['goods_img']);
1524         $arr[$temp_index]['url']               = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
1525         $temp_index ++;
1526     }
1527
1528     return $arr;
1529 }
1530
1531 /**
1532  * 取指定规格的货品信息
1533  *
1534  * @access      public
1535  * @param       string      $goods_id
1536  * @param       array       $spec_goods_attr_id
1537  * @return      array
1538  */
1539 function get_products_info($goods_id, $spec_goods_attr_id)
1540 {
1541     $return_array = array();
1542
1543     if (empty($spec_goods_attr_id) || !is_array($spec_goods_attr_id) || empty($goods_id))
1544     {
1545         return $return_array;
1546     }
1547
1548     $goods_attr_array = sort_goods_attr_id_array($spec_goods_attr_id);
1549
1550     if(isset($goods_attr_array['sort']))
1551     {
1552         $goods_attr = implode('|', $goods_attr_array['sort']);
1553
1554         $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('products'). " WHERE goods_id = '$goods_id' AND goods_attr = '$goods_attr' LIMIT 0, 1";
1555         $return_array = $GLOBALS['db']->getRow($sql);
1556     }
1557     return $return_array;
1558 }
1559
1560 /* 代码增加_start */
1561 function  is_exist_prod($first_arr, $one, $prod_exist_arr)
1562 {
1563     if (empty($prod_exist_arr))
1564     {
1565         return 0;
1566     }
1567     $first_arr[]=$one;
1568
1569     $all_valid =0;
1570     foreach($prod_exist_arr AS $item_exist)
1571     {        
1572         $first_exist=1;
1573         foreach($first_arr AS $first)
1574         {            
1575             if (!strstr($item_exist, '|'. $first .'|'))
1576             {
1577                 $first_exist=0;
1578                 break;
1579             }
1580         }
1581         if($first_exist)
1582         {
1583             $all_valid=1;
1584             break;
1585         }
1586     }
1587     return $all_valid;
1588 }
1589
1590 /* 代码增加_end */
1591 /**
1592  * 调用已售出数量
1593  *
1594  */
1595
1596  function selled_count($goods_id)
1597 {
1598     $sql = "select sum(goods_number) from " . $GLOBALS['ecs']->table('order_goods') . " AS g ,".$GLOBALS['ecs']->table('order_info') . " AS o WHERE o.pay_status = 2 AND o.order_status >= 1 and o.order_id=g.order_id and g.goods_id = " . $goods_id    ;/*注意订单状态*/
1599
1600    $res = $GLOBALS['db']->getOne($sql);
1601    if($res>0)
1602    {
1603        $res = $res;
1604     }
1605     else{
1606         $res = 0;
1607         }
1608     
1609 return $res;
1610 }
1611
1612 /**
1613 * 根据条形码获取对应商品信息
1614 * @param array $info  条形码数组信息
1615 */
1616 function get_goods_by_txm($info){
1617     global $db,$ecs;
1618
1619     $sql = "select bc.*,g.* from ".$ecs->table('bar_code')." as bc left join ".$ecs->table('goods')." as g on bc.goods_id=g.goods_id where bc.bar_code in(".implode(',',$info).")";
1620
1621     $ret = $db->query($sql);
1622     $goodsinfo = array();
1623     while($row = $db->fetchRow($ret)){
1624         $attr_info = get_goods_attr_txm($row['goods_id'],$row['taypes']);//获取商品属性信息
1625         if(empty($attr_info['info'])){
1626             $goods_number = $row['goods_number'];
1627             $attr_ids = '';
1628             $row['goods_attr'] = $row['goods_attr_id'] = $row['goods_attr_price'] = '';
1629         }else{
1630             $attr_ids = array_keys($attr_info['info']);
1631             $attr_ids = sort_goods_attr_id_array($attr_ids);//商品属性id排序
1632             $row['goods_attr_id'] = implode(',',$attr_ids['sort']);
1633             $row['goods_attr'] = $attr_info['info'];
1634             $row['goods_attr_price'] = (is_array($attr_info['price'])) ? array_sum($attr_info['price']) : 0;
1635             $goods_number = get_goods_nums_txm($row['goods_id'],$attr_ids['sort']);//货品总数量
1636         }
1637         $goodsinfo[$row['bar_code']]['goods_id'] = $row['goods_id'];
1638         $goodsinfo[$row['bar_code']]['goods_name'] = $row['goods_name'];
1639         $goodsinfo[$row['bar_code']]['goods_sn'] = $row['goods_sn'];
1640         $goodsinfo[$row['bar_code']]['product_id'] = '';
1641         $goodsinfo[$row['bar_code']]['goods_name'] = $row['goods_name'];
1642         $goodsinfo[$row['bar_code']]['market_price'] = $row['market_price']+$row['goods_attr_price'];
1643         $goodsinfo[$row['bar_code']]['goods_price'] = get_final_price($row['goods_id'],1,true,$attr_ids['sort']);//最终结算价格
1644         $goodsinfo[$row['bar_code']]['goods_number'] = intval($goods_number);
1645         $goodsinfo[$row['bar_code']]['goods_attr'] = $row['goods_attr'];
1646         $goodsinfo[$row['bar_code']]['goods_attr_id'] = $row['goods_attr_id'];
1647         $goodsinfo[$row['bar_code']]['goods_attr_price'] = $row['goods_attr_price'];
1648         $goodsinfo[$row['bar_code']]['goods_thumb'] = $row['goods_thumb'];
1649         $goodsinfo[$row['bar_code']]['goods_img'] = $row['goods_img'];
1650     }
1651     return $goodsinfo;
1652 }
1653
1654 function get_goods_attr_txm($goods_id,$taypes){
1655     if(empty($taypes)){
1656         return '';//扫描的商品没有属性,返回空
1657     }
1658     $taypesinfo = explode('+',$taypes);
1659     $goods_attr = get_goods_attr($goods_id);
1660     if(empty($goods_attr)){
1661         return '';//仓库中的商品没有属性,返回空
1662     }
1663     $attr = $price = array();
1664     foreach($taypesinfo as $key=>$val){
1665         foreach($goods_attr as $gkey=>$gval){
1666             foreach($gval['goods_attr_list'] as $akey=>$aval){
1667                 if($aval == $val){
1668                     $attr[$akey] = $gval['attr_name'].':'.$val;
1669                     if(intval($gval['goods_attr_price'][$akey])>0){
1670                         $attr[$akey] .= "[".$gval['goods_attr_price'][$akey]."]";
1671                         $price[$akey] = $gval['goods_attr_price'][$akey];
1672                     }
1673                 }
1674             }
1675         }
1676     }
1677     return array('info'=>$attr,'price'=>$price);
1678 }
1679
1680 function get_goods_nums_txm($goods_id,$attr_id){
1681     $goods_attr = implode('|', $attr_id);
1682
1683     $sql = "SELECT product_id, goods_id, goods_attr, product_sn, product_number
1684             FROM " . $GLOBALS['ecs']->table('products') . " 
1685             WHERE goods_id = $goods_id AND goods_attr = '".$goods_attr."' LIMIT 0, 1";
1686     $row = $GLOBALS['db']->getRow($sql);
1687     
1688     return $row['product_number'];
1689 }
1690
1691
1692 //-------------------------------------------------------------------------------
1693 // 预售活动
1694 //-------------------------------------------------------------------------------
1695
1696 /**
1697  * 判断当前商品是否为预售商品
1698  * @param unknown $goods_id
1699  * @return int pre_sale_id
1700  */
1701 function is_pre_sale_goods($goods_id)
1702 {
1703     $sql = "SELECT act_id " .
1704             "FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS b " .
1705             "WHERE goods_id = '$goods_id' " .
1706             "AND is_finished < ".PSS_FINISHED." " .
1707             "AND act_type = '" . GAT_PRE_SALE . "'";
1708     $pre_sale_id = $GLOBALS['db']->getOne($sql);
1709
1710     if(empty($pre_sale_id))
1711     {
1712         return null;
1713     }
1714
1715     return $pre_sale_id;
1716 }
1717
1718 /**
1719  * 根据预售活动编号取得预售活动信息
1720  * @param   int     $pre_sale_id    预售活动id
1721  * @param   int     $current_num    本次购买数量(计算当前价时要加上的数量)
1722  * @return  array
1723  *                  status    状态:</br>
1724  *                  formated_start_date    格式化预售开始时间</br>
1725  *                  formated_end_date    格式化预售结束时间</br>
1726  *                  formated_retainage_start    尾款支付开始时间</br>
1727  *                  formated_retainage_end    尾款支付结束时间</br>
1728  *                  formated_deposit    格式化后的保证金</br>
1729  *                  formated_sale_price    格式化后的预售价格</br>
1730  *                  price_ladder    阶梯价格[amount: 数量, price: 价格, formated_price: 格式化后的价格]</br>
1731  *                  total_order    总订单数</br>
1732  *                  total_goods    总商品数</br>
1733  *                  valid_order    有效订单数</br>
1734  *                  valid_goods    有效商品数</br>
1735  *                  cur_price    当前阶梯价格</br>
1736  *                  formated_cur_price    格式化后的当前阶梯价格</br>
1737  *                  cur_amount    当前阶梯数量</br>
1738  *                  give_integral    赠送积分</br>
1739  *                  status    预售活动状态</br>
1740  */
1741 function pre_sale_info($pre_sale_id, $current_num = 0)
1742 {
1743     /* 取得团购活动信息 */
1744     $pre_sale_id = intval($pre_sale_id);
1745     $sql = "SELECT b.*,g.*, b.act_id AS pre_sale_id, b.act_desc AS pre_sale_desc, b.start_time, b.end_time " .
1746             "FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS b " .
1747             "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON b.goods_id = g.goods_id " .
1748             "WHERE act_id = '$pre_sale_id' " .
1749             "AND act_type = '" . GAT_PRE_SALE . "'";
1750     $pre_sale = $GLOBALS['db']->getRow($sql);
1751
1752     /* 如果为空,返回空数组 */
1753     if (empty($pre_sale))
1754     {
1755         return array();
1756     }
1757
1758     $ext_info = unserialize($pre_sale['ext_info']);
1759     $pre_sale = array_merge($pre_sale, $ext_info);
1760
1761     /* 格式化时间 */
1762     $pre_sale['formated_start_date'] = local_date('Y-m-d H:i', $pre_sale['start_time']);
1763     $pre_sale['formated_end_date'] = local_date('Y-m-d H:i', $pre_sale['end_time']);
1764
1765     // 尾款支付的开始和结束时间
1766     $pre_sale['formated_retainage_start'] = local_date('Y-m-d H:i', $pre_sale['retainage_start']);
1767     $pre_sale['formated_retainage_end'] = local_date('Y-m-d H:i', $pre_sale['retainage_end']);
1768
1769     /* 格式化预售价格和保证金 */
1770     $pre_sale['formated_sale_price'] = price_format($pre_sale['sale_price'], false);
1771     $pre_sale['formated_deposit'] = price_format($pre_sale['deposit'], false);
1772
1773     // 本地时间,用于倒计时显示,符合JS格式
1774     $pre_sale['local_end_date'] = local_date('Y, m-1, d, H, i, s', $pre_sale['end_time']);
1775
1776     /* 处理价格阶梯 */
1777     $price_ladder = $pre_sale['price_ladder'];
1778
1779     /* 价格阶梯必须为有效,而且一定会有价格阶梯 */
1780     if (!is_array($price_ladder) || empty($price_ladder))
1781     {
1782         // 如果阶梯价格设置为空则设置默认值
1783         // 这种情况应该不允许出现
1784         $price_ladder = array(array('amount' => 0, 'price' => 0));
1785     }
1786     else
1787     {
1788         // 遍历阶梯价格
1789         foreach ($price_ladder as $key => $amount_price)
1790         {
1791             // 格式化每一个阶梯价格
1792             $price_ladder[$key]['formated_price'] = price_format($amount_price['price'], false);
1793         }
1794     }
1795     $pre_sale['price_ladder'] = $price_ladder;
1796     $pre_sale['price_ladder_count'] = count($price_ladder);
1797
1798     /* 统计信息 */
1799     $stat = pre_sale_stat($pre_sale_id, $pre_sale['deposit']);
1800     // 合并统计信息
1801     $pre_sale = array_merge($pre_sale, $stat);
1802
1803     /* 计算当前价 */
1804     $cur_price  = $price_ladder[0]['price']; // 初始化
1805     $cur_amount = $stat['valid_goods'] + $current_num; // 当前数量
1806     // 计算最低价格
1807     foreach ($price_ladder as $amount_price)
1808     {
1809         if ($cur_amount >= $amount_price['amount'])
1810         {
1811             $cur_price = $amount_price['price'];
1812         }
1813         else
1814         {
1815             break;
1816         }
1817     }
1818
1819     // 获取商品描述
1820     $pre_sale['goods_desc'] = $GLOBALS['db']->getOne("select goods_desc from ".$GLOBALS['ecs']->table('goods')." where goods_id=".$pre_sale['goods_id']);
1821     $pre_sale['cur_price'] = $cur_price;
1822     $pre_sale['$cur_amount'] = $cur_amount;
1823     $pre_sale['formated_cur_price'] = price_format($cur_price, false);
1824
1825     // 计算折扣
1826     if ($pre_sale['shop_price'] == 0)
1827     {
1828         $pre_sale['zhekou'] = 0;
1829     }
1830     else
1831     {
1832         $pre_sale['zhekou'] = number_format(intval($pre_sale['cur_price'])/intval($pre_sale['shop_price']),2) * 100;
1833     }
1834
1835     // 计算节省金额
1836     $pre_sale['jiesheng'] = $pre_sale['shop_price'] - $pre_sale['cur_price'];
1837
1838     /* 最终价 */
1839     $pre_sale['trans_price'] = $pre_sale['cur_price'];
1840     $pre_sale['formated_trans_price'] = $pre_sale['formated_cur_price'];
1841     $pre_sale['trans_amount'] = $pre_sale['valid_goods'];
1842
1843     /* 状态 */
1844     $pre_sale['status'] = pre_sale_status($pre_sale);
1845     if (isset($GLOBALS['_LANG']['gbs'][$pre_sale['status']]))
1846     {
1847         $pre_sale['status_desc'] = $GLOBALS['_LANG']['gbs'][$group_buy['status']];
1848     }
1849
1850     $pre_sale['start_time'] = $pre_sale['formated_start_date'];
1851     $pre_sale['end_time'] = $pre_sale['formated_end_date'];
1852
1853     $pre_sale['retainage_start'] = $pre_sale['formated_retainage_start'];
1854     $pre_sale['retainage_end'] = $pre_sale['formated_retainage_end'];
1855
1856     return $pre_sale;
1857 }
1858
1859 /**
1860  * 取得某预售活动统计信息
1861  * @param   int     $pre_sale_id    预售活动id
1862  * @param   float   $deposit        保证金
1863  * @return  array   统计信息
1864  *                  total_order     总订单数</br>
1865  *                  total_goods     总商品数</br>
1866  *                  valid_order     有效订单数</br>
1867  *                  valid_goods     有效商品数</br>
1868  */
1869 function pre_sale_stat($pre_sale_id, $deposit)
1870 {
1871     $pre_sale_id = intval($pre_sale_id);
1872
1873     /* 取得预售活动商品ID */
1874     $sql = "SELECT goods_id " .
1875             "FROM " . $GLOBALS['ecs']->table('goods_activity') .
1876             "WHERE act_id = '$pre_sale_id' " .
1877             "AND act_type = '" . GAT_PRE_SALE . "'";
1878     $pre_sale_goods_id = $GLOBALS['db']->getOne($sql);
1879
1880     /* 取得总订单数和总商品数 */
1881     $sql = "SELECT COUNT(*) AS total_order, SUM(g.goods_number) AS total_goods " .
1882             "FROM " . $GLOBALS['ecs']->table('order_info') . " AS o, " .
1883             $GLOBALS['ecs']->table('order_goods') . " AS g " .
1884             " WHERE o.order_id = g.order_id " .
1885             "AND o.extension_code = '" . PRE_SALE_CODE . "' " .
1886             "AND o.extension_id = '$pre_sale_id' " .
1887             "AND g.goods_id = '$pre_sale_goods_id' " .
1888             "AND (order_status = '" . OS_CONFIRMED . "' OR order_status = '" . OS_UNCONFIRMED . "')";
1889     $stat = $GLOBALS['db']->getRow($sql);
1890     if ($stat['total_order'] == 0)
1891     {
1892         $stat['total_goods'] = 0;
1893     }
1894
1895     /* 取得有效订单数和有效商品数 */
1896     $deposit = floatval($deposit);
1897     if ($deposit > 0 && $stat['total_order'] > 0)
1898     {
1899         $sql .= " AND (o.money_paid + o.surplus) >= '$deposit'";
1900         $row = $GLOBALS['db']->getRow($sql);
1901         $stat['valid_order'] = $row['total_order'];
1902         if ($stat['valid_order'] == 0)
1903         {
1904             $stat['valid_goods'] = 0;
1905         }
1906         else
1907         {
1908             $stat['valid_goods'] = $row['total_goods'];
1909         }
1910     }
1911     else
1912     {
1913         $stat['valid_order'] = $stat['total_order'];
1914         $stat['valid_goods'] = $stat['total_goods'];
1915     }
1916
1917     return $stat;
1918 }
1919
1920 /**
1921  * 获得预售的状态
1922  *
1923  * @access  public
1924  * @param   array
1925  * @return  integer
1926  */
1927 function pre_sale_status($pre_sale)
1928 {
1929     $now = gmtime();
1930     if ($pre_sale['is_finished'] == 0)
1931     {
1932         /* 未处理 */
1933         if ($now < $pre_sale['start_time'])
1934         {
1935             $status = PSS_PRE_START;
1936         }
1937         elseif ($now > $pre_sale['end_time'])
1938         {
1939             $status = PSS_FINISHED;
1940         }
1941         else
1942         {
1943             if ($pre_sale['restrict_amount'] == 0 || $pre_sale['valid_goods'] < $pre_sale['restrict_amount'])
1944             {
1945                 $status = PSS_UNDER_WAY;
1946             }
1947             else
1948             {
1949                 $status = PSS_FINISHED;
1950             }
1951         }
1952     }
1953     elseif ($pre_sale['is_finished'] == PSS_SUCCEED)
1954     {
1955         /* 已处理,团购成功 */
1956         $status = PSS_SUCCEED;
1957     }
1958     elseif ($pre_sale['is_finished'] == PSS_FAIL)
1959     {
1960         /* 已处理,团购失败 */
1961         $status = PSS_FAIL;
1962     }
1963
1964     return $status;
1965 }
1966
1967 /**
1968  * 获取指定商品的评论数量
1969  *
1970  * @param int $goods_id
1971  *            商品编号
1972  * @return number
1973  */
1974 function goods_comment_count ($goods_id)
1975 {
1976     $sql = "select count(*) from " . $GLOBALS['ecs']->table('comment') . " where id_value = '$goods_id' and status = 0 and parent_id = 0";
1977     $count = $GLOBALS['db']->getOne($sql);
1978     return intval($count);
1979 }
1980
1981 /**
1982  * 获取指定商品的的累计销量
1983  *
1984  * @param int $goods_id
1985  *            商品编号
1986  * @return number
1987  */
1988 function goods_sale_count($goods_id)
1989 {
1990     /* 查询该商品销量 */
1991     $sql = 'SELECT IFNULL(SUM(g.goods_number), 0) ' .
1992             'FROM ' . $GLOBALS['ecs']->table('order_info') . ' AS o, ' .
1993             $GLOBALS['ecs']->table('order_goods') . ' AS g ' .
1994             "WHERE o.order_id = g.order_id " .
1995             "AND o.order_status = " . OS_CONFIRMED .
1996             " AND o.pay_status = " . PS_PAYED .
1997             " AND g.goods_id = '$goods_id'";
1998     $sales_count = $GLOBALS['db']->getOne($sql);
1999
2000     return $sales_count;
2001
2002 }
2003
2004 ?>