<?php 
namespace Api\Controller;

class GoodsOtherController extends BaseController
{
	    /**
     * [search 搜索商品 method POST]
     * @AuthorHTL
     * @DateTime  2018-02-07T11:53:31+0800
     * @return    [type]                   [description]
     */
    public function search()
    {
        if (empty($this->data['keyword'])) {
            apiReturn(['status' => -1, 'msg' => '搜索关键字为空']);
        }
        if (empty($this->data['page'])) {
            apiReturn(['status' => -1, 'msg' => '分页为空']);
        }

        $list = M('goods')
                ->where(array('admin_id' => $this->data['business_id'], 'title' => array('LIKE', '%'.$this->data['keyword'].'%')))
                ->field('id, title, price, img, false_sell, sales, max_buy, content')
                ->page($this->data['page'])
                ->select();

        if (empty($list)) {
            apiReturn(['status' => -1, 'msg' => '没有数据']);
        }

        foreach ($list as $key => &$value) {
            $value['img'] = $this->url.'/Uploads/'.$value['img'];
            $value['sales'] += $value['false_sell'];
            unset($list[$key]['false_sell']);
        }

        apiReturn(['status' => -1, 'msg' => '已获取数据', 'data' => $list]);
    }

    /**
     * [cat_list 获取商品分类]
     * @AuthorHTL
     * @DateTime  2018-02-07T14:06:40+0800
     * @return    [type]                   [description]
     */
    public function cat_list()
    {
        $cat_list = M('goods_cat')->field('id, title, img')->where(array('admin_id' => $this->data['business_id']))->select();

        if (empty($cat_list)) {
            apiReturn(['status' => -1, 'msg' => '该商户下没有商品分类']);
        }

        foreach ($cat_list as $key => &$value) {
            $value['img'] = $this->url.'/Uploads/'.$value['img'];
        }

        apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $cat_list]);
    }

     /**
     * [cat_list 根据商品分类id获取对应商品列表]
     * @AuthorHTL
     * @DateTime  2018-02-07T14:06:40+0800
     * @return    [type]                   [description]
     */
    public function get_cat_goods_list()
    {
        if (empty($this->data['cat_id'])) {
            apiReturn(['status' => -1, 'msg' => '分类id为空']);
        }
        if (empty($this->data['page'])) {
            apiReturn(['status' => -1, 'msg' => '分页为空']);
        }

        $list = M('goods')->where(array('admin_id' => $this->data['business_id'], 'cat_id1' => $this->data['cat_id'], 'status' => 1))->page($this->data['page'])->getField('id, title, price, img, sales,max_buy,false_sell,content');

        if (empty($list)) {
            apiReturn(['status' => -1, 'msg' => '没有数据']);
        }

        foreach ($cat_list as $key => &$value) {
            $value['img'] = $this->url.'/Uploads/'.$value['img'];
        }

        foreach ($list as $key => &$value) {
            $value['img'] = $this->url.'/Uploads/'.$value['img'];
            $value['sales'] += $value['false_sell'];
            unset($list[$key]['false_sell']);
        }

        apiReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
    }
}