<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 关系
 */
class Relation extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //关系列表
    public function lists(){
        $uid = input('uid',$this->auth->id);

        //所有
        $list = Db::name('relation')->where('is_show',1)->order('weigh desc')->select();

        //已有关系
        $user_relation = Db::name('user_relation')->alias('ur')
            ->field('ur.*,u.avatar,u.nickname,to.avatar as to_avatar,to.nickname as to_nickname')
            ->join('user u','ur.uid = u.id','LEFT')
            ->join('user to','ur.to_uid = to.id','LEFT')
            ->where('ur.uid|ur.to_uid',$uid)->where('ur.status',1)->select();
        $user_relation = list_domain_image($user_relation,['avatar','to_avatar']);
        $new_user_relation = array_column($user_relation,null,'relation_id');

        //处理,留对方的头像和昵称
        foreach($list as $key => &$val){
            $val['info'] = [];
            if(isset($new_user_relation[$val['id']])){
                $info = $new_user_relation[$val['id']];
                if($info['uid'] == $uid){
                    $info['avatar'] = $info['to_avatar'];
                    $info['nickname'] = $info['to_nickname'];
                    unset($info['to_avatar']);
                    unset($info['to_nickname']);
                }else{
                    unset($info['to_avatar']);
                    unset($info['to_nickname']);
                }
                $val['info'] = $info;
            }
        }

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

    //新增
    public function addone(){
        $to_uid = input('to_uid',0);
        if($to_uid == $this->auth->id){
            $this->error('不能跟自己建立关系');
        }

        //关系检测
        $relation_id    = input('relation_id','');
        $relation = Db::name('relation')->where('id',$relation_id)->find();
        if(empty($relation)){
            $this->error('不存在的关系');
        }

        //检查已有关系
        $where = '(uid = '.$this->auth->id.' and to_uid = '.$to_uid.') or (uid = '.$to_uid.' and to_uid = '.$this->auth->id.')';
        $check = Db::name('user_relation')->where($where)->find();

        if($check){
            $now_relation = Db::name('relation')->where('id',$check['relation_id'])->value('name');

            if($check['status'] == 1){
                if($check['uid'] = $this->auth->id){
                    $this->error('您已经与该用户建立'.$now_relation.'关系');
                }else{
                    $this->error('该用户已经与您建立'.$now_relation.'关系');
                }
            }elseif($check['status'] == 0){
                if($check['uid'] = $this->auth->id){
                    $this->error('您已经申请建立'.$now_relation.'关系,待对方同意');
                }else{
                    $this->error('对方已经申请建立'.$now_relation.'关系,待您同意');
                }
            }elseif($check['status'] == 2){
                $this->error($now_relation.'关系的申请已被拒绝,七日内不能申请');
            }else{
                //可以申请
            }
        }else{
            //可以申请
        }

        //扣钱或道具

        //新增
        $data = [
            'uid' => $this->auth->id,
            'relation_id' => $relation_id,
            'to_uid' => $to_uid,
            'status' => 0,
            'createtime' => time(),
            'updatetime' => time(),
        ];

        $id = Db::name('user_relation')->insertGetId($data);

        //给对方一条消息
        $message = [
            'user_id' => $to_uid,
            'title'   => '关系审核',
            'content' => '有人想和你成为'.$relation['name'],
            'createtime' => time(),
            'status' => 0,
            'infotype' => 'newrelation',//建立关系
            'infotype_id' => 0,
        ];
        Db::name('message')->insertGetId($message);


        $this->success('申请成功,等待对方同意',$id);
    }

    //待审核关系
    public function audit_lists(){
        $list = Db::name('user_relation')->alias('ur')
            ->field('ur.*,relation.name as relation_name,user.nickname,user.avatar,user.gender,user.birthday,user.attribute,uw.vip_endtime')
            ->join('relation','ur.relation_id = relation.id','LEFT')
            ->join('user','ur.uid = user.id','LEFT')
            ->join('user_wallet uw','ur.uid = uw.user_id','LEFT')
            ->where('ur.to_uid',$this->auth->id)
            ->where('ur.status',0)
            ->order('ur.id desc')->autopage()->select();
        $list = list_domain_image($list,['avatar']);

        if(!empty($list)){
            foreach($list as $key => &$val){

                //用户年龄
                $val['age'] = birthtime_to_age($val['birthday']);
                unset($val['birthday']);

                //用户vip
                $val['is_vip'] = $val['vip_endtime'] > time() ? 1 : 0;
                unset($val['vip_endtime']);

            }
        }

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

    //审核
    public function audit(){
        $id = input('id',0);
        $status = input('status',1);

        //查询
        $info = Db::name('user_relation')->alias('ur')
            ->where('ur.id',$id)
            ->where('ur.to_uid',$this->auth->id)
            ->where('ur.status',0)
            ->find();
        if(!$info){
            $this->error('请刷新重试');
        }

        //更新
        $data = [
            'status' => $status,
            'updatetime' => time(),
        ];

        Db::name('user_relation')->where('id',$id)->update($data);

        if($status == 1){
            $remark = '您已同意建立关系';
            $remark2 = '已同意';
        }else{
            $remark = '已拒绝';
            $remark2 = '已拒绝';
        }

        //给发起方一条消息
        $relation_name = Db::name('relation')->where('id',$info['relation_id'])->value('name');
        $message = [
            'user_id' => $info['uid'],
            'title'   => '关系申请结果',
            'content' => '和'.$this->auth->nickname.'成为'.$relation_name.':'.$remark2,
            'createtime' => time(),
            'status' => 0,
            'infotype' => '',//建立关系
            'infotype_id' => 0,
        ];
        Db::name('message')->insertGetId($message);

        //
        $this->success($remark);
    }
}