package cc.smtweb.framework.core.mvc.service;

import cc.smtweb.framework.core.cache.AbstractCache;
import cc.smtweb.framework.core.cache.CacheManager;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.EntityHelper;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.db.vo.ModelTable;

/**
 * Created by Akmm at 2022/3/2 19:52
 * 默认实体实现
 */
public class DefaultDelHandler<T extends DefaultEntity> extends AbstractDelHandler {
    protected String tableName;

    public DefaultDelHandler(String tableName) {
        this.tableName = tableName;
    }

    @Override
    protected void checkValid() {
        EntityHelper.checkExists(tableName, id);
//        ModelTable table = ModelTableCache.getInstance().getByName(tableName);

    }

    @Override
    protected void delDb() {
        EntityDao dao = DbEngine.getInstance().findDao(tableName);
        dao.deleteEntity(id);
    }

    @Override
    protected void saveSuccess() {
        super.saveSuccess();
        ModelTable table = ModelTableCache.getInstance().getByName(tableName);
        if (table.isNeedCache()) {
            AbstractCache<T> cache = CacheManager.getIntance().getCache(tableName);
            cache.remove(id);
        }
    }
}
