<?php

namespace app\admin\model;

use think\Model;

/**
 * 客服上线下线日志模型
 */
class Servicetimelog extends Model
{

    //按日期用户查
    //
    public function getDayData($dataArray, $uidArray = [])
    {
        $ret = $this->where(['cdate' => ['IN', $dataArray]]);
        if ($uidArray) {
            $ret = $ret->where(['kfid' => ['IN', $uidArray]]);
        }
        $ret = $ret->order(['kfid' => 'asc', 'cdate' => 'asc', 'ctime' => 'asc'])->select();


        if (!$ret) {
            return false;
        }

        $return = $this->getOneUserData($ret);
        return $return;
    }

    private function getOneUserData($array)
    {

        $return = [];
        foreach ($array as $val) {
            if (!isset($return[$val->cdate][$val->kfid]['1'])) {
                $return[$val->cdate][$val->kfid]['1'] = $val->cdate . ' ' . $val->ctime;
            } else {
                if ($return[$val->cdate][$val->kfid]['1'] > $val->cdate . ' ' . $val->ctime) {
                    $return[$val->cdate][$val->kfid]['1'] = $val->cdate . ' ' . $val->ctime;
                }
            }

            if (!isset($return[$val->cdate][$val->kfid]['0'])) {
                $return[$val->cdate][$val->kfid]['0'] = $val->cdate . ' ' . $val->ctime;
            } else {
                if ($return[$val->cdate][$val->kfid]['0'] < $val->cdate . ' ' . $val->ctime) {
                    $return[$val->cdate][$val->kfid]['0'] = $val->cdate . ' ' . $val->ctime;
                }
            }
        }

        return $return;
    }


}
