<?php


namespace app\lumaapi\service;


use think\Db;
use think\Validate;

class PingService
{
    //用户评论
    public static function ping($uid,$aid,$content,$pingId,$pingId2,$member){

        // 限制会员每日100条评论数量 防止恶意刷评论
        $count=Db::name('article_ping')->whereTime('time', 'today')->where(['uid'=>$uid])->count();
        if($count>100){
            return ['status'=>-1,'msg'=>'每日发布评论限制100条内'];
        }else{
            if(!$pingId){
                $where['uid']=$uid;
                $where['aid']=$aid;
                $where['time']=time();
                $where['content']=$content;
                $db=Db::name('article_ping')->insert($where);
                if($db){
                    $tips = 0;
                    $name = Db::name('article')->where(['id'=>$aid])->find();
                    if(empty($name)){
                        return ['status'=>-1,'msg'=>"视频不存在"];
                    }
                    if($name['weitoutiao'] == 3 && $name['hide'] == 1){
                        $tips = _huitie_jiangli($member,$aid);
                    }
                    Db::name('article')->where(['id'=>$aid])->setInc('pingNum');
                    Db::name('article')->where(['id'=>$aid])->update(['update_time'=>time()]);
                    if(isset($tips['status']) == -1){
                        return ['status'=>-1,'msg'=>$tips['error']];
                    }
                    $ping=Db::name('article_ping')->where(['aid'=>$aid,'uid'=>$uid])->order("time desc")->find();
                    if($name['uid']!=$uid){
                        MsgService::weixinUserMoney($name['uid'], 0, 45, $name['title'],$uid,$ping);//评论成功
                    }
                    $ping['time']=date('m-d H:i',$ping['time']);
                    return ['status'=>1,'msg'=>"评论成功",'data'=>$ping];
                }else{
                    return ['status'=>-1,'msg'=>"评论失败"];
                }
            }else{
                $ping_uid=db::name('article_ping')->where(['id'=>$pingId])->value('uid');
                $where['ping_id']=$pingId;
                $where['ping_uid']=$ping_uid;
                $where['uid']=$uid;
                $where['aid']=$aid;
                $where['time']=time();
                if($pingId2){
                    $pingId2=db::name('article_ping_xia')->where(['id'=>$pingId2])->find();
                    $user=_user($pingId2['uid']);
                    $where['content']=$content.'//<a>@'.$user['username'].'</a>：'.$pingId2['content'];
                }else{
                    $where['content']=$content;
                }
                $db=Db::name('article_ping_xia')->insert($where);
                if($db){
                    Db::name('article')->where(['id'=>$aid])->update(['update_time'=>time()]);
                    $article_ping=db::name('article_ping')->where(['id'=>$pingId])->find();
                    $ping=Db::name('article_ping_xia')->where(['aid'=>$aid,'uid'=>$uid,'ping_id'=>$pingId,'ping_uid'=>$ping_uid])->order("time desc")->find();
                    if($ping_uid!=$uid){
                        MsgService::weixinUserMoney($ping_uid, 0, 45, $article_ping['content'],$uid,$ping);//用户点赞
                    }
                    $ping['time']=date('m-d H:i',$ping['time']);
                    return ['status'=>1,'msg'=>"评论成功",'data'=>$ping];
                }else{
                    return ['status'=>-1,'msg'=>"评论失败"];
                }
            }
        }
        return ['status'=>-1,'msg'=>"评论失败"];
    }


// 文章评论赞
    public static function pingZan($uid,$pingid){
        $zan=Db::name('article_ping_zan')->where(['pingId'=>$pingid,'uid'=>$uid])->cache(_cache('db'))->value('id');
        if($zan){
            return ['status'=>-1,'msg'=>"您已经赞过啦"];
        }else{
            $where['zan']=1;
            $where['uid']=$uid;
            $where['pingId']=$pingid;
            $where['time']=time();
            $db=Db::name('article_ping_zan')->insert($where);
            if($db){
                Db::name('article_ping')->where(['id'=>$pingid])->setInc('zan');
                $article=Db::name('article_ping')->where(['id'=>$pingid])->cache(_cache('db'))->find();
                if($article['uid']!=$uid){
                    $datazan=Db::name('article_ping_zan')->where(['pingId'=>$pingid,'uid'=>$uid])->cache(_cache('db'))->find();
                    MsgService::weixinUserMoney($article['uid'], 0, 40, $article['content'],$uid,$datazan);//评论点赞
                }
                return ['status'=>1,'msg'=>"点赞成功"];
            }else{
                return ['status'=>-1,'msg'=>"点赞失败"];
            }
        }

    }
    // 被评论赞
    public static function pingZan_xia($uid,$pingid){
        $zan=Db::name('article_ping_xia_zan')->where(['pingId'=>$pingid,'uid'=>$uid])->cache(_cache('db'))->value('id');
        if($zan){
            return ['status'=>-1,'msg'=>"您已经赞过啦"];
        }else{
            $where['zan']=1;
            $where['uid']=$uid;
            $where['pingId']=$pingid;
            $where['time']=time();
            $db=Db::name('article_ping_xia_zan')->insert($where);
            if($db){
                Db::name('article_ping_xia')->where(['id'=>$pingid])->setInc('zan');
                $article=Db::name('article_ping_xia')->where(['id'=>$pingid])->cache(_cache('db'))->find();
                if($article['uid']!=$uid) {
                    $datazan = Db::name('article_ping_xia_zan')->where(['pingId' => $pingid, 'uid' => $uid])->cache(_cache('db'))->find();
                    MsgService::weixinUserMoney($article['uid'], 0, 40, $article['content'], $uid, $datazan);//评论点赞
                }
                return ['status'=>1,'msg'=>"点赞成功"];
            }else{
                return ['status'=>-1,'msg'=>"点赞失败"];
            }
        }
    }


