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

import cc.smtweb.biz.mztb.web.base.DbSourceConfig;
import cc.smtweb.biz.mztb.web.base.PartyInfo;
import cc.smtweb.biz.mztb.web.tool.DbUtil;
import cc.smtweb.framework.core.common.R;
import cc.smtweb.framework.core.common.SwMap;
import cc.smtweb.framework.core.db.DbEngine;
import cc.smtweb.framework.core.mvc.service.AbstractHandler;
import cc.smtweb.framework.core.session.UserSession;
import cc.smtweb.framework.core.util.DateUtil;
import cc.smtweb.system.bpm.web.engine.model.common.ModelSaveHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.StringJoiner;

public class PartyInfoHandler extends AbstractHandler {
    private final static Logger logger  = LoggerFactory.getLogger(PartyInfoHandler.class);

    private String  buildInsertSql(SwMap entity) {
        entity.remove("_def_table_name");
        entity.remove("pi_update_at");
        entity.remove("pi_update_at_text");
        entity.remove("pi_national_economy_type_text");
        entity.remove("pi_change_time_text");
        entity.remove("pi_reg_time_text");
        entity.remove("pi_reg_dept_text");
        entity.remove("pi_change_type_text");
        entity.remove("pi_party_type_text");
        entity.remove("_status");
        entity.remove("pi_sys_party_id");
        StringJoiner key = new StringJoiner(",");
        StringJoiner value = new StringJoiner(",");
        StringBuilder sql = new StringBuilder();
        sql.append(" insert into ").append("pre_party_info").append(" (");
        for (Map.Entry<String, Object> entry : entity.entrySet()) {
            key.add(entry.getKey());
            value.add("'"+entry.getValue()+"'");
        }
        sql.append(key);
        sql.append(") values (");
        sql.append(value);
        sql.append(")");

        return sql.toString();
    }

    public R pushParty(SwMap params, UserSession us) {
        // 将修改的机构数据推送到前置库
        DbEngine instance = DbEngine.getInstance();
        DbSourceConfig dbSourceConfig = instance.findDao(DbSourceConfig.class).queryEntityWhere("dsc_use_state = 0");
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(dbSourceConfig.getWholeUrl(), dbSourceConfig.getUsername(), dbSourceConfig.getPassword());
        }catch (SQLException sqlException) {
            logger.debug("创建 Connection Exception：",sqlException);
        }
        PartyInfo partyInfo = instance.queryEntity(PartyInfo.class, us.getPartyId());
        DbUtil dbUtil = new DbUtil(connection);
        SwMap swmap = partyInfo.getData();
        // 处理前置库和本系统 机构表不同的字段
        swmap.put("state","0");
        swmap.put("wr_time", DateUtil.nowDateTimeLong());
        swmap.put("wr_aspect",1);
        swmap.put("id",instance.nextId());
        String sql = buildInsertSql(swmap);
        logger.info("====向前置库推送数据的sql==== ：{}",sql);
        try {
            boolean insert = dbUtil.insert(sql);
            if(!insert) logger.info("推送机构信息失败！！！！");
        } catch (SQLException e) {
            logger.debug("修改机构信息后，向前置库推送机构信息失败：",e);
        }
        return R.success();
    }
}
