| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace addons\HiGo\common\models;
- use addons\HiGo\common\enums\HiGoEnum;
- use addons\HiGo\common\logic\WalletLogic;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\helpers\LockHelper;
- use common\models\common\CapitalLog;
- use common\models\user\User;
- use Yii;
- /**
- * This is the model class for table "{{%addons_higo_wallet}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 会员id
- * @property float $hi_bei 当前嗨呗数量
- * @property float $hi_value 当前嗨值数量
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- * @property float $freeze_hi_value
- * @property float $freeze_hi_bei
- */
- class HigoWallet extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_higo_wallet}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['hi_bei', 'hi_value','freeze_hi_value','freeze_hi_value'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '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 ?:[];
- }
- }
|