    // 被赞用户列表
    public static function zanList($uid,$pages){
        $where['w.uid']=$uid;
        $join = [
            ['__ARTICLE_PING__ w','a.pingId=w.id'],
        ];
        $data=Db::name('article_ping_zan')->where($where)->alias('a')->join($join)->field('a.uid,w.content,a.pingId,w.aid,a.time')->order('a.time desc')->fetchSql(false)->page($pages)->select();
        foreach ($data as $k => $v) {
            $user=_user($v['uid']);
            $data[$k]['img']=_imgUrl().$user['img']."?imageView2/1/w/100/h/100";
            $data[$k]['username']=$user['username'];
            $data[$k]['time']=date('m-d H:i',$v['time']);
        }
        return $data;
    }


    // 被评论用户列表
    public static function pingList($uid,$pages){
        $where['ping_uid']=$uid;
        $data=Db::name('article_ping_xia')->where($where)->order('time desc')->fetchSql(false)->page($pages)->select();
        foreach ($data as $k => $v) {
            $user=_user($v['uid']);
            //$data['data'][$k]['img']=_imgUrl().$user['img']."?imageView2/1/w/100/h/100";
            $data[$k]['img']=$user['img']."?imageView2/1/w/100/h/100";
            $data[$k]['username']=$user['username'];
            $data[$k]['time']=date('m-d H:i',$v['time']);
            // 被评论内容
            $data[$k]['contentPing']=db::name('article_ping')->where(['id'=>$v['ping_id']])->value('content');
        }
        return $data;
    }

