<?php


namespace app\lumaapi\controller;


use app\lumaapi\model\MsgModel;
use app\lumaapi\service\EventService;
use app\lumaapi\service\MsgService;
use app\lumaapi\service\PingService;
use app\lumaapi\service\UserServices;
use think\Config;
use think\Db;
use think\Validate;

class Users extends Common
{
    /**
     * 前置操作 except不需要登录，only需要登录
     */
    protected $beforeActionList = [
        'need_login' => ['except' => ["login"
        ]],
    ];

    public function login(){
        $param=$this->request->param();
        if($this->user_id <=0){
            $this->error("用户不存在");
        }
        $uid= UserServices::registerLumaUser($this->user_id);
        Db::name('member')->where(['id'=>$uid])->update(['longin_time'=>time()]);
        $users=Db::name('member')->where(['id'=>$uid])->fetchSql(false)->find();
        $this->success($users);
    }

    /**
     * 用户主页
     */
    public function homepage(){
        $page=$this->request->param("page",1);
        $uid=$this->request->param("uid");//用户主页
        if(empty($uid)){
            $this->error("参数错误！");
        }
        $map['mychannel']=3;
        $map['hide']=1;
        $map['uid']=$uid;
        $article=Db::name('article')->where($map)->limit(12)->order("des","desc")->order("create_time","desc")->page($page)->select();
        foreach ($article as $key=>$val){
            $article[$key]['user']=Db::name('member')->field('nickname,img')->where(['id'=>$val['uid']])->cache(_cache('db'))->find();
            $zan=Db::name('article_zan')->where(['uid'=>$this->uid,'aid'=>$val['id']])->fetchSql(false)->find();
            $article[$key]['is_zan']=isset($zan['zan'])==1?true:false;
            $guanzhu=Db::name('member_guanzhu')->where(['uid'=>$this->uid,'gz_uid'=>$val['uid']])->fetchSql(false)->find();
            $article[$key]['is_guanzhu']=isset($guanzhu)?true:false;

        }
        $favorite=db('article_zan')->alias('f')->join('article b','f.aid=b.id')
            ->field('b.*')->where(['f.uid'=>$uid,'b.hide'=>1])->limit(12)->order("time","desc")->page($page)->select();//喜欢的作品
        foreach ($favorite as $key=>$val){
            $zan=Db::name('article_zan')->where(['uid'=>$this->uid,'aid'=>$val['id']])->fetchSql(false)->find();
            $favorite[$key]['is_zan']=isset($zan['zan'])==1?true:false;
            $favorite[$key]['user']=Db::name('member')->field('nickname,img')->where(['id'=>$val['uid']])->cache(_cache('db'))->find();
            $guanzhu=Db::name('member_guanzhu')->where(['uid'=>$this->uid,'gz_uid'=>$val['uid']])->fetchSql(false)->find();
            $favorite[$key]['is_guanzhu']=isset($guanzhu)?true:false;
        }
        //$data['browse']=Db::name('article')->where($map)->sum("click");
        $data['zan']=Db::name('article')->where($map)->sum("zan");
        $data['fans']=Db::name('member_guanzhu')->where(['gz_uid'=>$uid])->count();
        $data['guanzhu']=Db::name('member_guanzhu')->where(['uid'=>$uid])->count();
        $data['productionCount']=Db::name('article')->where($map)->count();
        $data['loveCount']=Db::name('article_zan')->where(['uid'=>$uid])->count();

        $count=Db::name('member_guanzhu')->where(['gz_uid'=>$uid,'uid'=>$this->uid])->count();//关注他人
        /*$count1=Db::name('member_guanzhu')->where("uid = ".$this->uid ." and gz_uid = ".$uid .") or ( uid = ".$uid ." and gz_uid = ".$this->uid)->count();
        $count2=$count1>1?0:1;$count==0?2:$count2*/
        $count1=Db::name('member_guanzhu')->where(['gz_uid'=>$this->uid,'uid'=>$uid])->count();//被人关注
        $status=2;
        if($count>0&&$count1>0){
            $status=0;
        }else if($count>0){
            $status=1;
        }
        $data['is_guanzhu']=$status;
        $member=Db::name('member')->where(['id'=>$uid])->find();
        $this->success(['data'=>$article,'favorite'=>$favorite,'info'=>$data,'member'=>$member]);
    }
    //用户签名
    public function signature(){
        $sex=$this->request->param("sex");
        $qianming=$this->request->param("signa");
        $birthday=$this->request->param("birthday");
        $mobile=$this->request->param("mobile");
        $email=$this->request->param("email");
        if(isset($mobile) && _isMobile($mobile)==false){
            $this->error("请填写正确的手机号！");
        }
        $db=Db::name('member')->where(['id'=>$this->uid])->update(['sex'=>$sex,'birthday'=>$birthday,
            'mobile'=>$mobile,'email'=>$email,'qianming'=>$qianming]);
        if($db){
            $this->success([],"签名成功！");
        }
        $this->error("签名失败！");
    }

