| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- namespace App\Console\Commands;
- use App\Facades\Servers\Logs\Log;
- use App\Models\CollectProduct;
- use App\Models\CollectEquipment;
- use App\Models\CollectTask;
- use App\Models\CollectTaskAllocate;
- use App\Models\CollectRegion;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- class CollectTaskCommand extends Command
- {
- protected $signature = 'collect-task';
- protected $description = '定时分配采集任务设备';
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- Log::info('collect-task','分配采集任务开始');
- $CollectTaskModel = new CollectTask();
- $endTime = strtotime(date('Y-m-d',time())) + 86400;
- $time = time();
- $week = date('w');
- $map = [
- ['status','=',0],
- ['sampling_start_time','<=',$time],
- ];
- $collectProductList = CollectProduct::query()
- ->where($map)
- ->where(function ($query) use ($time) {
- $query->where('sampling_end_time', '>=', $time)
- ->orWhere('sampling_end_time', 0);
- })
- ->get()
- ->toArray();
- if ($collectProductList){
- $region = [];
- $region = CollectRegion::query()->value('id');
- foreach ($collectProductList as $collectProduct){
- if ($collectProduct['sampling_cycle']){
- $weekArray = explode(',',$collectProduct['sampling_cycle']);
- if (!in_array($week,$weekArray)){
- continue;
- }
- //查询任务最新执行时间
- $collectTask = $CollectTaskModel::query()->where([['collect_product_id','=',$collectProduct['id']]])->select('insert_time')->orderBy('insert_time','desc')->first();
- if($collectTask){
- if (date('Y-m-d',time()) == date('Y-m-d',$collectTask['insert_time'])){
- continue;
- }
- }
- }
- if (!$collectProduct['platform']){
- continue;
- }
- $data = [];
- $platforms = explode(',',$collectProduct['platform']);
- if ($collectProduct['round_number'] > 1 && $collectProduct['round_number_config']){
- $data = $this->collectTask($collectProduct,$platforms,$region,$collectProduct['round_number_config']);
- }else{
- $data = $this->collectTask($collectProduct,$platforms,$region);
- }
- //添加到任务
- $res = $CollectTaskModel::query()->insert($data);
- if (!$res){
- Log::info('collect-task','添加采集任务失败');
- }
- }
- }else{
- Log::info('collect-task','暂无采集任务');
- }
- Log::info('collect-task','分配采集任务结束');
- return 0;
- }
- public function collectTask($collectProduct,$platforms,$region,$roundNumberConfig = null){
- $data = [];
- if ($roundNumberConfig){
- $roundNumberConfig = json_decode($roundNumberConfig,true);
- foreach ($roundNumberConfig as $roundNumber){
- foreach ($platforms as $platform){
- if ($collectProduct['region'] > 0 && $region){
- foreach ($region as $regionId){
- $data[] = [
- 'collect_product_id' => $collectProduct['id'],
- 'platform' => $platform,
- 'product_name' => $collectProduct['product_name'] ?? '',
- 'product_specs' => $collectProduct['product_specs'] ?? '',
- 'product_brand' => $collectProduct['product_brand'] ?? '',
- 'company_id' => $collectProduct['company_id'],
- 'sampling_cycle' => $collectProduct['sampling_cycle'],
- 'sampling_start_time' => $collectProduct['sampling_start_time'],
- 'sampling_end_time' => $collectProduct['sampling_end_time'],
- 'max_equipment_number' => 1,
- 'status' => 0,
- 'insert_time' => time(),
- 'region_id' => $regionId,
- 'round_start_time' => $roundNumber['round_start_time'],
- 'round_end_time' => $roundNumber['round_end_time'],
- ];
- }
- }else{
- $data[] = [
- 'collect_product_id' => $collectProduct['id'],
- 'platform' => $platform,
- 'product_name' => $collectProduct['product_name'] ?? '',
- 'product_specs' => $collectProduct['product_specs'] ?? '',
- 'product_brand' => $collectProduct['product_brand'] ?? '',
- 'company_id' => $collectProduct['company_id'],
- 'sampling_cycle' => $collectProduct['sampling_cycle'],
- 'sampling_start_time' => $collectProduct['sampling_start_time'],
- 'sampling_end_time' => $collectProduct['sampling_end_time'],
- 'max_equipment_number' => 1,
- 'status' => 0,
- 'insert_time' => time(),
- 'region_id' => $collectProduct['region'],
- 'round_start_time' => $roundNumber['round_start_time'],
- 'round_end_time' => $roundNumber['round_end_time'],
- ];
- }
- }
- }
- }else{
- foreach ($platforms as $platform){
- if ($collectProduct['region'] > 0 && $region){
- foreach ($region as $regionId){
- $data[] = [
- 'collect_product_id' => $collectProduct['id'],
- 'platform' => $platform,
- 'product_name' => $collectProduct['product_name'] ?? '',
- 'product_specs' => $collectProduct['product_specs'] ?? '',
- 'product_brand' => $collectProduct['product_brand'] ?? '',
- 'company_id' => $collectProduct['company_id'],
- 'sampling_cycle' => $collectProduct['sampling_cycle'],
- 'sampling_start_time' => $collectProduct['sampling_start_time'],
- 'sampling_end_time' => $collectProduct['sampling_end_time'],
- 'max_equipment_number' => 1,
- 'status' => 0,
- 'insert_time' => time(),
- 'region_id' => $regionId,
- ];
- }
- }else{
- $data[] = [
- 'collect_product_id' => $collectProduct['id'],
- 'platform' => $platform,
- 'product_name' => $collectProduct['product_name'] ?? '',
- 'product_specs' => $collectProduct['product_specs'] ?? '',
- 'product_brand' => $collectProduct['product_brand'] ?? '',
- 'company_id' => $collectProduct['company_id'],
- 'sampling_cycle' => $collectProduct['sampling_cycle'],
- 'sampling_start_time' => $collectProduct['sampling_start_time'],
- 'sampling_end_time' => $collectProduct['sampling_end_time'],
- 'max_equipment_number' => 1,
- 'status' => 0,
- 'insert_time' => time(),
- 'region_id' => $collectProduct['region'],
- ];
- }
- }
- }
- return $data;
- }
- }
|