    // 评论列表
    public static function pingAllList($uid,$aid,$pages,$order="time desc"){
        $where['aid']=$aid;
        $where['closed']=0;
        $data=Db::name('article_ping')->where($where)->order($order)->fetchSql(false)->page($pages)->select();
        foreach ($data as $k => $v) {
            $user = _user($v['uid']);
            $data[$k]['content']=$v['closed']==1?"该评论已被删除！":$v['content'];
            $data[$k]['img'] = $user['img'] . "?imageView2/1/w/100/h/100";
            $data[$k]['username'] = $user['username'];
            $data[$k]['time'] = date('m-d H:i', $v['time']);
            $pingNum = Db::name('article_ping_xia')->where(['ping_id' => $v['id'],'closed'=>0])->count();
            $data[$k]['huifu'] = $pingNum;
            $is_zan=Db::name('article_ping_zan')->where(['pingId' => $v['id'],"uid"=>$uid])->count();
            $data[$k]['is_zan'] = $is_zan?true:false;
            if ($pingNum) {

                $data[$k]['pingDataList']=[];
                /*$pingData = Db::name('article_ping_xia')->where(['ping_id' => $v['id']])->order($order)->limit(1)->select();
                foreach ($pingData as $ks => $vs) {
                    $users = _user($vs['uid']);
                    $str=explode("//<a>",$vs['content']);
                    $pattone=substr($vs['content'],strlen($str[0])+2);
                    $content=str_replace('a', "text",$pattone);
                    $data[$k]['pingDataList'][$ks]['id'] = $vs['id'];
                    $data[$k]['pingDataList'][$ks]['uid'] = $vs['uid'];
                    $data[$k]['pingDataList'][$ks]['ping_uid'] = $vs['ping_uid'];
                    $data[$k]['pingDataList'][$ks]['ping_id'] = $vs['ping_id'];
                    $data[$k]['pingDataList'][$ks]['aid'] = $vs['aid'];
                    $data[$k]['pingDataList'][$ks]['zan'] = $vs['zan'];
                    $data[$k]['pingDataList'][$ks]['content'] = $str[0];
                    $data[$k]['pingDataList'][$ks]['img'] = $users['img'] . "?imageView2/1/w/100/h/100";
                    $data[$k]['pingDataList'][$ks]['username'] = $users['username'];
                    $data[$k]['pingDataList'][$ks]['tipContent'] = $content;
                    $data[$k]['pingDataList'][$ks]['time'] = date('m-d H:i', $vs['time']);
                    $is_zan=Db::name('article_ping_xia_zan')->where(['pingId' => $vs['id'],"uid"=>$uid])->count();
                    $data[$k]['pingDataList'][$ks]['is_zan'] = $is_zan?true:false;
                }*/
                /*$pingDataHtml = '';
                foreach ($pingData as $ks => $vs) {
                    $pingDataHtml .= '<li class="aui-ellipsis-2">';
                    $user = _user($vs['uid']);
                    $pingDataHtml .= '<a Tapmode onclick="_url({id:' . $vs['uid'] . '},\'u_win\')">' . $user['username'] . ' ：</a>' . $vs['content'] . '</li>';
                }
                if ($pingNum > 2) {
                    $pingDataHtml .= '<li class="aui-ellipsis-2"><a>查看全部' . $pingNum . '条回复</a></li>';
                }
                $data[$k]['pingDataList'] = $pingDataHtml;
                */

            }
            $data[$k]['aid'] = $v['aid'];
            $zanUser = Db::name('article_ping_zan')->where(['pingId' => $v['id'], 'uid' => $uid])->cache(_cache('db'))->value('id');
            $data[$k]['zanUser'] = $zanUser;
        }
        return $data;
    }


    // 评论列表
    public static function pingHuiList($uid,$pingId,$pages,$order="time desc"){
        $pingData = Db::name('article_ping_xia')->where(['ping_id' => $pingId,'closed'=>0])->order($order)->page($pages,5)->select();
        foreach ($pingData as $ks => $vs) {
            $users = _user($vs['uid']);
            $str=explode("//<a>",$vs['content']);
            $pattone=substr($vs['content'],strlen($str[0])+2);
            $content=str_replace('a', "text",$pattone);
            $pingData[$ks]['id'] = $vs['id'];
            $pingData[$ks]['uid'] = $vs['uid'];
            $pingData[$ks]['ping_uid'] = $vs['ping_uid'];
            $pingData[$ks]['ping_id'] = $vs['ping_id'];
            $pingData[$ks]['aid'] = $vs['aid'];
            $pingData[$ks]['zan'] = $vs['zan'];
            $pingData[$ks]['content'] = $vs['closed']==1?"该评论已被删除！":$str[0];
            $pingData[$ks]['img'] = $users['img'] . "?imageView2/1/w/100/h/100";
            $pingData[$ks]['username'] = $users['username'];
            $pingData[$ks]['tipContent'] = $content;
            $pingData[$ks]['time'] = date('m-d H:i', $vs['time']);
            $is_zan=Db::name('article_ping_xia_zan')->where(['pingId' => $vs['id'],"uid"=>$uid])->count();
            $pingData[$ks]['is_zan'] = $is_zan?true:false;

        }
        /*$pingDataHtml = '';
        foreach ($pingData as $ks => $vs) {
            $pingDataHtml .= '<li class="aui-ellipsis-2">';
            $user = _user($vs['uid']);
            $pingDataHtml .= '<a Tapmode onclick="_url({id:' . $vs['uid'] . '},\'u_win\')">' . $user['username'] . ' ：</a>' . $vs['content'] . '</li>';
        }*/
        return $pingData;
    }
}