<?php

namespace app\api\controller;

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

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

    //打招呼/搭讪
    public function greet() {

        $other_uids = input('user_ids', '', 'trim');
        if(!$other_uids) {
            $this->error(__('Invalid parameters'));
        }

        $other_uids = explode(',',$other_uids);
        if(empty($other_uids)){
            $this->error(__('Invalid parameters'));
        }

        $map = [];
        foreach($other_uids as $key => $other_uid){
            $map[] = [
                'user_id' => $this->auth->id,
                'user_to_id' => $other_uid,
                'createtime' => time(),
            ];
        }

        Db::startTrans();
        $id = Db::name('user_greet')->insertAll($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();

        $greet = null;
        if($this->auth->gender == 0){
            $where['user_id'] = $this->auth->id;
            $greet = Db::name('user_greet_content')->where($where)->orderRaw('rand()')->find();
            if(!empty($greet) && $greet['type'] != 0){
                $greet['content'] = one_domain_image($greet['content']);
            }
        }else{
            $greet = Db::name('user_greet_boy')->orderRaw('rand()')->find();
        }

        $rs = [
            'gender' => $this->auth->gender,
            'greet'  => $greet,
        ];
        $this->success('操作成功', $rs);
    }

    //查询每天弹出搭讪框次数
    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);
    }

}