<?php
namespace Api\Model;

use Think\Model;

header("Content-type:text/html;charset=utf-8");
class GoodsModel extends Model
{
    /**
     * 获取 某一级分类的商品
     * @param  string $pcate 一级分类id
     * @return array
     */
    public function getFromLevel1($pcate)
    {
        $list = $this
        ->where("`pcate`={$pcate} AND `is_sale`=1")
        ->field("`id`,`goods_image`,`goods_name`,`des`,`goods_price`,`buy_num`,`cate_id`")
        ->select();
        foreach ($list as $key => $value) {
            // 0.3.4起
            $list[$key]['new_goods_image'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['goods_image'];
            $list[$key]['goods_image'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['goods_image'];
        }
        return $list;
    }

    /**
     * 获取首页推荐商品
     * @return array
     */
    public function getGoodsOnIndex()
    {
        $data = $this
            ->where("`is_recommend` > 0 AND `is_sale` > 0")
            ->order("`sort` ASC")
            ->field("`id`,`goods_name`,`goods_price`,`buy_num`,`goods_image`,`des`,`thumb`")
            ->select();
        foreach ($data as $k => $v) {
            $data[$k]['goods_image'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $v['goods_image'];
            $data[$k]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $v['thumb'];
        }
        return $data;
    }

    /**
     * 热销商品
     * @return array/null
     */
    public function hotGoodList()
    {
        $data = $this
        ->where("`is_choiceness` = 1 AND `is_sale` > 0")
        ->order("`sort` ASC,`id` DESC")
        ->field("`id`,`goods_name`,`goods_price`,`thumb`,`des`")
        ->select();
        foreach ($data as $k => $v) {
            $data[$k]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $v['thumb'];
        }
        return $data;
    }

    /**
     * 搜索商品
     * @return array/null
     */
    public function serchGoods()
    {
        $keyword = I("keyword");
        $data = $this
        ->where("`goods_name` LIKE '%{$keyword}%'")
        ->order("`sort` ASC,`id` DESC")
        ->field("`id`,`goods_name`,`goods_price`,`thumb`,`des`")
        // ->limit(8)
        ->select();
        foreach ($data as $key => $value) {
            $data[$key]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['thumb'];
        }
        return $data;
    }

    /**
     * 获取商品详情
     * @param  int $id 商品ID
     * @return array
     */
    public function getGoodsInfo($id)
    {
        $data = $this
        ->where("`id` = $id")
        ->field("`id`,`goods_name`,`goods_price`,`buy_num`,`goods_image`,`carousel`,`color`,`size`,`intro`,`parameter`,`des`,`options`,`thumb`")
        ->find();
        $data['goods_image'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $data['goods_image'];
        $data['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $data['thumb'];
        if ($data['intro']) $data['intro'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $data['intro'];
        $data['carousel'] = unserialize($data['carousel']);
        if ($data['carousel']) {
            $carouselArray = array();
            foreach ($data['carousel'] as $key => $value) {
                if ($value) {
                    $carouselArray[] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value;
                }
            }
            if ($carouselArray[0]) {
                $data['carousel'] = $carouselArray;
            }
        }
        if ($data['color']) $data['color'] = unserialize($data['color']);
        if ($data['size']) $data['size'] = unserialize($data['size']);
        if ($data['parameter']) $data['parameter'] = unserialize($data['parameter']);
        if ($data['options']) $data['options'] = unserialize($data['options']);
        return $data;
    }

    /**
     * 获取商品列表
     * @param  string $ids 商品ID串
     * @return array
     */
    public function getByIDs($ids)
    {
        return $list = $this->where("`id` IN($ids)")->select();
    }

    /**
     * 增加销量
     * @param  int $goodid 商品ID
     * @param  int $num 销售数量
     * @return mix
     */
    public function saleSuccess($goodid, $num)
    {
        if ($goodid && $num) {
            return $this->where("`id` = $goodid")->lock()->setInc("buy_num", $num);
        } else {
            return false;
        }
    }
}