<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 漂流瓶
 */
class Piaoliuping extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //捡一个
    public function getone(){

        //判断vip身份，限制每日捡的个数
        $vip_endtime = model('wallet')->getWallet($this->auth->id,'vip_endtime');
        if($vip_endtime < time()){

            $plp_unvip_daysnum = intval(config('site.plp_unvip_daysnum'));
            if($plp_unvip_daysnum > -1){

                $starttime = strtotime(date('Y-m-d'));
                $endtime = $starttime + 86399;

                $where = [
                    'user_id' => $this->auth->id,
                    'createtime' => ['BETWEEN',[$starttime,$endtime]],
                ];
                $count = Db::name('piaoliuping_log')->where($where)->count('id');
                if($count >= $plp_unvip_daysnum){
                    $this->error('非vip每天只能捡'.$plp_unvip_daysnum.'个');
                }
            }

        }

        //不能是自己发的
        $map = [
            'plp.user_id' => ['NEQ',$this->auth->id],
        ];

        //不能重复
        $log_list = Db::name('piaoliuping_log')->where('user_id',$this->auth->id)->column('plp_id');
        if(!empty($log_list)){
            $map['plp.id'] = ['NOTIN',$log_list];
        }

        $rs = Db::name('piaoliuping')->alias('plp')
            ->field('plp.*,user.nickname,user.avatar')
            ->join('user','plp.user_id = user.id','LEFT')
            ->where($map)->orderRaw('rand()')->find();

        //记录查看记录
        if(!empty($rs)){
            $log_data = [
                'user_id' => $this->auth->id,
                'plp_id'  => $rs['id'],
                'createtime' => time(),
            ];
            Db::name('piaoliuping_log')->insertGetId($log_data);
        }

        $this->success('success',$rs);
    }

    //扔一个
    public function addone(){
        $content = input('content','','trim,strip_tags,htmlspecialchars');
//        $cityname = input('cityname','','trim');
        if(empty($content)){
            $this->error('写点什么吧...');
        }

        $data = [
            'user_id' => $this->auth->id,
            'content' => $content,
            'createtime' => time(),
            'cityname' => $this->auth->cityname,
        ];

        Db::name('piaoliuping')->insertGetId($data);
        $this->success('成功');
    }
}
