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

import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.jdbc.IDbWorker;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by Akmm at 2022/3/2 19:44
 * 保存
 */
@Slf4j
public abstract class AbstractDelHandler extends AbstractHandler {
    protected long id;

    public R del() {
        id = readId();

        checkValid();
        DbEngine.getInstance().doTrans(new IDbWorker() {
            @Override
            public void work() {
                delDb();
            }

            @Override
            public void doAfterDbCommit() {
                saveSuccess();
            }

            @Override
            public void doAfterDbRollback() {
                saveFailed();
            }
        });
        return R.success();
    }

    /**
     * 读取页面传回来的id
     *
     * @return
     */
    protected long readId() {
        return params.readLong("id", 0L);
    }

    protected abstract void checkValid();

    protected abstract void delDb();

    protected void saveSuccess() {
    }

    protected void saveFailed() {
    }
}
