<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 喜欢
 */
class Userlike extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    //喜欢我的人列表
    public function like_me_list(){
        $list = Db::name('user_like')
            ->alias('like')
            ->join('user','like.uid = user.id','LEFT')
            ->field('user.id,user.nickname,user.avatar,user.bio,user.birthday,user.gender')
            ->where('like.like_uid',$this->auth->id)->order('like.id desc')->autopage()->select();

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

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

    //解锁喜欢我的人
    public function unlock_like_me(){
        $gold = intval(config('site.unlock_like_me'));
        if($gold > 0){

            Db::startTrans();
            $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$gold,62,'');
            if($rs['status'] === false){
                Db::rollback();
                $this->error($rs['msg']);
            }

            //tag任务赠送金币
            //消费解锁喜欢我的人
            $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,17);
            if($task_rs === false){
                Db::rollback();
                $this->error('完成任务赠送奖励失败');
            }

            Db::commit();
            $this->success('操作成功');
        }
        $this->success('操作成功');
    }

    //我喜欢的人列表
    public function my_like_list(){
        $list = Db::name('user_like')
            ->alias('like')
            ->join('user','like.like_uid = user.id','LEFT')
            ->field('user.id,user.nickname,user.avatar,user.bio,user.birthday,user.gender')
            ->where('like.uid',$this->auth->id)->order('like.id desc')->autopage()->select();

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

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

    //关注某人
    public function like_one(){
        $like_uid = input('like_uid',0);
        if(!$like_uid){
            $this->error(__('Invalid parameters'));
        }

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

        if($like_uid == $this->auth->id){
            $this->error('不能喜欢自己');
        }

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

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

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

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

    //取关某人
    public function un_like_one(){
        $like_uid = input('like_uid',0);
        if(!$like_uid){
            $this->error(__('Invalid parameters'));
        }

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

        //不检查，全删

        $rs = Db::name('user_like')->where($map)->delete();
        $this->success('操作成功');
    }
}
