<?php

use app\model\StaffFace;
use app\p\logic\Equipment as EquipmentLogic;
use think\Db;

define('APP_PATH', '/workspace/zhct/zhctproject/store/application/');
require '/workspace/zhct/zhctproject/store/env.php';
require '/workspace/zhct/zhctproject/store/thinkphp/base.php';
\think\App::initCommon();

$now = time();
$startTime = date('Y-m-d H:i:s');
$deviceCodes = [
    'new_bang_01',
    'mqtt',
    'pad_one_001',
    'face1',
    '206test',
    'dianca01',
    'asd',
];

$originalHearttime = Db::name('equipment')
    ->whereIn('code', $deviceCodes)
    ->column('hearttime', 'code');

try {
    Db::name('equipment')
        ->whereIn('code', $deviceCodes)
        ->update([
            'mate_flag' => 1,
            'hearttime' => $now,
            'update_time' => date('Y-m-d H:i:s'),
        ]);

    $staff = Db::name('staff')
        ->where('uuid', 'FDF0B2F4-AADD-816C-DB41-991210CF8F86')
        ->field('id,uuid,name')
        ->find();
    if (empty($staff)) {
        throw new RuntimeException('test staff not found');
    }

    $staffFace = new StaffFace();
    $staffFace->modifyFace([
        'staff_uuid' => $staff['uuid'],
        'hardware_img_uuid' => 'codex-mqtt-face-img',
        'hardware_uuid' => 'codexmqttfacehardware',
        'head_img_url' => '/static/upload/codex/mqtt-face.jpg',
        'feature' => 'codex_fake_feature_for_mqtt_path_check',
        'group_id' => 'codex_mqtt',
        'baidu_user_id' => (string)$staff['id'],
        'baidu_face_token' => 'codex_face_token_20260612',
        'is_del' => 0,
        'source' => 3,
    ], $staff);

    $equipmentLogic = new EquipmentLogic();
    $equipment = Db::name('equipment')
        ->where('code', 'asd')
        ->where('del_flag', 0)
        ->find();
    if (empty($equipment)) {
        throw new RuntimeException('test weigh equipment not found');
    }
    $equipmentLogic->pushDishesMsg(
        $equipment,
        'ECD50FF2-7B1D-A1DB-C229-B1BD4215242C',
        ''
    );

    $rows = Db::name('mq_message')
        ->where('create_time', '>=', $startTime)
        ->where(function ($query) use ($staff) {
            $query->where('staff_uuid', $staff['uuid'])
                ->whereOr('receive_device_id', 'asd');
        })
        ->field('id,type,receive_device_id,staff_uuid,status,publish_num,create_time,message')
        ->order('id', 'desc')
        ->select();

    echo json_encode([
        'code' => 0,
        'start_time' => $startTime,
        'rows' => $rows,
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), PHP_EOL;
} catch (Throwable $throwable) {
    echo json_encode([
        'code' => 1,
        'message' => $throwable->getMessage(),
        'file' => $throwable->getFile(),
        'line' => $throwable->getLine(),
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), PHP_EOL;
} finally {
    foreach ($originalHearttime as $code => $hearttime) {
        Db::name('equipment')
            ->where('code', $code)
            ->update([
                'hearttime' => $hearttime,
                'update_time' => date('Y-m-d H:i:s'),
            ]);
    }
}
