package com.hxhq.common.security.config;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.hxhq.common.security.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 *
 * @author memory
 */
public class BaseMetaObjectHandler implements MetaObjectHandler {
	/**
	 * 删除标记
	 */
	public static final String DEL_FLAG = "delFlag";
	/**
	 * 创建时间
	 */
	public static final String CREATE_BY = "createBy";
	/**
	 * 创建时间
	 */
	public static final String CREATE_DATE = "createTime";
	/**
	 * 创建时间
	 */
	public static final String UPDATE_BY = "updateBy";
	/**
	 * 更新时间
	 */
	public static final String UPDATE_DATE = "updateTime";

	/**
	 * 新增
	 */
	@Override
	public void insertFill(MetaObject metaObject) {
		// 创建用户
		Object createBy = getFieldValByName(CREATE_BY, metaObject);
		if (createBy == null || "".equals(createBy)) {
			setFieldValByName(CREATE_BY, SecurityUtils.getLoginUserOfEmpty().getUserid()==null?"": SecurityUtils.getLoginUserOfEmpty().getUserid().toString(), metaObject);
		}

		// 创建时间
		Object createDate = getFieldValByName(CREATE_DATE, metaObject);
		if (createDate == null) {
			setFieldValByName(CREATE_DATE, new Date(), metaObject);
		}

		// 删除标记
		Object delFlag = getFieldValByName(DEL_FLAG, metaObject);
		if (delFlag == null) {
			setFieldValByName(DEL_FLAG, "0", metaObject);
		}
	}

	@Override
	public void updateFill(MetaObject metaObject) {
//		Object updateBy = getFieldValByName(UPDATE_BY, metaObject);
//		if (updateBy == null || updateBy.equals("")) {
		setFieldValByName(UPDATE_BY, SecurityUtils.getLoginUserOfEmpty().getUserid()==null?"": SecurityUtils.getLoginUserOfEmpty().getUserid().toString(), metaObject);
//		}

		// 更新用户
//		Object updateDate = getFieldValByName(UPDATE_DATE, metaObject);
//		if (updateDate == null) {
		setFieldValByName(UPDATE_DATE, new Date(), metaObject);
//		}
	}
}