package cn.iocoder.yudao.framework.mybatis.core.handler;

import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseNoDeleteDO;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.MaterialBaseDO;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 通用参数填充实现类
 *
 * 如果没有显式的对通用参数进行赋值，这里会对通用参数进行填充、赋值
 *
 * 兼容两类基类：
 * 1) {@link BaseDO}：包含 deleted 逻辑删除字段
 * 2) {@link BaseNoDeleteDO}：不包含 deleted 字段
 *
 * @author hexiaowu
 */
public class DefaultDBFieldHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (Objects.isNull(metaObject) || Objects.isNull(metaObject.getOriginalObject())) {
            return;
        }

        Object original = metaObject.getOriginalObject();
        LocalDateTime current = LocalDateTime.now();
        Long userId = WebFrameworkUtils.getLoginUserId();

        // BaseDO：包含 deleted
        if (original instanceof BaseDO) {
            BaseDO baseDO = (BaseDO) original;
            baseDO.setDeleted(Boolean.FALSE);
            fillCommonOnInsert(baseDO, current, userId);
            return;
        }

        // BaseNoDeleteDO：不包含 deleted
        if (original instanceof BaseNoDeleteDO) {
            BaseNoDeleteDO baseDO = (BaseNoDeleteDO) original;
            if (Objects.isNull(baseDO.getCreateTime())) {
                baseDO.setCreateTime(current);
            }
            if (Objects.isNull(baseDO.getUpdateTime())) {
                baseDO.setUpdateTime(current);
            }
            if (Objects.nonNull(userId) && Objects.isNull(baseDO.getCreator())) {
                baseDO.setCreator(SecurityFrameworkUtils.getLoginUserNickname());
            }
            if (Objects.nonNull(userId) && Objects.isNull(baseDO.getUpdater())) {
                baseDO.setCreator(SecurityFrameworkUtils.getLoginUserNickname());
            }
        }
        if(original instanceof MaterialBaseDO){
            fillMaterialDO((MaterialBaseDO)original,current);
        }
    }

    private void fillCommonOnInsert(BaseDO baseDO, LocalDateTime current, Long userId) {
        // 创建时间为空，则以当前时间为插入时间
        if (Objects.isNull(baseDO.getCreateTime())) {
            baseDO.setCreateTime(current);
        }
        // 更新时间为空，则以当前时间为更新时间
        if (Objects.isNull(baseDO.getUpdateTime())) {
            baseDO.setUpdateTime(current);
        }

        // 当前登录用户不为空，创建人为空，则当前登录用户为创建人
        if (Objects.nonNull(userId) && Objects.isNull(baseDO.getCreator())) {
            baseDO.setCreator(SecurityFrameworkUtils.getLoginUserNickname());
        }
        // 当前登录用户不为空，更新人为空，则当前登录用户为更新人
        if (Objects.nonNull(userId) && Objects.isNull(baseDO.getUpdater())) {
            baseDO.setCreator(SecurityFrameworkUtils.getLoginUserNickname());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LocalDateTime current = LocalDateTime.now();
        // 更新时间为空，则以当前时间为更新时间
        Object modifyTime = getFieldValByName("updateTime", metaObject);
        if (Objects.isNull(modifyTime)) {
            setFieldValByName("updateTime", current, metaObject);
        }

        // 当前登录用户不为空，更新人为空，则当前登录用户为更新人
        Object modifier = getFieldValByName("updater", metaObject);
        Long userId = WebFrameworkUtils.getLoginUserId();
        if (Objects.nonNull(userId) && Objects.isNull(modifier)) {
            setFieldValByName("updater", SecurityFrameworkUtils.getLoginUserNickname(), metaObject);
        }
        Object original = metaObject.getOriginalObject();
        if(original instanceof MaterialBaseDO){
            fillMaterialDO((MaterialBaseDO)original,current);
        }
    }

    private void fillMaterialDO(MaterialBaseDO baseDO, LocalDateTime current) {
        if (Objects.isNull(baseDO.getAuditTime())) {
            baseDO.setAuditTime(current);
        }
        if (Objects.isNull(baseDO.getAuditResult())) {
            baseDO.setAuditResult("已通过");
        }
        if (Objects.isNull(baseDO.getAuditor())) {
            baseDO.setAuditor(SecurityFrameworkUtils.getLoginUserNickname());
        }
        if (Objects.isNull(baseDO.getAuditRemark())) {
            baseDO.setAuditRemark("无");
        }
    }
}
