'ID', 'mall_id' => 'Mall ID', 'user_id' => 'User ID', 'hi_bei' => '嗨贝', 'hi_value' => '嗨值', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } public function getUser(){ return $this->hasOne(User::class, ['id' => 'user_id']); } public static function setData($params) { try { if (isset($params['id']) && !empty($params['id'])) { $model = self::findOne(['id' => $params['id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (!$model) { throw new \Exception('数据不存在或者已被删除'); } } else { $model = new self(); $model->loadDefaultValues(); } if (!$model->load($params, '') || !$model->save()) { throw new \Exception($model->getErrorMessage()); } return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 保存数据 * $params数组参数如下 * mall_id 必填 商城id * user_id 必填 用户id * is_deduct 非必填 余额不足是否扣减至0元(money必须为负数),值为[true,false] * money 必填 增加或扣减的金额 * source_table 非必填 来源表,如 addons_redpack * source_table_id 非必填 来源表id * from_type 来源 值请参考 HiGoEnum::getFromType() * desc 非必填 变动说明 * @Author: lun * @DateTime: 2022/3/22 0022 10:29 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $params * @param $is_log 是否生成wallet日志 * @return array|bool */ public static function setWallet($params, $is_log = true) { $trans = Yii::$app->db->beginTransaction(); // 加锁 $lock_key = RedisKeyEnum::suffix(HiGoEnum::LOCK_HIGO_USER_WALLET, $params['user_id']); $identification = uniqid(); try { if ($params['money'] == 0) throw new \Exception("higowallet变动金额不能为0"); // 判断是否有被锁住 if (!LockHelper::lock($lock_key, $identification, 100, 100)) { throw new \Exception("higowallet被锁定请稍后进行操作"); } $model = self::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]); if (empty($model)) { $model = new self(); $model->mall_id = $params['mall_id']; $model->user_id = $params['user_id']; $model->loadDefaultValues(); } // higo变动字段处理 $wallet_type = $params['wallet_type']; if ($params['money'] < 0) { if (($model[$wallet_type] + $params['money']) < 0) { if (isset($params['is_deduct']) && $params['is_deduct'] == true) { $params['money'] = -$model[$wallet_type]; } else { throw new \Exception(WalletLogic::getWalletTypeMsg($params['mall_id'], $wallet_type) . "余额不足"); } } } $model[$wallet_type] = $model[$wallet_type] + $params['money']; if (!$model->save()) throw new \Exception($model->getErrorMessage()); $after_num = $model[$wallet_type]; // 变动后余额 // 增加余额变动记录 if ($is_log) { $res = HigoWalletLog::setData([ 'mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'wallet_type' => $wallet_type, 'change_type' => $params['money'] > 0 ? CapitalLog::CHANGE_TYPE_ADD : CapitalLog::CHANGE_TYPE_SUB, 'change_num' => abs($params['money']), 'after_num' => $after_num, 'desc' => $params['desc'] ?? '', 'source_table' => $params['source_table'] ?? '', 'source_table_id' => $params['source_table_id'] ?? 0, 'from_type' => $params['from_type'], ]); if ($res == false) throw new \Exception(HigoWalletLog::getStaticError()); } $trans->commit(); // 解锁 LockHelper::uLock($lock_key, $identification); return [$wallet_type => $model->toArray(), 'money' => abs($params['money'])]; } catch (\Exception $e) { self::setStaticError($e->getMessage()); $trans->rollBack(); // 解锁 LockHelper::uLock($lock_key, $identification); return false; } } /** * @param $mall_id * @param $user_id * @return array */ public static function getUserWallet($mall_id,$user_id) { $wallet = self::getOne([['=', 'mall_id', $mall_id],['=', 'user_id', $user_id]],true); return $wallet ?:[]; } }