package cc.smtweb.biz.mztb.web.base.prelibraryConfiguration;

import cc.smtweb.biz.mztb.web.base.DbSourceConfig;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.db.EntityHelper;
import cc.smtweb.framework.core.db.impl.DefaultEntity;
import cc.smtweb.framework.core.exception.BizException;
import cc.smtweb.system.bpm.web.engine.model.common.ModelListHandler;
import cc.smtweb.system.bpm.web.engine.model.common.ModelLoadHandler;
import cc.smtweb.system.bpm.web.engine.model.common.ModelSaveHandler;

import java.util.List;

public class PrelibraryConfigurationHandler {
    static class PrelibraryConfigurationLoadHandler extends ModelLoadHandler {

    }

    static class PrelibraryConfigurationListHandler extends ModelListHandler {

        @Override
        protected void afterQuery(List<SwMap> listData) {
            super.afterQuery(listData);
            listData.forEach(temp -> {
                temp.put("dsc_use_state_text",temp.readInt("dsc_use_state") == 0 ? "当前使用" : "未使用");
                temp.put("dsc_password","****");
            });
        }
    }
    static class PrelibraryConfigurationSaveHandler extends ModelSaveHandler<DbSourceConfig> {
        @Override
        protected void checkValid(DefaultEntity vo) {
            super.checkValid(vo);
            if(0 == vo.getInt("dsc_use_state")) { // 当前使用
                List<DbSourceConfig> dbSourceConfigs = DbEngine.getInstance().findDao(DbSourceConfig.class)
                        .queryWhere(" dsc_id not in (?) and dsc_use_state = 0", vo.getEntityId());
                //  判断是否只有一个当前使用
                if(dbSourceConfigs != null && dbSourceConfigs.size() > 0) {
                    throw new BizException("只允许配置一个有效的前置库连接");
                }
            }
        }
    }
}
