package com.storlead.framework.mybatis.handler;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.common.constant.UserCacheKeyConstants;
import com.storlead.framework.core.context.Context;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Objects;

/**
 * @program: sp-sales
 * @description:
 * @author: chenkq
 * @create: 2022-07-06 15:03
 */
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 自动更新
     * 需要  @TableField(fill = FieldFill.INSERT) 否则无效
     *
     * @param metaObject 要处理的对象
     * @author chenkq
     * @date 2022-2-3 12:25
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime = getFieldValByName("createTime", metaObject);
        if (Objects.isNull(createTime)) {
            DateTime time= new DateTime();
            this.setInsertFieldValByName("createTime",time,metaObject);
        }
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (Objects.isNull(updateTime)) {
            DateTime time= new DateTime();
            this.setUpdateFieldValByName( "updateTime",time, metaObject);
        }
        if (Objects.isNull(Context.getContext())) {
            return;
        }
        LoginUser loginUser = Context.getContext().getAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_KEY, LoginUser.class);
        Object createBy = getFieldValByName("createBy", metaObject);
        if (Objects.isNull(createBy)) {
            if (Objects.nonNull(loginUser)) {
                this.setInsertFieldValByName("createBy",loginUser.getId(),metaObject);
            }
        }
        Object ownerBy = getFieldValByName("ownerBy", metaObject);
        if (Objects.isNull(ownerBy)) {
            if (Objects.nonNull(loginUser)) {
                this.setInsertFieldValByName("ownerBy", loginUser.getId(), metaObject);
            }
        }
        Object updateBy = getFieldValByName("updateBy", metaObject);
        if (Objects.isNull(updateBy)) {
            if (Objects.nonNull(loginUser)) {
                this.setInsertFieldValByName("updateBy",loginUser.getId(),metaObject);
            }
        }
    }

    /**
     * 自动更新
     * 需要  @TableField(fill = FieldFill.UPDATE) 否则无效
     *
     * @param metaObject 要处理的对象
     * @author chenkq
     * @date 2022-2-3 12:25
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        DateTime time= new DateTime();
        this.setUpdateFieldValByName( "updateTime",time, metaObject);
        if (Objects.isNull(Context.getContext())) {
            return;
        }
        Object updateBy = getFieldValByName("updateBy", metaObject);
        LoginUser loginUser = Context.getContext().getAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_KEY, LoginUser.class);
        if (Objects.isNull(updateBy)) {
            if (Objects.nonNull(loginUser)) {
                this.setInsertFieldValByName("updateBy",loginUser.getId(),metaObject);
            }
        }
    }
}
