package com.taais.common.orm.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.taais.common.core.exception.ServiceException;
import com.taais.common.orm.core.domain.BaseEntity;
import com.taais.common.security.utils.LoginHelper;
import com.mybatisflex.annotation.InsertListener;

import java.util.Date;

/**
 * Entity实体类全局插入数据监听器
 *
 * @author wgk
 */
public class EntityInsertListener implements InsertListener {

    @Override
    public void onInsert(Object entity) {
        try {
            if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity)) {
                BaseEntity baseEntity = (BaseEntity) entity;

                Long loginUserId = LoginHelper.getUserId();

                Date createTime = ObjectUtil.isNotNull(baseEntity.getCreateTime())
                    ? baseEntity.getCreateTime() : new Date();

                baseEntity.setCreateBy(loginUserId);
                baseEntity.setCreateTime(createTime);
                baseEntity.setUpdateBy(loginUserId);
                baseEntity.setUpdateTime(createTime);
            }
        } catch (Exception e) {
            throw new ServiceException("全局插入数据监听器注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
        }
    }
}
