WokerTask/app/common/logic/task_template/TaskTemplateLogic.php

423 lines
18 KiB
PHP
Raw Normal View History

2023-12-27 14:06:33 +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_template;
use app\adminapi\logic\ConfigLogic;
use app\common\model\Company;
use app\common\model\task_template\TaskTemplate;
use app\common\logic\BaseLogic;
use app\common\model\company\CompanyProperty;
use app\common\model\informationg\UserInformationg;
use app\common\model\task_scheduling\TaskScheduling;
use app\common\model\user\User;
use app\common\model\vehicle\VehicleRent;
use think\facade\Db;
/**
* 任务模板逻辑
* Class TaskTemplateLogic
* @package app\adminapi\logic\task_template
*/
class TaskTemplateLogic extends BaseLogic
{
/**
* @notes 添加任务模板
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/08/06 17:30
*/
public static function add(array $params): bool
{
try {
Db::startTrans();
// 其他任务可重复创建
if ($params['type'] != 34) {
$find=TaskTemplate::where('task_scheduling', $params['task_scheduling'])->where('company_id',$params['company_id'])->where('type',$params['type'])->field('id,types,type')->find();
if($find&&$params['type']==$find['type']){
self::setError('已经有同一种任务类型了');
return false;
}
}
if($params['type']==33){
$count=UserInformationg::where('company_id',$params['company_id'])->where('status',1)->count();
if($count<300){
self::setError('用户档案数量300不足无法创建任务模板');
return false;
}
}
$moeny=TaskTemplate::where('company_id', $params['company_id'])->sum('money');
if($moeny+$params['money']>200){
self::setError('任务模板一阶段合计金额不能大于任务调度金额');
return false;
}
$money_two=TaskTemplate::where('company_id', $params['company_id'])->sum('money_two');
if($money_two+$params['money_two']>200){
self::setError('任务模板二阶段合计金额不能大于任务调度金额');
return false;
}
$money_three=TaskTemplate::where('company_id', $params['company_id'])->sum('money_three');
if($money_three+$params['money_three']>200){
self::setError('任务模板长期合计金额不能大于任务调度金额');
return false;
}
if($params['type']==32){
$VehicleRent = VehicleRent::where('rent_company_id',$params['company_id'])->find();
if(empty($VehicleRent)){
self::setError('该公司没有三轮车,请先租赁三轮车');
return false;
}
}
// 除了长期任务,其他阶段类型没有长期金额
if($params['types']!=2){
$params['money_three']=0;
}
if($params['type']==35){
$params['extend']=['shareholder'=>['user_id'=>$params['task_admin']]];
}
TaskTemplate::create([
'title' => $params['title'],
'admin_id' => $params['admin_id'],
'company_id' => $params['company_id'],
'task_scheduling' => $params['task_scheduling']??0,
'money' => $params['money'],
'money_two' => $params['money_two'],
'money_three' => $params['money_three'],
'type' => $params['type'],
'types' => $params['types'],
'status' => $params['status'],
'content' => $params['content'],
'extend'=>json_encode($params['extend']),
'stage_day_one' => $params['stage_day_one']??0,
'proportion_one' => $params['proportion_one']??0,
'stage_day_two' => $params['stage_day_two']??0,
'proportion_two' => $params['proportion_two']??0,
'recharge' => $params['recharge']??0,
]);
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/06 17:30
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
$company = Company::find($params['company_id']);
if ($company->company_type == 16) {
// 创建 镇合伙人公司 任务模板
$taskScheduleAmount = 700;
} else {
$taskScheduleAmount = 200;
}
$find=TaskTemplate::where('task_scheduling', $params['task_scheduling'])->where('company_id',$params['company_id'])->where('type',$params['type'])->field('id,types,type,money,money_two,money_three,extend')->find();
if($find && $find['id']!=$params['id']&&$params['type']==$find['type']){
self::setError('已经有同一种任务类型了');
return false;
}
if($params['type']==33){
$count=UserInformationg::where('company_id',$params['company_id'])->where('status',1)->count();
if($count<300){
self::setError('用户档案数量300不足无法创建任务模板');
return false;
}
}
$moeny=TaskTemplate::where('company_id', $params['company_id'])->sum('money');
if($moeny+$params['money']-$find['money']>$taskScheduleAmount){
self::setError('任务模板一阶段合计金额不能大于任务调度金额');
return false;
}
$money_two=TaskTemplate::where('company_id', $params['company_id'])->sum('money_two');
if($money_two+$params['money_two']-$find['money_two']>$taskScheduleAmount){
self::setError('任务模板二阶段合计金额不能大于任务调度金额');
return false;
}
$money_three=TaskTemplate::where('company_id', $params['company_id'])->sum('money_three');
if($money_three+$params['money_three']-$find['money_three']>$taskScheduleAmount){
self::setError('任务模板长期合计金额不能大于任务调度金额');
return false;
}
if($params['type']==32){
$VehicleRent = VehicleRent::where('rent_company_id',$params['company_id'])->find();
if(empty($VehicleRent)){
self::setError('该公司没有三轮车,请先租赁三轮车');
return false;
}
}
// 除了长期任务,其他阶段类型没有长期金额
if($params['types']!=2){
$params['money_three']=0;
}
if($params['type']==35){
$params['extend']=['shareholder'=>['user_id'=>$params['task_admin']]];
}
TaskTemplate::where('id', $params['id'])->update([
'title' => $params['title'],
'admin_id' => $params['admin_id'],
'money' => $params['money'],
'money_two' => $params['money_two'],
'money_three' => $params['money_three'],
'type' => $params['type'],
'types' => $params['types'],
'status' => $params['status'],
'content' => $params['content'],
'extend'=>json_encode(array_merge($find['extend'],$params['extend'])),
'stage_day_one' => $params['stage_day_one']??0,
'proportion_one' => $params['proportion_one']??0,
'stage_day_two' => $params['stage_day_two']??0,
'proportion_two' => $params['proportion_two']??0,
'recharge' => $params['recharge']??0,
]);
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/06 17:30
*/
public static function delete(array $params): bool
{
return TaskTemplate::destroy($params['id']);
}
/**
* @notes 获取任务模板详情
* @param $params
* @return array
* @author likeadmin
* @date 2023/08/06 17:30
*/
public static function detail($params): array
{
return TaskTemplate::findOrEmpty($params['id'])->toArray();
}
public static function initCompanyWithdrawDeadline($companyId)
{
// 如果是公司第一次创建安排任务,初始化公司的可提现周期截止时间
$templateCount = TaskTemplate::where(['company_id' => $companyId])->count();
if ($templateCount == 1) {
$dictData = ConfigLogic::getDictTypeValueByType('withdraw_cycle');
$cycle = $dictData['withdraw_cycle_1']['value']; // 数据字典-提现周期 单位:天数
$today = strtotime(date('Y-m-d'));
$withdrawDeadline = $today + $cycle * 24 * 60 * 60 + 86400;
$company = Company::find($companyId);
$company->withdraw_deadline = $withdrawDeadline;
$company->save();
}
}
/**
* @notes 添加任务模板
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/08/06 17:30
*/
public static function addTownTaskTemplate(array $params): bool
{
try {
Db::startTrans();
// $params['extend']['task_role'] 扩展字段 任务角色 1总负责人 2市场部长 3服务部长
$taskScheduleAmount = 700;
if ($params['extend']['task_role'] == 1) {
$serviceManagerUser = (new User())->searchMaster($params['company_id']);
if (empty($serviceManagerUser)) {
self::setError('公司还没有负责人,无法指派任务');
return false;
}
}
if ($params['extend']['task_role'] == 2) {
$serviceManagerUser = (new User())->searchMarketingManager($params['company_id']);
if (empty($serviceManagerUser)) {
self::setError('公司还没有市场部长,无法指派任务');
return false;
}
}
if ($params['extend']['task_role'] == 3) {
$serviceManagerUser = (new User())->searchServiceManager($params['company_id']);
if (empty($serviceManagerUser)) {
self::setError('公司还没有服务部长,无法指派任务');
return false;
}
}
$find = TaskTemplate::where('task_scheduling', $params['task_scheduling'])->where('company_id',$params['company_id'])->where('type',$params['type'])->field('id,types,type')->find();
if($find && $params['type'] == $find['type']){
self::setError('已经有同一种任务类型了');
return false;
}
$moeny = TaskTemplate::where('company_id', $params['company_id'])->sum('money');
if($moeny + $params['money'] > $taskScheduleAmount){
self::setError('任务模板一阶段合计金额不能大于任务调度金额');
return false;
}
$moneyTwo = TaskTemplate::where('company_id', $params['company_id'])->sum('money_two');
if($moneyTwo + $params['money_two'] > $taskScheduleAmount){
self::setError('任务模板二阶段合计金额不能大于任务调度金额');
return false;
}
$newMoneyThree = TaskTemplate::where('company_id', $params['company_id'])->sum('new_money_three');
if($newMoneyThree + $params['new_money_three'] > $taskScheduleAmount){
self::setError('任务模板三阶段合计金额不能大于任务调度金额');
return false;
}
$moneyThree = TaskTemplate::where('company_id', $params['company_id'])->sum('money_three');
if($moneyThree + $params['money_three']>$taskScheduleAmount){
self::setError('任务模板长期合计金额不能大于任务调度金额');
return false;
}
TaskTemplate::create([
'title' => $params['title'],
'admin_id' => $params['admin_id'],
'company_id' => $params['company_id'],
'task_scheduling' => $params['task_scheduling']??0,
'money' => $params['money'],
'money_two' => $params['money_two'],
'money_three' => $params['money_three'],
'type' => $params['type'],
'types' => $params['types'],
'status' => $params['status'],
'content' => $params['content'],
'extend'=>json_encode($params['extend']),
'stage_day_one' => $params['stage_day_one']??0,
'proportion_one' => $params['proportion_one']??0,
'stage_day_two' => $params['stage_day_two']??0,
'proportion_two' => $params['proportion_two']??0,
'recharge' => $params['recharge']??0,
'stage_day_three' => $params['stage_day_three']??0,
'new_money_three' => $params['new_money_three']??0,
]);
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/06 17:30
*/
public static function addVillageTaskTemplate(array $params): bool
{
try {
Db::startTrans();
$serviceManagerUser = (new User())->searchLiaisonMan($params['company_id']);
if (empty($serviceManagerUser)) {
self::setError('公司还没有村联络员,无法指派任务');
return false;
}
$find = TaskTemplate::where('task_scheduling', $params['task_scheduling'])->where('company_id',$params['company_id'])->where('type',$params['type'])->field('id,types,type')->find();
if($find && $params['type'] == $find['type']){
self::setError('已经有同一种任务类型了');
return false;
}
$moeny = TaskTemplate::where('company_id', $params['company_id'])->sum('money');
if($moeny + $params['money'] > 200){
self::setError('任务模板一阶段合计金额不能大于任务调度金额');
return false;
}
$moneyTwo = TaskTemplate::where('company_id', $params['company_id'])->sum('money_two');
if($moneyTwo + $params['money_two'] > 200){
self::setError('任务模板二阶段合计金额不能大于任务调度金额');
return false;
}
$newMoneyThree = TaskTemplate::where('company_id', $params['company_id'])->sum('new_money_three');
if($newMoneyThree + $params['new_money_three'] > 200){
self::setError('任务模板三阶段合计金额不能大于任务调度金额');
return false;
}
$moneyThree = TaskTemplate::where('company_id', $params['company_id'])->sum('money_three');
if($moneyThree + $params['money_three']>200){
self::setError('任务模板长期合计金额不能大于任务调度金额');
return false;
}
TaskTemplate::create([
'title' => $params['title'],
'admin_id' => $params['admin_id'],
'company_id' => $params['company_id'],
'task_scheduling' => $params['task_scheduling']??0,
'money' => $params['money'],
'money_two' => $params['money_two'],
'money_three' => $params['money_three'],
'type' => $params['type'],
'types' => $params['types'],
'status' => $params['status'],
'content' => $params['content'],
'extend'=>json_encode($params['extend']),
'stage_day_one' => $params['stage_day_one']??0,
'proportion_one' => $params['proportion_one']??0,
'stage_day_two' => $params['stage_day_two']??0,
'proportion_two' => $params['proportion_two']??0,
'recharge' => $params['recharge']??0,
'stage_day_three' => $params['stage_day_three']??0,
'new_money_three' => $params['new_money_three']??0,
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
}