<?php

namespace app\api\controller;

use think\facade\Lang;
use think\facade\Db;

/**
 * ============================================================================
 * 
 * ============================================================================
 * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司，并保留所有权利。
 * 网站地址: https://www.valimart.net/
 * ----------------------------------------------------------------------------
 * 
 * ============================================================================
 * 收藏商品控制器
 */
class Memberfavorites extends MobileMember {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberfavorites.lang.php');
    }

    /**
     * @api {POST} api/memberfavorites/favorites_list 商品收藏列表
     * @apiVersion 1.0.0
     * @apiGroup MemberFavorites
     * 
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {String} per_page 每页显示数量
     * @apiParam {String} page 当前页数
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.favorites_list  收藏列表
     * @apiSuccess {Int} result.favorites_list.fav_id  收藏ID
     * @apiSuccess {Int} result.favorites_list.goods_id  商品ID
     * @apiSuccess {String} result.favorites_list.goods_image  商品图片名称
     * @apiSuccess {String} result.favorites_list.goods_image_url  商品图片完整路径
     * @apiSuccess {String} result.favorites_list.goods_name  商品名称
     * @apiSuccess {Float} result.favorites_list.goods_price  商品价格
     * @apiSuccess {Int} result.favorites_list.store_id  店铺ID
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function favorites_list() {
        $favorites_model = model('favorites');
        $favorites_list = $favorites_model->getGoodsFavoritesList(array(array('member_id' ,'=', $this->member_info['member_id'])), '*', $this->pagesize);
        $favorites_id = '';
        foreach ($favorites_list as $value) {
            $favorites_id .= $value['fav_id'] . ',';
        }
        $favorites_id = rtrim($favorites_id, ',');

        $goods_model = model('goods');
        $field = 'goods_id,goods_name,goods_price,goods_image,store_id';
        // 默认不显示预订商品
        $goods_list = $goods_model->getGoodsList(array(array('goods_id', 'in', $favorites_id)), $field);
        foreach ($goods_list as $key => $value) {
            $goods_list[$key]['fav_id'] = $value['goods_id'];
            $goods_list[$key]['goods_image_url'] = goods_cthumb($value['goods_image'], 240, $value['store_id']);
        }
        $result = array_merge(array('favorites_list' => $goods_list), mobile_page($favorites_model->page_info));
        ds_json_encode(10000, '', $result);
    }

    /**
     * @api {POST} api/memberfavorites/address_list 添加商品收藏
     * @apiVersion 1.0.0
     * @apiGroup MemberFavorites
     * 
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {String} goods_id 商品ID
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function favorites_add() {
        $goods_id = intval(input('post.goods_id'));
        if ($goods_id <= 0) {
            ds_json_encode(10001, lang('param_error'));
        }

        $favorites_model = model('favorites');

        //判断是否已经收藏
        $favorites_info = $favorites_model->getOneFavorites(array(
            'fav_id' => $goods_id, 'fav_type' => 'goods',
            'member_id' => $this->member_info['member_id']
        ));
        if (!empty($favorites_info)) {
            ds_json_encode(10001, lang('favorite_already_favorite_goods'));
        }

        //判断商品是否为当前会员所有
        $goods_model = model('goods');
        $goods_info = $goods_model->getGoodsInfoByID($goods_id);
        $seller_info = model('seller')->getSellerInfo(array('member_id' => $this->member_info['member_id']));
        if ($goods_info['store_id'] == $seller_info['store_id']) {
            ds_json_encode(10001, lang('favorite_no_my_product'));
        }

        //添加收藏
        $insert_arr = array();
        $insert_arr['member_id'] = $this->member_info['member_id'];
        $insert_arr['member_name'] = $this->member_info['member_name'];
        $insert_arr['fav_id'] = $goods_id;
        $insert_arr['fav_type'] = 'goods';
        $insert_arr['fav_time'] = TIMESTAMP;
        $result = $favorites_model->addFavorites($insert_arr);

        if ($result) {
            //增加收藏数量
            $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect+1')), $goods_id);
            ds_json_encode(10000, '', 1);
        } else {
            ds_json_encode(10001, lang('ds_common_op_fail'));
        }
    }

    /**
     * @api {POST} api/memberfavorites/favorites_del 删除商品收藏
     * @apiVersion 1.0.0
     * @apiGroup MemberFavorites
     * 
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {String} fav_id 主键ID
     * 
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function favorites_del() {
        $fav_id = intval(input('post.fav_id'));
        if ($fav_id <= 0) {
            ds_json_encode(10001, lang('param_error'));
        }

        $favorites_model = model('favorites');
        $goods_model = model('goods');

        $condition = array();
        $condition[] = array('fav_type','=','goods');
        $condition[] = array('fav_id','=',$fav_id);
        $condition[] = array('member_id','=',$this->member_info['member_id']);

        //判断收藏是否存在
        $favorites_info = $favorites_model->getOneFavorites($condition);
        if (empty($favorites_info)) {
            ds_json_encode(10001, lang('favorite_goods_not_exist'));
        }

        $favorites_model->delFavorites($condition);

        $goods_model->editGoodsById(array('goods_collect' => Db::raw('goods_collect-1')), $fav_id);
        ds_json_encode(10000, '', 1);
    }

}
