Browse Source

修改获取资源列表的

master
王泽彦 9 months ago
parent
commit
4f07de429d
  1. 15
      niucloud/app/command/ClientCommand/TestCommand.php
  2. 29
      niucloud/app/command/TestCommand.php
  3. 47
      niucloud/app/dict/schedule/schedule.php
  4. 20
      niucloud/app/job/custmer/ResourceAutoAllocation.php
  5. 163
      niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php
  6. 2
      niucloud/config/console.php

15
niucloud/app/command/ClientCommand/TestCommand.php

@ -0,0 +1,15 @@
<?php
namespace app\command\ClientCommand;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class TestCommand extends Command
{
protected function execute(Input $input, Output $output)
{
$output->writeln('test');
}
}

29
niucloud/app/command/TestCommand.php

@ -0,0 +1,29 @@
<?php
declare (strict_types = 1);
namespace app\command;
use app\job\transfer\schedule\ResourceAutoAllocation;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class TestCommand extends Command
{
protected function configure()
{
// 指令配置
$this->setName('testcommand')
->setDescription('the testcommand command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$obj = new ResourceAutoAllocation();
$obj->doJob();
$output->writeln('testcommand');
}
}

47
niucloud/app/dict/schedule/schedule.php

@ -1,31 +1,44 @@
<?php <?php
return [ return [
// [
// 'key' => 'order_close',
// 'name' => '未支付订单自动关闭',
// 'desc' => '',
// 'time' => [
// 'type' => 'min',
// 'min' => 1
// ],
// 'class' => '',
// 'function' => ''
// ],
// [
// 'key' => 'transfer_check_finish',
// 'name' => '检验在线转账是否处理完毕',
// 'desc' => '',
// 'time' => [
// 'type' => 'min',
// 'min' => 5
// ],
// 'class' => 'app\job\transfer\schedule\CheckFinish',
// 'function' => ''
// ],
[ [
'key' => 'order_close', 'key' => 'resource_auto_allocation',
'name' => '未支付订单自动关闭', 'name' => '自动分配资源',
'desc' => '',
'time' => [
'type' => 'min',
'min' => 1
],
'class' => '',
'function' => ''
],
[
'key' => 'transfer_check_finish',
'name' => '检验在线转账是否处理完毕',
'desc' => '', 'desc' => '',
'time' => [ 'time' => [
'type' => 'min', 'type' => 'day',
'day' => 1,
'hour' => 0,
'min' => 5 'min' => 5
], ],
'class' => 'app\job\transfer\schedule\CheckFinish', 'class' => 'app\job\transfer\schedule\CheckFinish',
'function' => '' 'function' => ''
], ],
[ [
'key' => 'resource_auto_allocation', 'key' => 'course_schedule_job',
'name' => '自动分配资源', 'name' => '自动排课',
'desc' => '', 'desc' => '',
'time' => [ 'time' => [
'type' => 'day', 'type' => 'day',
@ -33,7 +46,7 @@ return [
'hour' => 0, 'hour' => 0,
'min' => 5 'min' => 5
], ],
'class' => 'app\job\transfer\schedule\CheckFinish', 'class' => 'app\job\transfer\schedule\CourseScheduleJob',
'function' => '' 'function' => ''
] ]
]; ];

20
niucloud/app/job/custmer/ResourceAutoAllocation.php

@ -1,20 +0,0 @@
<?php
namespace app\job\custmer;
use app\model\customer_resources\CustomerResources;
use app\model\resource_sharing\ResourceSharing;
use core\base\BaseJob;
class ResourceAutoAllocation extends BaseJob
{
public function doJob()
{
//获取当天的资源列表模型CustomerResources
// CustomerResources::where('created_at')
//获取当天销售获取资源的数量ResourceSharing
// ResourceSharing::where('shared_at')->
//遍历资源列表按照资源数量的最少的人员优先分配
return true;
}
}

163
niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php

@ -2,7 +2,10 @@
namespace app\job\transfer\schedule; namespace app\job\transfer\schedule;
use app\model\campus_person_role\CampusPersonRole;
use app\model\resource_sharing\ResourceSharing;
use core\base\BaseJob; use core\base\BaseJob;
use think\facade\Db;
use think\facade\Log; use think\facade\Log;
/** /**
@ -10,37 +13,173 @@ use think\facade\Log;
*/ */
class ResourceAutoAllocation extends BaseJob class ResourceAutoAllocation extends BaseJob
{ {
/**
* 执行任务
*/
public function doJob() public function doJob()
{ {
Log::write('自动分配资源'); Log::write('开始自动分配资源');
// 获取待分配的资源
$resources = $this->getResource();
if (empty($resources)) {
Log::write('没有可分配的资源');
return;
}
// 获取销售人员
$salesmen = $this->getSalesman();
if (empty($salesmen)) {
Log::write('没有可用的销售人员');
return;
}
// 分配资源
$this->allocateResource($resources, $salesmen);
Log::write('资源分配完成');
} }
/** /**
* 获取销售人员 * 获取销售人员(角色ID为6或7的人员)
* @return array 销售人员列表
*/ */
public function getSalesman() public function getSalesman()
{ {
Log::write('获取销售人员'); Log::write('获取销售人员');
// 获取角色ID为6或7的人员ID
$salesmen = CampusPersonRole::where('role_id', 'in', [6, 7])
->where('deleted_at', 0) // 未删除的记录
->field('person_id, role_id')
->select()
->toArray();
if (empty($salesmen)) {
Log::write('未找到销售人员');
return [];
}
// 获取每个销售人员当前拥有的资源数量
foreach ($salesmen as &$salesman) {
$resourceCount = ResourceSharing::where('shared_by', $salesman['person_id'])
->count();
$salesman['resource_count'] = $resourceCount;
}
// 按资源数量升序排序,资源少的排在前面
array_multisort(array_column($salesmen, 'resource_count'), SORT_ASC, $salesmen);
Log::write('找到' . count($salesmen) . '个销售人员');
return $salesmen;
} }
/** /**
* 获取资源 * 获取待分配的资源
* @return array 待分配资源列表
*/ */
public function getResource() public function getResource()
{ {
Log::write('获取资源'); Log::write('获取待分配资源');
course_schedule
::where('status', 1)->select(); // 获取role_id不是6,7的,shared_by是0的资源ID
$resources = ResourceSharing::where(function ($query) {
$query->where('role_id', 'not in', [6, 7])
->whereOr('role_id', 'null');
})
->where('shared_by', 0)
->field('id, resource_id')
->select()
->toArray();
Log::write('找到' . count($resources) . '个待分配资源');
return $resources;
} }
/** /**
* 按照现在销售人员的资源拥有情况分配资源 * 按照销售人员的资源拥有情况分配资源
* @param array $resources 待分配的资源列表
* @param array $salesmen 销售人员列表
*/ */
public function allocateResource() public function allocateResource($resources, $salesmen)
{ {
Log::write('按照现在销售人员的资源拥有情况分配资源'); Log::write('按照销售人员的资源拥有情况分配资源');
if (empty($resources) || empty($salesmen)) {
Log::write('没有资源或销售人员,无法分配');
return;
}
// 记录分配结果
$allocations = [];
// 开始分配
foreach ($resources as $resource) {
// 重新获取销售人员的资源数量排序,确保每次分配都是给最少资源的人
$currentSalesmen = $this->refreshSalesmenResourceCount($salesmen);
if (empty($currentSalesmen)) {
Log::write('没有可用的销售人员');
break;
}
// 选择资源最少的销售人员
$targetSalesman = $currentSalesmen[0];
// 插入新的资源共享记录
try {
Db::startTrans();
// 插入新的资源分配记录
$insertData = [
'resource_id' => $resource['resource_id'],
'user_id' => $targetSalesman['person_id'],
'role_id' => $targetSalesman['role_id'],
'shared_by' => $targetSalesman['person_id'], // shared_by是接收资源的人员ID
'shared_at' => date('Y-m-d H:i:s')
];
ResourceSharing::create($insertData);
// 记录分配结果
$allocations[] = [
'resource_id' => $resource['resource_id'],
'salesman_id' => $targetSalesman['person_id']
];
Db::commit();
Log::write('资源ID:' . $resource['resource_id'] . ' 分配给销售人员ID:' . $targetSalesman['person_id']);
} catch (\Exception $e) {
Db::rollback();
Log::write('资源分配失败:' . $e->getMessage());
}
}
Log::write('成功分配' . count($allocations) . '个资源');
}
/**
* 刷新销售人员的资源数量并重新排序
* @param array $salesmen 销售人员列表
* @return array 更新后的销售人员列表
*/
private function refreshSalesmenResourceCount($salesmen)
{
if (empty($salesmen)) {
return [];
}
foreach ($salesmen as &$salesman) {
$resourceCount = ResourceSharing::where('shared_by', $salesman['person_id'])
->count();
$salesman['resource_count'] = $resourceCount;
}
// 按资源数量升序排序
array_multisort(array_column($salesmen, 'resource_count'), SORT_ASC, $salesmen);
return $salesmen;
} }
} }

2
niucloud/config/console.php

@ -19,6 +19,8 @@ $data = [
//wokrerman的启动停止和重启 //wokrerman的启动停止和重启
'workerman' => 'app\command\workerman\Workerman', 'workerman' => 'app\command\workerman\Workerman',
'testcommand'=>'app\command\TestCommand'
], ],
]; ];
return (new DictLoader("Console"))->load($data); return (new DictLoader("Console"))->load($data);

Loading…
Cancel
Save