WokerTask/app/api/controller/CronController.php

219 lines
8.7 KiB
PHP

<?php
namespace app\api\controller;
use app\common\model\task_scheduling_plan\TaskSchedulingPlan;
use app\job\TaskInformationJob;
use app\job\TownTaskAdd;
use app\job\TownTaskSettlementJob;
use app\job\VillageTaskAdd;
use app\job\VillageTaskSettlementJob;
use think\facade\Log;
use app\common\model\Company;
use app\common\model\task_template\TaskTemplate;
use app\common\model\task_scheduling\TaskScheduling;
use app\job\TaskAdd;
/**
* index
* Class IndexController
* @package app\api\controller
*/
class CronController extends BaseApiController
{
public array $notNeedLogin = ['settlement','task_add', 'town_task_add', 'town_task_settlement','village_task_add', 'village_task_settlement'];
/**
* 小组服务公司任务下发
*/
public function task_add(){
//任务下发
// $time=strtotime(date('Y-m-d',strtotime('-1 day')));
$time = strtotime(date('Y-m-d'));
// $tiem_end=$time+86399;
// $plan_all = TaskScheduling::where('cron_time', '<', $time)->where('status', 1)->with('company_info')->select()->toArray();
$plan_all = TaskScheduling::where('cron_time', '<', $time)->where('status', 1)->where('company_type', 18)->with('company_info')->select()->toArray();
$plan_ids = [];
foreach ($plan_all as $k => $v) {
$all = TaskTemplate::where('status', 1)->where('task_scheduling', $v['id'])->limit(30)->select()->toArray();
$plan_ids[] = $v['id'];
if ($all) {
$plan_all[$k]['template'] = $all;
} else {
unset($plan_all[$k]);
}
}
$company_id = [];
foreach ($plan_all as $k => $v) {
foreach ($v['template'] as $kk => $vv) {
queue(TaskAdd::class,['data'=>$vv,'data_two'=>$v]);
}
$company_id[] = $v['company_id'];
}
Company::where('id', 'in', $company_id)->inc('day_count')->update();
TaskScheduling::where('id', 'in', $plan_ids)->update(['cron_time' => time()]);
Log::info('小组服务公司定时任务下发执行成功' . date('Y-m-d H:i:s'));
return $this->success('定时任务下发执行成功');
}
/**
* 小组服务公司任务结算
*/
public function settlement(){
// $all=TaskSchedulingPlan::where('is_pay',0)->with(['template_info','scheduling'])->select()->toArray();
$all = TaskSchedulingPlan::whereDay('end_time','today')
->withJoin(['scheduling'], 'left')
->where('scheduling.company_type', 18)
->where('is_pay',0)
->with(['template_info'])
->select()
->toArray();
foreach($all as $k=>$v){
// 任务运行中,后台删除了任务模板
if (empty($v['template_info'])) {
continue;
}
queue(TaskInformationJob::class,$v);
}
Log::info('小组服务公司定时任务结算执行成功'.date('Y-m-d H:i:s'));
return $this->success('小组服务公司定时任务结算执行成功');
}
/**
* 村管理公司任务下发
*/
public function village_task_add()
{
//任务下发
$time = strtotime(date('Y-m-d'));
// 查询系统 所有镇农科公司 未下发 的 任务安排
$taskSchedulingList = TaskScheduling::where('cron_time', '<', $time)
->where('status', 1)
->where('company_type', 17)
->with('company_info')
->select()
->toArray();
$taskSchedulingIds = [];
$companyIds = [];
foreach ($taskSchedulingList as $k => $taskScheduling) {
$templateList = TaskTemplate::where('status', 1)->where('task_scheduling', $taskScheduling['id'])->limit(30)->select()->toArray();
$taskSchedulingIds[] = $taskScheduling['id'];
$companyIds[] = $taskScheduling['company_id'];
foreach ($templateList as $template) {
queue(VillageTaskAdd::class, $template);
// TaskLogic::TownCronAdd($template); // 手动下发用
}
if (empty($templateList)) {
unset($taskSchedulingList[$k]);
}
}
Company::where('id', 'in', $companyIds)->inc('day_count')->update();
TaskScheduling::where('id', 'in', $taskSchedulingIds)->update(['cron_time' => time()]);
Log::info('村管理公司定时任务下发执行成功' . date('Y-m-d H:i:s'));
return $this->success('村管理公司定时任务下发执行成功');
}
/**
* 村管理公司任务结算
*/
public function village_task_settlement()
{
Log::info('村管理公司定时任务结算执行-开始'.date('Y-m-d H:i:s'));
//today 今日未结算的任务计划
$taskSchedulingPlanList = TaskSchedulingPlan::whereDay('end_time','today')
->withJoin(['scheduling'], 'left')
->where('scheduling.company_type', 17)
->where('is_pay',0)
->with(['template_info'])
->select()
->toArray();
foreach($taskSchedulingPlanList as $taskSchedulingPlan){
// 任务运行中,后台删除了任务模板
if (empty($taskSchedulingPlan['template_info'])) {
continue;
}
queue(VillageTaskSettlementJob::class, $taskSchedulingPlan);
}
Log::info('村管理公司定时任务结算执行-结束'.date('Y-m-d H:i:s'));
return $this->success('村管理公司定时任务结算执行成功');
}
/**
* 镇合伙人公司任务下发
*/
public function town_task_add()
{
//任务下发
$time = strtotime(date('Y-m-d'));
// 查询系统 所有镇农科公司 未下发 的 任务安排
$taskSchedulingList = TaskScheduling::where('cron_time', '<', $time)
->where('status', 1)
->where('company_type', 16)
->with('company_info')
->select()
->toArray();
$taskSchedulingIds = [];
$companyIds = [];
foreach ($taskSchedulingList as $k => $taskScheduling) {
$templateList = TaskTemplate::where('status', 1)->where('task_scheduling', $taskScheduling['id'])->limit(30)->select()->toArray();
$taskSchedulingIds[] = $taskScheduling['id'];
$companyIds[] = $taskScheduling['company_id'];
foreach ($templateList as $template) {
queue(TownTaskAdd::class, $template);
// TaskLogic::TownCronAdd($template); // 手动下发用
}
if (empty($templateList)) {
unset($taskSchedulingList[$k]);
}
}
Company::where('id', 'in', $companyIds)->inc('day_count')->update();
TaskScheduling::where('id', 'in', $taskSchedulingIds)->update(['cron_time' => time()]);
Log::info('镇农科公司定时任务下发执行成功' . date('Y-m-d H:i:s'));
return $this->success('镇农科公司定时任务下发执行成功');
}
/**
* 镇农科公司任务结算
*/
public function town_task_settlement()
{
Log::info('镇合伙人公司定时任务结算执行-开始'.date('Y-m-d H:i:s'));
//today 今日未结算的任务计划
$taskSchedulingPlanList = TaskSchedulingPlan::whereDay('end_time','today')
->withJoin(['scheduling'], 'left')
->where('scheduling.company_type', 16)
->where('is_pay',0)
->with(['template_info'])
->select()
->toArray();
foreach($taskSchedulingPlanList as $taskSchedulingPlan){
// 任务运行中,后台删除了任务模板
if (empty($taskSchedulingPlan['template_info'])) {
continue;
}
// 解耦 三个角色分开结算,避免某个角色的结算逻辑出现异常,导致整个镇农科公司的任务结算都终止
$taskTemplateInfo = $taskSchedulingPlan['template_info'];
// 负责人任务结算
if ($taskTemplateInfo['extend']['task_role'] == 1) {
queue(TownTaskSettlementJob::class, $taskSchedulingPlan);
}
// 市场部长任务结算
if ($taskTemplateInfo['extend']['task_role'] == 2) {
queue(TownTaskSettlementJob::class, $taskSchedulingPlan);
}
// 服务部长任务结算
if ($taskTemplateInfo['extend']['task_role'] == 3) {
queue(TownTaskSettlementJob::class, $taskSchedulingPlan);
}
}
Log::info('镇镇合伙人公司定时任务结算执行-结束'.date('Y-m-d H:i:s'));
return $this->success('镇农科公司定时任务结算执行成功');
}
}