<?php


namespace app\lumaapi\service;


use app\lumaapi\model\MemberModel;
use think\Db;
use think\Validate;

class EventService
{

    // 添加用户收藏
    // uid 用户ID
    // aid 收藏ID
    // type 收藏类型ID  在_mychannel表查看
    public static function favoriteAdd($aid,$uid){
        //结束验证
        // 检测是否已收藏
        $shou=Db::name('member_favorite')->where(['uid'=>$uid,'aid'=>$aid,'type'=>0])->value('id');
        if($shou) return ['status'=>-1,'msg'=>'您已经收藏过了，在会员中心查看我的收藏'];
        // 添加用户收藏
        $where['uid']=$uid;
        $where['aid']=$aid;
        $where['time']=time();
        $db=Db::name('member_favorite')->insert($where);
        if($db){
            _records($uid,'收藏ID：'.$aid);
            return ['status'=>1,'msg'=>'已收藏'];
        }else{
            return ['status'=>-1,'msg'=>'收藏失败'];
        }
        return ['status'=>-1,'msg'=>'收藏失败'];
    }
    // 删除用户收藏
    // id 收藏ID
    public static function favoriteDel($aid,$uid){
        //结束验证
        $db=Db::name('member_favorite')->where(['uid'=>$uid,'aid'=>$aid,'type'=>0])->delete();
        if($db){
            _records($uid,'删除收藏ID：'.$aid);
            return ['status'=>1,'msg'=>'取消收藏'];
        }else{
            return ['status'=>-1,'msg'=>'取消失败'];
        }
        return ['status'=>-1,'msg'=>'取消失败'];
    }

    //阅读文章
    public static function readfavorite($uid,$id){
        // 记录阅读历史
        $lishi=Db::name('member_favorite')->where(['aid'=>$id,'uid'=>$uid,'type'=>1])->value('id');
        if(!$lishi){
            db::name('member_favorite')->insert(['aid'=>$id,'uid'=>$uid,'time'=>time(),'type'=>1]);
        }
    }

    // 关注会员
    public static function guanzhu($uid,$gz_uid){
        // 检测是否已关注
        $shou=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$gz_uid])->value('id');
        if($shou)  return ['status'=>-1,'msg'=>'您已经关注过了，在我的查看我的关注'];
        // 添加用户收藏
        $where['uid']=$uid;
        $where['gz_uid']=$gz_uid;
        $where['time']=time();
        $db=Db::name('member_guanzhu')->insert($where);
        if($db){
            _records($uid,'关注会员ID：'.$gz_uid);
            if($gz_uid!=$uid){
                $user=getUser($uid);
                $gz=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$gz_uid])->cache(_cache('db'))->find();
                MsgService::weixinUserMoney($gz_uid, 0, 50, '您已被'.$user['nickname']."关注成功",$uid,$gz);//用户关注
            }
            return ['status'=>1,'msg'=>'关注成功'];
        }else{
            return ['status'=>-1,'msg'=>'关注失败'];
        }
        return ['status'=>-1,'msg'=>'关注失败'];
    }
    // 取消关注会员
    public static function guanzhuDel($uid,$gz_uid){
        $gz=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$gz_uid])->cache(_cache('db'))->find();
        // 检测是否存在
        $db=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$gz_uid])->delete();
        if($db){
            _records($uid,'取消关注会员ID：'.$gz_uid);
            if($gz_uid!=$uid){
            $user=getUser($uid);
            MsgService::weixinUserMoney($gz_uid, 0, 51, '您已被'.$user['nickname']."解除关注",$uid,$gz);//用户关注
            }
            return ['status'=>1,'msg'=>'取消成功'];
        }else{
            return ['status'=>-1,'msg'=>'取消失败'];
        }
        return ['status'=>-1,'msg'=>'取消失败'];
    }

    public static function guanzhuList($gz_uid,$uid,$type,$order="time desc",$pages){//1他的关注（他人），2关注他的（他人），3我的关注（自己），4关注我的（自己）
        if($type==1){
            $data=Db::name('member_guanzhu')->where(['uid'=>$gz_uid])->order($order)->fetchSql(false)->page($pages)->select();
        }else if($type==2){
            $data=Db::name('member_guanzhu')->where(['gz_uid'=>$gz_uid])->order($order)->fetchSql(false)->page($pages)->select();
        }else if($type==3){
            $data=Db::name('member_guanzhu')->where(['uid'=>$uid])->order($order)->fetchSql(false)->page($pages)->select();
        }else if($type==4){
            $data=Db::name('member_guanzhu')->where(['gz_uid'=>$uid])->order($order)->fetchSql(false)->page($pages)->select();
        }

        foreach ($data as $key=>$val){
            $status=0;
            if($type==1){
                $users = _user($val['gz_uid']);
                $status=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$val['gz_uid']])->count();
            }else if($type==2){
                $users = _user($val['uid']);
                $status=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$val['uid']])->count();
            }else if($type==3){
                $users = _user($val['gz_uid']);
                $status=Db::name('member_guanzhu')->where(['uid'=>$val['uid'],'gz_uid'=>$val['gz_uid']])->count();
            }else if($type==4){
                $users = _user($val['uid']);
                $status=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$val['uid']])->count();
            }else{
                $users = _user($val['uid']);
            }

            $guguan=Db::name('member_guanzhu')->where("uid = ".$val['uid'] ." and gz_uid = ".$val['gz_uid'] .") or ( uid = ".$val['gz_uid'] ." and gz_uid = ".$val['uid'] )->count();
            $data[$key]['img']=$users['img']."?imageView2/1/w/100/h/100";
            $data[$key]['username'] = $users['username'];
            $status=$status>0?1:2;
            $data[$key]['is_guanzhu'] =$guguan>1?0:$status;
        }
        return $data;
    }

    // 文章赞和踩
    public static function zancai($uid,$aid,$type=1){
        //结束验证
        $zan=Db::name('article_zan')->where(['aid'=>$aid,'uid'=>$uid])->cache(_cache('db'))->find();
        if($zan){
            if($zan['zan']){
                $text='您已经赞过啦！';
            }else{
                $text='您已经踩过啦！';
            }
            return ['status'=>-1,'msg'=>$text];
        }else{
            if($type==1){
                $where['zan']=1;
                $ziduan='zan';
            }else if($type==2){
                $where['cai']=1;
                $ziduan='cai';
            }
            $where['uid']=$uid;
            $where['aid']=$aid;
            $where['time']=time();
            $db=Db::name('article_zan')->insert($where);
            if($db){
                Db::name('article')->where(['id'=>$aid])->setInc($ziduan);
                $article=Db::name('article')->where(['id'=>$aid])->cache(_cache('db'))->find();
                if($article['uid']!=$uid){
                    $datazan=Db::name('article_zan')->where(['aid'=>$aid,'uid'=>$uid])->cache(_cache('db'))->find();
                    MsgService::weixinUserMoney($article['uid'], 0, 40, $article['title'],$uid,$datazan);//用户点赞
                }
                return ['status'=>1,'msg'=>'赞成功'];
            }else{
                return ['status'=>-1,'msg'=>'赞失败'];
            }
        }
    }
}