package com.gree.mall.manager.config;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gree.mall.manager.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * 自动补充插入或更新时的值
 */
@Slf4j
@Component
public class MpMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入时的填充策略
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        String by = CommonUtils.getNickName();
        String username = CommonUtils.getUsername();
        if(StringUtils.isNotBlank(username)){
            by = "(" + username+ ")" + by;
        }
        if (this.getFieldValByName("isThreeOrder",metaObject) != null)
            return;

            this.setFieldValByName("createBy", by, metaObject);
            this.setFieldValByName("updateBy", by, metaObject);
            this.setFieldValByName("createTime", new Date(), metaObject);
            this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    /**
     * 更新时的填充策略
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        String by = CommonUtils.getNickName();
        String username = CommonUtils.getUsername();
        if(StringUtils.isNotBlank(username)){
            by = "(" + username+ ")" + by;
        }

        this.setFieldValByName("updateBy", by, metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
