|
@@ -13,6 +13,7 @@ use think\Exception;
|
|
use think\Db;
|
|
use think\Db;
|
|
use app\common\exception\BusinessException;
|
|
use app\common\exception\BusinessException;
|
|
use app\common\Enum\OrderEnum;
|
|
use app\common\Enum\OrderEnum;
|
|
|
|
+use app\common\Service\Lottery\LotteryActivityService;
|
|
/**
|
|
/**
|
|
* 抽奖机会服务类
|
|
* 抽奖机会服务类
|
|
* 处理用户获得抽奖机会的逻辑
|
|
* 处理用户获得抽奖机会的逻辑
|
|
@@ -51,7 +52,7 @@ class LotteryChanceService
|
|
|
|
|
|
// try {
|
|
// try {
|
|
// 获取当前正在进行的单个抽奖活动
|
|
// 获取当前正在进行的单个抽奖活动
|
|
- $activity = static::getCurrentRunningActivity();
|
|
|
|
|
|
+ $activity = LotteryActivityService::getCurrentRunningActivity();
|
|
|
|
|
|
// 如果没有正在进行的活动,返回null
|
|
// 如果没有正在进行的活动,返回null
|
|
if (!$activity) {
|
|
if (!$activity) {
|
|
@@ -94,7 +95,7 @@ class LotteryChanceService
|
|
|
|
|
|
try {
|
|
try {
|
|
// 获取所有正在进行的抽奖活动 一段时间 有且只有一个抽奖活动
|
|
// 获取所有正在进行的抽奖活动 一段时间 有且只有一个抽奖活动
|
|
- $activities = LotteryService::getRunningActivities();
|
|
|
|
|
|
+ $activities = LotteryActivityService::getRunningActivities();
|
|
|
|
|
|
foreach ($activities as $activity) {
|
|
foreach ($activities as $activity) {
|
|
try {
|
|
try {
|
|
@@ -137,7 +138,7 @@ class LotteryChanceService
|
|
|
|
|
|
try {
|
|
try {
|
|
// 获取当前正在进行的单个抽奖活动
|
|
// 获取当前正在进行的单个抽奖活动
|
|
- $activity = static::getCurrentRunningActivity();
|
|
|
|
|
|
+ $activity = LotteryActivityService::getCurrentRunningActivity();
|
|
|
|
|
|
// 如果没有正在进行的活动,返回null
|
|
// 如果没有正在进行的活动,返回null
|
|
if (!$activity) {
|
|
if (!$activity) {
|
|
@@ -182,7 +183,7 @@ class LotteryChanceService
|
|
|
|
|
|
try {
|
|
try {
|
|
// 获取所有正在进行的抽奖活动
|
|
// 获取所有正在进行的抽奖活动
|
|
- $activities = LotteryService::getRunningActivities();
|
|
|
|
|
|
+ $activities = LotteryActivityService::getRunningActivities();
|
|
|
|
|
|
foreach ($activities as $activity) {
|
|
foreach ($activities as $activity) {
|
|
try {
|
|
try {
|
|
@@ -295,19 +296,7 @@ class LotteryChanceService
|
|
return $result;
|
|
return $result;
|
|
}
|
|
}
|
|
|
|
|
|
- // ============ 活动查询方法 ============
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取当前正在进行的单个抽奖活动
|
|
|
|
- * 假设同一时间段只有一个活动在进行
|
|
|
|
- *
|
|
|
|
- * @return LotteryActivity|null
|
|
|
|
- */
|
|
|
|
- public static function getCurrentRunningActivity()
|
|
|
|
- {
|
|
|
|
- $activities = LotteryService::getRunningActivities();
|
|
|
|
- return empty($activities) ? null : $activities[0];
|
|
|
|
- }
|
|
|
|
|
|
|
|
// ============ 私有处理方法 ============
|
|
// ============ 私有处理方法 ============
|
|
|
|
|
|
@@ -863,8 +852,8 @@ class LotteryChanceService
|
|
];
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
- // 获取机会获得记录
|
|
|
|
- $getRecords = LotteryUserChanceRecord::getUserChanceRecords($activityId, $userId);
|
|
|
|
|
|
+ // 获取机会获得记录(使用服务类方法)
|
|
|
|
+ $getRecords = static::getUserChanceRecord($userId, $activityId);
|
|
|
|
|
|
return [
|
|
return [
|
|
'total_chances' => $userChance->total_chances,
|
|
'total_chances' => $userChance->total_chances,
|
|
@@ -1233,7 +1222,12 @@ class LotteryChanceService
|
|
*/
|
|
*/
|
|
public static function getUserChanceRecords($activityId, $userId, $page = 1, $limit = 20)
|
|
public static function getUserChanceRecords($activityId, $userId, $page = 1, $limit = 20)
|
|
{
|
|
{
|
|
- return LotteryUserChanceRecord::getUserChanceRecords($activityId, $userId, $page, $limit);
|
|
|
|
|
|
+ return LotteryUserChanceRecord::where('activity_id', $activityId)
|
|
|
|
+ ->where('user_id', $userId)
|
|
|
|
+ ->with(['condition', 'order', 'admin'])
|
|
|
|
+ ->order('get_time desc')
|
|
|
|
+ ->page($page, $limit)
|
|
|
|
+ ->select();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1244,7 +1238,32 @@ class LotteryChanceService
|
|
*/
|
|
*/
|
|
public static function getActivityChanceRecordStats($activityId)
|
|
public static function getActivityChanceRecordStats($activityId)
|
|
{
|
|
{
|
|
- return LotteryUserChanceRecord::getActivityChanceStats($activityId);
|
|
|
|
|
|
+ $records = LotteryUserChanceRecord::where('activity_id', $activityId)->select();
|
|
|
|
+
|
|
|
|
+ $stats = [
|
|
|
|
+ 'total_records' => count($records),
|
|
|
|
+ 'total_chances' => 0,
|
|
|
|
+ 'type_stats' => [],
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ foreach ($records as $record) {
|
|
|
|
+ $stats['total_chances'] += $record->chances;
|
|
|
|
+
|
|
|
|
+ $getType = $record->get_type;
|
|
|
|
+ if (!isset($stats['type_stats'][$getType])) {
|
|
|
|
+ $stats['type_stats'][$getType] = [
|
|
|
|
+ 'type' => $getType,
|
|
|
|
+ 'type_text' => LotteryEnum::getChanceGetTypeText($getType),
|
|
|
|
+ 'count' => 0,
|
|
|
|
+ 'chances' => 0,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $stats['type_stats'][$getType]['count']++;
|
|
|
|
+ $stats['type_stats'][$getType]['chances'] += $record->chances;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $stats;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1254,9 +1273,48 @@ class LotteryChanceService
|
|
* @param int $activityId 活动ID(可选)
|
|
* @param int $activityId 活动ID(可选)
|
|
* @return array
|
|
* @return array
|
|
*/
|
|
*/
|
|
- public static function getUserChanceRecordStats($userId, $activityId = null)
|
|
|
|
|
|
+ public static function getUserChanceRecord($userId, $activityId = null)
|
|
{
|
|
{
|
|
- return LotteryUserChanceRecord::getUserChanceStats($userId, $activityId);
|
|
|
|
|
|
+ $query = LotteryUserChanceRecord::where('user_id', $userId);
|
|
|
|
+
|
|
|
|
+ if ($activityId) {
|
|
|
|
+ $query->where('activity_id', $activityId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $records = $query->select();
|
|
|
|
+
|
|
|
|
+ $stats = [
|
|
|
|
+ 'total_records' => count($records),
|
|
|
|
+ 'total_chances' => 0,
|
|
|
|
+ 'type_stats' => [],
|
|
|
|
+ 'recent_records' => [],
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ foreach ($records as $record) {
|
|
|
|
+ $stats['total_chances'] += $record->chances;
|
|
|
|
+
|
|
|
|
+ $getType = $record->get_type;
|
|
|
|
+ if (!isset($stats['type_stats'][$getType])) {
|
|
|
|
+ $stats['type_stats'][$getType] = [
|
|
|
|
+ 'type' => $getType,
|
|
|
|
+ 'type_text' => LotteryEnum::getChanceGetTypeText($getType),
|
|
|
|
+ 'count' => 0,
|
|
|
|
+ 'chances' => 0,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $stats['type_stats'][$getType]['count']++;
|
|
|
|
+ $stats['type_stats'][$getType]['chances'] += $record->chances;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 获取最近的记录
|
|
|
|
+ $stats['recent_records'] = LotteryUserChanceRecord::where('user_id', $userId)
|
|
|
|
+ ->with(['activity'])
|
|
|
|
+ ->order('get_time desc')
|
|
|
|
+ ->limit(5)
|
|
|
|
+ ->select();
|
|
|
|
+
|
|
|
|
+ return $stats;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -1267,17 +1325,152 @@ class LotteryChanceService
|
|
*/
|
|
*/
|
|
public static function batchCreateChanceRecords($records)
|
|
public static function batchCreateChanceRecords($records)
|
|
{
|
|
{
|
|
- return LotteryUserChanceRecord::batchCreateRecords($records);
|
|
|
|
|
|
+ if (empty($records)) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 为每条记录添加创建时间
|
|
|
|
+ foreach ($records as &$record) {
|
|
|
|
+ $record['createtime'] = time();
|
|
|
|
+ $record['get_time'] = $record['get_time'] ?? time();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return LotteryUserChanceRecord::insertAll($records);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 验证机会获取记录数据
|
|
* 验证机会获取记录数据
|
|
*
|
|
*
|
|
* @param array $data 记录数据
|
|
* @param array $data 记录数据
|
|
- * @return bool|string
|
|
|
|
|
|
+ * @return bool|string true表示验证通过,string表示错误信息
|
|
*/
|
|
*/
|
|
public static function validateChanceRecord($data)
|
|
public static function validateChanceRecord($data)
|
|
{
|
|
{
|
|
- return LotteryUserChanceRecord::validateRecord($data);
|
|
|
|
|
|
+ // 验证必填字段
|
|
|
|
+ if (empty($data['activity_id'])) {
|
|
|
|
+ return '活动ID不能为空';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (empty($data['user_id'])) {
|
|
|
|
+ return '用户ID不能为空';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (empty($data['get_type'])) {
|
|
|
|
+ return '获取类型不能为空';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (empty($data['chances']) || $data['chances'] <= 0) {
|
|
|
|
+ return '机会次数必须大于0';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 验证获取类型是否有效
|
|
|
|
+ if (!LotteryEnum::isValidChanceGetType($data['get_type'])) {
|
|
|
|
+ return '无效的获取类型';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 根据获取类型验证相关字段
|
|
|
|
+ switch ($data['get_type']) {
|
|
|
|
+ case LotteryEnum::CHANCE_GET_TYPE_BUY_GOODS:
|
|
|
|
+ case LotteryEnum::CHANCE_GET_TYPE_ORDER_AMOUNT:
|
|
|
|
+ if (empty($data['order_id'])) {
|
|
|
|
+ return '订单ID不能为空';
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case LotteryEnum::CHANCE_GET_TYPE_RECHARGE:
|
|
|
|
+ if (empty($data['recharge_amount']) || $data['recharge_amount'] <= 0) {
|
|
|
|
+ return '充值金额必须大于0';
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case LotteryEnum::CHANCE_GET_TYPE_ADMIN_GRANT:
|
|
|
|
+ if (empty($data['admin_id'])) {
|
|
|
|
+ return '管理员ID不能为空';
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建单条机会获取记录
|
|
|
|
+ *
|
|
|
|
+ * @param array $data 记录数据
|
|
|
|
+ * @return bool|int 成功返回记录ID,失败返回false
|
|
|
|
+ */
|
|
|
|
+ public static function createChanceRecord($data)
|
|
|
|
+ {
|
|
|
|
+ // 验证数据
|
|
|
|
+ $validation = static::validateChanceRecord($data);
|
|
|
|
+ if ($validation !== true) {
|
|
|
|
+ throw new BusinessException($validation);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 设置默认值
|
|
|
|
+ $data['createtime'] = time();
|
|
|
|
+ $data['get_time'] = $data['get_time'] ?? time();
|
|
|
|
+
|
|
|
|
+ $record = new LotteryUserChanceRecord($data);
|
|
|
|
+ if ($record->save()) {
|
|
|
|
+ return $record->id;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取用户在某个活动中的最新记录
|
|
|
|
+ *
|
|
|
|
+ * @param int $activityId 活动ID
|
|
|
|
+ * @param int $userId 用户ID
|
|
|
|
+ * @param int $limit 限制数量
|
|
|
|
+ * @return array
|
|
|
|
+ */
|
|
|
|
+ public static function getUserLatestRecords($activityId, $userId, $limit = 10)
|
|
|
|
+ {
|
|
|
|
+ return LotteryUserChanceRecord::where('activity_id', $activityId)
|
|
|
|
+ ->where('user_id', $userId)
|
|
|
|
+ ->with(['condition', 'order', 'admin'])
|
|
|
|
+ ->order('get_time desc')
|
|
|
|
+ ->limit($limit)
|
|
|
|
+ ->select();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取活动中某种获取类型的记录统计
|
|
|
|
+ *
|
|
|
|
+ * @param int $activityId 活动ID
|
|
|
|
+ * @param int $getType 获取类型
|
|
|
|
+ * @return array
|
|
|
|
+ */
|
|
|
|
+ public static function getActivityRecordsByType($activityId, $getType)
|
|
|
|
+ {
|
|
|
|
+ $records = LotteryUserChanceRecord::where('activity_id', $activityId)
|
|
|
|
+ ->where('get_type', $getType)
|
|
|
|
+ ->select();
|
|
|
|
+
|
|
|
|
+ $stats = [
|
|
|
|
+ 'total_records' => count($records),
|
|
|
|
+ 'total_chances' => 0,
|
|
|
|
+ 'users' => [],
|
|
|
|
+ ];
|
|
|
|
+
|
|
|
|
+ foreach ($records as $record) {
|
|
|
|
+ $stats['total_chances'] += $record->chances;
|
|
|
|
+
|
|
|
|
+ if (!isset($stats['users'][$record->user_id])) {
|
|
|
|
+ $stats['users'][$record->user_id] = [
|
|
|
|
+ 'user_id' => $record->user_id,
|
|
|
|
+ 'records' => 0,
|
|
|
|
+ 'chances' => 0,
|
|
|
|
+ ];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $stats['users'][$record->user_id]['records']++;
|
|
|
|
+ $stats['users'][$record->user_id]['chances'] += $record->chances;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $stats;
|
|
}
|
|
}
|
|
}
|
|
}
|