    //关注
    public function guanzhu(){
        $gz_uid=$this->request->param("gz_uid");
        if(empty($gz_uid)){
            $this->error("参数不能为空！");
        }
        $res=EventService::guanzhu($this->uid,$gz_uid);
        if($res['status']>0){
            $this->success();
        }
        $this->error($res['msg']);
    }

    //取消关注
    public function guanzhuDel(){
        $gz_uid=$this->request->param("gz_uid");
        if(empty($gz_uid)){
            $this->error("参数不能为空！");
        }
        $res=EventService::guanzhuDel($this->uid,$gz_uid);
        if($res['status']>0){
            $this->success();
        }
        $this->error($res['msg']);
    }

    //关注列表
    public function guanzhuList(){
        $gz_uid=$this->request->param("gz_uid");
        $pages=$this->request->param("page",1);
        $type=$this->request->param("type",1);//1他的关注（他人），2关注他的（他人），3我的关注（自己），4关注我的（自己）
        $order=$this->request->param("order","time desc");
        //开始验证
        $validate = new Validate([
            'page'         =>      'number',
            'gz_uid'          =>       'require|number',
        ]);
        $validata=[
            'page'         =>      $pages,
            'gz_uid'          => $gz_uid,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
       $data= EventService::guanzhuList($gz_uid,$this->uid,$type,$order,$pages);
       $this->success($data);
    }

   /* //喜欢收藏
    public function favorite(){
        $aid=$this->request->param("aid");
        //开始验证
        $validate = new Validate([
            'aid'           =>      'require|number',
        ]);
        $validata=[
            'aid'           =>      $aid,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $res=EventService::favoriteAdd($aid,$this->uid);
        if($res['status']>0){
            $this->success();
        }
        $this->error($res['msg']);
    }

    //取消喜欢收藏
    public function favoriteDel(){
        $aid=$this->request->param("aid");
        //开始验证
        $validate = new Validate([
            'aid'           =>      'require|number',
        ]);
        $validata=[
            'aid'           =>      $aid,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $res=EventService::favoriteDel($aid,$this->uid);
        if($res['status']>0){
            $this->success();
        }
        $this->error($res['msg']);
    }*/



    /**
     * 点赞
     */
    public function zan(){
        $aid=$this->request->param("aid");
        $type=$this->request->param("type",1);//1zan，2cai
        if(empty($aid)){
            $this->error("参数不能为空！");
        }
        $res=EventService::zancai($this->uid,$aid,$type);
        if($res['status']>0){
            $this->success();
        }
        $this->error($res['msg']);
    }

    // 文章评论赞
    public function pingZan(){
        $pingid=$this->request->param("pingid");
        //开始验证
        $validate = new Validate([
            'pingid'           =>      'require|number',
        ]);
        $validata=[
            'pingid'           =>      $pingid,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $res=PingService::pingZan($this->uid,$pingid);
        if($res['status']>0){
            $this->success();
        }
        $this->error($res['msg']);

    }
    // 被评论赞
    public function pingZanXia(){
        $pingid=$this->request->param("pingid");
        //开始验证
        $validate = new Validate([
            'pingid'           =>      'require|number',
        ]);
        $validata=[
            'pingid'           =>      $pingid,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $res=PingService::pingZan_xia($this->uid,$pingid);
        if($res['status']>0){
            $this->success();
        }
        $this->error($res['msg']);
    }
    /**
     * 用户评论
     */
    public function ping(){
        $aid=$this->request->param("aid");
        $content=$this->request->param("content");
        $pingId=$this->request->param("pingId");
        $pingId2=$this->request->param("pingId2");
        //开始验证
        $validate = new Validate([
            'aid'           =>      'require|number',
            'pingId'        =>      'number',
            'pingId2'       =>      'number',
            'content'       =>      'require',
        ]);
        $validata=[
            'aid'           =>      $aid,
            'pingId'        =>      $pingId,//评论id
            'pingId2'       =>      $pingId2,//评论回复用户id
            'content'       =>      $content,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $res=PingService::ping($this->uid,$aid,$content,$pingId,$pingId2,$this->member);
        if($res['status']>0){
            $this->success($res['data']);
        }
        $this->error($res['msg']);
    }
    // 评论删除
    public function pingDel(){
        $id=$this->request->param("id");
        if(empty($id)){
            $this->error("id参数错误！");
        }
        //$db=Db::name('article_ping')->delete($id);
        $db=Db::name('article_ping')->where(['id'=>$id])->update(['closed'=>1]);
        $ping=Db::name('article_ping')->where(['id'=>$id])->order("time desc")->find();
        $article = Db::name('article')->where(['id'=>$ping['aid']])->find();
        if($article['uid']!=$this->uid) {
            MsgService::weixinUserMoney($article['uid'], 0, 46, $article['title'], $this->uid, $ping);//用户点赞
        }
        if($db){
            $this->success([],"删除成功！");
        }
        $this->error("删除失败！");
    }
    // 评论删除 下
    public function pingXaiDel(){
        $id=$this->request->param("id");
        if(empty($id)){
            $this->error("id参数错误！");
        }
        //$db=Db::name('article_ping_xia')->delete($id);
        $db=Db::name('article_ping_xia')->where(['id'=>$id])->update(['closed'=>1]);
        $ping_xia=Db::name('article_ping_xia')->where(['id'=>$id])->order("time desc")->find();
        $ping=Db::name('article_ping')->where(['id'=>$ping_xia['ping_id']])->order("time desc")->find();
        if($ping_xia['ping_uid']!=$this->uid) {
            MsgService::weixinUserMoney($ping_xia['ping_uid'], 0, 46, $ping['content'],$this->uid,$ping_xia);//用户点赞
        }
        if($db){
            $this->success([],"删除成功！");
        }
        $this->error("删除失败！");
    }

    //评论列表
    public function pingAlllist(){
        $aid=$this->request->param("aid");
        $pages=$this->request->param("page",1);
        $order=$this->request->param("order") ? $this->request->param("order") : 'time desc';
        //开始验证
        $validate = new Validate([
            'page'         =>      'number',
            'aid'           =>      'require|number',
        ]);
        $validata=[
            'aid'           =>      $aid,
            'page'         =>      $pages,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $data=PingService::pingAllList($this->uid,$aid,$pages,$order);
        $this->success($data);
    }

    //评论回复列表
    public function pingHuiList(){
        $pingId=$this->request->param("ping_id");
        $pages=$this->request->param("page",1);
        $order=$this->request->param("order") ? $this->request->param("order") : 'time asc';
        //开始验证
        $validate = new Validate([
            'page'         =>      'number',
            'ping_id'           =>      'require|number',
        ]);
        $validata=[
            'ping_id'           =>      $pingId,
            'page'         =>      $pages,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $data=PingService::pingHuiList($this->uid,$pingId,$pages,$order);
        $this->success($data);

    }

    //统计消息数据
    public function MsgIndex(){
        $data['guanzhu']=MsgModel::getAdvicesCount($this->uid,['IN','5']);
        $data['zan']=MsgModel::getAdvicesCount($this->uid,['IN','3']);
        $data['pinglun']=MsgModel::getAdvicesCount($this->uid,['IN','4']);
        $this->success($data);
    }


    /**
     * 消息通知信息
     */
    public function MsgInfo(){
        $pages=$this->request->param("page",1);
        $source=$this->request->param("source",5);
        //开始验证
        $validate = new Validate([
            'page'         =>      'number',
        ]);
        $validata=[
            'page'         =>      $pages,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $list=MsgService::MsgInfo($this->uid,$pages,$source);
        $this->success($list);
    }





    // 被赞用户列表
    public function zanList(){
        $pages=$this->request->param("page",1);
        //开始验证
        $validate = new Validate([
            'page'         =>      'number',
        ]);
        $validata=[
            'page'         =>      $pages,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $data=PingService::zanList($this->uid,$pages);
        $this->success($data);
    }
    // 被评论用户列表
    public function pingList(){
        $pages=$this->request->param("page",1);
        //开始验证
        $validate = new Validate([
            'page'         =>      'number',
        ]);
        $validata=[
            'page'         =>      $pages,
        ];
        if (!$validate->check($validata)) {
            $this->error($validate->getError());
        }
        $data=PingService::pingList($this->uid,$pages);
        $this->success($data);
    }

}