| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace App\Module\Point\Logic;
- use App\Module\Point\Models\PointCirculationModel;
- /**
- * 积分流转逻辑类
- *
- * 处理同一用户不同积分账户间的流转操作
- * 专注于整数积分处理,不涉及小数运算
- */
- class Circulation
- {
- /**
- * 处理积分流转
- *
- * @param int $userId 用户ID
- * @param int $fromPointId 源积分类型ID
- * @param int $toPointId 目标积分类型ID
- * @param int $amount 流转积分数量
- * @param int $reId 关联ID
- * @param string $reType 关联类型
- * @param string $remark 备注
- * @return int|string 成功返回流转记录ID,失败返回错误信息
- */
- public static function handle(int $userId, int $fromPointId, int $toPointId, int $amount, int $reId, string $reType, string $remark)
- {
- # 参数验证
- if ($userId <= 0) {
- return '用户ID无效';
- }
- if ($fromPointId === $toPointId) {
- return '源积分类型和目标积分类型不能相同';
- }
- if ($amount <= 0) {
- return '流转积分数量必须大于0';
- }
- if (empty($reType)) {
- return '关联类型不能为空';
- }
- # 创建流转记录
- $circulation = PointCirculationModel::createRecord(
- $userId,
- $fromPointId,
- $toPointId,
- $amount,
- $reId,
- $reType,
- $remark
- );
- if (!$circulation) {
- return '创建流转记录失败';
- }
- return $circulation->id;
- }
- /**
- * 批量处理积分流转
- *
- * @param array $circulations 流转操作数组
- * @return array 处理结果数组
- */
- public static function batchHandle(array $circulations): array
- {
- $results = [];
- foreach ($circulations as $index => $circulation) {
- if (!isset(
- $circulation['user_id'],
- $circulation['from_point_id'],
- $circulation['to_point_id'],
- $circulation['amount'],
- $circulation['re_id'],
- $circulation['re_type'],
- $circulation['remark']
- )) {
- $results[$index] = '流转参数不完整';
- continue;
- }
- $result = self::handle(
- $circulation['user_id'],
- $circulation['from_point_id'],
- $circulation['to_point_id'],
- $circulation['amount'],
- $circulation['re_id'],
- $circulation['re_type'],
- $circulation['remark']
- );
- $results[$index] = $result;
- }
- return $results;
- }
- /**
- * 完成流转操作
- *
- * @param int $circulationId 流转记录ID
- * @return bool|string 成功返回true,失败返回错误信息
- */
- public static function complete(int $circulationId)
- {
- $circulation = PointCirculationModel::find($circulationId);
- if (!$circulation) {
- return '流转记录不存在';
- }
- if ($circulation->isCompleted()) {
- return '流转记录已完成';
- }
- if ($circulation->isFailed()) {
- return '流转记录已失败,无法完成';
- }
- return $circulation->markCompleted();
- }
- /**
- * 标记流转失败
- *
- * @param int $circulationId 流转记录ID
- * @return bool|string 成功返回true,失败返回错误信息
- */
- public static function fail(int $circulationId)
- {
- $circulation = PointCirculationModel::find($circulationId);
- if (!$circulation) {
- return '流转记录不存在';
- }
- if ($circulation->isCompleted()) {
- return '流转记录已完成,无法标记为失败';
- }
- if ($circulation->isFailed()) {
- return '流转记录已失败';
- }
- return $circulation->markFailed();
- }
- /**
- * 获取用户流转记录
- *
- * @param int $userId 用户ID
- * @param int $limit 限制数量
- * @return array 流转记录数组
- */
- public static function getUserRecords(int $userId, int $limit = 50): array
- {
- $records = PointCirculationModel::getUserRecords($userId, $limit);
- $result = [];
- foreach ($records as $record) {
- $result[] = [
- 'id' => $record->id,
- 'user_id' => $record->user_id,
- 'from_point_id' => $record->from_point_id,
- 'to_point_id' => $record->to_point_id,
- 'amount' => $record->amount,
- 're_id' => $record->re_id,
- 're_type' => $record->re_type,
- 'remark' => $record->remark,
- 'status' => $record->status,
- 'status_name' => $record->getStatusName(),
- 'create_time' => $record->create_time,
- 'update_time' => $record->update_time,
- ];
- }
- return $result;
- }
- /**
- * 获取指定积分类型的流转记录
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- * @param int $limit 限制数量
- * @return array 流转记录数组
- */
- public static function getPointRecords(int $userId, int $pointId, int $limit = 50): array
- {
- $records = PointCirculationModel::getPointRecords($userId, $pointId, $limit);
- $result = [];
- foreach ($records as $record) {
- $result[] = [
- 'id' => $record->id,
- 'user_id' => $record->user_id,
- 'from_point_id' => $record->from_point_id,
- 'to_point_id' => $record->to_point_id,
- 'amount' => $record->amount,
- 're_id' => $record->re_id,
- 're_type' => $record->re_type,
- 'remark' => $record->remark,
- 'status' => $record->status,
- 'status_name' => $record->getStatusName(),
- 'create_time' => $record->create_time,
- 'update_time' => $record->update_time,
- ];
- }
- return $result;
- }
- /**
- * 验证流转操作的有效性
- *
- * @param int $userId 用户ID
- * @param int $fromPointId 源积分类型ID
- * @param int $toPointId 目标积分类型ID
- * @param int $amount 流转积分数量
- * @return bool|string 有效返回true,无效返回错误信息
- */
- public static function validate(int $userId, int $fromPointId, int $toPointId, int $amount)
- {
- # 检查用户ID
- if ($userId <= 0) {
- return '用户ID无效';
- }
- # 检查积分类型
- if ($fromPointId === $toPointId) {
- return '源积分类型和目标积分类型不能相同';
- }
- # 检查积分数量
- if ($amount <= 0) {
- return '流转积分数量必须大于0';
- }
- # 检查源积分余额
- $checkResult = User::checkBalance($userId, $fromPointId, $amount);
- if ($checkResult !== true) {
- return $checkResult;
- }
- return true;
- }
- }
|