package cc.mrbird.febs.common.datasource.starter.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.utils.FebsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动填充插入更新时间
 */
@Component
@Slf4j
public class FebsMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        boolean hasCreateDate = metaObject.hasSetter("createDate");
        boolean hasCreateId = metaObject.hasSetter("createId");
        boolean hasCreateName = metaObject.hasSetter("createName");
        boolean hasCreateTime = metaObject.hasSetter("createTime");

        Boolean isLogin = FebsUtil.isLogin();
        if (hasCreateDate) {
            Object createDate = this.getFieldValByName("createDate", metaObject);
            if (createDate == null) {
                this.setFieldValByName("createDate", LocalDateTime.now(), metaObject);
            }
        }

        if (hasCreateTime) {
            Object createTime = this.getFieldValByName("createTime", metaObject);
            if (createTime == null) {
                this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
            }
        }

        if (hasCreateId && isLogin) {
            Object createId = this.getFieldValByName("createId", metaObject);
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (createId == null && currentUser != null) {
                this.setFieldValByName("createId", currentUser.getUserId(), metaObject);
            }
        }
        if (hasCreateName && isLogin) {
            Object createName = this.getFieldValByName("createName", metaObject);
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (createName == null && currentUser != null) {
                this.setFieldValByName("createName", currentUser.getNickname(), metaObject);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        boolean hasUpdateDate = metaObject.hasSetter("updateDate");
        boolean hasUpdateTime = metaObject.hasSetter("updateTime");
        boolean hasUpdateId = metaObject.hasSetter("updateId");
        Boolean isLogin = FebsUtil.isLogin();
        if (hasUpdateDate) {
            Object updateDate = this.getFieldValByName("updateDate", metaObject);
            if (updateDate == null) {
                this.setFieldValByName("updateDate", LocalDateTime.now(), metaObject);
            }
        }

        if (hasUpdateTime) {
            Object updateTime = this.getFieldValByName("updateTime", metaObject);
            if (updateTime == null) {
                this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
            }
        }

        if (hasUpdateId && isLogin) {
            Object updateId = this.getFieldValByName("updateId", metaObject);
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (updateId == null && currentUser != null) {
                this.setFieldValByName("updateId", currentUser.getUserId(), metaObject);
            }
        }

    }
}
