WithdrawMoneyLog.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\model\user;
  13. use app\common\model\BaseModel;
  14. use app\common\enum\user\balanceLog\Scene as SceneEnum;
  15. use app\common\model\User;
  16. use Exception;
  17. use think\facade\Db;
  18. use think\facade\Log;
  19. /**
  20. * 用户余额变动明细模型
  21. * Class BalanceLog
  22. * @package app\common\model\user
  23. */
  24. class WithdrawMoneyLog extends BaseModel
  25. {
  26. // 定义表名
  27. protected $name = 'withdraw_money_log';
  28. // 定义主键
  29. protected $pk = 'log_id';
  30. protected $updateTime = false;
  31. /**
  32. * 关联会员记录表
  33. * @return \think\model\relation\BelongsTo
  34. */
  35. public function user()
  36. {
  37. $module = self::getCalledModule();
  38. return $this->belongsTo("app\\{$module}\\model\\User");
  39. }
  40. /**
  41. * 新增记录
  42. * @param int $scene
  43. * @param array $data
  44. * @param array $describeParam
  45. */
  46. public static function add(int $scene, array $data, array $describeParam)
  47. {
  48. $model = new static;
  49. $model->save(array_merge([
  50. 'scene' => $scene,
  51. 'describe' => vsprintf(SceneEnum::data()[$scene]['describe'], $describeParam),
  52. 'store_id' => $model::$storeId
  53. ], $data));
  54. }
  55. /**
  56. * 记录分佣明细
  57. * @param $userId
  58. * @param $scene
  59. * @param $money
  60. * @param string $describe
  61. * @param string $remark
  62. * @return bool
  63. * @throws Exception
  64. */
  65. public static function addNewLog($userId,$scene,$money,$describe='',$remark=''){
  66. $log = new self();
  67. $user = User::find($userId);
  68. if ($money == 0){
  69. return $user?$user->can_withdraw_money:0;
  70. }
  71. try {
  72. if (!$user){
  73. throw new Exception('用户不存在'.$userId);
  74. }
  75. $log->save([
  76. 'user_id' => $userId,
  77. 'scene' => $scene,
  78. 'money' => $money,
  79. 'describe' => $describe,
  80. 'remark' => $remark,
  81. ]);
  82. //提现余额也要累加计算
  83. $user->can_withdraw_money = Db::raw('can_withdraw_money+'.$money);
  84. //可提现金额
  85. $user->ktxyj_amount = Db::raw('ktxyj_amount+'.$money);
  86. $user->save();
  87. }catch (Exception $e){
  88. Log::error('order_id::'.$remark.',desc::'.$describe.','.$e->getMessage());
  89. //throw $e;
  90. return false;
  91. }
  92. $user = User::find($userId);
  93. return $user->can_withdraw_money;
  94. }
  95. }