WithdrawsLimitLog.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace common\models\user;
  3. use common\models\base\BaseActiveRecord;
  4. use common\enums\StatusEnum;
  5. use Exception;
  6. use Yii;
  7. use yii\helpers\Json;
  8. /**
  9. * This is the model class for table "{{%withdraws_limit_log}}".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城ID
  13. * @property int $user_id 消费者ID
  14. * @property float $amount 提现额度,单位:元
  15. * @property int $from 提现来源1佣金2余额
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at 创建时间
  18. * @property int $updated_at 修改时间
  19. * @property int $type 0:公共额度(如果开启提现余额限额则不包含余额的额度),1:提现余额额度
  20. */
  21. class WithdrawsLimitLog extends BaseActiveRecord
  22. {
  23. const COMMON_LIMIT = 0;
  24. const BALANCE_LIMIT = 1;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%withdraws_limit_log}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'user_id', 'from', 'status', 'created_at', 'updated_at', 'type'], 'integer'],
  39. [['amount'], 'number'],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'mall_id' => 'Mall ID',
  50. 'user_id' => 'User ID',
  51. 'amount' => 'Amount',
  52. 'from' => 'From',
  53. 'status' => 'Status',
  54. 'created_at' => 'Created At',
  55. 'updated_at' => 'Updated At',
  56. 'type' => 'Type',
  57. ];
  58. }
  59. /**
  60. * 保存数据
  61. * @param array $params
  62. * @return bool|UserWallet
  63. */
  64. public static function setData(array $params)
  65. {
  66. try {
  67. if (!empty($params['user_id'])) {
  68. $model = self::findOne(['user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'type' => $params['type']]);
  69. if (empty($model)) {
  70. $model = new self();
  71. $model->loadDefaultValues();
  72. }
  73. } else {
  74. if (empty($model)) throw new Exception('用户尚未注册,请先注册');
  75. }
  76. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  77. return $model;
  78. } catch (Exception $e) {
  79. self::$static_error = $e->getMessage();
  80. return false;
  81. }
  82. }
  83. public static function resetCashAmount($params){
  84. $user_id = $params['user_id'];
  85. $mall_id = $params['mall_id'];
  86. $payment = \Yii::$app->services->setting->mall->get($mall_id, 'withdraw_income');
  87. if(!empty($payment["withdraws_limit_enable"])){
  88. $withdraws_limit_money = isset($payment["withdraws_limit_money"]) ? $payment["withdraws_limit_money"] : 0;
  89. $withdrawsLimitLog = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED,'from'=>1, 'type' => self::COMMON_LIMIT]);
  90. if(!empty($withdrawsLimitLog)){
  91. $withdrawsLimitLog->amount = $withdraws_limit_money;
  92. if(!$withdrawsLimitLog->save()){
  93. \Yii::error('提现额度保存失败,user_id:'.$user_id);
  94. return false;
  95. }
  96. }
  97. if (!empty($payment['is_independent_balance_withdraw'])) {
  98. $withdraws_limit_money = $payment["independent_balance_withdraw_max_amount"] ?? 0;
  99. $withdrawsLimitLog = self::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED,'from'=>1, 'type' => self::BALANCE_LIMIT]);
  100. if(!empty($withdrawsLimitLog)){
  101. $withdrawsLimitLog->amount = $withdraws_limit_money;
  102. if(!$withdrawsLimitLog->save()){
  103. \Yii::error('提现额度保存失败,user_id:'.$user_id);
  104. return false;
  105. }
  106. }
  107. }
  108. }
  109. return true;
  110. }
  111. /**
  112. * 根据配置如果开启了 提现余额限额 则type=1
  113. *
  114. * @param int $mallId
  115. * @param string $withdrawType
  116. * @param array $setting
  117. *
  118. * @return int
  119. */
  120. public static function getType(int $mallId, string $withdrawType, array $setting = []): int
  121. {
  122. if ($withdrawType !== 'balance') {
  123. return self::COMMON_LIMIT;
  124. }
  125. if (empty($setting)) {
  126. $setting = \Yii::$app->services->setting->mall->get($mallId, 'withdraw_income');
  127. }
  128. return !empty($setting['withdraws_limit_enable']) && !empty($setting['is_independent_balance_withdraw']) ? self::BALANCE_LIMIT : self::COMMON_LIMIT;
  129. }
  130. }