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.InsertListener;

import java.time.LocalDateTime;

/**
 * @author: DB
 * @Date: 2023/08/04/15:12
 * @Description: 添加自动填充
 */
public class BaseInsertListener implements InsertListener {

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