<?php

namespace app\model;

use think\facade\Db;

/**
 * 工地表
 *
 * Class Worksite
 * @package app\model
 */
class Worksite extends Base
{
    public const STATUS_NO  = 0;//禁用
    public const STATUS_YES = 1;//正常

    public static function statusText(): array
    {
        return [
            self::STATUS_NO  => '禁用',
            self::STATUS_YES => '正常',
        ];
    }
    // 获取指定距离内由近及远的N条记录

    /**
     * @param  string  $lng  经度
     * @param  string  $lat  维度
     * @param  float|int  $area  距离 单位米 默认500
     * @param  int  $limit  条数
     * @return mixed
     */
    public static function getListByDistance(string $lng, string $lat, float $area = 500, int $limit = 10)
    {
        $sql = 'SELECT
                    `id`,
                    `name`,
                    `lat`,
                    `lng`,
                    st_distance ( point ( lng, lat ), point ( :lng, :lat ) )* 111195 AS distance 
                FROM
                    bee_worksite 
                HAVING
                    distance <= :area 
                ORDER BY
                    distance
                LIMIT :limit
                ';

        return Db::query($sql, ['lng' => $lng, 'lat' => $lat, 'area' => $area, 'limit' => $limit]);
    }

    // 获取指定范围内最近工地

    /**
     * 获取所在工地 默认按指定范围搜索最近
     *
     * @param  string  $lng  经度
     * @param  string  $lat  维度
     * @param  float|int  $area  范围 米 默认500
     * @return array|mixed
     */
    public static function getNearest(string $lng, string $lat, float $area = 500)
    {
        $sql = 'SELECT
                    `id`,
                    `name`,
                    `lat`,
                    `lng`,
                    st_distance ( point ( lng, lat ), point ( :lng, :lat ) )* 111195 AS distance 
                FROM
                    bee_worksite 
                HAVING
                    distance <= :area 
                ORDER BY
                    distance
                LIMIT 1
                ';

        $res = Db::query($sql, ['lng' => $lng, 'lat' => $lat, 'area' => $area]);
        return $res[0] ?? [];
    }

    public static function list()
    {
        return self::order('sort', 'desc')->order('id', 'desc')->column('name', 'id');
    }

    // 验证是否在打卡时间
    public static function checkSignTime(int $worksiteId, string $type): bool
    {
        if (!$item = self::find($worksiteId)) {
            return false;
        }
        $morningBegin   = $item['old_morning_begin'];
        $morningEnd     = $item['old_morning_end'];
        $afternoonBegin = $item['old_afternoon_begin'];
        $afternoonEnd   = $item['old_afternoon_end'];

        $now = time();

        if ($now > $item['start_at']) {
            $morningBegin   = $item['morning_begin'];
            $morningEnd     = $item['morning_end'];
            $afternoonBegin = $item['afternoon_begin'];
            $afternoonEnd   = $item['afternoon_end'];
        }

        // 时间戳
        $morningBeginTs   = strtotime($morningBegin);
        $morningEndTs     = strtotime($morningEnd);
        $afternoonBeginTs = strtotime($afternoonBegin);
        $afternoonEndTs   = strtotime($afternoonEnd);

        $result = false;
        switch ($type) {
            // 普通用户打卡  不限制时间
            case ClockLog::TYPE_NORMAL:
                $result = true;
                break;
            // 上午上班时间 打卡时间在上班前的10分钟
            case ClockLog::TYPE_MORNING_ON:
                if (($morningBeginTs - 10 * 60) <= $now && $now <= $morningBeginTs) {
                    $result = true;
                }
                break;
            // 上午下班时间 打卡时间在下班时间后 下次上班时间以前
            case ClockLog::TYPE_MORNING_OFF:
                if (($morningEndTs) <= $now && $now < $afternoonBeginTs) {
                    $result = true;
                }
                break;

            // 下午上班时间 打卡时间在上班前的10分钟
            case ClockLog::TYPE_AFTERNOON_ON:
                if (($afternoonBeginTs - 10 * 60) <= $now && $now <= $afternoonBeginTs) {
                    $result = true;
                }
                break;

            // 下午下班时间 打卡时间在下班时间后 当天23:59:59以前
            case ClockLog::TYPE_AFTERNOON_OFF:
                if (($afternoonEndTs) <= $now && $now < strtotime('23:59:59')) {
                    $result = true;
                }
                break;
        }

        return $result;
    }
}