| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace addons\ScoreExpansion\common\models;
- use addons\Redpack\common\enums\RedpackEnum;
- use addons\ScoreExpansion\common\enums\ScoreExpansionEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\helpers\LockHelper;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_redpack_wallet}}".
- *
- * @property int $id
- * @property int $mall_id 商城id
- * @property int $user_id 会员id
- * @property float $integral 激活积分
- * @property float $frozen_integral 冻结积分
- * @property float $total_integral 总激活积分
- * @property float $total_frozen_integral 总冻结积分
- * @property float $total_use_integral 已使用激活积分
- * @property float $total_use_frozen_integral 已使用冻结积分
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class ScoreExpansionWallet extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_score_expansion_wallet}}';
- }
- public static function briefTableName()
- {
- return 'addons_score_expansion_wallet';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['integral', 'frozen_integral', 'total_integral', 'total_frozen_integral', 'total_use_integral', 'total_use_frozen_integral'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城id',
- 'user_id' => '会员id',
- 'integral' => '激活积分',
- 'frozen_integral' => '冻结积分',
- 'total_integral' => '总激活积分',
- 'total_frozen_integral' => '总冻结积分',
- 'total_use_integral' => '已使用激活积分',
- 'total_use_frozen_integral' => '已使用冻结积分',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- * $params数组参数如下
- * mall_id 必填 商城id
- * user_id 必填 用户id
- * is_frozen 必填 是否是冻结,值为[true,false]
- * is_deduct 非必填 余额不足是否扣减至0元(money必须为负数),值为[true,false]
- * money 必填 增加或扣减的金额
- * source_table 非必填 来源表,如 addons_redpack
- * source_table_id 非必填 来源表id
- * from_type 来源 值请参考 RedpackEnum::getFromType()
- * desc 非必填 变动说明
- * @Author: lun
- * @DateTime: 2022/3/22 0022 10:29
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @param $is_log 是否生成红包日志
- * @return array|bool
- */
- public static function setData($params, $is_log = true)
- {
- $trans = Yii::$app->db->beginTransaction();
- // 加锁
- $lock_key = RedisKeyEnum::suffix(ScoreExpansionEnum::LOCK_WALLET , $params['user_id']);
- $identification = uniqid();
- try {
- if ($params['integral'] == 0) throw new \Exception("变动积分数额不能为0");
- // 判断是否有被锁住
- if (!LockHelper::lock($lock_key, $identification, 100, 100)) {
- throw new \Exception("积分被锁定请稍后进行操作");
- }
- $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();
- }
- // 红包变动字段处理
- $field = $params['is_frozen'] == false ? 'integral' : 'frozen_integral';
- $use_field = $params['is_frozen'] == false ? 'total_use_integral' : 'total_use_frozen_integral';
- $total_field = $params['is_frozen'] == false ? 'total_integral' : 'total_frozen_integral';
- $redpack_wallet_type = $params['is_frozen'] == false ? ScoreExpansionEnum::WALLET_INTEGRAL : ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN;
- $after_num = $model[$field];// 当前余额
- if ($params['integral'] < 0) {
- if (($model[$field] + $params['integral']) < 0) {
- if (isset($params['is_deduct']) && $params['is_deduct'] == true) {
- $params['integral'] = -$model[$field];
- if ($params['integral'] == 0) {
- throw new \Exception(ScoreExpansionEnum::getRedpackText($redpack_wallet_type, $params['mall_id']) . "余额不足");
- }
- } else {
- throw new \Exception(ScoreExpansionEnum::getRedpackText($redpack_wallet_type, $params['mall_id']) . "余额不足");
- }
- }
- isset($params['money']) && $model[$use_field] = $model[$use_field] + abs($params['money']);
- }
- $model[$field] = $model[$field] + $params['integral'];
- $params['integral'] > 0 && empty($params['no_icr_total']) && $model[$total_field] = $model[$total_field] + $params['integral'];
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- // 增加余额变动记录
- if ($is_log) {
- $res = ScoreExpansionWalletLog::setData([
- 'mall_id' => $params['mall_id'],
- 'user_id' => $params['user_id'],
- 'integral_wallet_type' => $redpack_wallet_type,
- 'change_type' => $params['integral'] > 0 ? ScoreExpansionEnum::CHANGE_TYPE_ADD : ScoreExpansionEnum::CHANGE_TYPE_SUB,
- 'change_num' => abs($params['integral']),
- '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'],
- 'addon_name' => $params['addon_name'] ?? ScoreExpansionEnum::ADDONS_NAME,
- ]);
- if ($res == false) throw new \Exception(ScoreExpansionWalletLog::getStaticError());
- }
- $trans->commit();
- // 解锁
- LockHelper::uLock($lock_key, $identification);
- return ['redpack' => $model->toArray(), 'money' => abs($params['integral']), 'log' => isset($res) ? $res->toArray() : []];
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- $trans->rollBack();
- // 解锁
- LockHelper::uLock($lock_key, $identification);
- return false;
- }
- }
- }
|