<?php

class meetingClassModel extends Model
{

    public $hyarra, $ytarra, $hyarrb;

    public function initModel()
    {
        $this->hyarra = array('正常', '会议中', '结束', '取消');
        $this->ytarra = array('等待', '待进行', '进行中', '已结束');
        $this->hyarrb = array('green', 'blue', '#ff6600', '#888888');
    }

    /**
     *	判断会议室是否重复申请了
     */


    public function isapplymsg($startdt, $enddt, $hyname, $id=0, $meetType=1)
    {
        $msg 		= '';
        if ($meetType == 1) {
            $sql        =  /** @lang text */
                'select id, meeting_room, start_time, end_time, title
                   from xinhu_meeting  where id <> '.$id.' and meet_state in (0, 1)
                 union all
                 select id, meeting_room, start_time, end_time, title
                   from xinhu_meeting_key where meet_state in (0, 1)';
        } else {
            $sql        =  /** @lang text */
                'select id, meeting_room, start_time, end_time, title
                   from xinhu_meeting  where meet_state in (0, 1)
                 union all
                 select id, meeting_room, start_time, end_time, title 
                   from xinhu_meeting_key where id <> '.$id.' and meet_state in (0, 1)';
        }
        m('log')->addlog("验证", $sql);

        $rows 		= $this->db->getall($sql);

        foreach($rows as $k=>$rs){
            if($rs['meeting_room'] != $hyname)continue;
            $sdt = $rs['start_time'];
            $edt = $rs['end_time'];
            m('log')->addlog("验证", json_encode($rs));
            if(
                ($sdt<=$startdt && $edt>$startdt)
                || ($sdt<$enddt && $edt>=$enddt)
                || ($sdt>$startdt && $edt<$enddt)
                || ($sdt==$startdt && $edt==$enddt)
            )$msg = '该会议室的时间段已被申请过了，主题“'.$rs['title'].'”';
        }
        return $msg;
    }

    public function firstMeetingTopicStart($meeting_id) {

        $topic = m('meeting_topics')->getall("mid={$meeting_id}", "id", 'sort');
        if (isset($topic[0]['id'])) {
            m('meeting_topics')->update(['topic_state'=>'2'], 'id='.$topic[0]['id']);
        }
        if (isset($topic[1]['id'])) {
            m('meeting_topics')->update(['topic_state'=>'1'], 'id='.$topic[1]['id']);
        }
    }

    // 会议室状态
    public function getstatezt($zt)
    {
        if (isset($this->hyarrb[$zt])) {
            $html = '<font color="'.$this->hyarrb[$zt].'">'.$this->hyarra[$zt].'</font>';
            return $html;
        } else {
            return null;
        }
    }


    // 会议状态
    public function meetingState($stime, $etime, $type=1) {
        $code = [0, 1, 2];
        $info = m('meeting')->hyarra;

        if ($stime > date("Y-m-d H:i:s")) {
            // 开始时间比当前时间大——未开始
            return $type == 1 ? $code[0] : $info[0];
        } else if ($etime < date("Y-m-d H:i:s")) {
            // 当前时间 比 结束时间比——已结束
            return $type == 1 ? $code[2] : $info[2];
        } else {
            // 在时间段内——会议中
            return $type == 1 ? $code[1] : $info[1];
        }
    }
}