SaasUserPriceLog.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use Yii;
  9. use yii\behaviors\TimestampBehavior;
  10. /**
  11. * This is the model class for table "{{%saas_user_price_log}}".
  12. *
  13. * @property integer $id
  14. * @property integer $user_id
  15. * @property string $order_no
  16. * @property integer $amount_type
  17. * @property integer $amount
  18. * @property string $desc
  19. * @property integer $before
  20. * @property integer $after
  21. * @property integer $log_type
  22. * @property integer $created_at
  23. */
  24. class SaasUserPriceLog extends \yii\db\ActiveRecord
  25. {
  26. /**
  27. * 收入类型
  28. */
  29. CONST LOG_TYPE_INCOME = 1;
  30. /**
  31. * 支出类型
  32. */
  33. CONST LOG_TYPE_EXPEND = 2;
  34. public static $type_budget = [
  35. self::LOG_TYPE_INCOME,
  36. self::LOG_TYPE_EXPEND
  37. ];
  38. const AMOUNT_TYPE_SUPPLIER_PARENT = 11;
  39. /**
  40. * @inheritdoc
  41. */
  42. public static function tableName()
  43. {
  44. return '{{%saas_user_price_log}}';
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function rules()
  50. {
  51. return [
  52. [['saas_id', 'amount', 'desc', 'before', 'after', 'log_type'], 'required'],
  53. [['saas_id', 'created_at', 'log_type', 'amount_type'], 'integer'],
  54. [['amount', 'before', 'after'], 'number'],
  55. [['desc', 'order_no'], 'string'],
  56. ];
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'saas_id' => 'saas_id',
  66. 'order_no' => '订单',
  67. 'amount_type' => '佣金类型:0:店铺分销,1:saas平台分销, 2:推荐分账, 3:服务商分账',
  68. 'amount' => '变动数',
  69. 'desc' => '变动说明',
  70. 'before' => '变动前',
  71. 'after' => '变动后',
  72. 'log_type' => '类型:1=收入,2=支出',
  73. 'created_at' => '添加时间',
  74. ];
  75. }
  76. /**
  77. *
  78. * @param $saas_id
  79. * @param $amount
  80. * @param $amount_type
  81. * @param $log_type
  82. * @param int $order_no
  83. * @param string $desc
  84. * @return bool
  85. */
  86. public static function saveLog($saas_id, $amount, $amount_type, $log_type, $order_no = '', $desc = "") {
  87. try{
  88. $t = \Yii::$app->db->beginTransaction();
  89. $form = new self();
  90. $saas = SaasUser::findOne($saas_id);
  91. if(!$saas){
  92. return false;
  93. }
  94. $form->saas_id = $saas_id;
  95. $form->amount = $amount;
  96. $form->amount_type = $amount_type;
  97. $form->before = $saas->price;
  98. if ($log_type == self::LOG_TYPE_INCOME) {
  99. $form->after = $saas->price + $amount;
  100. } else {
  101. $form->after = $saas->price - $amount;
  102. }
  103. $form->desc = $desc;
  104. $form->order_no = $order_no;
  105. $form->log_type = $log_type;
  106. $form->created_at = time();
  107. if ($form->save()) {
  108. if ($log_type == AccountLog::LOG_TYPE_INCOME) {
  109. $saas->price += $amount;
  110. $saas->total_price += $amount;
  111. } else {
  112. $saas->price -= $amount;
  113. }
  114. if($saas->save()){
  115. $t->commit();
  116. return true;
  117. }
  118. }
  119. } catch (\Exception $e) {
  120. \Yii::error(['saasUserPriceLog->saveLog', $form, $saas]);
  121. }
  122. $t->rollBack();
  123. return false;
  124. }
  125. }