diff --git a/niucloud/app/event.php b/niucloud/app/event.php index 54824f14..6c7781c5 100644 --- a/niucloud/app/event.php +++ b/niucloud/app/event.php @@ -115,11 +115,17 @@ $system_event = [ 'app\listener\system\ShowAppListener' ], - //计算业绩 + //计算市场人员业绩 'CalculatePerformance' => [ 'app\listener\personnel\CalculatePerformance' ], //学员 'Student' => [ 'app\listener\personnel\Student' ], + + //计算销售人员业绩 + 'SalesPerformance' => [ 'app\listener\custom\SalesPerformanceListener' ], + + //计算教练业绩 + 'CoachPerformance' => [ 'app\listener\custom\CoachPerformanceListener' ], ], 'subscribe' => [ ], diff --git a/niucloud/app/listener/custom/CoachPerformanceListener.php b/niucloud/app/listener/custom/CoachPerformanceListener.php new file mode 100644 index 00000000..652a938d --- /dev/null +++ b/niucloud/app/listener/custom/CoachPerformanceListener.php @@ -0,0 +1,180 @@ +find($params['course_id']); + if (empty($courseInfo)) { + return false; + } + + // 检查教练是否存在 + $personnel = (new Personnel())->find($params['coach_id']); + if (empty($personnel)) { + return false; + } + + // 业绩类型,默认为课程授课 + $performanceType = $params['performance_type'] ?? 'course'; + + // 计算业绩值 + $performanceValue = $this->calculatePerformanceValue($courseInfo, $performanceType, $params); + + // 记录业绩 + return $this->recordPerformance([ + 'staff_id' => $params['coach_id'], + 'course_id' => $params['course_id'], + 'student_id' => $params['student_id'] ?? 0, + 'performance_type' => $performanceType, + 'performance_value' => $performanceValue, + 'remarks' => '系统自动计算教练业绩', + ]); + } + + /** + * 计算业绩值 + * @param object $courseInfo 课程信息 + * @param string $performanceType 业绩类型 + * @param array $params 额外参数 + * @return float 业绩值 + */ + protected function calculatePerformanceValue($courseInfo, $performanceType, $params) + { + $value = 0; + + // 获取课程时长(小时) + $courseDuration = floatval($courseInfo['duration'] ?? 1); + + // 根据业绩类型计算业绩值 + switch ($performanceType) { + case 'course': + // 普通课程授课业绩,按照课时和课程基础系数计算 + $baseRate = 50; // 每小时基本业绩值 + $value = $courseDuration * $baseRate; + + // 如果有学员参数,增加学员数量系数 + if (!empty($params['student_count'])) { + $studentCount = intval($params['student_count']); + // 每多一名学员,业绩系数增加5% + $studentFactor = 1 + (($studentCount - 1) * 0.05); + $value = $value * $studentFactor; + } + break; + + case 'private': + // 私教课程业绩,私教课程通常有更高的业绩系数 + $privateRate = 150; // 每小时私教业绩值 + $value = $courseDuration * $privateRate; + + // 检查学员等级,高级别学员可以提高业绩 + if (!empty($params['student_id'])) { + $student = (new Student())->find($params['student_id']); + if (!empty($student) && !empty($student['level'])) { + // 根据学员等级增加业绩系数 + $levelFactor = 1 + (intval($student['level']) * 0.1); + $value = $value * $levelFactor; + } + } + break; + + case 'assessment': + // 考核评估业绩,固定业绩值加上考核难度系数 + $assessmentBaseValue = 100; // 基础考核业绩值 + $difficultyLevel = intval($params['difficulty_level'] ?? 1); // 难度等级1-5 + $value = $assessmentBaseValue * $difficultyLevel; + break; + + default: + // 默认按课时计算 + $value = $courseDuration * 50; + } + + return $value; + } + + /** + * 记录业绩 + * @param array $data 业绩数据 + * @return bool + */ + protected function recordPerformance(array $data) + { + // 准备记录数据 + $recordData = [ + 'staff_id' => $data['staff_id'], + 'performance_type' => $data['performance_type'], + 'performance_value' => $data['performance_value'], + 'remarks' => $data['remarks'] + ]; + + // 如果有课程ID,记录到扩展字段 + if (!empty($data['course_id'])) { + $recordData['extend_id'] = $data['course_id']; + $recordData['extend_type'] = 'course'; + } + + // 如果有学员ID,记录到相关字段 + if (!empty($data['student_id'])) { + $recordData['resource_id'] = $data['student_id']; + } + + // 查询是否已存在该课程的业绩记录 + $exists = (new PerformanceRecords())->where([ + ['extend_id', '=', $data['course_id'] ?? 0], + ['staff_id', '=', $data['staff_id']], + ['performance_type', '=', $data['performance_type']] + ])->find(); + + if ($exists) { + // 更新已有记录 + return (new PerformanceRecords())->where('id', $exists['id'])->update([ + 'performance_value' => $data['performance_value'], + 'remarks' => $data['remarks'], + 'updated_at' => date('Y-m-d H:i:s') + ]); + } else { + // 创建新记录 + $recordData['created_at'] = date('Y-m-d H:i:s'); + $recordData['updated_at'] = date('Y-m-d H:i:s'); + $model = new PerformanceRecords(); + $result = $model->create($recordData); + return !empty($result->id); + } + } +} \ No newline at end of file diff --git a/niucloud/app/listener/custom/SalesPerformanceListener.php b/niucloud/app/listener/custom/SalesPerformanceListener.php new file mode 100644 index 00000000..9617b924 --- /dev/null +++ b/niucloud/app/listener/custom/SalesPerformanceListener.php @@ -0,0 +1,125 @@ +find($params['order_id']); + if (empty($orderInfo)) { + return false; + } + + // 检查销售人员是否存在 + $personnel = (new Personnel())->find($params['staff_id']); + if (empty($personnel)) { + return false; + } + + // 业绩类型,默认为销售 + $performanceType = $params['performance_type'] ?? 'sales'; + + // 计算业绩值 + $performanceValue = $this->calculatePerformanceValue($orderInfo, $performanceType); + + // 记录业绩 + return $this->recordPerformance([ + 'staff_id' => $params['staff_id'], + 'order_id' => $params['order_id'], + 'resource_id' => $orderInfo['resource_id'] ?? 0, + 'order_status' => $orderInfo['status'], + 'performance_type' => $performanceType, + 'performance_value' => $performanceValue, + 'remarks' => '系统自动计算销售业绩', + ]); + } + + /** + * 计算业绩值 + * @param object $orderInfo 订单信息 + * @param string $performanceType 业绩类型 + * @return float 业绩值 + */ + protected function calculatePerformanceValue($orderInfo, $performanceType) + { + $value = 0; + + // 根据订单金额和业绩类型计算业绩值 + $orderAmount = floatval($orderInfo['total_amount'] ?? 0); + + if ($performanceType == 'sales') { + // 销售业绩直接等于订单金额 + $value = $orderAmount; + } elseif ($performanceType == 'commission') { + // 佣金业绩,假设为订单金额的10% + $commissionRate = 0.1; // 佣金比例,可以从配置中获取 + $value = $orderAmount * $commissionRate; + } + + return $value; + } + + /** + * 记录业绩 + * @param array $data 业绩数据 + * @return bool + */ + protected function recordPerformance(array $data) + { + // 查询是否已存在该订单的业绩记录 + $exists = (new PerformanceRecords())->where([ + ['order_id', '=', $data['order_id']], + ['staff_id', '=', $data['staff_id']], + ['performance_type', '=', $data['performance_type']] + ])->find(); + + if ($exists) { + // 更新已有记录 + return (new PerformanceRecords())->where('id', $exists['id'])->update([ + 'performance_value' => $data['performance_value'], + 'order_status' => $data['order_status'], + 'remarks' => $data['remarks'], + 'updated_at' => date('Y-m-d H:i:s') + ]); + } else { + // 创建新记录 + $data['created_at'] = date('Y-m-d H:i:s'); + $data['updated_at'] = date('Y-m-d H:i:s'); + $model = new PerformanceRecords(); + $result = $model->create($data); + return !empty($result->id); + } + } +} \ No newline at end of file