Message.php 13.8 KB
<?php

namespace app\api\controller\inspection;

use app\common\controller\Api;
use fast\Tree;
use think\Db;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Message extends Api
{

    /**
     * Depart模型对象
     * @var \app\admin\model\inspection\Depart
     */
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\inspection\Message;
        $this->modellog = new \app\admin\model\inspection\Messagelog;
        \think\Lang::load(APP_PATH . '/admin/lang/zh-cn/inspection/message.php');
        \think\Lang::load(APP_PATH . '/admin/lang/zh-cn/inspection/message_log.php');
    }

    public function unread_message_count()
    {
        $count = $this->modellog
            ->where('user_id', $this->auth->id)
            ->where('status', '0')
            ->count();
        $data = ['UnreadMessages' => $count];
        $this->success('', $data);
    }

    public function list()
    {
        $cat = $this->request->param('cat', 0);
        if ($cat == 1) {
            //合并版app
            $condtion = [];
            $page = $this->request->param('page', 1);
            $limit = $this->request->param('total', 15);
            $type = $this->request->param('type');
            $time = $this->request->param('time');
            $issend = $this->request->param('issend', 0);
            $optstatus = $this->request->param("optstatus", 0);//0全部 1已读 2未读

            $uid = $this->auth->id;


            $opt = "LEFT";
            $cdt = "log.user_id='{$uid}' and log.insepection_message_id=a.id";
            if ($optstatus == 2) {
//            $condtion['log.status'] = ["neq","1"];
                $cdt = "log.insepection_message_id=a.id";
            } elseif ($optstatus == 1) {
                $opt = "INNER";
                $condtion['log.status'] = ["=", "1"];
            }
            $total = Db::name("inspection_message")
                ->alias("a")
                ->join('inspection_message_log log', $cdt, $opt)
                ->field('a.id')
                ->where($condtion)
                ->group("a.id")
                ->order('a.id desc')
                ->select();
            $list = Db::name("inspection_message")->alias("a")
                ->join('inspection_message_log log', $cdt, $opt)
                ->field('a.id,a.type,a.title,a.content,from_unixtime(a.createtime,"%Y-%m-%d %H:%i:%s") as crts')
                ->where($condtion)
                ->group("a.id")
                ->order('a.id desc')
                ->page($page, $limit)
                ->select();
            if (!empty($list)) {
                foreach ($list as $k => $v) {
                    $read = Db::name("inspection_message_log")->where(["insepection_message_id" => $v['id'], "user_id" => $uid, "status" => "1"])->find();
                    if ($read) {
                        $list[$k]["status"] = 1;
                        if ($optstatus == 2) {
                            unset($list[$k]);
                        }
                    } else {
                        $list[$k]['status'] = 0;
                    }
                }
            }

            $total = count($total);


            $ids = Db::name("inspection_message")
                //->where("type","2")
                ->column("id");
            if (!empty($ids)) {
                $ww = [];
                $ww["insepection_message_id"] = ["in", $ids];
                $ww["user_id"] = ["=", $uid];
                $ww["status"] = ["=", "1"];
                $hasread = Db::name("inspection_message_log")->where($ww)->count();
                $noread_num = count($ids) - $hasread > 0 ? count($ids) - $hasread : 0;
            } else {
                $noread_num = 0;
            }

            $data = [
                'page' => $page,
                'total' => $total,
                'list' => $list,
                'noread_num' => $noread_num
            ];
            $this->success('', $data);
        }
        $condtion = [];
        $page = $this->request->param('page', 1);
        $limit = $this->request->param('limit', 10);
        $type = $this->request->param('type');
        $time = $this->request->param('time');
        if ($time) {
            $createtime = strtotime($time);
            $createtimeend = strtotime($time . ' 23:59:59');
            $condtion['message.createtime'] = ['between', [$createtime, $createtimeend]];
        }


        if (in_array($type, ['1', '2'])) {
            $condtion['message.type'] = ['=', $type];
        }

        $condtion['log.user_id'] = ['=', $this->auth->id];
        $total = $this->model
            ->alias('message')
            ->join('inspection_message_log log', 'log.insepection_message_id=message.id', 'LEFT')
            ->where($condtion)
            ->count();
        $list = $this->model
            ->alias('message')
            ->join('inspection_message_log log', 'log.insepection_message_id=message.id', 'LEFT')
            ->field('message.id,message.type,message.title,from_unixtime(message.createtime,"%Y-%m-%d %H:%i:%s") as crts,log.status')
            ->where($condtion)
            ->order('message.createtime desc')
            ->page($page, $limit)
            ->select();
        $noread = Db::name('inspection_message_log')->where('user_id', $this->auth->id)->where('status', 0)->count();
        $data = [
            'page' => $page,
            'total' => $total,
            'list' => $list,
            'noread' => $noread
        ];
        $this->success('', $data);
    }

    public function details($ids = null)
    {
        $row = Db::name('inspection_message')->where(['id' => $ids])->find();
        if (!$row) {
            $this->error('信息不存在');
        }
        $ids = $row['user_ids'];
        $names = Db::name("inspection_staff")->where(["user_id" => ["in", $ids]])->column("staff_name");
        $row['unames'] = implode($names, ",");

        $row['weather'] = $row['weather'] ? json_decode($row['weather'], true) : [];
        $row['createtime'] = date("Y-m-d H:i:s", $row['createtime']);
        //图片和文件 地址处理
        if ($row['images']) {
            $picarr = explode(',', $row['images']);
            //var_dump($picarr);
            $row['images'] = $this->setQiniuFileUrl($picarr);
        }

        if ($row['filenames']) {
            $row['fileoldnameArr'] = explode(',', $row['filenames']);
        } else {
            $row['fileoldnameArr'] = [];
        }
        if ($row['fileurl']) {
            $farr = explode(',', $row['fileurl']);
            $row['fileurl'] = $this->setQiniuFileUrl($farr);
            foreach ($farr as $kk => $vv) {
                $file_data[$kk]["name"] = $row['fileoldnameArr'][$kk];
                $file_data[$kk]["url"] = full_image($vv);
                $file_data[$kk]["type"] = explode(".", $vv)[1];
            }
        }
        $row['file_data'] = $file_data;
        //已读状态追加
        if ($row['user_ids'] && empty($row['reservoir_ids'])) {
            $w = [];
            $w["l.user_id"] = ["IN", $row['user_ids']];
            $w["l.insepection_message_id"] = ["=", $row['id']];
            $row['staff_read_status'] =
                Db::name("inspection_message_log")->alias("l")
                    ->join("inspection_staff s", "s.user_id=l.user_id", "LEFT")
                    ->where($w)
                    ->where('l.user_id>0')
                    ->field("l.user_id,l.status,s.staff_name")
                    ->select();
        }

        $this->success('获取成功', $row);
    }

    public function confirm($ids = null)
    {
        $row = $this->modellog->get(['user_id' => $this->auth->id, 'insepection_message_id' => $ids]);
        if (!$row) {
            $this->error('通知不存在');
        } else if ($row->status == '1') {
            $this->error('通知是已读状态');
        }
        $row->status = '1';
        $res = $row->save();
        if (!$res) {
            $this->error('网络繁忙');
        }
        $this->success('操作成功');


    }

    /***
     * 通知公告发布
     * add
     */
    public function messageAdd()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data['title'])) {
                $this->error('请填写标题内容');
            }
            if (empty($data['type'])) {
                $this->error("请选择分类");
            }
            if ($data['type'] == 1) {
//                if (empty($data['user_ids'])) {
//                    $this->error("请选择指派人");
//                }
                unset($data['reservoir_ids']);
            }
            if ($data['type'] == 2) {
//                if (empty($data['reservoir_ids'])) {
//                    $this->error("请选择水库");
//                }
                unset($data['user_ids']);
            }
            if (empty($data['content'])) {
                $this->error('请填写详情内容');
            }
            $data['createtime'] = time();
            $data['images'] = implode(',', $data['imagearr']);
            $data['fileurl'] = implode(',', $data['filearr']);
            $data['filenames'] = implode(",", $data['fileoldnameArr']);
            unset($data['imagearr']);
            unset($data['filearr']);
            unset($data['fileoldnameArr']);
            $data['add_uid'] = $this->auth->id;
            $data['user_ids'] = $data['user_ids'] ? $data['user_ids'] : $this->auth->id;//$this->auth->id;

            if (!empty($data['weather'])) {
                $data['weather'] = json_encode($data['weather'], JSON_UNESCAPED_UNICODE);
            }
            $rs = Db::name("inspection_message")->insertGetId($data);
            if ($rs) {
                //追加未读记录
                if (!empty($data['user_ids'])) {
                    $ids = explode(",", $data['user_ids']);
                    if (!empty($ids)) {
                        foreach ($ids as $k => $v) {
                            $log = [];
                            $log = [
                                "user_id" => $v,
                                "insepection_message_id" => $rs,
                                "status" => 0
                            ];
                            Db::name("inspection_message_log")->insert($log);
                        }
                    }

                }
                //unipiush推送
//                $jgPush = new JgPush();
//                $res = $jgPush->pushMessage($rs);
//                if ($res['state'] == true) {
//                    $msg = "【计划推送人数:" . $res['data']['sum_count'] . ";实际推送人数:" . $res['data']['react_count'] . "】";
//                } else {
//                    $msg = "[" . $res['msg'] . "]";
//                }
                $this->success('发布成功', $rs);
            } else {
                $this->error('发布失败!');
            }
        }
    }

    /**
     * 更新通知公告阅读状态
     *
     */
    public function isRead()
    {
        if ($this->request->isPost()) {
            $ids = $this->request->post('ids', 0);
            $user_id = $this->auth->id;

            $info = $this->model::get($ids);
            if (empty($info)) {
                $this->error('信息不存在');
            }
            $res = Db::name("inspection_message_log")->where(["user_id" => $user_id, "insepection_message_id" => $ids])->find();
            if (empty($res)) {
                Db::name("inspection_message_log")->insert(["user_id" => $user_id, "insepection_message_id" => $ids, "status" => 1]);

            } else {
                Db::name("inspection_message_log")->where(["user_id" => $user_id, "insepection_message_id" => $ids])->update(["status" => 1]);
            }
            $this->success('更新成功');
        }
    }

    /**
     * 处理七牛一的文件地址:图片、视频、文件
     * $data 可以传入一维数组
     * 文件地址前添加域名地址
     */
    private function setQiniuFileUrl($data)
    {
        $domen = 'https://qiniu.ynzhsk.cn';
        if (is_array($data)) {
            $arr = array();
            foreach ($data as $v) {
                if ($v) {
                    array_push($arr, $domen . $v);
                }
            }
            return $arr;
        } else {
            return $domen . $data;
        }

    }


    /**
     * 36、我发布的公告列表
     */
    public function my_post_gonggao_list()
    {
        $condtion = [];
        $page = $this->request->param('page', 1);
        $limit = $this->request->param('total', 15);
        $issend = $this->request->param('issend', 1);

        $uid = $this->auth->id;
        $condtion['message.add_uid'] = ['=', $this->auth->id];
        $condtion['message.type'] = ['=', 2];//公告
//        if(!empty($uid)){
//            $condtion['log.user_id'] = ['=',$uid];
//        }
        $total = $this->model
            ->alias('message')
            ->join('inspection_message_log log', 'log.insepection_message_id=message.id', 'LEFT')
            ->field('message.id')
            ->where($condtion)
            ->order('message.id desc')
            ->distinct(true)
            ->group('message.id')
            ->select();
        $list = $this->model
            ->alias('message')
            ->join('inspection_message_log log', 'log.insepection_message_id=message.id', 'LEFT')
            ->field('message.id,message.type,message.title,message.content,from_unixtime(message.createtime,"%Y-%m-%d %H:%i") as crts,log.status as `status`')
            ->where($condtion)
            ->order('message.id desc')
            ->distinct(true)
            ->group('message.id')
            ->page($page, $limit)
            ->select();

        $total = count($total);
        $data = [
            'page' => $page,
            'total' => $total,
            'list' => $list
        ];
        $this->success('', $data);
    }
}