package cn.estsh.i3plus.pojo.base.bean;

import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description : 实体对象监听
 * @Reference :
 * @Author :  alwaysfrin
 * @CreateDate : 2020-08-24 22:28
 * @Modify:
 **/
public class BaseBeanListener {

    //实体保存前
    @PrePersist
    public void prePersist(BaseBean baseBean) {
        // 创建用户
        if (StringUtils.isBlank(baseBean.getCreateUser())) {
            if(StringUtils.isNotBlank(BaseThreadLocal.getThreadEmpName())) {
                baseBean.setCreateUser(BaseThreadLocal.getThreadEmpName());
                baseBean.setModifyUser(BaseThreadLocal.getThreadEmpName());
            }
        }
        //组织代码
        if (StringUtils.isBlank(baseBean.getOrganizeCode())) {
            if(StringUtils.isNotBlank(BaseThreadLocal.getThreadOrganizeCode())) {
                baseBean.setOrganizeCode(BaseThreadLocal.getThreadOrganizeCode());
            }
        }
        // 创建时间
        if (StringUtils.isBlank(baseBean.getCreateDatetime())){
            baseBean.setCreateDatetime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
            baseBean.setModifyDatetime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
        }
        //有效性
        if(baseBean.getIsValid() == null ||
                (baseBean.getIsValid() != CommonEnumUtil.IS_VAILD.VAILD.getValue() && baseBean.getIsValid() != CommonEnumUtil.IS_VAILD.INVAILD.getValue())){
            baseBean.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());  //有效
        }
        //是否删除
        if(baseBean.getIsDeleted() == null ||
                (baseBean.getIsDeleted() != CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() && baseBean.getIsDeleted() != CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue())){
            baseBean.setIsDeleted(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());   //未删除
        }
    }

    //实体对象保存之后
    /*@PostPersist
    public void afterPersist(){
    }*/

    //实体对象修改之前
    @PreUpdate
    public void preUpdate(BaseBean baseBean) {
        // 修改用户
        if (StringUtils.isBlank(baseBean.getModifyUser())) {
            if(StringUtils.isNotBlank(BaseThreadLocal.getThreadEmpName())) {
                baseBean.setModifyUser(BaseThreadLocal.getThreadEmpName());
            }
        }

        // 修改时间
        if (StringUtils.isEmpty(baseBean.getModifyDatetime())){
            baseBean.setModifyDatetime((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()));
        }

        //组织代码
        if (StringUtils.isBlank(baseBean.getOrganizeCode())) {
            if(StringUtils.isNotBlank(BaseThreadLocal.getThreadOrganizeCode())) {
                baseBean.setOrganizeCode(BaseThreadLocal.getThreadOrganizeCode());
            }
        }
        //有效性
        if(baseBean.getIsValid() == null ||
                (baseBean.getIsValid() != CommonEnumUtil.IS_VAILD.VAILD.getValue() && baseBean.getIsValid() != CommonEnumUtil.IS_VAILD.INVAILD.getValue())){
            baseBean.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());  //有效
        }
        //是否删除
        if(baseBean.getIsDeleted() == null ||
                (baseBean.getIsDeleted() != CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() && baseBean.getIsDeleted() != CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue())){
            baseBean.setIsDeleted(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());   //未删除
        }
    }
}
