| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- namespace App\Module\Point\Services;
- use App\Module\Point\Dto\CirculationDto;
- use App\Module\Point\Dto\PointAccountDto;
- use App\Module\Point\Dto\PointAdminDto;
- use App\Module\Point\Dto\TradeResultDto;
- use App\Module\Point\Dto\TransferResultDto;
- use App\Module\Point\Enums\POINT_TYPE;
- use App\Module\Point\Enums\LOG_TYPE;
- use App\Module\Point\Models\PointAdminModel;
- use App\Module\Point\Models\PointModel;
- use App\Module\Point\Logic\Circulation;
- use App\Module\Point\Logic\Transfer;
- use App\Module\Point\Logic\User;
- use Illuminate\Support\Facades\DB;
- use UCore\Db\Helper;
- /**
- * 积分服务类
- *
- * 提供积分相关的核心业务功能,专注于整数积分处理
- * 包括积分增减、流转、转账等操作
- */
- class PointService
- {
- /**
- * 用户ID
- */
- private int $userId;
- /**
- * 积分类型ID
- */
- private int $pointId;
- /**
- * 构造函数
- *
- * @param int $userId 用户ID
- * @param int $pointId 积分类型ID
- */
- public function __construct(int $userId, int $pointId)
- {
- $this->userId = $userId;
- $this->pointId = $pointId;
- }
- /**
- * 积分流转
- * (同用户,不同积分账户)
- *
- * @param POINT_TYPE $toPointId 目标积分类型
- * @param int $amount 积分数量
- * @param int $reId 关联ID
- * @param string $reType 关联类型
- * @param string $remark 备注
- * @return CirculationDto|string 成功返回DTO,失败返回错误信息
- */
- public function circulation(POINT_TYPE $toPointId, int $amount, int $reId, string $reType, string $remark)
- {
- # 检查积分类型是否相同
- if ($this->pointId === $toPointId->valueInt()) {
- return '源积分类型和目标积分类型不能相同';
- }
- # 检查积分数量
- if ($amount <= 0) {
- return '积分数量必须大于0';
- }
- Helper::check_tr();
-
- # 先进行流转记录
- $circulation_id = Circulation::handle($this->userId, $this->pointId, $toPointId->valueInt(), $amount, $reId, $reType, $remark);
- if (is_string($circulation_id)) {
- return $circulation_id;
- }
- # 进行双方的积分修改
- # 先减少源积分
- $result1 = User::handle($this->userId, $this->pointId, -$amount, LOG_TYPE::CIRCULATION, $circulation_id, $remark);
- if (is_string($result1)) {
- return $result1;
- }
- # 再增加目标积分
- $result2 = User::handle($this->userId, $toPointId->valueInt(), $amount, LOG_TYPE::CIRCULATION, $circulation_id, $remark);
- if (is_string($result2)) {
- return $result2;
- }
- # 返回流转结果
- return new CirculationDto([
- 'circulation_id' => $circulation_id,
- 'from_point_id' => $this->pointId,
- 'to_point_id' => $toPointId->valueInt(),
- 'amount' => $amount,
- 'user_id' => $this->userId,
- 'remark' => $remark
- ]);
- }
- /**
- * 转账
- * (不同人,同积分类型)
- *
- * @param int $toUserId 接收用户ID
- * @param int $amount 积分数量
- * @param string $remark 备注
- * @return TransferResultDto|string 成功返回DTO,失败返回错误信息
- */
- public function transfer(int $toUserId, int $amount, string $remark)
- {
- # 检查参数
- if ($this->userId === $toUserId) {
- return '不能向自己转账';
- }
- if ($amount <= 0) {
- return '积分数量必须大于0';
- }
- # 开启事务
- DB::beginTransaction();
-
- # 先进行转账记录
- $transfer_id = Transfer::to_user($this->userId, $this->pointId, $toUserId, $amount, $remark);
- if (is_string($transfer_id)) {
- DB::rollBack();
- return $transfer_id;
- }
- # 进行双方的积分修改
- # 先减少转出方积分
- $result1 = User::handle($this->userId, $this->pointId, -$amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
- if (is_string($result1)) {
- DB::rollBack();
- return $result1;
- }
- # 再增加转入方积分
- $result2 = User::handle($toUserId, $this->pointId, $amount, LOG_TYPE::TRANSFER, $transfer_id, $remark);
- if (is_string($result2)) {
- DB::rollBack();
- return $result2;
- }
- DB::commit();
- # 返回转账结果
- return new TransferResultDto([
- 'transfer_id' => $transfer_id,
- 'from_user_id' => $this->userId,
- 'to_user_id' => $toUserId,
- 'point_id' => $this->pointId,
- 'amount' => $amount,
- 'remark' => $remark
- ]);
- }
- /**
- * 增加积分
- *
- * @param int $amount 积分数量
- * @param LOG_TYPE $logType 日志类型
- * @param string $operateId 操作ID
- * @param string $remark 备注
- * @return TradeResultDto|string 成功返回DTO,失败返回错误信息
- */
- public function increase(int $amount, LOG_TYPE $logType, string $operateId, string $remark)
- {
- if ($amount <= 0) {
- return '积分数量必须大于0';
- }
- $result = User::handle($this->userId, $this->pointId, $amount, $logType, $operateId, $remark);
- if (is_string($result)) {
- return $result;
- }
- return new TradeResultDto([
- 'user_id' => $this->userId,
- 'point_id' => $this->pointId,
- 'amount' => $amount,
- 'log_type' => $logType,
- 'operate_id' => $operateId,
- 'remark' => $remark,
- 'success' => true
- ]);
- }
- /**
- * 减少积分
- *
- * @param int $amount 积分数量
- * @param LOG_TYPE $logType 日志类型
- * @param string $operateId 操作ID
- * @param string $remark 备注
- * @return TradeResultDto|string 成功返回DTO,失败返回错误信息
- */
- public function decrease(int $amount, LOG_TYPE $logType, string $operateId, string $remark)
- {
- if ($amount <= 0) {
- return '积分数量必须大于0';
- }
- $result = User::handle($this->userId, $this->pointId, -$amount, $logType, $operateId, $remark);
- if (is_string($result)) {
- return $result;
- }
- return new TradeResultDto([
- 'user_id' => $this->userId,
- 'point_id' => $this->pointId,
- 'amount' => -$amount,
- 'log_type' => $logType,
- 'operate_id' => $operateId,
- 'remark' => $remark,
- 'success' => true
- ]);
- }
- /**
- * 获取积分余额
- *
- * @return int 积分余额
- */
- public function getBalance(): int
- {
- return PointModel::getBalance($this->userId, $this->pointId);
- }
- /**
- * 检查积分余额是否足够
- *
- * @param int $amount 需要的积分数量
- * @return bool 是否足够
- */
- public function checkBalance(int $amount): bool
- {
- return PointModel::checkBalance($this->userId, $this->pointId, $amount);
- }
- /**
- * 获取用户积分账户信息
- *
- * @return PointAccountDto 积分账户DTO
- */
- public function getAccountInfo(): PointAccountDto
- {
- $account = PointModel::getAccount($this->userId, $this->pointId);
-
- return new PointAccountDto([
- 'user_id' => $this->userId,
- 'point_id' => $this->pointId,
- 'balance' => $account ? $account->balance : 0,
- 'create_time' => $account ? $account->create_time : 0,
- 'update_time' => $account ? $account->update_time : 0,
- ]);
- }
- /**
- * Admin 积分操作
- *
- * @param int $admin_id 管理员ID
- * @param POINT_TYPE $point_id 积分类型
- * @param int $point_amount 积分数量
- * @param string $remark 备注
- * @return PointAdminDto|string 成功返回DTO,失败返回错误信息
- */
- public function admin_operate(int $admin_id, POINT_TYPE $point_id, int $point_amount, string $remark)
- {
- $data = [
- 'total_points' => $point_amount,
- 'status' => 1,
- 'user_id' => $this->userId,
- 'point_id' => $point_id->valueInt(),
- 'admin_id' => $admin_id,
- 'create_time' => time(),
- 'remark' => $remark
- ];
- # 启动事务
- DB::beginTransaction();
-
- # 写日志
- $point_admin = new PointAdminModel();
- $point_admin->setData($data);
- if ($point_admin->save() === false) {
- DB::rollBack();
- return '_Model-error';
- }
- # 进行积分操作
- $result = User::handle($this->userId, $point_id->valueInt(), $point_amount, LOG_TYPE::ADMIN_OPERATE, $point_admin->id, $remark);
- if (is_string($result)) {
- DB::rollBack();
- return $result;
- }
- DB::commit();
- return new PointAdminDto([
- 'id' => $point_admin->id,
- 'user_id' => $this->userId,
- 'point_id' => $point_id->valueInt(),
- 'admin_id' => $admin_id,
- 'total_points' => $point_amount,
- 'remark' => $remark,
- 'create_time' => time()
- ]);
- }
- }
|