<?php


namespace App\Web\Models;



use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;


class MeetingSpeechScheduleModel extends BaseModel
{
    /**
     * 状态字段
     */
    const DELETED_AT = 'status';
    /**
     * @var string
     */
    protected $table = 'meeting_speech_schedule';


    /**
     * 保存日程
     * */
    public function saveSpeechScheduleData($meetingId,$speechScheduleData=array(),$userId=0){
        $time=nowTime();
        $ids=[];
        if(!empty($speechScheduleData)){
            foreach ($speechScheduleData as $item){
                $saveData=[
                    'theme'=>!empty($item['theme'])?$item['theme']:'',
                    'address'=>!empty($item['address'])?$item['address']:'',
                    'title'=>!empty($item['title'])?$item['title']:'',
                    'description'=>!empty($item['description'])?$item['description']:'',
                    'cover_url'=>!empty($item['cover_url'])?$item['cover_url']:'',
                    'cover_alt'=>!empty($item['cover_alt'])?$item['cover_alt']:'',
                    'expert_name'=>!empty($item['expert_name'])?$item['expert_name']:'',
                    'expert_des'=>!empty($item['expert_des'])?$item['expert_des']:'',
                    'expert_expert'=>!empty($item['expert_expert'])?$item['expert_expert']:'',
                    'start_date'=>!empty($item['start_date'])?$item['start_date']:'',
                    'end_date'=>!empty($item['end_date'])?$item['end_date']:'',
                    'update_time'=>$time,
                ];
                if (!empty($item['expand_content'])) {
                    $saveData['expand_content'] = empty($item['expand_content'])?'':json_encode($item['expand_content']);
                }
                if(!empty($item['id'])){
                    $saveData['id']=$item['id'];
                    $ids[]=$saveData['id'];
                    $this->newInstance()->where('id', $saveData['id'])->update($saveData);
                }else{
                    $saveData['user_id']=$userId;
                    $saveData['meeting_id']=$meetingId;
                    $saveData['create_time']=$time;
                    $saveData['update_time']=$time;
//                    Log::info($saveData);
                    $id = $this->newInstance()->insertGetId($saveData);
                    $ids[]=$id;
                }
            }
            //删除移除的权限
            $this->where('meeting_id',$meetingId)->where('status',0)
                ->WhereNotIn('id',$ids)
                ->update(['status'=>2,'update_time'=>$time]);
        }else{
            $this->where('meeting_id',$meetingId)->where('status',0)
                ->update(['status'=>2,'update_time'=>$time]);
        }
        return $meetingId;
    }

    /**
     * 获取产品类型ids
     * */
    public function getSpeechScheduleByMeetingId($meetingId){
        $typeIds= $this->newInstance()
            ->where('meeting_id','=',$meetingId)
            ->where('status','<',2)
            ->get()->toArray();
        return $typeIds;
    }



}
