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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryDataCmd extends AbstractCommonCommand<Map<String, Object>> {
    public QueryDataCmd(Map<String, Object> params){
        this.params = params;
    }
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        String id = params.get("id").toString();
        Map<String, Object> responseMap = new HashMap<>();
        Map<String, Object> dataMap = new HashMap<>();
        List dataList = new ArrayList();
        dataList.add(dataMap);
        responseMap.put("data",dataList);
        //查询主表
        String mainSql = "select * from uf_dsfgl where id=? and scbs!=1";
        RecordSet rs = new RecordSet();
        try {
            rs.executeQuery(mainSql,id);
            if (rs.next()){
                //主表数据
                Map<String,String> mainMap = new HashMap<>();
                String[] columns =  rs.getColumnName();
                for (String columnName :columns){
                    mainMap.put(columnName.toLowerCase(),rs.getString(columnName));
                }
                List<Map<String,String>> mainList = new ArrayList<>();
                mainList.add(mainMap);
                //明细表1
                List<Map<String,String>> detail1List = getDetailData(rs,id,"uf_dsfgl_dt1");
                //明细表2
                List<Map<String,String>> detail2List = getDetailData(rs,id,"uf_dsfgl_dt2");
                //明细表3
                List<Map<String,String>> detail3List = getDetailData(rs,id,"uf_dsfgl_dt3");

                dataMap.put("main",mainList);
                dataMap.put("detail1",detail1List);
                dataMap.put("detail2",detail2List);
                dataMap.put("detail3",detail3List);

            }

        }catch (Exception e){
            responseMap.put("code","500");
            responseMap.put("message",e.getMessage());
        }
        if (dataMap.size() == 0){
            responseMap.put("message","数据为空");
            responseMap.put("code","400");
        }else {
            responseMap.put("code","0");
        }
        return responseMap;
    }

    public List<Map<String,String>> getDetailData(RecordSet rs,String id,String tableName){
        List<Map<String,String>> dataList = new ArrayList();
        String sql = "select * from "+tableName+" where mainId=?";
        rs.executeQuery(sql,id);
        while (rs.next()){
            Map<String,String> detailMap = new HashMap<>();
            String[] columns =  rs.getColumnName();
            for (String columnName :columns){
                detailMap.put(columnName.toLowerCase(),rs.getString(columnName));
            }
            dataList.add(detailMap);
        }
        return dataList;
    }
}
