Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

孙龙 / note-library

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • note-library
  • app
  • Model
  • CronLogModel.php
  • 孙龙's avatar
    init · 1f46a6ed
    孙龙 committed 5 years ago
    1f46a6ed
CronLogModel.php 4.92 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
<?php
namespace App\Model;

use Jenssegers\Mongodb\Eloquent\Model as Moloquent;

class CronLogModel extends Moloquent{    
    protected $connection = 'mongodb';  //库名    
    protected $collection = 'cron_log';     //文档名    
    protected $primaryKey = '_id';    //设置id    
    protected $guarded = ['_id'];  //设置字段黑名单
    public $timestamps = false;

    // 获取任务执行日志
    public function lists($request)
    {
    	$page  = $request->input('page', 1);
        $limit = $request->input('limit', 10);

        $map['job_name'] = $request->input('job_name', '');
        $map['plan_begin_time']       = $request->input('plan_begin_time', '') ? strtotime($request->input('plan_begin_time')) : '';
        $map['plan_end_time']         = $request->input('plan_end_time', '') ? strtotime($request->input('plan_end_time')) + 86399 : '';
        $map['schedule_begin_time']   = $request->input('schedule_begin_time', '') ? strtotime($request->input('schedule_begin_time')) : '';
        $map['schedule_end_time']     = $request->input('schedule_end_time', '') ? strtotime($request->input('schedule_end_time')) + 86399 : '';
        $map['exec_start_begin_time'] = $request->input('exec_start_begin_time', '') ? strtotime($request->input('exec_start_begin_time')) : '';
        $map['exec_start_end_time']   = $request->input('exec_start_end_time', '') ? strtotime($request->input('exec_start_end_time')) + 86399 : '';
        $map['exec_end_begin_time']   = $request->input('exec_end_begin_time', '') ? strtotime($request->input('exec_end_begin_time')) : '';
        $map['exec_end_end_time']     = $request->input('exec_end_end_time', '') ? strtotime($request->input('exec_end_end_time')) + 86399 : '';

        $list = $this->where('jobName', $map['job_name'])
                ->where(function($query) use($map) {
        			// 计划时间
                    if(!empty($map['plan_begin_time']) && !empty($map['plan_end_time'])) {  
                        $query->whereBetween('planTime', [$map['plan_begin_time'], $map['plan_end_time']]);
                    } else if(!empty($map['plan_begin_time'])) {
                        $query->where('planTime', '>=', $map['plan_begin_time']);
                    } else if(!empty($map['plan_end_time'])) {
                        $query->where('planTime', '<=', $map['plan_end_time']);
                    }

                    // 调度时间
                    if(!empty($map['schedule_begin_time']) && !empty($map['schedule_end_time'])) {  
                        $query->whereBetween('scheduleTime', [$map['schedule_begin_time'], $map['schedule_end_time']]);
                    } else if(!empty($map['schedule_begin_time'])) {
                        $query->where('scheduleTime', '>=', $map['schedule_begin_time']);
                    } else if(!empty($map['schedule_end_time'])) {
                        $query->where('scheduleTime', '<=', $map['schedule_end_time']);
                    }

                    // 执行时间
                    if(!empty($map['exec_start_begin_time']) && !empty($map['exec_start_end_time'])) {  
                        $query->whereBetween('startTime', [$map['exec_start_begin_time'], $map['exec_start_end_time']]);
                    } else if(!empty($map['exec_start_begin_time'])) {
                        $query->where('startTime', '>=', $map['exec_start_begin_time']);
                    } else if(!empty($map['exec_start_end_time'])) {
                        $query->where('startTime', '<=', $map['exec_start_end_time']);
                    }

                    // 执行时间
                    if(!empty($map['exec_end_begin_time']) && !empty($map['exec_end_end_time'])) {  
                        $query->whereBetween('endTime', [$map['exec_end_begin_time'], $map['exec_end_end_time']]);
                    } else if(!empty($map['exec_end_begin_time'])) {
                        $query->where('endTime', '>=', $map['exec_end_begin_time']);
                    } else if(!empty($map['exec_end_end_time'])) {
                        $query->where('endTime', '<=', $map['exec_end_end_time']);
                    }
        		})
        		->orderBy('endTime', 'DESC')
        		->paginate(intval($limit), ['*'], 'page', $page)
                ->toArray();

    	$data = $this->handleData($list['data']);

    	return ['code'=>0, 'msg'=>'获取成功', 'data'=>$data, 'count'=>$list['total']];
    }

    // 处理数据
    public function handleData($data)
    {
        if (empty($data)) return $data;

        $CmsModel = new CmsModel();

        foreach ($data as $k=>&$v) {
            $v['planTime']     = $v['planTime'] ? date('Y-m-d H:i:s', $v['planTime']) : '';
            $v['scheduleTime'] = $v['scheduleTime'] ? date('Y-m-d H:i:s', $v['scheduleTime']) : '';
            $v['startTime']    = $v['startTime'] ? date('Y-m-d H:i:s', $v['startTime']) : '';
            $v['endTime']      = $v['endTime'] ? date('Y-m-d H:i:s', $v['endTime']) : '';
		}

        return $data;
    }

}