| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- /**
- * UserGrowthValue.php
- * todo 文件描述
- * Created on 2025/4/17 15:45
- * @author: hankaige
- */
- namespace app\models;
- /**
- * This is the model class for table "{{%user_growth_value_log}}".
- * @property int $id
- * @property int $store_id
- * @property int $user_id
- * @property int $log_type
- * @property int $amount
- * @property int $from
- * @property int $created_at
- * @property string $desc
- * @property int $after
- * @property int $before
- */
- class UserGrowthValueLog extends \yii\db\ActiveRecord
- {
- const LOG_TYPE_ADD = 1;
- const LOG_TYPE_SUB = 2;
- const TYPE_BUY = 1;
- const TYPE_SHARE = 2;
- const TYPE_SIGN = 3;
- const TYPE_CON = 4;
- const LOG_TYPE_MAP = [
- self::LOG_TYPE_ADD => '增加',
- self::LOG_TYPE_SUB => '减少',
- ];
- const TYPE_MAP = [
- self::TYPE_BUY => '消费获赠',
- self::TYPE_SHARE => '分享注册获赠',
- self::TYPE_SIGN => '签到获赠',
- self::TYPE_CON => '每日消耗',
- ];
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%user_growth_value_log}}';
- }
- public function rules()
- {
- return [
- [['store_id','user_id','log_type','amount','from','created_at','after','before'],'integer'],
- [['desc'],'string'],
- [['log_type'],'in','range'=>[1,2]],
- [['from'],'in','range'=>[1,2,3,4]],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => 'Store ID',
- 'user_id' => 'User ID',
- 'log_type' => 'Log Type',
- 'amount' => 'Amount',
- 'type' => 'Type',
- 'created_at' => 'Created At',
- 'desc' => 'Desc',
- 'after' => 'After',
- 'before' => 'Before',
- ];
- }
- public static function setLog($storeId,$userId,$logType,$amount,$type,$desc){
- $t = \Yii::$app->db->beginTransaction();
- try{
- $user = User::findOne($userId);
- $before = $user->growth_value;
- if($logType == self::LOG_TYPE_SUB){
- if($amount > $user->growth_value){
- $user->growth_value = 0;
- }else{
- $user->growth_value -= $amount;
- }
- }else{
- $user->growth_value += $amount;
- }
- if(!$user->save()){
- $t->rollBack();
- return false;
- }
- $after = $user->growth_value;;
- $model = new self();
- $model->store_id = $storeId;
- $model->user_id = $userId;
- $model->log_type = $logType;
- $model->amount = $amount;
- $model->from = $type;
- $model->desc = $desc;
- $model->created_at = time();
- $model->before = $before;
- $model->after = $after;
- if(!$model->save()){
- $t->rollBack();
- return false;
- }
- $t->commit();
- return true;
- }catch(\Exception $e){
- $t->rollBack();
- return false;
- }
- }
- }
|