TempUserWallet.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\logic\CurrencyLogic;
  9. use Exception;
  10. use Yii;
  11. use yii\behaviors\TimestampBehavior;
  12. use yii\db\ActiveRecord;
  13. /**
  14. * This is the model class for table "{{%temp_user_wallet}}".
  15. * @property int id ID
  16. * @property int store_id
  17. * @property int user_id
  18. * @property int saas_id
  19. * @property float money
  20. * @property float money_total
  21. * @property float frozen
  22. * @property float withdraw
  23. * @property string currency_code
  24. * @property int created_at
  25. * @property int updated_at
  26. */
  27. class TempUserWallet extends \yii\db\ActiveRecord
  28. {
  29. /**
  30. * 商城订单
  31. */
  32. const TYPE_ORDER = 'order';
  33. /**
  34. * 当面付
  35. */
  36. const TYPE_SCAN = 'scan';
  37. /**
  38. * 合伙人
  39. */
  40. const TYPE_PARTNER = 'partner';
  41. /**
  42. * 红包
  43. */
  44. const TYPE_RED_PACKET = 'red_packet';
  45. /**
  46. * 提现
  47. */
  48. const TYPE_CASH = 'cash';
  49. /**
  50. * 后台改动
  51. */
  52. const TYPE_OPERATOR_BACK = 'operator_back';
  53. const TYPE_NAME_LIST = array(
  54. self::TYPE_ORDER => '商城订单',
  55. self::TYPE_SCAN => '当面付',
  56. self::TYPE_RED_PACKET => '串码红包',
  57. self::TYPE_CASH => '提现',
  58. self::TYPE_OPERATOR_BACK => '后台充值',
  59. );
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public static function tableName()
  64. {
  65. return '{{%temp_user_wallet}}';
  66. }
  67. public function behaviors()
  68. {
  69. return [
  70. [
  71. 'class' => TimestampBehavior::class,
  72. 'attributes' => [
  73. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  74. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  75. ]
  76. ]
  77. ];
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function rules()
  83. {
  84. return [
  85. [['store_id', 'user_id', 'saas_id'], 'integer'],
  86. [['money', 'money_total', 'frozen', 'withdraw'], 'number'],
  87. [['created_at', 'updated_at'], 'safe'],
  88. [['currency_code'], 'string', 'max' => 50],
  89. ];
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function attributeLabels()
  95. {
  96. return [
  97. 'id' => 'ID',
  98. 'store_id' => '店铺id',
  99. 'user_id' => '用户id',
  100. 'saas_id' => '联盟id',
  101. 'money' => '余额',
  102. 'money_total' => '余额总金额',
  103. 'frozen' => '冻结',
  104. 'withdraw' => '提现',
  105. 'currency_code' => '业务货币代码',
  106. 'created_at' => '创建时间',
  107. 'updated_at' => '更新时间',
  108. ];
  109. }
  110. /**
  111. * 获取用户业务货币钱包
  112. */
  113. public static function getCurrencyWallet($store_id, $user_id, $currency_code)
  114. {
  115. $currency = Currency::findOne(['code' => $currency_code]);
  116. $wallet = self::find()->where(['user_id' => $user_id, 'store_id' => $store_id, 'currency_code' => $currency_code])->one();
  117. if (empty($wallet)) {
  118. $wallet = new self();
  119. $wallet->store_id = $store_id;
  120. $wallet->user_id = $user_id;
  121. if ($store_id == -1) {
  122. $wallet->saas_id = $user_id;
  123. } else {
  124. $wallet->saas_id = SaasUser::findSaasIdByUserId($user_id);
  125. }
  126. $wallet->currency_code = $currency_code;
  127. if (!$wallet->save()) {
  128. $msg = "店铺:{$store_id},业务:{$currency_code},用户:{$user_id} 钱包保存失败" . json_encode($wallet->getErrors());
  129. //debug_log([__METHOD__, __LINE__, $msg], "app_debug.log");
  130. }
  131. }
  132. $wallet->money = round($wallet->money, $currency->scale);
  133. $wallet->money_total = round($wallet->money_total, $currency->scale);
  134. $wallet->frozen = round($wallet->frozen, $currency->scale);
  135. $wallet->withdraw = round($wallet->withdraw, $currency->scale);
  136. return $wallet;
  137. }
  138. public static function getCurrencyWalletBalance($saas_id, $currency_code)
  139. {
  140. $currency = Currency::findOne(['code' => $currency_code]);
  141. $total = TempUserWallet::find()->where([
  142. 'saas_id' => $saas_id,
  143. 'currency_code' => $currency_code
  144. ])->select("SUM(`money`) as total")->scalar();
  145. return $total > 0 ? round($total, $currency->scale) : 0;
  146. }
  147. /**
  148. * 新增余额操作
  149. * @param $currency
  150. * @param $user_id
  151. * @param $store_id
  152. * @param $money
  153. * @param $desc
  154. * @param $type
  155. * @param string $source_table
  156. * @param int $source_id
  157. * @param bool $reduce_frozen
  158. * @return boolean
  159. */
  160. public static function addLog($currency, $store_id, $user_id, $money, $desc, $type, string $source_table = '', int $source_id = 0, bool $reduce_frozen = false)
  161. {
  162. $trans = Yii::$app->db->beginTransaction();
  163. try {
  164. // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} enable:{$currency['enable']} =============="], "app_debug.log");
  165. if ($currency['enable'] == 0) throw new Exception('货币' . $currency['code'] . '未开启');
  166. if ($money == 0 || empty($user_id)) {
  167. $trans->commit();
  168. return 1;
  169. }
  170. $user_wallet = self::getCurrencyWallet($store_id, $user_id, $currency['code']);
  171. if (($money + $user_wallet->money) < 0) {
  172. // //debug_log([__METHOD__, __LINE__, "============== UserWallet addLog money:{$money} user_id:{$user_id} currency:{$currency['code']} 不足 =============="], "app_debug.log");
  173. throw new Exception($currency['name'] . '不足');
  174. }
  175. if ($money < 0 && $user_wallet->money <= abs($money)) {
  176. $money = -$user_wallet->money;
  177. }
  178. $before_money = $user_wallet->money ?? 0;
  179. $user_wallet->money += $money * 1;
  180. if ($money > 0) {
  181. $user_wallet->money_total += $money * 1;
  182. }
  183. $res = $user_wallet->save();
  184. if ($res === false) throw new Exception($user_wallet->getErrorMessage());
  185. $trans->commit();
  186. } catch (Exception $e) {
  187. $trans->rollBack();
  188. //debug_log([__METHOD__, __LINE__, "用户钱包保存失败:{$e->getMessage()}"], "app_wallet_debug.log");
  189. return false;
  190. }
  191. }
  192. }