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 weaver.conn.RecordSet;

import java.util.HashMap;
import java.util.Map;

public class UpdateContractDataCmd extends AbstractCommonCommand<Map<String, Object>> {
    public UpdateContractDataCmd(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 sql = "update "+tableName +" set ";
                StringBuilder sb = new StringBuilder();
                for (Map.Entry<String,Object> data : dataMap.entrySet()){
                    if (data.getKey().equals("ID")){
                        continue;
                    }
                    sb.append(data.getKey());
                    sb.append("=");
                    sb.append("'");
                    sb.append(data.getValue());
                    sb.append("'");
                    sb.append(",");
                }
                sql = sql+sb.toString().substring(0,sb.toString().length()-1) +" where id=?";
                boolean response = rs.executeUpdate(sql,dataMap.get("ID"));
                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;
    }
}
