HigoWallet.php 6.4 KB

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