<?php

namespace app\api\controller;

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

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = [];

    //常用语,系统的
    public function get_greet_sys(){

        $where = [];
        if($this->auth->gender == 0){
            $where['gender'] = ['IN',[0,2]];
        }else{
            $where['gender'] = ['IN',[1,2]];
        }

        $list = Db::name('greet_sys')->field('id,title')->where($where)->order('weigh desc')->select();
        $this->success(1,$list);
    }

    //我的打招呼列表,自己看所有的
    public function get_greet(){
        $type = input('type',1);

        $list = Db::name('user_greet')->where('user_id',$this->auth->id)->where('type',$type)->select();
        $list = list_domain_image($list,['image']);

        $this->success(1,$list);
    }

    //我的打招呼列表,对外的已过审的
    public function get_greet_checked(){
        $type = input('type',1);

        $list = Db::name('user_greet')->where('user_id',$this->auth->id)->where('type',$type)->where('status',1)->select();
        $list = list_domain_image($list,['image']);

        $this->success(1,$list);
    }

    //添加打招呼
    public function add_greet(){

        if($this->auth->gender == 1 && $this->auth->idcard_status != 1){
            $this->error('请先完成实名认证');
        }
        if($this->auth->gender == 0 && $this->auth->real_status != 1){
            $this->error('请先完成真人认证');
        }

        $type = input('type',1);
        $title = input('title','');
        $image = input('image','');

        if($type == 1){
            $image = '';
        }else{
            $title = '';
        }

        $data = [
            'user_id' => $this->auth->id,
            'type' => $type,
            'title' => $title,
            'image' => $image,
            'status' => 0,
        ];

        Db::name('user_greet')->insertGetId($data);

        $this->success('添加成功,等待审核');
    }

    //删除招呼
    public function del_greet(){
        $id = input('id',0);

        Db::name('user_greet')->where('id',$id)->where('user_id',$this->auth->id)->delete();

        $this->success();
    }

    //一键搭讪
    public function once_greet(){

        if($this->auth->gender == 1){

            $today = strtotime(date('Y-m-d'));
            $end = $today + 86399;
            $apiLimitTime = $end - time();

            $times = config('site.man_dashan_times') ?: 50;
            $rs = $this->apiLimit($times,$apiLimitTime*1000);
            if(!$rs){
                $this->error('今日搭讪次数已用完');
            }


            $where['gender'] = ['IN',[1,2]];
            $chat = Db::name('greet_sys')->field('id,title')->where($where)->orderRaw('rand()')->find();
            $result = [
                'chat' => !empty($chat) ? $chat['title'] : '',
                'image' => '',
            ];

        }else{
            if($this->auth->real_status != 1){
                $this->error('请先完成真人认证');
            }

            $type1 = Db::name('user_greet')->where('user_id',$this->auth->id)->where('status',1)->where('type',1)->orderRaw('rand()')->find();

            $type2 = Db::name('user_greet')->where('user_id',$this->auth->id)->where('status',1)->where('type',2)->orderRaw('rand()')->find();
            $type2 = info_domain_image($type2,['image']);

            $result = [
                'chat' => !empty($type1) ? $type1['title'] : '',
                'image' => !empty($type2) ? $type2['image'] : '',
            ];

        }


        $this->success(1,$result);
    }


}