package com.engine.htsc.contractmanagement.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.cube.biz.CodeBuilder;
import weaver.conn.RecordSet;
import java.util.HashMap;
import java.util.Map;

public class AddContractDataCmd extends AbstractCommonCommand<Map<String, Object>> {
    public AddContractDataCmd(Map<String, Object> params){
        this.params = params;
    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> responseMap = new HashMap<>();

        try {
            RecordSet rs = new RecordSet();
            for (Map.Entry<String,Object> map : params.entrySet()){
                String tableName = map.getKey();
                Map<String,Object> dataMap = (Map<String,Object>)map.getValue();
                String names="";
                String values="";
                for (Map.Entry entry :dataMap.entrySet()){
                    if (entry.getKey().equals("ID")){
                        continue;
                    }
                    names = names + entry.getKey() +",";
                    values = values +"'"+entry.getValue()+"'" +",";
                };
                names = names.substring(0,names.length()-1);
                values = values.substring(0,values.length()-1);
                String insertSql = "insert into "+tableName+" ("+names+") values ("+values+")";
                boolean response =  rs.executeUpdate(insertSql);
                if (response){
                    responseMap.put("code","0");
                }else {
                    responseMap.put("code","400");
                    responseMap.put("message","插入失败");
                }
            }
        }catch (Exception e){
            responseMap.put("code","500");
            responseMap.put("message",e.getMessage());
        }

        return responseMap;
    }
}
