package com.bonus.core.starter;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.bonus.core.customer.model.PigxUser;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.model.LoginUser;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

@Component
public class TableFillHandler implements MetaObjectHandler {
   private static final Logger log = LoggerFactory.getLogger(TableFillHandler.class);

   public void insertFill(MetaObject metaObject) {
      this.strictInsertFill(metaObject, "crtime", LocalDateTime::now, LocalDateTime.class);
      LoginUser loginUser = SecurityUtils.getLoginUser();
      loginUser.setUsername(SecurityUtils.getUsername());
      loginUser.setUserid(SecurityUtils.getUserId());
      Objects.requireNonNull(loginUser);
      this.strictInsertFill(metaObject, "crby", loginUser::getUsername, String.class);
      this.strictUpdateFill(metaObject, "uptime", LocalDateTime::now, LocalDateTime.class);
      loginUser = SecurityUtils.getLoginUser();
      loginUser.setUsername(SecurityUtils.getUsername());
      loginUser.setUserid(SecurityUtils.getUserId());
      Objects.requireNonNull(loginUser);
      this.strictUpdateFill(metaObject, "upby", loginUser::getUsername, String.class);
   }

   public void updateFill(MetaObject metaObject) {
      this.strictUpdateFill(metaObject, "uptime", LocalDateTime::now, LocalDateTime.class);
      LoginUser var10003 = SecurityUtils.getLoginUser();
      var10003.setUsername(SecurityUtils.getUsername());
      Objects.requireNonNull(var10003);
      this.strictUpdateFill(metaObject, "upby", var10003::getUsername, String.class);
   }
}
