<?php


namespace app\lumaapi\service;


use app\lumaapi\model\MsgModel;
use think\Db;
use think\Exception;
use think\Log;

class MsgService
{
    /**消息模板
     * @description:
     * @param {type}
     * @return:
     */
    public static function weixinUserMoney($user_id=0,$money=0,$type=0,$msg=null,$uid=null,$data=[]){
        $Notice=[];
        switch ($type){
            case 31://会员通知
                $Notice=MsgModel::addMsg('300001',$user_id,$money,$msg,$uid,$data);
                break;
            case 32://用户余额变动
                $Notice=MsgModel::addMsg('300002',$user_id,$money,$msg,$uid,$data);
                break;
            case 35://积分变化通知
                $Notice=MsgModel::addMsg('300005',$user_id,$money,$msg,$uid,$data);
                break;
            case 40://点赞变化通知成功
                $Notice=MsgModel::addMsg('400000',$user_id,$money,$msg,$uid,$data);
                break;
            case 41://取消点赞变化通知
                $Notice=MsgModel::addMsg('400001',$user_id,$money,$msg,$uid,$data);
                break;
            case 45://评论变化通知
                $Notice=MsgModel::addMsg('400005',$user_id,$money,$msg,$uid,$data);
                break;
            case 46://删除评论变化通知
                $Notice=MsgModel::addMsg('400006',$user_id,$money,$msg,$uid,$data);
                break;
            case 50://关注变化通知
                $Notice=MsgModel::addMsg('500000',$user_id,$money,$msg,$uid,$data);
                break;
            case 51://关注变化通知
                $Notice=MsgModel::addMsg('500001',$user_id,$money,$msg,$uid,$data);
                break;
            default:
                $Notice=MsgModel::addMsg($type,$user_id,$money,$msg,$uid,$data);
                break;
        }
    }


