package com.engine.htsc.hthy.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HyxxDataByMailCmd extends AbstractCommonCommand<Map<String, Object>> {

    public HyxxDataByMailCmd(Map<String, Object> params) {
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        RecordSet rs = new RecordSet();

        BaseBean bb = new BaseBean();
        Map<String, Object> apimap = new HashMap<String, Object>();


        String billid = Util.null2String(params.get("billid"));
        bb.writeLog("billid:"+billid);

        List<Map<String,Object>> sjrlist = new ArrayList<Map<String,Object>>();
        Map<String,Object> sjrmap = null;
        //List<String> sjr_list = new ArrayList<String>();

        List<Map<String,Object>> csrlist = new ArrayList<Map<String,Object>>();
        Map<String,Object> csrmap = null;
//        List<String> csr_list = new ArrayList<String>();
        String sjrArray = "" ;

        String csrArray = "" ;
        String wjmcs = "";
        try{
            String sql =" select *\n" +
                    "  from (select l.id,\n" +
                    "               l.ryxm,\n" +
                    "               t.id as hyryid,\n" +
                    "               row_number() over(partition by l.yx order by t.id desc) xh\n" +
                    "          from uf_hywh_dt1 t\n" +
                    "         inner join uf_hyxx l on l.id = t.ry\n" +
                    "         where mainid = "+billid+") p\n" +
                    " where p.xh=1\n" +
                    " order by p.hyryid \n";

            bb.writeLog("sql:"+sql);
            rs.execute(sql);
            while (rs.next()){
                String ry = Util.null2String(rs.getString("id"));
                String ryxm = Util.null2String(rs.getString("ryxm"));
                sjrmap = new HashMap<String,Object>();
                sjrmap.put("id",ry);
                sjrmap.put("name",ryxm);
                sjrlist.add(sjrmap);

                sjrArray += sjrArray=="" ? ry : ","+ry ;
            }

            sql =   " select *\n" +
                    "  from (select l.id,\n" +
                    "               l.ryxm,\n" +
                    "               t.id as hyryid,\n" +
                    "               row_number() over(partition by l.yx order by t.id desc) xh\n" +
                    "          from uf_hywh_dt2 t\n" +
                    "         inner join uf_hyxx l on l.id = t.ry\n" +
                    "         where mainid = "+billid+") p\n" +
                    " where p.xh=1\n" +
                    " order by p.hyryid \n";

            bb.writeLog("sql:"+sql);
            rs.execute(sql);
            while (rs.next()){
                String ry = Util.null2String(rs.getString("id"));
                String ryxm = Util.null2String(rs.getString("ryxm"));
                csrmap = new HashMap<String,Object>();
                csrmap.put("id",ry);
                csrmap.put("name",ryxm);
                csrlist.add(csrmap);
                //csr_list.add(ry);

                csrArray += csrArray=="" ? ry : ","+ry ;
            }

            sql = " select wjmc from uf_hywh_dt5 where mainid="+billid+" order by yxjx,wjlx,gdrq desc ";
            bb.writeLog("sql:"+sql);
            rs.execute(sql);
            while (rs.next()){
                String wjmc = Util.null2String(rs.getString("wjmc"));
                wjmcs += wjmcs == "" ? wjmc : ","+wjmc ;
            }
        }catch (Exception e){
            System.out.println(e);
        }

        apimap.put("sjrdata",sjrlist);
        apimap.put("sjrarray",sjrArray);
        apimap.put("csrdata",csrlist);
        apimap.put("csrarray",csrArray);
        apimap.put("wjmcs",wjmcs);
        System.out.println(apimap.toString());
        return apimap;
    }



    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("billid","115");

        HyxxDataByMailCmd ItlxlcLdspInfoCmd = new HyxxDataByMailCmd(params);
        CommandContext ceshi= new CommandContext(null,null);
        ItlxlcLdspInfoCmd.execute(ceshi);

    }
}
