AccountService.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\Accounts;
  4. use app\common\traits\ServiceTrait;
  5. use think\Log;
  6. use think\Db;
  7. class AccountService
  8. {
  9. use ServiceTrait;
  10. /** @var Accounts */
  11. public static $Model = Accounts::class;
  12. /**
  13. * 更新账户余额 正值为加,负值为减
  14. * @param $user_id
  15. * @param $amount
  16. * @param int|float $freeze
  17. * @param int $integral
  18. * @param int $credit_score
  19. * @param int $log_id
  20. * @return mixed
  21. * @throws \Exception
  22. */
  23. public static function updateData($user_id, $amount, $freeze = 0, int $credit_score = 0, int $integral = 0, int $log_id = 0)
  24. {
  25. $fields = [
  26. 'amount' => $amount,
  27. 'freeze' => $freeze,
  28. 'integral' => $integral,
  29. 'credit_score' => $credit_score,
  30. ];
  31. $where = ['user_id' => $user_id];
  32. $old_account = self::getOne($where);
  33. if (!$old_account) {
  34. exception('该账户未找到');
  35. }
  36. // 检查每个字段更新后是否为负值
  37. foreach ($fields as $field => $change) {
  38. if ($change < 0) {
  39. $current = $old_account->$field;
  40. $required = -$change;
  41. if ($current < $required) {
  42. $accountTypeName = AccountLogService::ACCOUNT_TYPE_MAP[$field] ?? '余额';
  43. Log::info(__CLASS__ . "账户{$accountTypeName}不足,更新失败: 当前={$current}, 扣除={$required}");
  44. exception("账户{$accountTypeName}不足");
  45. }
  46. }
  47. }
  48. // 构建要更新的字段
  49. $updateData = [];
  50. foreach ($fields as $field => $change) {
  51. if ($change != 0) {
  52. $updateData[$field] = Db::raw("{$field} + {$change}");
  53. }
  54. }
  55. $res = self::where($where)->update($updateData);
  56. if (!$res) {
  57. Log::info(__CLASS__ . ' 更新用户余额失败:' . json_encode($updateData, JSON_UNESCAPED_UNICODE));
  58. exception('更新用户余额失败');
  59. }
  60. //记录数据变动日志
  61. $now_account = self::getOne($where);
  62. DataLogService::addLog('accounts', 'account_logs', $log_id, $old_account->toArray(), $now_account->toArray());
  63. return $res;
  64. }
  65. /**
  66. * @param $demandId
  67. * @param $userId
  68. * @return void
  69. * @throws \Exception
  70. */
  71. public static function freezeAmountRefund($demandId, $userId)
  72. {
  73. $list = AccountLogService::where(['refer_id' => $demandId, 'user_id' => $userId, 'account_type' => AccountLogService::ACCOUNT_TYPE_FREEZE, 'type' => AccountLogService::TYPE_FREEZE, 'status' => CommonService::STATUS_OFF])->select();
  74. foreach ($list as $item) {
  75. $amount = $item->amount;
  76. $log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, '需求完成失败-冻结金额退回', AccountLogService::TYPE_FREEZE, 0, AccountLogService::INC_EXP_EXPEND, $demandId);
  77. //减去冻结
  78. AccountService::updateData($userId, 0, -$amount, 0, 0, $log_res->id);
  79. $item->status = CommonService::STATUS_ON;
  80. $saveRes = $item->save();
  81. if (!$saveRes) {
  82. exception('更新冻结余额日志失败');
  83. }
  84. }
  85. }
  86. }