| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace common\models\user;
- use common\models\base\BaseActiveRecord;
- use services\common\UserWalletService;
- use common\enums\RedisKeyEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\LockHelper;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%balance_recharge_log}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id
- * @property float $num 充值金额
- * @property int $withdraw_status 申请状态 0--申请 1--同意 2--驳回
- * @property string $remark 备注
- * @property string $payment_evidence 支付凭证
- * @property int $status 状态[0禁用 1启用 -1删除]
- * @property int $created_at 添加时间戳
- * @property int $updated_at 更新时间戳
- */
- class BalanceRechargeLog extends BaseActiveRecord
- {
- const STATUS_APPLY = 0;
- const STATUS_AGREE = 1;
- const STATUS_REJECT = 2;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%balance_recharge_log}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id'], 'required'],
- [['mall_id', 'user_id', 'withdraw_status', 'status', 'created_at', 'updated_at'], 'integer'],
- [['num'], 'number'],
- [['remark', 'payment_evidence'], 'string', 'max' => 255],
- ];
- }
- public function getUser(): \yii\db\ActiveQuery
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'user_id' => 'User ID',
- 'num' => 'Num',
- 'withdraw_status' => 'Withdraw Status',
- 'remark' => 'Remark',
- 'payment_evidence' => 'Payment Evidence',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- * @param array $params
- * @return bool|UserWallet
- */
- public static function setData(array $params){
- try{
- if(!empty($params['user_id'])){
- $exist = self::find()->where(['user_id'=>$params['user_id'],'withdraw_status'=>BalanceRechargeLog::STATUS_APPLY])->select('id')->one();
- if($exist){
- throw new \Exception('尚有未审核的申请!');
- }
- }
- if(!empty($params['id'])){
- $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
- if(empty($model)) throw new \Exception('数据不存在或已删除');
- }else{
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->loadDefaultValues();
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new Exception($model->getErrorMessage());
- }
- return $model->toArray();
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @throws Exception
- */
- public static function updateLog($params)
- {
- $lock_key = RedisKeyEnum::suffix('BalanceRecharge', $params['id']);
- $identification = uniqid();
- if (!LockHelper::lock($lock_key, $identification, 100, 100)) {
- \Yii::error('充值审核 获取锁(' . $lock_key . ')失败阻塞,充值审核');
- return false;
- }
- $where = [['id' => $params['id']]];
- $log = self::getOne($where);
- if (!$log) {
- throw new \Exception('记录不存在');
- }
- if ($log->withdraw_status == 1) {
- throw new \Exception('审核已通过');
- }
- if ($log->withdraw_status == 2) {
- throw new \Exception('审核已驳回');
- }
- if ($params['status'] <= $log->withdraw_status) {
- throw new \Exception('状态错误, 请刷新重试');
- }
- $t = \Yii::$app->db->beginTransaction();
- try {
- switch ($params['status']) {// 1=提现申请通过,2=拒绝提现
- case 1:
- self::remit($log,$params);
- break;
- case 2:
- self::reject($log,$params);
- break;
- default:
- throw new \Exception('错误的类型');
- }
- $t->commit();
- LockHelper::uLock($lock_key, $identification);
- } catch (\Exception $exception) {
- $t->rollBack();
- LockHelper::uLock($lock_key, $identification);
- \Yii::error("BalanceRecharge error=" . $exception->getFile() . ";line:" . $exception->getLine() . ";message:" . $exception->getMessage());
- throw new \Exception($exception->getMessage());
- }
- return true;
- }
- /**
- * @throws Exception
- */
- private static function remit($log,$params)
- {
- $userInfo = User::getOne([['id' => $log->user_id, 'status' => StatusEnum::ENABLED]]);
- if (!$userInfo) {
- throw new \Exception('该用户不存在!');
- }
- $desc = "用户线下充值到余额:" . $log->num . "元";
- $data = [
- 'message_id' => 0,
- 'mall_id' => $log->mall_id, 'user_id' => $log->user_id,
- 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE,
- 'is_frozen' => false, 'unfrozen' => false, 'money' => abs($log->num), 'desc' => $desc,
- 'source_table' => self::tableName(), 'source_table_id' => $log->id,
- 'from_type' => UserWalletEnum::RECHARGE,
- 'capital_type' => UserWalletEnum::RECHARGE_OFFLINE,
- ];
- $res = UserWalletService::handle(0, $data);
- if ($res) {
- $log->withdraw_status = BalanceRechargeLog::STATUS_AGREE;
- if(!empty($params['remark'])){
- $log->remark = $params['remark'];
- }
- if(!$log->save()){
- throw new \Exception('保存失败');
- }
- }else{
- throw new \Exception($msg);
- }
- }
- /**
- * @Note:拒绝
- * @param $log
- * @return void
- * @throws \Exception
- */
- private static function reject($log,$params)
- {
- if (empty($params['remark'])) {
- throw new \Exception('请填写备注');
- }
- $log->withdraw_status = BalanceRechargeLog::STATUS_REJECT;
- $log->remark = $params['remark'];
- if (!$log->save()) {
- throw new \Exception('保存失败');
- }
- return true;
- }
- }
|