package cn.sh.stc.sict.cloud.common.data.mybatis;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.sh.stc.sict.cloud.common.core.constant.Constant;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import cn.sh.stc.sict.cloud.common.security.service.SictUser;
import cn.sh.stc.sict.cloud.common.security.util.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName MyMetaObjectHandler
 * @Description 自定义公共字段填充处理器
 * @Author Y
 * @Date 2020-01-06 18:21
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入操作自动填充
     *
     * @author:
     * @param:
     * @return:
     * @date:
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createUserName = getFieldValByName("createUserName", metaObject);
        Object createUserId = getFieldValByName("createUserId", metaObject);
        Object createTime = getFieldValByName("createTime", metaObject);
        Object isDeteled = getFieldValByName("isDeleted", metaObject);
        Object isUse = getFieldValByName("isUse", metaObject);
        SictUser user = SecurityUtils.getUser();
        Date currentDate = DateUtil.date();
        if (createUserName == null) {
            String name = ObjectUtil.isNull(user) ? "未获取" : StrUtil.isBlank(user.getName()) ? user.getUsername() : user.getName();
            setFieldValByName("createUserName", name, metaObject);
        }
        if (createUserId == null) {
            Long userId = ObjectUtil.isNull(user) ? null : user.getId();
            setFieldValByName("createUserId", userId, metaObject);
        }
        if (createTime == null) {
            setFieldValByName("createTime", currentDate, metaObject);
        }
        if (isDeteled == null) {
            setFieldValByName("isDeleted", Constant.BYTE_NO, metaObject);
        }
        if (isUse == null) {
            setFieldValByName("isUse", Constant.BYTE_YES, metaObject);
        }
    }

    /**
     * 修改操作自动填充
     *
     * @author:
     * @param:
     * @return:
     * @date:
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Object updateUserName = getFieldValByName("updateUserName", metaObject);
        Object updateUserId = getFieldValByName("updateUserId", metaObject);
        Object updateTime = getFieldValByName("updateTime", metaObject);
        SictUser user = SecurityUtils.getUser();
        DateTime currentDate = DateUtil.date();
        if (updateUserName == null) {
            String name = ObjectUtil.isNull(user) ? "未获取" : StrUtil.isBlank(user.getName()) ? user.getUsername() : user.getName();
            setFieldValByName("updateUserName", name, metaObject);
        }
        if (updateUserId == null) {
            Long userId = ObjectUtil.isNull(user) ? null : user.getId();
            setFieldValByName("updateUserId", userId, metaObject);
        }
        if (updateTime == null) {
            setFieldValByName("updateTime", currentDate, metaObject);
        }
    }

}
