<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 黑名单
 */
class Userblack extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //拉黑我的人列表
    public function black_me_list(){
        $list = Db::name('user_black')
            ->alias('black')
            ->join('user','black.uid = user.id','LEFT')
            ->field('user.id,user.nickname,user.avatar,user.bio,user.birthday,user.gender')
            ->where('black.black_uid',$this->auth->id)->order('black.id desc')->autopage()->select();

        $list = list_domain_image($list,['avatar']);
        $list = list_birthday_age($list);

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

    //我拉黑的人列表
    public function my_black_list(){
        $list = Db::name('user_black')
            ->alias('black')
            ->join('user','black.black_uid = user.id','LEFT')
            ->field('user.id,user.nickname,user.avatar,user.bio,user.birthday,user.gender')
            ->where('black.uid',$this->auth->id)->order('black.id desc')->autopage()->select();

        $list = list_domain_image($list,['avatar']);
        $list = list_birthday_age($list);

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

    //拉黑某人
    public function black_one(){
        $black_uid = input('black_uid',0);
        if(!$black_uid){
            $this->error(__('Invalid parameters'));
        }

        $checkuser = Db::name('user')->find($black_uid);
        if(empty($checkuser)){
            $this->error('此用户不存在');
        }

        if($black_uid == $this->auth->id){
            $this->error('不能拉黑自己');
        }

        $map = [
            'uid' => $this->auth->id,
            'black_uid' => $black_uid,
        ];

        $check = Db::name('user_black')->where($map)->find();
        if($check){
            $this->error('已经拉黑了此人');
        }

        $id = Db::name('user_black')->insertGetId($map);

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

    //取消拉黑某人
    public function un_black_one(){
        $black_uid = input('black_uid',0);
        if(!$black_uid){
            $this->error(__('Invalid parameters'));
        }

        $map = [
            'uid' => $this->auth->id,
            'black_uid' => $black_uid,
        ];

        //不检查，全删
        $rs = Db::name('user_black')->where($map)->delete();
        $this->success('操作成功');
    }
}
