package cc.smtweb.system.bpm.web.design.form;

import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityDao;
import cc.smtweb.framework.core.mvc.service.DefaultDelHandler;
import cc.smtweb.framework.core.util.CommUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Akmm at 2022-08-15 15:00
 */
public class ModelFormDelHandler extends DefaultDelHandler<ModelForm> {
    //被删除的id
    private Set<ModelForm> listDeled = new HashSet<>();

    public ModelFormDelHandler() {
        super(ModelForm.ENTITY_NAME);
    }

    @Override
    protected void checkValid() {
//        super.checkValid();
    }

    @Override
    protected void delDb() {
        EntityDao<ModelForm> dao = DbEngine.getInstance().findDao(ModelForm.class);
        Set<ModelForm> l1 = ModelFormCache.getInstance().getListByModule(id);
        Set<ModelForm> list = l1 != null ? new HashSet<>(l1): null;
        if (CommUtil.isEmpty(list)) {
            listDeled = null;
            dao.deleteEntity(id);
        } else {
            listDeled = new HashSet<>(list);
            List<Long> ids = new ArrayList<>();
            ids.add(id);
            for (ModelForm form : list) {
                ids.add(form.getId());
            }
            dao.deleteEntity(ids);
        }
    }

    @Override
    protected void saveSuccess() {
        ModelFormCache cache = ModelFormCache.getInstance();
        if (!CommUtil.isEmpty(listDeled)) {
            for (ModelForm form : listDeled) {
                cache.remove(form.getId());
            }
        }
        cache.remove(id);
    }
}
