184 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			184 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			PHP
		
	
	
<?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_on'];
 | 
						|
        $morningEnd     = $item['old_morning_off'];
 | 
						|
        $afternoonBegin = $item['old_afternoon_on'];
 | 
						|
        $afternoonEnd   = $item['old_afternoon_off'];
 | 
						|
 | 
						|
        $now = time();
 | 
						|
 | 
						|
        if ($now > $item['start_at']) {
 | 
						|
            $morningBegin   = $item['morning_on'];
 | 
						|
            $morningEnd     = $item['morning_off'];
 | 
						|
            $afternoonBegin = $item['afternoon_on'];
 | 
						|
            $afternoonEnd   = $item['afternoon_off'];
 | 
						|
        }
 | 
						|
 | 
						|
        // 时间戳
 | 
						|
        $morningBeginTs   = strtotime($morningBegin);
 | 
						|
        $morningEndTs     = strtotime($morningEnd);
 | 
						|
        $afternoonBeginTs = strtotime($afternoonBegin);
 | 
						|
        $afternoonEndTs   = strtotime($afternoonEnd);
 | 
						|
 | 
						|
        $result = false;
 | 
						|
        switch ($type) {
 | 
						|
            // 上午上班时间 打卡时间在上班前的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;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取工地工作时间
 | 
						|
     *
 | 
						|
     * @param  int  $worksiteId 工地ID
 | 
						|
     * @return array
 | 
						|
     * @throws \think\db\exception\DataNotFoundException
 | 
						|
     * @throws \think\db\exception\DbException
 | 
						|
     * @throws \think\db\exception\ModelNotFoundException
 | 
						|
     */
 | 
						|
    public static function worktime(int $worksiteId): array
 | 
						|
    {
 | 
						|
        if (!$worksite = self::where('id', $worksiteId)->find()) {
 | 
						|
            return [];
 | 
						|
        }
 | 
						|
 | 
						|
        $res = [
 | 
						|
            'morning_on'    => $worksite['morning_on'],
 | 
						|
            'morning_off'   => $worksite['morning_off'],
 | 
						|
            'afternoon_on'  => $worksite['afternoon_on'],
 | 
						|
            'afternoon_off' => $worksite['afternoon_off'],
 | 
						|
        ];
 | 
						|
 | 
						|
        // 当前时间小于start_at 使用old_系列时间
 | 
						|
        if ($worksite['start_at'] > time() && $worksite['start_at'] != 0) {
 | 
						|
            $res = [
 | 
						|
                'morning_on'    => $worksite['old_morning_on'],
 | 
						|
                'morning_off'   => $worksite['old_morning_off'],
 | 
						|
                'afternoon_on'  => $worksite['old_afternoon_on'],
 | 
						|
                'afternoon_off' => $worksite['old_afternoon_off'],
 | 
						|
            ];
 | 
						|
        }
 | 
						|
 | 
						|
        return $res;
 | 
						|
    }
 | 
						|
}
 |