package cc.smtweb.system.bpm.web.engine.model.listcard.lcms;

import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.db.cache.ModelTableCache;
import cc.smtweb.framework.core.db.vo.ModelTable;
import cc.smtweb.system.bpm.web.design.form.define.PageDataset;
import cc.smtweb.system.bpm.web.engine.model.common.ModelDelHandler;

/**
 * @Author:lip
 * @Date : 2022/8/28 20:40
 * 主子表删除handler
 */
public class LCMsDelHandler extends ModelDelHandler {
    @Override
    protected void checkDel(PageDataset pageDataSet, long id) {
        super.checkDel(pageDataSet, id);
        // 子表删除校验
    }

    @Override
    protected void localDel(long id, ModelTable table) {
        super.localDel(id, table);
        // 获取主子表模型配置
        LCMsConfig.Config config = new LCMsConfig.Config(form);
        // 子表删除
       batchDelDetail(config.getSubTableId(),config.getSubFKField(),id);
    }

    /**
     * 批量删除明细 供子类调用
     * @param tableId 表定义ID
     * @param fk 关联字段
     * @param id 主表ID
     */
    protected void batchDelDetail(long tableId,String fk,long id){
        if(tableId<=0L)return;
        ModelTable table = ModelTableCache.getInstance().get(tableId);
        if(table == null) return;
        EntityDao dao =DbEngine.getInstance().findDao(table.getName());
        if(dao!=null){
            dao.deleteEntity(String.format(" where %s = ? ",fk),id);
        }
    }
}
