/**
 * Copyright 2018-2020 stylefeng & fengshuonan (sn93@qq.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.inkasso.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.inkasso.adapter.bean.AbstractUser;
import com.inkasso.adapter.web.LoginContext;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectionException;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author zc
 * @Date 2023/07/25
 */
@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object delFlag = null;
        try {
            delFlag = getFieldValByName("deleteFlag", metaObject);
            if (delFlag == null) {
                setFieldValByName("deleteFlag", false, metaObject);
            }
        } catch (ReflectionException e) {
            //没有此字段，则不处理
        }

        Object createTime = null;
        try {
            createTime = getFieldValByName("createTime", metaObject);
            if (createTime == null) {
                setFieldValByName("createTime", new Date(), metaObject);
            }
        } catch (ReflectionException e) {
            //没有此字段，则不处理
        }

        Object createUserId = null;
        try {
            createUserId = getFieldValByName("createUserId", metaObject);
            if (createUserId == null) {

                //获取当前登录用户
                Object userId = getCurrentUserId();

                setFieldValByName("createUserId", userId, metaObject);
            }
        } catch (ReflectionException e) {
            //没有此字段，则不处理
        }


        Object createUserName = null;
        try {
            createUserName = getFieldValByName("userCreate", metaObject);
            if (createUserName == null) {

                //获取当前登录用户
                Object userName = getCurrentUserName();

                setFieldValByName("userCreate", userName, metaObject);
            }
        } catch (ReflectionException e) {
            //没有此字段，则不处理
        }


        Object version = null;
        try {
            version = getFieldValByName("version", metaObject);
            if (version == null) {
                setFieldValByName("version",1, metaObject);
            }
        } catch (ReflectionException e) {
            //没有此字段，则不处理
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            setFieldValByName("updateTime", new Date(), metaObject);
        } catch (ReflectionException e) {
            //没有此字段，则不处理
        }

        Object updateUserId = null;
        try {
            updateUserId = getFieldValByName("updateUserId", metaObject);
            if (updateUserId == null) {
                Object userId = getCurrentUserId();
                setFieldValByName("updateUserId", userId, metaObject);
            }
        } catch (ReflectionException e) {
            //没有此字段，则不处理
        }


        Object updateUserName = null;
        try {
            updateUserName = getFieldValByName("userModified", metaObject);
            if (updateUserName == null) {
                updateUserName  = getCurrentUserName();
                setFieldValByName("userModified", updateUserName, metaObject);
            }
        } catch (ReflectionException e) {
            //没有此字段，则不处理
        }
    }

    protected Long getCurrentUserId(){
        return LoginContext.getUserId();
    }

    protected String getCurrentUserName(){

        AbstractUser baseUser = LoginContext.getCurrentUser();
        if(baseUser == null){
            return "未找到";
        }
        return baseUser.getUserName();
    }

}