<?php

namespace app\admin\model;

use think\Model;

/**
 * 客服状态改变时长模型
 */
class Kfstatetimes extends Model
{
    public function getDayData($dataArray, $uidArray = [])
    {
        $ret = $this->where(['sday' => ['IN', $dataArray]]);
        if ($uidArray) {
            $ret = $ret->where(['kfuid' => ['IN', $uidArray]]);
        }
        $ret = $ret->order(['kfuid' => 'asc'])->select();
        if (!$ret) {
            return false;
        }

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

        return $return;
    }

    private function getOneUserData($array)
    {
        $return = [];
        foreach ($array as $val) {
            $return[$val->sday][$val->kfuid]['s'.$val->kstatus] = $val->stime;
        }

        foreach ($return as $sday => $sval) {
            foreach ($sval as $kfuid => $tval) {

                if (!isset($tval['s0'])) {
                    $return[$sday][$kfuid]['s0'] = 0;
                }

                if (!isset($tval['s1'])) {
                    $return[$sday][$kfuid]['s1'] = 0;
                }
                if (!isset($tval['s2'])) {
                    $return[$sday][$kfuid]['s2'] = 0;
                }
                if (!isset($tval['s3'])) {
                    $return[$sday][$kfuid]['s3'] = 0;
                }

                $return[$sday][$kfuid]['all'] = intval($return[$sday][$kfuid]['s1']) + intval($return[$sday][$kfuid]['s2']) + intval($return[$sday][$kfuid]['s3']);

                if ($return[$sday][$kfuid]['all'] > 0) {
                    $return[$sday][$kfuid]['s1p'] = floatPointDigit($return[$sday][$kfuid]['s1'] / $return[$sday][$kfuid]['all']);
                    $return[$sday][$kfuid]['s2p'] = floatPointDigit($return[$sday][$kfuid]['s2'] / $return[$sday][$kfuid]['all']);
                    $return[$sday][$kfuid]['s3p'] = floatPointDigit($return[$sday][$kfuid]['s3'] / $return[$sday][$kfuid]['all']);
                } else {
                    $return[$sday][$kfuid]['s1p'] = 0;
                    $return[$sday][$kfuid]['s2p'] = 0;
                    $return[$sday][$kfuid]['s3p'] = 0;
                }
            }
        }

        return $return;
    }


}