    public static function MsgInfo($uid,$page,$source){
        $list=[];
        if($source==5){//关注
            $list=   MsgModel::getAdvicesList(['user_id'=>$uid,'source'=>5],"*",$page);
            foreach ($list as $key=>$val){
                $users = _user($val['db_msg']['uid']);
                $list[$key]['img'] = $users['img'] . "?imageView2/1/w/100/h/100";
                $list[$key]['username'] = $users['username'];

                $list[$key]['msg'] =$val['msg_text']['mold']==1?"关注了你":"不再关注你了";
                $list[$key]['ctime'] = date('y-m-d H:i', $val['ctime']);
            }
        }else if($source==3){//点赞
            $list=   MsgModel::getAdvicesList(['user_id'=>$uid,'source'=>3],"*",$page);
            foreach ($list as $key=>$val){
                if(isset($val['db_msg']['aid'])){
                    $article = Db::name('article')->where(['id'=>$val['db_msg']['aid']])->find();
                    $article['user']  =Db::name('member')->field('nickname,img')->where(['id'=>$article['uid']])->cache(_cache('db'))->find();
                    $zan=Db::name('article_zan')->where(['uid'=>$uid,'aid'=>$article['id']])->fetchSql(false)->find();
                    $article['is_zan']=isset($zan['zan'])==1?true:false;
                    $guanzhu=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$article['uid']])->fetchSql(false)->find();
                    $article['is_guanzhu']=isset($guanzhu)?true:false;

                    /*$list[$key]['title'] = $article['title'];
                    $list[$key]['qiniu_video'] = $article['qiniu_video'];
                    $list[$key]['videodate'] = $article['videodate'];*/
                    $list[$key]['article'] = $article;
                    $list[$key]['msg'] = "赞了我的视频";
                    $list[$key]['zan_type'] = 0;
                }else if(isset($val['db_msg']['pingId'])){
                    $article_zan = Db::name('article_ping_zan')->where(['id'=>$val['db_msg']['id'],'uid'=>$val['db_msg']['uid'],'pingId'=>$val['db_msg']['pingId']])->find();
                    if($article_zan){
                        $articleping = Db::name('article_ping')->where(['id'=>$val['db_msg']['pingId']])->find();
                    }else{
                        $articleping = Db::name('article_ping_xia')->where(['id'=>$val['db_msg']['pingId']])->find();
                    }
                    $article = Db::name('article')->where(['id'=>$articleping['aid']])->find();
                    $list[$key]['title'] = $article['title'];
                    $list[$key]['qiniu_video'] = $article['qiniu_video'];
                    $str=explode("//<a>",$articleping['content']);
                    $user = _user($articleping['uid']);
                    $list[$key]['pattone']['username'] = $user['username'];
                    $list[$key]['pattone']['msg']=$str[0];
                    $list[$key]['msg'] = "赞了我的评论";
                    $list[$key]['zan_type'] =1;
                }
                $users = _user($val['msg_text']['uid']);
                $list[$key]['img'] = $users['img'] . "?imageView2/1/w/100/h/100";
                $list[$key]['username'] = $users['username'];
                $list[$key]['ctime'] = date('y-m-d H:i', $val['ctime']);
            }
        }else if($source==4){//评论
            $list=   MsgModel::getAdvicesList(['user_id'=>$uid,'source'=>4],"*",$page);
            foreach ($list as $key=>$val){
                $str=explode("//<a>",$val['db_msg']['content']);
                $pattone=substr($val['db_msg']['content'],strlen($str[0])+2);
                $users = _user($val['db_msg']['uid']);
                $list[$key]['pattone']['img']= $users['img'] . "?imageView2/1/w/100/h/100";
                $list[$key]['pattone']['username']=$users['username'];
                if(count($str)>1){
                    $list[$key]['pattone']['msg']=$str[0];
                    $list[$key]['pattone']['reply_msg']=$str[1];
                    $list[$key]['zan_type'] =0;
                    $list[$key]['msg'] = "回复了我的评论";
                }else{
                    if(isset($val['db_msg']['ping_uid'])){
                        $articlepingxia=  Db::name('article_ping_xia')->where(['id'=>$val['db_msg']['id'],'uid'=>$val['db_msg']['uid'],'ping_uid'=>$val['db_msg']['ping_uid'],'ping_id'=>$val['db_msg']['ping_id']])->find();
                        $articleping=  Db::name('article_ping')->where(['id'=>$articlepingxia['ping_id'],'uid'=>$articlepingxia['ping_uid'],'aid'=>$articlepingxia['aid']])->find();
                        $user = _user($articleping['uid']);
                        $list[$key]['pattone']['msg'] =  $articlepingxia['content'];
                        $list[$key]['pattone']['reply_msg'] = $user['username']." : ". $articleping['content'];
                        $list[$key]['zan_type'] =0;
                        $list[$key]['msg'] = "回复了我的评论";
                    }else{
                    $article = Db::name('article')->where(['id'=>$val['db_msg']['aid']])->find();
                    $article['user']  =Db::name('member')->field('nickname,img')->where(['id'=>$article['uid']])->cache(_cache('db'))->find();
                    $zan=Db::name('article_zan')->where(['uid'=>$uid,'aid'=>$article['id']])->fetchSql(false)->find();
                    $article['is_zan']=isset($zan['zan'])==1?true:false;
                    $guanzhu=Db::name('member_guanzhu')->where(['uid'=>$uid,'gz_uid'=>$article['uid']])->fetchSql(false)->find();
                    $article['is_guanzhu']=isset($guanzhu)?true:false;
                    /*$list[$key]['pattone']['title']=$article['title'];
                    $list[$key]['pattone']['qiniu_video']=$article['qiniu_video'];
                    $list[$key]['pattone']['videodate'] = $article['videodate'];*/
                    $list[$key]['pattone']['article']=$article;
                    $list[$key]['pattone']['msg']=$str[0];
                    $list[$key]['zan_type'] =1;
                    $list[$key]['msg'] = "评论了我视频";
                  }
                }
                $list[$key]['ctime'] = date('Y-m-d H:i', $val['ctime']);
            }
        }
        return $list;
    }
}