<?php
namespace Api\Model;

use Think\Model;

header("Content-type:text/html;charset=utf-8");
class GoodsCategoriesModel extends Model
{
    /**
     * 首页分类&商品 (分类没有图片, 商品没有描述)
     * @return array
     */
    public function getIndexGoods($village_id)
    {
        $Levels = $this->order("`sort` ASC")->field("`id`,`name`")->select();
        foreach ($Levels as $key => $value) {
            $goods = M("goods")
                ->where("`pcate`={$value['id']} AND `is_sale`=2 AND `deleted`=-1")
                ->field("`id`,`goods_name`,`pcate`,`goods_price`,`thumb`,`des`")
                ->order("`sort` ASC")
                ->select();
            foreach ($goods as $kg => $vg) {
                if ($vg['thumb']) {
                    $goods[$kg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $vg['thumb'];
                } else {
                    // 没有保存缩略图
                    $goods[$kg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Public/img/torch.png";
                }
                // 查询村独立设置的价格
                $villagePrice =  M("goodsVillageSet")->where("`good_id`={$vg['id']} AND `village_id`={$village_id}")->getField("price");
                if ($villagePrice > 0) {
                    $goods[$kg]['goods_price'] = $villagePrice;
                }
            }
            $Levels[$key]['goods'] = $goods;
        }
        return $Levels;
    }

    /**
     * 分类列表
     */
    public function getCategories()
    {
        $Levels = $this->order("`sort` ASC")->field("`id`,`name`,`img`")->select();
        foreach ($Levels as $key => $value) {
            $Levels[$key]['img'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['img'];
        }
        return $Levels;
    }

    /**
     * 分类页
     * @return array
     */
    public function getCatogoryGoods($village_id)
    {
        $Levels = $this->order("`sort` ASC")->field("`id`,`name`,`img`")->select();
        foreach ($Levels as $key => $value) {
            $Levels[$key]['img'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['img'];
            $goods = M("goods")
                ->where("`pcate`={$value['id']} AND `is_sale`=2 AND `deleted`=-1")
                ->field("`id`,`goods_name`,`pcate` AS `cate_id`,`goods_price`,`thumb`,`des`")
                ->order("`sort` ASC")
                ->select();
            foreach ($goods as $kg => $vg) {
                if ($vg['thumb']) {
                    $goods[$kg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $vg['thumb'];
                } else {
                    // 没有保存缩略图
                    $goods[$kg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Public/img/torch.png";
                }
                // 查询村独立设置的价格
                $villagePrice =  M("goodsVillageSet")->where("`good_id`={$vg['id']} AND `village_id`={$village_id}")->getField("price");
                if ($villagePrice > 0) {
                    $goods[$kg]['goods_price'] = $villagePrice;
                }
            }
            $Levels[$key]['goods'] = $goods;
        }
        return $Levels;
    }
}