<?php

namespace app\api\controller\v1;

use addons\webman\model\AppVersion;
use Exception;
use support\Request;
use support\Response;

class VersionController
{
    /**
     * 登录接口
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function getAppUpdate(Request $request): Response
    {
        // 更新比较时，查询的是版本标识，大于传入的版本标识时，返回新版本的数据
        $version_key = $request->header('app-version-key', '');
        // 传入检测的系统
        $system_key = $request->header('system-key', '');
        if (empty($version_key) || empty($system_key)) {
            return jsonFailResponse(trans('incomplete_parameters', [], 'message'));
        }
        $data = AppVersion::query()
            ->where('app_version_key', '>=', $version_key)
            ->where(['status' => 1])
            ->where('status', 1)
            ->where('system_key', $system_key)
            ->where('department_id', request()->department_id)
            ->whereDate('regular_update', '<', date("Y-m-d H:i:s", time()))
            ->select(['id', 'system_key', 'app_version', 'app_version_key', 'apk_url', 'hot_update_url', 'force_update', 'hot_update', 'regular_update', 'update_content', 'notes'])
            ->orderBy('id', 'desc')
            ->first();

        return jsonSuccessResponse('success', [
            'data' => $data
        ]);
    }
}