CollectTaskCommand.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Facades\Servers\Logs\Log;
  4. use App\Models\CollectProduct;
  5. use App\Models\CollectEquipment;
  6. use App\Models\CollectTask;
  7. use App\Models\CollectTaskAllocate;
  8. use App\Models\CollectRegion;
  9. use Carbon\Carbon;
  10. use Illuminate\Console\Command;
  11. class CollectTaskCommand extends Command
  12. {
  13. protected $signature = 'collect-task';
  14. protected $description = '定时分配采集任务设备';
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. }
  19. public function handle()
  20. {
  21. Log::info('collect-task','分配采集任务开始');
  22. $CollectTaskModel = new CollectTask();
  23. $endTime = strtotime(date('Y-m-d',time())) + 86400;
  24. $time = time();
  25. $week = date('w');
  26. $map = [
  27. ['status','=',0],
  28. ['sampling_start_time','<=',$time],
  29. ];
  30. $collectProductList = CollectProduct::query()
  31. ->where($map)
  32. ->where(function ($query) use ($time) {
  33. $query->where('sampling_end_time', '>=', $time)
  34. ->orWhere('sampling_end_time', 0);
  35. })
  36. ->get()
  37. ->toArray();
  38. if ($collectProductList){
  39. $region = [];
  40. $region = CollectRegion::query()->value('id');
  41. foreach ($collectProductList as $collectProduct){
  42. if ($collectProduct['sampling_cycle']){
  43. $weekArray = explode(',',$collectProduct['sampling_cycle']);
  44. if (!in_array($week,$weekArray)){
  45. continue;
  46. }
  47. //查询任务最新执行时间
  48. $collectTask = $CollectTaskModel::query()->where([['collect_product_id','=',$collectProduct['id']]])->select('insert_time')->orderBy('insert_time','desc')->first();
  49. if($collectTask){
  50. if (date('Y-m-d',time()) == date('Y-m-d',$collectTask['insert_time'])){
  51. continue;
  52. }
  53. }
  54. }
  55. if (!$collectProduct['platform']){
  56. continue;
  57. }
  58. $data = [];
  59. $platforms = explode(',',$collectProduct['platform']);
  60. if ($collectProduct['round_number'] > 1 && $collectProduct['round_number_config']){
  61. $data = $this->collectTask($collectProduct,$platforms,$region,$collectProduct['round_number_config']);
  62. }else{
  63. $data = $this->collectTask($collectProduct,$platforms,$region);
  64. }
  65. //添加到任务
  66. $res = $CollectTaskModel::query()->insert($data);
  67. if (!$res){
  68. Log::info('collect-task','添加采集任务失败');
  69. }
  70. }
  71. }else{
  72. Log::info('collect-task','暂无采集任务');
  73. }
  74. Log::info('collect-task','分配采集任务结束');
  75. return 0;
  76. }
  77. public function collectTask($collectProduct,$platforms,$region,$roundNumberConfig = null){
  78. $data = [];
  79. if ($roundNumberConfig){
  80. $roundNumberConfig = json_decode($roundNumberConfig,true);
  81. foreach ($roundNumberConfig as $roundNumber){
  82. foreach ($platforms as $platform){
  83. if ($collectProduct['region'] > 0 && $region){
  84. foreach ($region as $regionId){
  85. $data[] = [
  86. 'collect_product_id' => $collectProduct['id'],
  87. 'platform' => $platform,
  88. 'product_name' => $collectProduct['product_name'] ?? '',
  89. 'product_specs' => $collectProduct['product_specs'] ?? '',
  90. 'product_brand' => $collectProduct['product_brand'] ?? '',
  91. 'company_id' => $collectProduct['company_id'],
  92. 'sampling_cycle' => $collectProduct['sampling_cycle'],
  93. 'sampling_start_time' => $collectProduct['sampling_start_time'],
  94. 'sampling_end_time' => $collectProduct['sampling_end_time'],
  95. 'max_equipment_number' => 1,
  96. 'status' => 0,
  97. 'insert_time' => time(),
  98. 'region_id' => $regionId,
  99. 'round_start_time' => $roundNumber['round_start_time'],
  100. 'round_end_time' => $roundNumber['round_end_time'],
  101. ];
  102. }
  103. }else{
  104. $data[] = [
  105. 'collect_product_id' => $collectProduct['id'],
  106. 'platform' => $platform,
  107. 'product_name' => $collectProduct['product_name'] ?? '',
  108. 'product_specs' => $collectProduct['product_specs'] ?? '',
  109. 'product_brand' => $collectProduct['product_brand'] ?? '',
  110. 'company_id' => $collectProduct['company_id'],
  111. 'sampling_cycle' => $collectProduct['sampling_cycle'],
  112. 'sampling_start_time' => $collectProduct['sampling_start_time'],
  113. 'sampling_end_time' => $collectProduct['sampling_end_time'],
  114. 'max_equipment_number' => 1,
  115. 'status' => 0,
  116. 'insert_time' => time(),
  117. 'region_id' => $collectProduct['region'],
  118. 'round_start_time' => $roundNumber['round_start_time'],
  119. 'round_end_time' => $roundNumber['round_end_time'],
  120. ];
  121. }
  122. }
  123. }
  124. }else{
  125. foreach ($platforms as $platform){
  126. if ($collectProduct['region'] > 0 && $region){
  127. foreach ($region as $regionId){
  128. $data[] = [
  129. 'collect_product_id' => $collectProduct['id'],
  130. 'platform' => $platform,
  131. 'product_name' => $collectProduct['product_name'] ?? '',
  132. 'product_specs' => $collectProduct['product_specs'] ?? '',
  133. 'product_brand' => $collectProduct['product_brand'] ?? '',
  134. 'company_id' => $collectProduct['company_id'],
  135. 'sampling_cycle' => $collectProduct['sampling_cycle'],
  136. 'sampling_start_time' => $collectProduct['sampling_start_time'],
  137. 'sampling_end_time' => $collectProduct['sampling_end_time'],
  138. 'max_equipment_number' => 1,
  139. 'status' => 0,
  140. 'insert_time' => time(),
  141. 'region_id' => $regionId,
  142. ];
  143. }
  144. }else{
  145. $data[] = [
  146. 'collect_product_id' => $collectProduct['id'],
  147. 'platform' => $platform,
  148. 'product_name' => $collectProduct['product_name'] ?? '',
  149. 'product_specs' => $collectProduct['product_specs'] ?? '',
  150. 'product_brand' => $collectProduct['product_brand'] ?? '',
  151. 'company_id' => $collectProduct['company_id'],
  152. 'sampling_cycle' => $collectProduct['sampling_cycle'],
  153. 'sampling_start_time' => $collectProduct['sampling_start_time'],
  154. 'sampling_end_time' => $collectProduct['sampling_end_time'],
  155. 'max_equipment_number' => 1,
  156. 'status' => 0,
  157. 'insert_time' => time(),
  158. 'region_id' => $collectProduct['region'],
  159. ];
  160. }
  161. }
  162. }
  163. return $data;
  164. }
  165. }