package com.cpop.core.base.entity;

import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.stp.StpUtil;
import com.cpop.core.constant.Constants;
import com.mybatisflex.annotation.UpdateListener;

import java.time.LocalDateTime;

/**
 * @author: DB
 * @Date: 2023/08/04/15:13
 * @Description:
 */
public class BaseUpdateListener implements UpdateListener {

    @Override
    public void onUpdate(Object entity) {
        BaseEntity baseEntity = (BaseEntity) entity;
        String userId;
        try {
            userId = StpUtil.getLoginIdDefaultNull() == null ? Constants.SUPER_ADMIN_ID : StpUtil.getLoginIdDefaultNull().toString();
        } catch (SaTokenException e) {
            userId = Constants.SUPER_ADMIN_ID;
        }
        //设置 account 被更新时的一些默认数据
        baseEntity.setUpdateTime(LocalDateTime.now());
        baseEntity.setUpdateUserId(userId);
    }
}
