<?php


namespace App\Services;


use App\Models\DynamicZanModel;
use App\Models\UserLikeModel;
use App\Models\UserSystemMessageModel;
use Illuminate\Support\Facades\DB;
use PHPUnit\Util\Exception;
use function Symfony\Component\Translation\t;

class NoticeService
{
    /**
     * 获取消息列表
     */
    public function get_notice_list(){

    }

    /**
     * 获取喜欢列表
     */
    public function get_like_list($param){
        $query = UserLikeModel::query();
        if($param['type']==1){
            //我喜欢
//            $query = UserLikeModel::query();
//            $query = $query->with(['like_user'=>function($query){
//                $query->select('id','sex','is_vip','tencent_im_user_id');
//            },'like_user_info'])->where('user_id',$param['user_id']);


            $query = $query->leftJoin('users','users_like.like_id','=','users.id')
                ->leftJoin('users_info','users_like.like_id','=','users_info.user_id')
                ->where('users_like.user_id',$param['user_id'])
                ->where('users.is_distory',0)
                ->select(['users_like.id','users_like.user_id','users_like.like_id','users_like.atime','users.sex','users.is_vip','users.tencent_im_user_id','users_info.nickname','users_info.avatar','users_info.birthday','users_info.work','users_info.height','users_info.weight']);

        }else{
            //喜欢我
//            $query = $query->with(['user'=>function($query){
//                $query->select('id','sex','is_vip','tencent_im_user_id');
//            },'user_info'])->where('like_id',$param['user_id']);
            $query = $query->leftJoin('users','users_like.user_id','=','users.id')
                ->leftJoin('users_info','users_like.user_id','=','users_info.user_id')
                ->where('users_like.like_id',$param['user_id'])
                ->where('users.is_distory',0)
                ->select(['users_like.id','users_like.user_id','users_like.like_id','users_like.atime','users.sex','users.is_vip','users.tencent_im_user_id','users_info.nickname','users_info.avatar','users_info.birthday','users_info.work','users_info.height','users_info.weight']);


            //清除喜欢我未读状态
            UserLikeModel::query()->where(['like_id'=>$param['user_id'],'status'=>0])->update(['status'=>1]);
        }

            $query = $query->orderByDesc('id')
                ->paginate(request('perPage',20));
        $query = $query->toArray();
        foreach ($query['data'] as $k=>$v){
            $query['data'][$k]['distance'] = 0;  //计算距离
            $query['data'][$k]['age'] = !empty($v['birthday'])?birthday($v['birthday']):0;
        }
        return $query;
    }


    /**
     * 获取点赞列表
     */
    public function get_zan_list($param){
        $query = DynamicZanModel::query();
        if($param['type']==1){
            //他赞了我
//            $query = $query->with(['dynamic','user'=>function($query){
//                $query->select('id','sex','is_vip','tencent_im_user_id');
//            },'user_info'=>function($query){
//                $query->select(['user_id','nickname','avatar','height','weight','work','birthday']);
//            }])->whereHas('dynamic',function ($query)use($param){
//                $query->where('user_id',$param['user_id'])->select();
//            });


            $query = $query
                ->leftJoin('dynamic','dynamic_zan.dynamic_id','=','dynamic.id')
                ->leftJoin('users','dynamic_zan.user_id','=','users.id')
                ->leftJoin('users_info','dynamic_zan.user_id','=','users_info.user_id')
                ->where('dynamic.user_id',$param['user_id'])
                ->where('users.is_distory',0)
                ->select(['dynamic_zan.id','dynamic_zan.atime','dynamic_zan.user_id','dynamic.content','dynamic.img_url','users.sex','users.is_vip','users.tencent_im_user_id','users_info.nickname','users_info.avatar','users_info.birthday','users_info.work','users_info.height','users_info.weight']);

            //清除他赞了我未读状态
            DynamicZanModel::query()->with(['dynamic'])->where('status','=',0)->whereHas('dynamic',function ($query)use($param){
                $query->where('user_id',$param['user_id'])->select('id','user_id','content','img_url');
            })->update(['status'=>1]);
        }else{

            //我赞了他
//            $query = $query->with(['dynamic'=>function($query){
//                $query->select('id','user_id','content','img_url');
//            },'dynamic.user'=>function($querty){
//                $querty->select('id','sex','is_vip','tencent_im_user_id');
//            },'dynamic.user_info'=>function($querty){
//                $querty->select(['user_id','nickname','avatar','height','weight','work','birthday']);
//            }])
//                ->where('user_id',$param['user_id']);

            $query = $query
                ->leftJoin('dynamic','dynamic_zan.dynamic_id','=','dynamic.id')
                ->leftJoin('users','dynamic.user_id','=','users.id')
                ->leftJoin('users_info','dynamic.user_id','=','users_info.user_id')
                ->where('dynamic_zan.user_id',$param['user_id'])
                ->where('users.is_distory',0)
                ->select(['dynamic_zan.id','dynamic_zan.atime','dynamic.user_id','dynamic.content','dynamic.img_url','users.sex','users.is_vip','users.tencent_im_user_id','users_info.nickname','users_info.avatar','users_info.birthday','users_info.work','users_info.height','users_info.weight']);
        }
        $query = $query->orderByDesc('id')
            ->paginate(request('perPage',20));

        $query = $query->toArray();
        foreach ($query['data'] as $k=>$v){
            $query['data'][$k]['atime'] = uc_time_ago(strtotime($v['atime']));
            $query['data'][$k]['img_url'] = $v['img_url']=='[]'? null :json_decode($v['img_url'],true);
            $query['data'][$k]['age'] = !empty($v['birthday'])?birthday($v['birthday']):0;
        }
        return $query;
    }

    /**
     * 删除消息
     */
    public function del_message($id){
        if(empty($id)){
            throw new Exception("参数错误");
        }
        $user = auth('api')->user();
        if(!$message = UserSystemMessageModel::query()->where(['id'=>$id,'user_id'=>$user->id])->find($id)){
            throw new Exception("消息不存在");
        }
        $message->is_delete = 1;
        $message->save();
        return true;
    }


}
