<?php
/**
 * Created by PhpStorm.
 * User: chenkuan
 * Date: 2017/11/3
 * Time: 下午3:09
 */

namespace frontend\controllers;


use backend\helpers\PaginationHelper;
use backend\helpers\ValidatorHelper;
use backend\models\Notice;
use backend\models\NoticeRead;

class NoticeController extends BaseController
{
    /**
     * 用户页面获取公告列表
     */
    public function actionIndex()
    {
        $data = \Yii::$app->getRequest()->get();
        $data['member_id'] = isset($data['member_id']) ? intval($data['member_id']) : 0;
        $data['page'] = isset($data['page']) ? max(intval($data['page']), 1) : 1;
        $data['page_size'] = isset($data['page_size']) ? intval($data['page_size']) : 20;
        $data['title'] = isset($data['title']) ? $data['title'] : '';
        $data['orderBy'] = isset($data['orderBy']) ? trim($data['orderBy']) : 'id desc';
        $data = ValidatorHelper::validateData($data, [
            ['page', 'integer', 'min' => 1],
            ['page_size', 'integer', 'min' => 1],
            ['orderBy', 'string'],
        ], $errors);
        if ($data == false) {
            return $this->outJson(0, '', $errors);
        }

        $query = Notice::find()
            ->select('id, type, title, FROM_UNIXTIME(in_time/1000) as in_time')
            ->where(['type' => 0])
            ->orderBy($data['orderBy']);
        // 按标题搜索
        if (!empty($data['title'])) {
            $query->andFilterWhere(['like', 'title', $data['title']]);
        }

        $result = PaginationHelper::queryPage($query, $data['page'], $data['page_size']);
        if (!empty($result)) {
            foreach ($result['dataList'] as $key=>$item) {
                $isRead = false;
                $nr = $this->notice_read($data['member_id'], $item['id']);
                if ($nr != null) {
                    $isRead = true;
                }
                $result['dataList'][$key]['isRead'] = $isRead;
            }
        }
        return $this->outJson(1, $result);
    }

    /**
     * 公告详情
     */
    public function actionView()
    {
        $data = \Yii::$app->getRequest()->get();
        $data['id'] = isset($data['id']) ? (int)$data['id'] : '';
        $data['member_id'] = isset($data['member_id']) ? (int)$data['member_id'] : '';

        if ($data['id'] <= 0) {
            return $this->outJson(0, [], '未指定数据对象');
        }
        $notice = Notice::find()->where(['id' => $data['id']])->asArray()->limit(1)->one();
        if ($notice == null) {
            return $this->outJson(0, [], '数据不存在');
        }

        $isRead = $this->notice_read($data['member_id'], $data['id']);
        if ($isRead == null) {
            $noticeRead = new NoticeRead();
            $noticeRead->member_id = $data['member_id'];
            $noticeRead->notice_id = $data['id'];
            $noticeRead->save();
        }
        return $this->outJson(1, $notice);
    }

    /**
     * 获取未读公告数
     */
    public function actionNotReadCount()
    {
        $data = \Yii::$app->getRequest()->get();
        $data['member_id'] = isset($data['member_id']) ? (int)$data['member_id'] : '';

        $sql = "
        select count(*) from crm_notice a 
        where a.id not in(select notice_id from crm_notice_read where member_id=:member_id) 
        and a.type=:type
        ";
        $sqlParams[':member_id'] = $data['member_id'];
        $sqlParams[':type'] = 0;

        $num = intval(\Yii::$app->db->createCommand($sql, $sqlParams)->queryScalar());
        $this->outJson(1, ['unReadNum' => $num]);
    }

    /**
     * @title 公告已读未读判断
     * @param int $member_id
     * @param int $notice_id
     * @return array|null
     */
    public function notice_read($member_id, $notice_id)
    {
        return NoticeRead::find()
            ->where(['member_id' => $member_id, 'notice_id' => $notice_id])
            ->limit(1)->one();
    }
}
