<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 打招呼
 */
class Greet extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //打招呼/搭讪
    public function greet() {
        /*if ($this->auth->gender == 0 && $this->auth->real_status != 1 && $this->auth->idcard_status != 1) {
            $this->error('请先完成实名认证或真人认证');
        }*/

        $other_uid = input('user_id', 0, 'intval');
        if(!$other_uid) {
            $this->error(__('Invalid parameters'));
        }
        if($other_uid == $this->auth->id){
            $this->error('这是您自己');
        }
        $checkuser = Db::name('user')->find($other_uid);
        if(empty($checkuser)) {
            $this->error('此用户不存在');
        }
        $map = [
            'user_id' => $this->auth->id,
            'user_to_id' => $other_uid,
        ];
        // 取消限制 2023年12月14日 18点47分
        // $check = Db::name('user_greet')->where($map)->find();
        // if($check){
        //     $this->error('已经打过招呼了');
        // }

        $map['createtime'] = time();

        Db::startTrans();
        $id = Db::name('user_greet')->insertGetId($map);
        if (!$id) {
            Db::rollback();
            $this->error('您的网络开小差了~');
        }

        //tag任务赠送金币
        //搭讪奖励
        $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,24);
        if($task_rs === false){
            Db::rollback();
            $this->error('完成任务赠送奖励失败');
        }
        Db::commit();

        //不需要送礼物了
        /*$gift_greet = Db::name('gift_greet')->find();
        if ($gift_greet) {
            $gift_greet['special'] = one_domain_image($gift_greet['special']);
            $gift_greet['image'] = one_domain_image($gift_greet['image']);
        } else {
            $gift_greet = (object)[];
        }*/
        $gift_greet = (object)[];

        $this->success('操作成功', $gift_greet);
    }

    //查询每天弹出搭讪框次数
    public function freegreetnum() {
        $user = $this->auth->getUser();
        if (isset($user['is_kefu']) && $user['is_kefu'] == 1){
            $data['free_greet_num'] = 0;
            $data['vip_free_greet_num'] = 0;
            $data['boy_free_greet_num'] = 0;
            $data['boy_vip_free_greet_num'] = 0;
        }else{
            //非会员每天弹出搭讪框次数
            $data['free_greet_num'] = config('site.free_greet_num') > 0 ? config('site.free_greet_num') : 5;
            //vip每天弹出搭讪框次数
            $data['vip_free_greet_num'] = config('site.vip_free_greet_num') > 0 ? config('site.vip_free_greet_num') : 5;

            // 男性 非会员每天弹出搭讪框次数
            $data['boy_free_greet_num'] = config('site.boy_free_greet_num') > 0 ? config('site.boy_free_greet_num') : 1;
            // 男性 vip每天弹出搭讪框次数
            $data['boy_vip_free_greet_num'] = config('site.boy_vip_free_greet_num') > 0 ? config('site.boy_vip_free_greet_num') : 1;
        }

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

    /**
     * 男性打招呼内容
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function greetBoy()
    {
        $data = Db::name('user_greet_boy')->select();
        $this->success('success', $data);
    }

}