<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\api\controller\goods;

use app\api\controller\Base;
use think\Db;

class Collection extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function collection(){
        $id = $this->param("id/d");
        $goods = Db::name("bbfx_goods")->where(['id'=>$id,'uniacid'=>UNIACID])->find();
        if(empty($goods)){
            __error("商品信息错误");
        }
        $collection = Db::name("bbfx_goods_collection")->where(['uniacid'=>UNIACID,'user_id'=>$this->user['id'],'goods_id'=>$goods['id']])->find();
        if(empty($collection)){
            $data = [
                'uniacid'=>UNIACID,
                'user_id'=>$this->user['id'],
                'goods_id'=>$goods['id'],
                'createtime'=>time()
            ];

            Db::name("bbfx_goods_collection")->insert($data);

            __success("收藏成功");
        }
        else{
            Db::name("bbfx_goods_collection")->where(['id'=>$collection['id']])->delete();

            __success("取消收藏成功");
        }

    }
    
    public function getList(){
        $page = max(1,$this->param("page/d"));
        $limit = 10;
        $where = ['a.uniacid'=>UNIACID,'a.user_id'=>$this->user['id']];
        $list = Db::name("bbfx_goods_collection")->alias("a")->join("bbfx_goods b","a.goods_id=b.id","LEFT")->where($where)->order("a.id desc")->field("a.*,b.title,b.thumb,b.price,(b.sales_volume+b.virtual_sale) as sales_volume")->limit(($page-1)*$limit,$limit)->select();
        foreach($list as $key => &$val){
            $val['thumb'] = cdnurl($val['thumb']);
            $val['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
        }
        unset($val);
        __success("操作成功",['list'=>$list]);
    }
}