UserGrowthValueLog.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * UserGrowthValue.php
  4. * todo 文件描述
  5. * Created on 2025/4/17 15:45
  6. * @author: hankaige
  7. */
  8. namespace app\models;
  9. /**
  10. * This is the model class for table "{{%user_growth_value_log}}".
  11. * @property int $id
  12. * @property int $store_id
  13. * @property int $user_id
  14. * @property int $log_type
  15. * @property int $amount
  16. * @property int $from
  17. * @property int $created_at
  18. * @property string $desc
  19. * @property int $after
  20. * @property int $before
  21. */
  22. class UserGrowthValueLog extends \yii\db\ActiveRecord
  23. {
  24. const LOG_TYPE_ADD = 1;
  25. const LOG_TYPE_SUB = 2;
  26. const TYPE_BUY = 1;
  27. const TYPE_SHARE = 2;
  28. const TYPE_SIGN = 3;
  29. const TYPE_CON = 4;
  30. const LOG_TYPE_MAP = [
  31. self::LOG_TYPE_ADD => '增加',
  32. self::LOG_TYPE_SUB => '减少',
  33. ];
  34. const TYPE_MAP = [
  35. self::TYPE_BUY => '消费获赠',
  36. self::TYPE_SHARE => '分享注册获赠',
  37. self::TYPE_SIGN => '签到获赠',
  38. self::TYPE_CON => '每日消耗',
  39. ];
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%user_growth_value_log}}';
  46. }
  47. public function rules()
  48. {
  49. return [
  50. [['store_id','user_id','log_type','amount','from','created_at','after','before'],'integer'],
  51. [['desc'],'string'],
  52. [['log_type'],'in','range'=>[1,2]],
  53. [['from'],'in','range'=>[1,2,3,4]],
  54. ];
  55. }
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'id' => 'ID',
  60. 'store_id' => 'Store ID',
  61. 'user_id' => 'User ID',
  62. 'log_type' => 'Log Type',
  63. 'amount' => 'Amount',
  64. 'type' => 'Type',
  65. 'created_at' => 'Created At',
  66. 'desc' => 'Desc',
  67. 'after' => 'After',
  68. 'before' => 'Before',
  69. ];
  70. }
  71. public static function setLog($storeId,$userId,$logType,$amount,$type,$desc){
  72. $t = \Yii::$app->db->beginTransaction();
  73. try{
  74. $user = User::findOne($userId);
  75. $before = $user->growth_value;
  76. if($logType == self::LOG_TYPE_SUB){
  77. if($amount > $user->growth_value){
  78. $user->growth_value = 0;
  79. }else{
  80. $user->growth_value -= $amount;
  81. }
  82. }else{
  83. $user->growth_value += $amount;
  84. }
  85. if(!$user->save()){
  86. $t->rollBack();
  87. return false;
  88. }
  89. $after = $user->growth_value;;
  90. $model = new self();
  91. $model->store_id = $storeId;
  92. $model->user_id = $userId;
  93. $model->log_type = $logType;
  94. $model->amount = $amount;
  95. $model->from = $type;
  96. $model->desc = $desc;
  97. $model->created_at = time();
  98. $model->before = $before;
  99. $model->after = $after;
  100. if(!$model->save()){
  101. $t->rollBack();
  102. return false;
  103. }
  104. $t->commit();
  105. return true;
  106. }catch(\Exception $e){
  107. $t->rollBack();
  108. return false;
  109. }
  110. }
  111. }