<?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()
    {
        //print_r($this->data['business_id']);die;
        $cat_list = M('goods_cat')->field('id, title, img')->where(array('admin_id' => $this->data['business_id']))->order("seat_no DESC")->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]);
    }

    /**
     * [add_car 添加购物车]
     * @AuthorHTL
     * @DateTime  2018-03-08T10:55:04+0800
     */
    public function add_car()
    {
        if (empty($this->data['goods_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '商品id为空']);

        if (empty($this->data['user_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '用户id为空']);

        if (empty($this->data['goods_num'])) $this->ajaxReturn(['status' => -1, 'msg' => '商品数量为空']);

        $is_in = M('goods_car')->where(array('user_id' => $this->data['user_id'], 'goods_id' => $this->data['goods_id']))->find();

        if (empty($is_in)) {
            $result = M('goods_car')->add(
                        array(
                            'user_id' => $this->data['user_id'], 
                            'goods_id' => $this->data['goods_id'],
                            'goods_num' => $this->data['goods_num'],
                        )
                    );
        } else {
            $result = M('goods_car')->where(array('goods_id' => $this->data['goods_id'], 'user_id' => $this->data['user_id']))->setInc('goods_num', $this->data['goods_num']);
        }

        if ($result) {
            $this->ajaxReturn(['status' => 1, 'msg' => '添加购物车成功']);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '添加购物车失败']);
        }
    }

    /**
     * [delete_car 通过商品id删除购物车]
     * @AuthorHTL
     * @DateTime  2018-03-08T10:57:21+0800
     * @return    [type]                   [description]
     */
    public function delete_car()
    {
        if (empty($this->data['user_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '用户id为空']);

        $this->data['goods_ids'] = split(',', $this->data['goods_ids']);

        if (empty($this->data['goods_ids'])) $this->ajaxReturn(['status' => -1, 'msg' => '商品ids为空']);

        foreach ($this->data['goods_ids'] as $key => $id) {
            M('goods_car')->where(array('user_id' => $this->data['user_id'], 'goods_id' => $id))->delete();
        }

        $this->ajaxReturn(['status' => 1, 'msg' => '删除购物车成功！']);
    }

    /**
     * [edit_car 通过商品id编辑购物车]
     * @AuthorHTL
     * @DateTime  2018-03-08T10:57:21+0800
     * @return    [type]                   [description]
     */
    public function edit_car()
    {
        if (empty($this->data['user_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '用户id为空']);

        if (empty($this->data['goods_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '商品id为空']);

        if ($this->data['goods_num'] == 0) {
            $result = M('goods_car')->where(array('user_id' => $this->data['user_id'], 'goods_id' => $this->data['goods_id']))->delete();
        } else {
            $result = M('goods_car')->where(array('user_id' => $this->data['user_id'], 'goods_id' => $this->data['goods_id']))->save(array('goods_num' => $this->data['goods_num']));
        }

        if ($result) {
            $this->ajaxReturn(['status' => 1, 'msg' => '编辑购物车成功！']);
        } else {
             $this->ajaxReturn(['status' => -1, 'msg' => '编辑购物车成功！']);
        }
    }

    /**
     * [special_goods_list_api 获取特价商品列表]
     * @AuthorHTL
     * @DateTime  2018-03-16T17:09:04+0800
     * @return    [type]                   [description]
     */
    public function special_goods_list_api()
    {
        if (empty($this->data['page'])) $this->ajaxReturn(['status' => -1, 'msg' => '分页为空']);

        $where = 'admin_id = '.$this->data['business_id'].' AND special_price > -1 AND status = 1';
        $list = M('goods')->where($where)->page($this->data['page'])->field('id, title, price, special_price, img, sales,max_buy,false_sell,content')->select();

        if (empty($list)) $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        
        foreach ($list as $key => &$value) {
            $value['sales'] += $value['false_sell'];
            unset($list[$key]['false_sell']);
            $value['img'] = $this->url.'/Uploads/'.$value['img'];
        }

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

    /**
     * [add_browse_num 添加访问次数]
     * @AuthorHTL
     * @DateTime  2018-03-20T11:05:32+0800
     */
    public function add_browse_num()
    {
        if (empty($this->data['goods_id'])) $this->ajaxReturn(['status' => -1, 'msg' => '商品id为空']);

        $result = M('goods')->where(array('id' => $this->data['goods_id']))->setInc('browse_num', 1);

        if ($result) {
            $this->ajaxReturn(['status' => 1, 'msg' => '添加访问量成功']);
        } else {
            $this->ajaxReturn(['status' => -1, 'msg' => '添加访问量失败']);
        }
    }
}