__('全部状态'), self::Recharge => __('充值'), self::Withdraw => __('提现'), self::Pay => __('交易'), self::PayBack => __('交易'), self::OrderBonus => __('佣金'), self::Commission => __('奖励'), self::SystemChange => __('系统'), self::Reject => __('驳回'), ]; return $status_names; } /* * 订单状态 * 0未支付 1完成 2冻结 3取消 */ public function getStatusNames($type = -1){ $status_names = $this->getStatusNamesArr(); if($type == -1){ return $status_names; } if(isset($status_names[$type])){ return $status_names[$type]; } return __('未定义'); } /** * 变更会员余额 * @param int $money 余额 * @param int $user_id 会员ID * @param string $memo 备注 */ public function change($user_id, $amount, $action, $from_id, $memo) { $user = (new Users())->lock(true)->find($user_id); if(empty($user)){ throw new Exception(__('会员信息不存在')); } if($amount == 0){ throw new Exception(__('参数有误')); } if (!array_key_exists($action, $this->getStatusNamesArr())) { throw new Exception(__('账变类型不存在')); } //余额 $balance = bcadd($user->balance, $amount, 2); //只有支付订单或系统调整的时候,可以余额为负数 if(($action != self::Pay && $action != self::SystemChange) && $amount < 0 && $balance < 0){ throw new Exception(__('余额不足')); } //更新会员信息 $user->save(['balance' => $balance]); //写入日志 MoneyLog::create([ 'user_id' => $user_id, 'amount' => $amount, 'balance' => $balance, 'action' => $action, 'from_id' => $from_id, 'note' => $memo ]); } // public function users() { return $this->hasOne('Users','id','user_id',[],'LEFT')->setEagerlyType(0); } public function fromuser() { return $this->hasOne('Users','id','from_id',[],'LEFT')->setEagerlyType(0); } public function getCreateTimeTextAttr($value, $data) { $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : ''); return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } protected function setCreateTimeAttr($value) { return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value); } }