ScoreExpansionWallet.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace addons\ScoreExpansion\common\models;
  3. use addons\Redpack\common\enums\RedpackEnum;
  4. use addons\ScoreExpansion\common\enums\ScoreExpansionEnum;
  5. use common\enums\RedisKeyEnum;
  6. use common\enums\StatusEnum;
  7. use common\helpers\LockHelper;
  8. use Yii;
  9. use yii\db\Exception;
  10. /**
  11. * This is the model class for table "{{%addons_redpack_wallet}}".
  12. *
  13. * @property int $id
  14. * @property int $mall_id 商城id
  15. * @property int $user_id 会员id
  16. * @property float $integral 激活积分
  17. * @property float $frozen_integral 冻结积分
  18. * @property float $total_integral 总激活积分
  19. * @property float $total_frozen_integral 总冻结积分
  20. * @property float $total_use_integral 已使用激活积分
  21. * @property float $total_use_frozen_integral 已使用冻结积分
  22. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  23. * @property int $created_at
  24. * @property int $updated_at
  25. */
  26. class ScoreExpansionWallet extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_score_expansion_wallet}}';
  34. }
  35. public static function briefTableName()
  36. {
  37. return 'addons_score_expansion_wallet';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['mall_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
  46. [['integral', 'frozen_integral', 'total_integral', 'total_frozen_integral', 'total_use_integral', 'total_use_frozen_integral'], 'number'],
  47. ];
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'mall_id' => '商城id',
  57. 'user_id' => '会员id',
  58. 'integral' => '激活积分',
  59. 'frozen_integral' => '冻结积分',
  60. 'total_integral' => '总激活积分',
  61. 'total_frozen_integral' => '总冻结积分',
  62. 'total_use_integral' => '已使用激活积分',
  63. 'total_use_frozen_integral' => '已使用冻结积分',
  64. 'status' => '状态[-1:删除;0:禁用;1启用]',
  65. 'created_at' => 'Created At',
  66. 'updated_at' => 'Updated At',
  67. ];
  68. }
  69. /**
  70. * 保存数据
  71. * $params数组参数如下
  72. * mall_id 必填 商城id
  73. * user_id 必填 用户id
  74. * is_frozen 必填 是否是冻结,值为[true,false]
  75. * is_deduct 非必填 余额不足是否扣减至0元(money必须为负数),值为[true,false]
  76. * money 必填 增加或扣减的金额
  77. * source_table 非必填 来源表,如 addons_redpack
  78. * source_table_id 非必填 来源表id
  79. * from_type 来源 值请参考 RedpackEnum::getFromType()
  80. * desc 非必填 变动说明
  81. * @Author: lun
  82. * @DateTime: 2022/3/22 0022 10:29
  83. * @Copyright: copyright (c) 2021 广东七件事集团
  84. * @param $params
  85. * @param $is_log 是否生成红包日志
  86. * @return array|bool
  87. */
  88. public static function setData($params, $is_log = true)
  89. {
  90. $trans = Yii::$app->db->beginTransaction();
  91. // 加锁
  92. $lock_key = RedisKeyEnum::suffix(ScoreExpansionEnum::LOCK_WALLET , $params['user_id']);
  93. $identification = uniqid();
  94. try {
  95. if ($params['integral'] == 0) throw new \Exception("变动积分数额不能为0");
  96. // 判断是否有被锁住
  97. if (!LockHelper::lock($lock_key, $identification, 100, 100)) {
  98. throw new \Exception("积分被锁定请稍后进行操作");
  99. }
  100. $model = self::findOne(['user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
  101. if (empty($model)) {
  102. $model = new self();
  103. $model->mall_id = $params['mall_id'];
  104. $model->user_id = $params['user_id'];
  105. $model->loadDefaultValues();
  106. }
  107. // 红包变动字段处理
  108. $field = $params['is_frozen'] == false ? 'integral' : 'frozen_integral';
  109. $use_field = $params['is_frozen'] == false ? 'total_use_integral' : 'total_use_frozen_integral';
  110. $total_field = $params['is_frozen'] == false ? 'total_integral' : 'total_frozen_integral';
  111. $redpack_wallet_type = $params['is_frozen'] == false ? ScoreExpansionEnum::WALLET_INTEGRAL : ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN;
  112. $after_num = $model[$field];// 当前余额
  113. if ($params['integral'] < 0) {
  114. if (($model[$field] + $params['integral']) < 0) {
  115. if (isset($params['is_deduct']) && $params['is_deduct'] == true) {
  116. $params['integral'] = -$model[$field];
  117. if ($params['integral'] == 0) {
  118. throw new \Exception(ScoreExpansionEnum::getRedpackText($redpack_wallet_type, $params['mall_id']) . "余额不足");
  119. }
  120. } else {
  121. throw new \Exception(ScoreExpansionEnum::getRedpackText($redpack_wallet_type, $params['mall_id']) . "余额不足");
  122. }
  123. }
  124. isset($params['money']) && $model[$use_field] = $model[$use_field] + abs($params['money']);
  125. }
  126. $model[$field] = $model[$field] + $params['integral'];
  127. $params['integral'] > 0 && empty($params['no_icr_total']) && $model[$total_field] = $model[$total_field] + $params['integral'];
  128. if (!$model->save()) throw new Exception($model->getErrorMessage());
  129. // 增加余额变动记录
  130. if ($is_log) {
  131. $res = ScoreExpansionWalletLog::setData([
  132. 'mall_id' => $params['mall_id'],
  133. 'user_id' => $params['user_id'],
  134. 'integral_wallet_type' => $redpack_wallet_type,
  135. 'change_type' => $params['integral'] > 0 ? ScoreExpansionEnum::CHANGE_TYPE_ADD : ScoreExpansionEnum::CHANGE_TYPE_SUB,
  136. 'change_num' => abs($params['integral']),
  137. 'after_num' => $after_num,
  138. 'desc' => $params['desc'] ?? '',
  139. 'source_table' => $params['source_table'] ?? '',
  140. 'source_table_id' => $params['source_table_id'] ?? 0,
  141. 'from_type' => $params['from_type'],
  142. 'addon_name' => $params['addon_name'] ?? ScoreExpansionEnum::ADDONS_NAME,
  143. ]);
  144. if ($res == false) throw new \Exception(ScoreExpansionWalletLog::getStaticError());
  145. }
  146. $trans->commit();
  147. // 解锁
  148. LockHelper::uLock($lock_key, $identification);
  149. return ['redpack' => $model->toArray(), 'money' => abs($params['integral']), 'log' => isset($res) ? $res->toArray() : []];
  150. } catch (\Exception $e) {
  151. self::setStaticError($e->getMessage());
  152. $trans->rollBack();
  153. // 解锁
  154. LockHelper::uLock($lock_key, $identification);
  155. return false;
  156. }
  157. }
  158. }