<?php
/**
 * Created by PhpStorm.
 * User: wuzhenke
 * Date: 2018/12/4
 * Time: 13:47
 */

namespace App\Repositories;

use App\Models\Pms;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use Encore\Admin\Facades\Admin;

class PmsRepository extends BaseRepository
{

    public function model()
    {
        return Pms::class;
    }

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    public function comPms($where)
    {
        return $this->model->where($where)->where(function ($query) {
            $query->where('started_at', '<=', strtotime(date("Y-m-d", time())))->where('ended_at', '>', strtotime(date("Y-m-d", time()))+60*60*24);
            $query->orwhere(['started_at'=>0,'ended_at'=>0]);
        })->orderBy('created_at', 'desc')->paginate(10, ['*']);
    }
    //弹窗消息
    public function findFirstPms($where)
    {
        return $this->model->where($where)->orderBy('started_at', 'asc')->first();
    }
    public function findMsg($ids)
    {
        return $this->model->whereIn('id', $ids)->first();
    }

    public function updateNew($ids, $where, $data)
    {
        return $this->model->whereIn('id', $ids)->where($where)->update($data);
    }
    public function writePmsNotice($user, $message)
    {
        $insert_data = array(
            'utype'      => $user->utype,
            'msgtype'    => 1,
            'msgfromuid'=> Admin::user()->id,
            'msgfrom'   => Admin::user()->username,
            'msgtoname' => $user->username,
            'msgtouid'  => $user->uid,
            'message'   => $message,
            'new'       => 1
        );
        return $this->model->create($insert_data);
    }
    public function deleteAll($ids,$company_id = null)
    {
        if($company_id){
            return $this->model->where("msgtouid",$company_id)->whereIn("id",$ids)->delete();
        }else{
            return $this->model->whereIn("id",$ids)->delete();
        }

    }

    public function getPms($where)
    {
        return $this->model->where($where)->where(function ($query){
            $query->where('started_at','<=',strtotime(date("Y-m-d", time())))->where('ended_at','>',strtotime(date("Y-m-d", time()))+60*60*24);
            $query->orwhere(['started_at'=>0,'ended_at'=>0]);
        })->orderBy('id', 'desc')->paginate(10);
    }

    public function getPmsCount($where)
    {
        return $this->model->where($where)->where(function ($query) {
            $query->where('started_at', '<=', strtotime(date("Y-m-d", time())))->where('ended_at', '>', strtotime(date("Y-m-d", time()))+60*60*24);
            $query->orwhere(['started_at'=>0,'ended_at'=>0]);
        })->count();
    }
    public function getPmsById($id)
    {
        return $this->model->find($id);
    }
    public function updateNewById($id, $data)
    {
        return $this->model->where('id', $id)->update($data);
    }

    public function getPmsCountByUid($where)
    {
        return $this->model->where($where)->count();
    }

    public function getNewestPms($where)
    {
        return $this->model->withTrashed()->where($where)->orderBy('id', 'desc')->first();
    }

    public function insertData($data)
    {
        return $this->model->insert($data);
    }
}