From 4f07de429dd87116774397170bbf6c3ac3e0b0a4 Mon Sep 17 00:00:00 2001 From: wangzeyan <258785420@qq.com> Date: Thu, 26 Jun 2025 08:17:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E5=88=97=E8=A1=A8=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/command/ClientCommand/TestCommand.php | 15 ++ niucloud/app/command/TestCommand.php | 29 ++++ niucloud/app/dict/schedule/schedule.php | 47 +++-- .../job/custmer/ResourceAutoAllocation.php | 20 --- .../schedule/ResourceAutoAllocation.php | 163 ++++++++++++++++-- niucloud/config/console.php | 2 + 6 files changed, 227 insertions(+), 49 deletions(-) create mode 100644 niucloud/app/command/ClientCommand/TestCommand.php create mode 100644 niucloud/app/command/TestCommand.php delete mode 100644 niucloud/app/job/custmer/ResourceAutoAllocation.php diff --git a/niucloud/app/command/ClientCommand/TestCommand.php b/niucloud/app/command/ClientCommand/TestCommand.php new file mode 100644 index 00000000..dce27442 --- /dev/null +++ b/niucloud/app/command/ClientCommand/TestCommand.php @@ -0,0 +1,15 @@ +writeln('test'); + } +} \ No newline at end of file diff --git a/niucloud/app/command/TestCommand.php b/niucloud/app/command/TestCommand.php new file mode 100644 index 00000000..c31f9458 --- /dev/null +++ b/niucloud/app/command/TestCommand.php @@ -0,0 +1,29 @@ +setName('testcommand') + ->setDescription('the testcommand command'); + } + + protected function execute(Input $input, Output $output) + { + // 指令输出 + $obj = new ResourceAutoAllocation(); + $obj->doJob(); + $output->writeln('testcommand'); + } +} diff --git a/niucloud/app/dict/schedule/schedule.php b/niucloud/app/dict/schedule/schedule.php index 7709b7e4..a73db7f2 100644 --- a/niucloud/app/dict/schedule/schedule.php +++ b/niucloud/app/dict/schedule/schedule.php @@ -1,31 +1,44 @@ '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', - 'name' => '未支付订单自动关闭', - 'desc' => '', - 'time' => [ - 'type' => 'min', - 'min' => 1 - ], - 'class' => '', - 'function' => '' - ], - [ - 'key' => 'transfer_check_finish', - 'name' => '检验在线转账是否处理完毕', + 'key' => 'resource_auto_allocation', + 'name' => '自动分配资源', 'desc' => '', 'time' => [ - 'type' => 'min', + 'type' => 'day', + 'day' => 1, + 'hour' => 0, 'min' => 5 ], 'class' => 'app\job\transfer\schedule\CheckFinish', 'function' => '' ], [ - 'key' => 'resource_auto_allocation', - 'name' => '自动分配资源', + 'key' => 'course_schedule_job', + 'name' => '自动排课', 'desc' => '', 'time' => [ 'type' => 'day', @@ -33,7 +46,7 @@ return [ 'hour' => 0, 'min' => 5 ], - 'class' => 'app\job\transfer\schedule\CheckFinish', + 'class' => 'app\job\transfer\schedule\CourseScheduleJob', 'function' => '' ] ]; diff --git a/niucloud/app/job/custmer/ResourceAutoAllocation.php b/niucloud/app/job/custmer/ResourceAutoAllocation.php deleted file mode 100644 index fb26380c..00000000 --- a/niucloud/app/job/custmer/ResourceAutoAllocation.php +++ /dev/null @@ -1,20 +0,0 @@ - - //遍历资源列表按照资源数量的最少的人员优先分配 - return true; - } -} \ No newline at end of file diff --git a/niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php b/niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php index 0ec2256e..4641b933 100644 --- a/niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php +++ b/niucloud/app/job/transfer/schedule/ResourceAutoAllocation.php @@ -2,7 +2,10 @@ namespace app\job\transfer\schedule; +use app\model\campus_person_role\CampusPersonRole; +use app\model\resource_sharing\ResourceSharing; use core\base\BaseJob; +use think\facade\Db; use think\facade\Log; /** @@ -10,37 +13,173 @@ use think\facade\Log; */ class ResourceAutoAllocation extends BaseJob { + /** + * 执行任务 + */ 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() { 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() { - Log::write('获取资源'); - course_schedule - ::where('status', 1)->select(); + Log::write('获取待分配资源'); + + // 获取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; } - - - } \ No newline at end of file diff --git a/niucloud/config/console.php b/niucloud/config/console.php index 244e1bf5..d2d266f8 100644 --- a/niucloud/config/console.php +++ b/niucloud/config/console.php @@ -19,6 +19,8 @@ $data = [ //wokrerman的启动停止和重启 'workerman' => 'app\command\workerman\Workerman', + + 'testcommand'=>'app\command\TestCommand' ], ]; return (new DictLoader("Console"))->load($data);