GroupModel.php 6.3 KB
<?php
/**
 * Created by PhpStorm.
 * auther: sgj
 * Date: 2019/1/2
 * Time: 15:05
 */

namespace app\portal\model;

use think\Model;

class GroupModel extends Model
{
        public function index(){

        }

    /**
     * 获取所有组
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllGroup(){
            $map['delete_time']=null;
           $group= $this->where($map)->select();
            return  $group;
        }

    /**
     * 获取我的圈子
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function getMyGroup(){
            $user_id=cmf_get_current_user_id();
            $group_user_map['user_id']=$user_id;
            $group_user_map['delete_time']=null;
            $group_id=db('group_user')->where($group_user_map)->column('group_id');
            $group_id=implode(',',$group_id);
            $group_map['id']=['in',$group_id];
            $group_map['delete_time']=null;
            $group=db('group')->where($group_map)->select();
            return $group;
        }

    /** 检查用户权限
     * @param $user_id
     * @param $group_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function checkUser($user_id,$group_id){
            $map['group_id']=$group_id;
            $map['user_id']=$user_id;
            $isin=db('group_user')->where($map)->find();
            return $isin;
        }

    /** 获取朋友圈信息
     * @param $group_id
     * @return mixed
     */
        public function getFriendsInfo($group_id){
            $map['t.delete_time']=null;
            $map['t.group_id']=$group_id;
            $user_id= cmf_get_current_user_id();
            $info=db('group_text')
                ->alias('t')
                ->field('u.avatar,u.user_login,t.*,t.id as text_id')
                ->join('bro_user u','u.id=t.user_id')

                ->where($map)
                ->order('t.id','desc')
                ->select()
                ->toArray();
            foreach ($info as $k=>$v){
                $info[$k]['like']=$this->getLIke($v['text_id']);
                $is_like=array_search($user_id, array_column($info[$k]['like'], 'user_id'));
                if ($is_like===false){
                    $info[$k]['is_like']=0;
                }else{
                    $info[$k]['is_like']=1;
                }
                $info[$k]['comment']=$this->getComment($v['text_id']);
            }
            return $info;
        }

        public function getGroup($group_id){
            $group=db('group')->where('id',$group_id)->find();
            return $group;
        }

    /**
     * 获取点赞人
     * @param $text_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function getLIke($text_id){
            $info=db('group_like')->where('text_id',$text_id)->select()->toArray();
            return $info;
        }

    /**
     * 获取评论
     * 文章id
     * @param $text_id
     * @return mixed
     */
        public function getComment($text_id){
            $comment=db('friend_comment')
                ->where('text_id',$text_id)
                ->order('id asc')
                ->select()
                ->toArray();
            return $comment;
        }

    /**
     * 提交赞
     * @param $user_id
     * @param $text_id
     * @return int|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
        public function postLike($user_id,$text_id){
            /*查看是否有相应的信息*/
            $map['text_id']=$text_id;
            $map['user_id']=$user_id;
            $isin=db('group_like')->where($map)->find();
            if (!empty($isin)){
               $result=db('group_like')->where($map)->delete();
              return $result;
            }
            /*若没有记录添加记录*/
            $user_map['id']=$user_id;
            $user=db('user')->where($user_map)->find();
            $insert['text_id']=$text_id;
            $insert['user_name']=$user['user_login'];
            $insert['user_id']=$user_id;
            $insert['add_time']=time();
            $result=db('group_like')->insert($insert);
            return $result;
        }


        public function postContent($user_id,$content,$text_id,$to_id=''){
           $insert['user_id']=$user_id;
           $insert['content']=$content;
           $insert['text_id']=$text_id;
           $insert['to_id']=$to_id;
           $user=db('user')->where('id',$user_id)->find();
           $insert['user_name']=$user['user_login'];
           $insert['add_time']=time();
           $result=db('friend_comment')->insert($insert);
           return $result;
        }

    /**
     * 获取文章列表
     * @param $group_id
     */
        public function getArticleList($group_id){
         $article=db('portal_group_post')
                ->alias('gp')
                ->field('gp.*,p.*,p.more as amore')
                ->join('bro_portal_post p','gp.post_id=p.id')
                ->join('bro_user u','u.id=author_id','LEFT')
                ->where('gp.group_id',$group_id)
                ->select();
            return $article;
        }

    /**
     * 检查是否可用发送朋友圈
     * @param $user_id
     * @param $group_id
     */
        public function silenceCheck($user_id,$group_id){
           /*检查是否为版主*/
            /*检查是否开启禁言*/
            $group=$this->getGroup($group_id);
            if ($group['silence']=='0'){
                return 0;
            }
            if ($user_id==$group['owner']){
                return 0;
            }
            return 1;
        }
}