TaskSystem/app/common/logic/task/TaskLogic.php

254 lines
9.6 KiB
PHP
Raw Normal View History

2023-08-05 16:14:43 +08:00
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\logic\task;
use app\common\model\task\Task;
use app\common\logic\BaseLogic;
use app\common\model\informationg\UserInformationg;
2023-08-10 17:10:12 +08:00
use app\common\model\task_scheduling_plan\TaskSchedulingPlan;
2023-08-16 11:13:31 +08:00
use app\common\model\task_template\TaskTemplate;
2023-08-18 16:32:41 +08:00
use app\common\model\user\User;
2023-08-05 16:14:43 +08:00
use think\facade\Db;
2023-08-10 17:10:12 +08:00
use think\facade\Log;
2023-08-05 16:14:43 +08:00
/**
* 任务逻辑
* Class TaskLogic
* @package app\adminapi\logic\task
*/
class TaskLogic extends BaseLogic
{
/**
* @notes 添加任务
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/08/05 13:39
*/
public static function add(array $params): bool
{
Db::startTrans();
try {
Task::create([
'title' => $params['title'],
'template_id' => $params['template_id'],
'company_id' => $params['company_id'],
// 'admin_id' => $params['admin_id'],
'start_time' => strtotime($params['start_time']),
2023-08-10 17:10:12 +08:00
'end_time' => strtotime($params['end_time']),
2023-08-05 16:14:43 +08:00
'director_uid' => $params['director_uid'],
'type' => $params['type'],
'status' => $params['status'],
'content' => $params['content'],
2023-08-10 17:10:12 +08:00
'extend' => json_encode($params['extend'])
2023-08-05 16:14:43 +08:00
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
2023-08-10 17:10:12 +08:00
/**
* 定时添加任务
*/
2023-08-28 09:31:13 +08:00
public static function CronAdd(array $v,$datas): bool
2023-08-10 10:29:57 +08:00
{
2023-08-18 16:32:41 +08:00
try {
$time=strtotime(date('Y-m-d'));
2023-08-28 18:46:43 +08:00
$TaskSchedulingPlan_data=[
2023-08-18 16:32:41 +08:00
'create_user_id' => 0,
'company_id' => $v['company_id'],
'template_id' => $v['id'],
'scheduling_id' => $v['task_scheduling'],
'start_time' => $time,
'end_time' => $time+86399,
'sn' =>User::createUserSn(),
'status' => 1
2023-08-28 18:46:43 +08:00
];
//入股任务
if($v['type']==35){
$user_ids=User::where('company_id', $v['company_id'])->where('is_captain',1)->field('id,nickname')->select();
if($user_ids){
foreach($user_ids as $u_k=>$u_v){
$find_35=Task::where('company_id', $v['company_id'])->where('id', $v['id'])->where('type',35)->where('director_uid',$u_v['id'])->where('status',3)->find();
if(!$find_35){
$TaskSchedulingPlan=TaskSchedulingPlan::create($TaskSchedulingPlan_data);
$arr = [
'template_id' => $v['id'],
'scheduling_plan_id' => $TaskSchedulingPlan['id'],
'company_id' => $v['company_id'],
'title' => $v['title'],
'money' => $v['money'],
'type' => $v['type'],
'content' => $v['content'],
'start_time' => $time,
'end_time' => $time+86399,
'create_time' => time(),
'update_time' => time(),
];
$data = $arr;
$data['money'] = 0;
2023-08-28 19:26:10 +08:00
$data['extend'] = json_encode(['shareholder'=>['money'=>$v['recharge'],'over_decimal'=>$v['over_decimal'],'count_money'=>$v['recharge']*count($user_ids)]]);
2023-08-28 18:46:43 +08:00
$data['type'] = 35;
$data['title'] = $u_v['nickname'].'的入股任务';
$data['status'] = 2;
$data['director_uid'] = $u_v['id'];
$task_id = (new Task())->insertGetId($data);
TaskSchedulingPlan::where('id', $TaskSchedulingPlan['id'])->update(['task_id' => $task_id, 'is_execute' => 1]);
}
}
return true;
}else{
Log::info('没有队长,任务下发失败'. json_encode($v));
return false;
}
}
$TaskSchedulingPlan=TaskSchedulingPlan::create($TaskSchedulingPlan_data);
2023-08-18 16:32:41 +08:00
$arr = [
'template_id' => $v['id'],
'scheduling_plan_id' => $TaskSchedulingPlan['id'],
'company_id' => $v['company_id'],
'title' => $v['title'],
'money' => $v['money'],
'type' => $v['type'],
'content' => $v['content'],
'start_time' => $time,
'end_time' => $time+86399,
'create_time' => time(),
'update_time' => time(),
];
$data = $arr;
2023-08-28 09:31:13 +08:00
$data['money'] = self::task_money($v,$datas);
2023-08-18 17:29:51 +08:00
$data['extend'] = json_encode($v['extend']);
2023-08-28 09:31:13 +08:00
//信息更新
2023-08-18 16:32:41 +08:00
if ($v['type'] == 31) {
2023-08-28 09:31:13 +08:00
$finds=TaskTemplate::where('id', $v['id'])->field('information_count,information_day_count')->find();
$update=0;
if($finds['information_count']>$finds['information_day_count']){
$day_count=$finds['information_count']-$finds['information_day_count'];
if($day_count>=5){
$update=5;
}else{
$update=$day_count;
}
}
$data["extend"] = json_encode(['informationg' => ['count' => 5, 'update' => $update]]);
2023-08-18 16:32:41 +08:00
TaskTemplate::where('id', $v['id'])->inc('information_day_count', 5)->update();
}
if ($v['type'] == 32) {
2023-08-28 09:31:13 +08:00
$data['director_uid'] = $datas['company_info']['user_id'];
2023-08-10 17:10:12 +08:00
}
2023-08-18 16:32:41 +08:00
$task_id = (new Task())->insertGetId($data);
TaskSchedulingPlan::where('id', $TaskSchedulingPlan['id'])->update(['task_id' => $task_id, 'is_execute' => 1]);
2023-08-28 09:31:13 +08:00
// TaskTemplate::where('id', $v['id'])->update(['cron_time' => time()]);
2023-08-18 16:32:41 +08:00
return true;
} catch (\Exception $e) {
Log::error('定时任务添加失败', [$e->getMessage()]);
return false;
2023-08-11 16:35:07 +08:00
}
2023-08-16 11:13:31 +08:00
}
//任务金额
2023-08-28 09:31:13 +08:00
private static function task_money($v,$datas)
2023-08-16 11:13:31 +08:00
{
2023-08-18 16:32:41 +08:00
if ($v['types'] == 1 || $v['types'] == 3) {
2023-08-28 09:31:13 +08:00
if ($datas['company_info'] && $datas['company_info']['day_count'] <= $v['stage_day_one']) {
2023-08-18 16:32:41 +08:00
return $v['money'];
2023-08-16 11:13:31 +08:00
} else {
2023-08-18 16:32:41 +08:00
return $v['money_two'];
2023-08-16 11:13:31 +08:00
}
2023-08-18 16:32:41 +08:00
} elseif ($v['types'] == 2) {
2023-08-28 09:31:13 +08:00
if ($datas['company_info']['day_count'] <= $v['stage_day_one']) {
2023-08-18 16:32:41 +08:00
return $v['money'];
2023-08-28 09:31:13 +08:00
} elseif ($datas['company_info']['day_count'] <= $v['stage_day_two']) {
2023-08-18 16:32:41 +08:00
return $v['money_two'];
2023-08-16 11:13:31 +08:00
} else {
2023-08-18 16:32:41 +08:00
return $v['money_three'];
2023-08-16 11:13:31 +08:00
}
} else {
2023-08-28 09:31:13 +08:00
if ( $datas['company_info']['day_count'] <= $v['stage_day_one']) {
2023-08-18 16:32:41 +08:00
$a = $v['money'];
2023-08-16 11:13:31 +08:00
} else {
2023-08-18 16:32:41 +08:00
$a = $v['money_two'];
2023-08-16 11:13:31 +08:00
}
2023-08-28 09:31:13 +08:00
if ($datas['company_info'] && $datas['company_info']['day_count'] >= $v['stage_day_two']) {
2023-08-18 16:32:41 +08:00
TaskTemplate::where('id', $v['id'])->update(['status' => 0]);
2023-08-16 11:13:31 +08:00
}
return $a;
2023-08-10 17:10:12 +08:00
}
2023-08-10 10:29:57 +08:00
}
2023-08-05 16:14:43 +08:00
2023-08-10 17:10:12 +08:00
/**
* @notes 更新任务状态
2023-08-05 16:14:43 +08:00
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/08/05 13:39
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
2023-08-10 17:10:12 +08:00
Task::where('id', $params['id'])->update(['status' => 3, 'update_time' => time()]);
2023-08-05 16:14:43 +08:00
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 删除任务
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/08/05 13:39
*/
public static function delete(array $params): bool
{
return Task::destroy($params['id']);
}
/**
* @notes 获取任务详情
* @param $params
* @return array
* @author likeadmin
* @date 2023/08/05 13:39
*/
public static function detail($params): array
{
2023-08-10 17:10:12 +08:00
$task = Task::findOrEmpty($params['id'])->toArray();
if ($task) {
if (isset($task['extend']['informationg'])) {
$task['extend']['informationg'] = UserInformationg::where('id', $task['extend']['informationg'])->field('name,phone,sex,age,update_time')
->find()->toArray();
2023-08-05 16:14:43 +08:00
}
}
return $task;
}
2023-08-10 17:10:12 +08:00
}