<?php
namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\admin\model\OperationData as OperationDataModel;
use app\admin\model\KefuTime;



/**
 * Worker 命令行类
 */
class OperationData extends Command
{
    public function configure()
    {
        $this->setName('operation:data')
            ->setDescription('每日0点统计前一天的数据');
    }

    public function execute(Input $input, Output $output)
    {
        //结束前一天客服服务时间（未结束的，今日重新开始）
        // $list = KefuTime::where('status', 0)->where("created_at", '<', date("Y-m-d"))->order('id', 'desc')->select()->toArray();
        // foreach($list as $item) {
        //     KefuTime::where('id', $item['id'])->update([
        //         'status' => 1,
        //         'end_time' => time(),
        //     ]);
        //     KefuTime::addData($item['admin_id'], $item['type']);
        // }

        //统计前一天的接线总数
        $exists = OperationDataModel::where('type', 1)->where("date", date("Y-m-d", strtotime("-1 day")))->find();
        if (!$exists) {
            OperationDataModel::create([
                'type' => 1,
                'num' => $this->chatNum(),
                'date' => date('Y-m-d', strtotime('-1 day')),
            ]);
        }
    }

    /**
     * 统计前一天的接线总数
     */
    public function chatNum()
    {
        return  KefuTime::where('type', 3)
                    ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-1 day')))
                    ->where('created_at', '<=', date('Y-m-d 23:59:59', strtotime('-1 day')))
                    ->count();
    }

}