package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;

import com.ruoyi.common.core.domain.model.LoginSiteUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SiteSecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

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

/**
 * mybatis-plus 自动填充字段
 *
 * @author theodo
 */
@Component
public class FieldMetaObjectConfig implements MetaObjectHandler {
    private final static String CREATE_TIME = "createTime";
    //    private final static String DEPT_ID = "deptId";
    private final static String CREATE_BY = "createBy";
    private final static String CREATE_BY_ID = "createById";
    private final static String UPDATE_TIME = "updateTime";
    private final static String UPDATE_BY = "updateBy";
    private final static String UPDATE_BY_ID = "updateById";
    private final static String DEL_FLAG = "delFlag";

    @Override
    public void insertFill(MetaObject metaObject) {

        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if(SecurityUtils.isLogin()) {
            LoginUser loginUser = null;
            LoginSiteUser siteUser = null;
            String name = "";
            Long userId = null;
            try {
                loginUser = SecurityUtils.getLoginUser();
                name =loginUser.getUsername();
                userId = loginUser.getUserId();
            }catch (Exception ignored){
                siteUser = SiteSecurityUtils.getLoginSiteUser();
                name = siteUser.getUsername();
                userId = siteUser.getUserId();
            }
            // 创建者
            strictInsertFill(metaObject, CREATE_BY, String.class, name);
            strictInsertFill(metaObject, CREATE_BY_ID, Long.class, userId);
            // 创建时间
            strictInsertFill(metaObject, CREATE_TIME, String.class, formattedDate);
            // 更新者
            strictInsertFill(metaObject, UPDATE_BY, String.class, name);
            strictInsertFill(metaObject, UPDATE_BY_ID, Long.class, userId);
            // 更新时间
            strictInsertFill(metaObject, UPDATE_TIME, String.class, formattedDate);
            // 删除标识
            strictInsertFill(metaObject, DEL_FLAG, String.class, "0");
        }else {
            // 创建者
            strictInsertFill(metaObject, CREATE_BY, String.class, "匿名登录");
            strictInsertFill(metaObject, CREATE_BY_ID, Long.class, 0l);
            // 创建时间
            strictInsertFill(metaObject, CREATE_TIME, String.class, formattedDate);
            // 更新者
            strictInsertFill(metaObject, UPDATE_BY, String.class, "匿名登录");
            strictInsertFill(metaObject, UPDATE_BY_ID, Long.class, 0l);
            // 更新时间
            strictInsertFill(metaObject, UPDATE_TIME, String.class, formattedDate);
            // 删除标识
            strictInsertFill(metaObject, DEL_FLAG, String.class, "0");
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        LoginUser loginUser = null;
        LoginSiteUser siteUser = null;
        String name = "";
        Long userId = null;
        LocalDateTime now = LocalDateTime.now();
        String formattedDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        try {
            loginUser = SecurityUtils.getLoginUser();
            if(Objects.nonNull(loginUser)){
                name = loginUser.getUsername();
                userId = loginUser.getUserId();
            }
            siteUser = SiteSecurityUtils.getLoginSiteUser();
            if(Objects.nonNull(siteUser)){
                name = siteUser.getUsername();
                userId = siteUser.getUserId();
            }
        }catch (Exception ignored){

        }
        if(Objects.isNull(loginUser)){
            strictUpdateFill(metaObject, UPDATE_BY, String.class, "nobody");
            strictUpdateFill(metaObject, UPDATE_BY_ID, Long.class, -1l);
        }else {
            // 更新者
            strictUpdateFill(metaObject, UPDATE_BY, String.class, loginUser.getUsername());
            strictUpdateFill(metaObject, UPDATE_BY_ID, Long.class, loginUser.getUserId());
        }
        // 更新时间
        strictUpdateFill(metaObject, UPDATE_TIME, String.class, formattedDate);
    }
}
