// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\common\model\user; use app\common\model\BaseModel; use app\common\enum\user\balanceLog\Scene as SceneEnum; use app\common\model\User; use Exception; use think\facade\Db; use think\facade\Log; /** * 用户余额变动明细模型 * Class BalanceLog * @package app\common\model\user */ class WithdrawMoneyLog extends BaseModel { // 定义表名 protected $name = 'withdraw_money_log'; // 定义主键 protected $pk = 'log_id'; protected $updateTime = false; /** * 关联会员记录表 * @return \think\model\relation\BelongsTo */ public function user() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\User"); } /** * 新增记录 * @param int $scene * @param array $data * @param array $describeParam */ public static function add(int $scene, array $data, array $describeParam) { $model = new static; $model->save(array_merge([ 'scene' => $scene, 'describe' => vsprintf(SceneEnum::data()[$scene]['describe'], $describeParam), 'store_id' => $model::$storeId ], $data)); } /** * 记录分佣明细 * @param $userId * @param $scene * @param $money * @param string $describe * @param string $remark * @return bool * @throws Exception */ public static function addNewLog($userId,$scene,$money,$describe='',$remark=''){ $log = new self(); $user = User::find($userId); if ($money == 0){ return $user?$user->can_withdraw_money:0; } try { if (!$user){ throw new Exception('用户不存在'.$userId); } $log->save([ 'user_id' => $userId, 'scene' => $scene, 'money' => $money, 'describe' => $describe, 'remark' => $remark, ]); //提现余额也要累加计算 $user->can_withdraw_money = Db::raw('can_withdraw_money+'.$money); //可提现金额 $user->ktxyj_amount = Db::raw('ktxyj_amount+'.$money); $user->save(); }catch (Exception $e){ Log::error('order_id::'.$remark.',desc::'.$describe.','.$e->getMessage()); //throw $e; return false; } $user = User::find($userId); return $user->can_withdraw_money; } }