/** * @brief 账户减除或者增加金额 * * @return public function * @retval * @see * @note * @author 吕宝贵 * @date 2015/12/30 14:39:00 **/ public function balance($balanceType, $money, $transId, $transTypeId, $transTypeName, $description, $currency = 1) { if ($balanceType === static::BALANCE_TYPE_PLUS) { $this->balance += $money; } else { if ($balanceType === static::BALANCE_TYPE_MINUS) { $this->balance -= $money; } else { $this->addError('uid', '不支持提交的账户操作类型'); return false; } } if ($this->save()) { //记录账单 $bill = new Bill(); $bill->uid = $this->uid; $bill->trans_id = $transId; $bill->trans_type_id = $transTypeId; $bill->trans_type_name = $transTypeName; $bill->money = $money; $bill->balance_type = $balanceType; $bill->currency = $currency; $bill->description = $description; if (!$bill->save()) { $this->addErrors($bill->getErrors()); return false; } //账户快照产生 $accountLog = new UserAccountLog(); $accountLog->uid = $this->uid; $accountLog->account_type = $this->type; $accountLog->currency = $currency; $accountLog->balance = $this->balance; $accountLog->deposit = $this->deposit; $accountLog->frozen_money = $this->frozen_money; $accountLog->balance_type = $balanceType; $accountLog->trans_money = $money; $accountLog->trans_desc = $description; if (!$accountLog->save()) { $this->addErrors($accountLog->getErrors()); return false; } return true; } else { return false; } }
/** * @brief 用户帐户变动的核心函数,所有的变动都需要通过此函数来记录 * * @return public function * @retval * @see * @note * @author 吕宝贵 * @date 2015/12/30 14:39:00 **/ protected function balance($uid, $balanceType, $money, $transId, $description, $currency) { $freezeCat = false; $freeze = null; $trans = null; Yii::warning('进入balance页面', __METHOD__); Yii::warning('uid and balanceType:' . $uid . '---' . $balanceType, __METHOD__); if (in_array($balanceType, [UserAccount::BALANCE_TYPE_FREEZE, UserAccount::BALANCE_TYPE_UNFREEZE, UserAccount::BALANCE_TYPE_FINISH_FREEZE])) { $freezeCat = true; $freeze = Freeze::findOne($transId); if (!$freeze) { $this->addError('trans', '不存在该交易订单'); return false; } } else { //根据交易号查找对应的交易 $trans = Trans::findOne($transId); if (!$trans) { $this->addError('trans', '不存在该交易订单'); return false; } } Yii::warning('获取用户帐户', __METHOD__); $userAccount = $this->getUserAccount($uid); Yii::warning('判断账户变更类型', __METHOD__); switch ($balanceType) { case UserAccount::BALANCE_TYPE_PLUS: $userAccount->plus($money); break; case UserAccount::BALANCE_TYPE_MINUS: $userAccount->minus($money); break; case UserAccount::BALANCE_TYPE_FREEZE: Yii::warning('进入帐号金额冻结'); $userAccount->freeze($money); break; case UserAccount::BALANCE_TYPE_UNFREEZE: $userAccount->unFreeze($money); break; case UserAccount::BALANCE_TYPE_FINISH_FREEZE: $userAccount->finishFreeze($money); break; default: $this->addError(__METHOD__, '不支持提交的账户操作类型'); return false; } if ($userAccount->save()) { //记录账单 Yii::warning('产生用户账单', __METHOD__); $bill = new Bill(); $bill->uid = $userAccount->uid; if (!$freezeCat) { $bill->trans_id = $trans->id; $bill->trans_type_id = $trans->trans_type_id; $bill->trans_type_name = $trans->transType->name; } else { $bill->trans_id = $freeze->id; } $bill->money = $money; $bill->balance_type = $balanceType; $bill->currency = $currency; $bill->description = $description; if (!$bill->save()) { $this->addErrors($bill->getErrors()); return false; } Yii::warning('产生账户快照', __METHOD__); //账户快照产生 $accountLog = new UserAccountLog(); $accountLog->uid = $userAccount->uid; $accountLog->account_type = $userAccount->type; $accountLog->currency = $currency; if (!$freezeCat) { $accountLog->trans_id = $trans->id; } else { $accountLog->trans_id = $freeze->id; } $accountLog->balance = $userAccount->balance; $accountLog->deposit = $userAccount->deposit; $accountLog->frozen_money = $userAccount->frozen_money; $accountLog->balance_type = $balanceType; $accountLog->trans_money = $money; $accountLog->trans_desc = $description; Yii::warning('保存账户信息', __METHOD__); if (!$accountLog->save()) { //$this->addErrors($accountLog->getErrors()); Yii::warning('保存账户快照失败', __METHOD__); Yii::warning($accountLog->getErrors(), __METHOD__); return false; } else { Yii::warning('保存账户快照成功', __METHOD__); return true; } } else { return false; } }
/** * @brief 获取一个trans_id对应的所有账单 * * @return public function * @retval * @see * @note * @author 吕宝贵 * @date 2015/11/30 16:50:53 **/ public function getBills() { return $this->hasMany(Bill::className(), ['trans_id' => 'id']); }