WokerTask/app/job/TaskInformationJob.php

180 lines
10 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\job;
use app\api\controller\RemoteController;
use app\common\logic\finance\ShareProfit;
use app\common\model\task\Task;
use think\queue\Job;
use think\facade\Log;
use app\common\model\Company;
use app\common\model\task_scheduling_plan\TaskSchedulingPlan;
use app\common\model\task_template\TaskTemplate;
use think\facade\Db;
/**
* 任务结算执行的具体逻辑
*/
class TaskInformationJob
{
public function fire(Job $job, $data)
{
// if ($job->attempts() > 1) {
// //通过这个方法可以检查这个任务已经重试了几次了
// }
try {
TaskSchedulingPlan::where('id', $data['id'])->update(['is_pay' => 1]);
$company = Company::where('id', $data['company_id'])->field('id,deposit,responsible_area,company_money,shareholder_money,user_id,day_count,company_type,province,city,area,street,village,brigade')->find(); // 可能要判断预存金是否满足
$arr['status'] = 0;
$arr['company_account_type'] = 1;
//信息更新
if ($data['template_info']['type'] == 31) {
if ($data['template_info']['information_count'] < $data['template_info']['information_day_count']) {
Log::info('信息更新任务,信息更新未达到要求:' . json_encode($data));
Task::where('id', $data['task_id'])->update(['status' => 5]);
TaskTemplate::where('id',$data['template_id'])->update(['information_count'=>$data['template_info']['information_day_count']]);
return false;
}
$name = '小组队长';
$arr['status'] = 1;
$task = Task::where('id', $data['task_id'])->field('director_uid,money')->where('status', 3)->find();
$arr['money'] = $task['money'];
Task::where('id', $data['task_id'])->update(['status' => 3]);
} elseif
//交易金额
($data['template_info']['type'] == 33) {
$shang_date_total_price = App(RemoteController::class)->shang_date_total_price($company,[],$data['template_id']);
if ($shang_date_total_price == false) {
Log::info('交易金额任务,交易金额未达到要求:' . json_encode($data));
Task::where('id', $data['task_id'])->update(['status' => 5]);
return false;
}
$transaction_pool=$data['template_info']['transaction_pool'];//交易金额剩余池
$count_money=bcadd($shang_date_total_price['arr']['total_price'],$transaction_pool,2);//交易金额加资金池金额
if($count_money>$shang_date_total_price['arr']['day_money']){
$day_money=bcsub($count_money,$shang_date_total_price['arr']['day_money'],2);//当计算剩余池before_transaction_pool
$shang_date_total_price['arr']['before_transaction_pool']=$transaction_pool;//变化前
$shang_date_total_price['arr']['after_count_transaction_pool']=$count_money;//变化后
$shang_date_total_price['arr']['after_transaction_pool']=$day_money;//变化后
Task::where('id', $data['task_id'])->update(['status' => 3,'extend'=>json_encode(['transaction'=>$shang_date_total_price])]);
TaskTemplate::where('id',$data['template_info']['id'])->update(['transaction_pool'=>$day_money]);
$shang_date_total_price['arr']['status']=1;
}else{
Log::info('交易金额任务,交易金额小于今日金额:' . json_encode($data));
Task::where('id', $data['task_id'])->update(['status' => 5]);
return false;
}
$name = $shang_date_total_price['name'];
$arr['status'] = $shang_date_total_price['arr']['status'];
$task = Task::where('id', $data['task_id'])->field('money')->find();
$arr['money'] = $task['money'];
} elseif
//三轮车
($data['template_info']['type'] == 32){
$task = Task::where('id', $data['task_id'])->field('director_uid,money')->where('status', 3)->with('director_info')->find();
if(empty($task)){
Log::info('三轮车任务 ' . $data['template_info']['title'] . '结算失败,任务为空:' . json_encode($data));
Task::where('id', $data['task_id'])->update(['status' => 5]);
return true;
}
$name=$task['director_info']['nickname'];
$arr['status'] = 1;
$arr['money'] = $task['money'];
}elseif
//入股任务
($data['template_info']['type'] == 35){
$task_35 = Task::where('id', $data['task_id'])->field('director_uid,status,money,start_time,end_time')->with('director_info')->find();
if($task_35){
$day= $data['template_info']['stage_day_one'] + $data['template_info']['stage_day_two'];
// if($task_35['status']==3 && $data['template_info']['day_count']<=$day){
// $name = $task_35['director_info']['nickname'];
// $arr['status'] = 1;
// $arr['money'] = $task_35['money'];
// $arr['company_account_type'] = 2;
// }else{
if ($task_35['status'] == 3) {
return true;
}
/**
* 判断入股任务 单次类型 是否超时
* 未超时时刷新开始结束时间
* 超时则关闭
*/
if($data['template_info']['day_count']<=$day){
TaskSchedulingPlan::where('id', $data['id'])->update(['is_pay' => 0]);
try{
Task::where('id', $data['task_id'])->update(['create_time' => $task_35['start_time']+86400,'update_time' =>time(),'start_time'=>$task_35['start_time']+86400,'end_time'=>$task_35['start_time']+86400+86399]);
}catch(\Exception $e){
$start_time = strtotime(date('Y-m-d'));
Task::where('id', $data['task_id'])->update(['create_time' => $start_time+86400,'update_time' =>time(),'start_time'=>$start_time+86400,'end_time'=> $start_time + 86400+86399]);
}
return false;
}
if($data['template_info']['day_count']>$day){
Task::where('id', $data['task_id'])->update(['status' =>5]);
Log::info('入股任务 ' . $data['template_info']['title'] . '结算失败,任务为超时:' . json_encode($data));
return false;
}
// }
}else{
Log::info('入股任务 ' . $data['template_info']['title'] . '结算失败,任务为空:' . json_encode($data));
return false;
}
}
else {
//其他类型任务
$task = Task::where('id', $data['task_id'])->with('director_info')->find();
if ($task['status'] == 3) {
$name = $task['director_info']['nickname'];
$arr['status'] = 1;
$arr['money'] = $task['money'];
} else if ($task['status'] == 2) {
// 是否超时
$day= $data['template_info']['stage_day_one'] + $data['template_info']['stage_day_two'];
if($data['template_info']['day_count'] >= $day){
Task::where('id', $data['task_id'])->update(['status' =>5]);
Log::info( $data['template_info']['title'] . '结算失败,任务为超时:' . json_encode($data));
return false;
}
if($data['template_info']['day_count'] < $day){
TaskSchedulingPlan::where('id', $data['id'])->update(['is_pay' => 0]);
try{
Task::where('id', $data['task_id'])->update(['create_time' => $task['start_time']+86400,'update_time' =>time(),'start_time'=>$task['start_time']+86400,'end_time'=>$task['start_time']+86400+86399]);
}catch(\Exception $e){
$start_time = strtotime(date('Y-m-d'));
Task::where('id', $data['task_id'])->update(['create_time' => $start_time+86400,'update_time' =>time(),'start_time'=>$start_time+86400,'end_time'=> $start_time + 86400+86399]);
}
return false;
}
}
}
} catch (\Exception $e) {
Log::error('line:'.$e->getLine().'异常报错:任务结算失败:' . $data['template_info']['title'] . $e->getMessage() . json_encode($data));
return false;
}
if ($arr['status'] == 1) {
$arr['company_id'] = $data['scheduling']['company_id'];
$arr['msg'] = '来自任务【' . $data['template_info']['title'] . '】,完成方:' . $name . ',任务结算';
// $arr['proportion_one'] = $data['template_info']['proportion_one'];
// $arr['proportion_two'] = $data['template_info']['proportion_two'];
$arr['sn'] = $data['sn'];
$arr['id'] = $data['id'];
(new ShareProfit())->first($arr, $company,$data);
} else {
Task::where('id', $data['task_id'])->update(['status' => 5]);
Log::info('任务status=0结算失败' . $data['template_info']['title'] . '未完成' . json_encode($data));
}
//如果任务执行成功后 记得删除任务不然这个任务会重复执行直到达到最大重试次数后失败后执行failed方法
$job->delete();
}
public function failed($data)
{
Log::error('任务结算失败' . $data);
// ...任务达到最大重试次数后,失败了
}
}