package com.engine.htsc.hyld.web;


import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会议选择类型字段联动
 */
public class HyldControlAction {

    
    @GET
    @Path("/getHyChange")
    @Produces({MediaType.TEXT_PLAIN})
    public String getHyChange(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        JSONObject jsonObject = new JSONObject();
        Integer hylxNew = Util.getIntValue(request.getParameter("hylx"),1);
        Integer hylx = 0;
        try{
            //查询上级分类id
//            RecordSet sqlFindId = new RecordSet();
////            sqlFindId.execute("SELECT HYLXSJ FROM uf_hylx WHERE sfxs = 0 and  id = "+hylxNew);
////            if (sqlFindId.next()){
////                hylx = Util.getIntValue(sqlFindId.getString("HYLXSJ"),hylxNew);
////            }
            //查询出席人员
            List<Map<String,Object>> listOne = new ArrayList<>();
            RecordSet sqlHyldOne = new RecordSet();
            sqlHyldOne.execute("SELECT cxry FROM UF_HYRY_DT1 WHERE mainid = (SELECT id FROM uf_hyry WHERE hylx = "+hylxNew+")");
            while (sqlHyldOne.next()){
                Integer cxryOne = Util.getIntValue(sqlHyldOne.getString("cxry"));
                RecordSet sqlPersonOne = new RecordSet();
                sqlPersonOne.execute("SELECT id,zwx,yx,ryxm FROM uf_hyxx WHERE sfxs = 0 AND zwx = "+cxryOne);
                while (sqlPersonOne.next()){
                    Map<String,Object> printOne = new HashMap<String,Object>();
                    Integer idOne = Util.getIntValue(sqlPersonOne.getString("id"));
                    Integer zwOne = Util.getIntValue(sqlPersonOne.getString("zwx"));
                    String  yxOne = Util.null2String(sqlPersonOne.getString("yx"));
                    String  ryxmOne = Util.null2String(sqlPersonOne.getString("ryxm"));
                    printOne.put("id",idOne);
                    printOne.put("zw",zwOne);
                    printOne.put("yx",yxOne);
                    printOne.put("ryxm",ryxmOne);
                    listOne.add(printOne);
                }
            }
            //查询列席人员
            List<Map<String,Object>> listTwo = new ArrayList<>();
            RecordSet sqlHyldTwo = new RecordSet();
            sqlHyldTwo.execute("SELECT lxry FROM UF_HYRY_DT2  WHERE mainid =  (SELECT id FROM uf_hyry WHERE hylx = "+hylxNew+")");
            while (sqlHyldTwo.next()){
                Integer lxryTwo = Util.getIntValue(sqlHyldTwo.getString("lxry"));
                RecordSet sqlPersonTwo = new RecordSet();
                sqlPersonTwo.execute("SELECT id,zwx,yx,ryxm FROM uf_hyxx WHERE sfxs = 0 AND zwx = "+lxryTwo);
                while (sqlPersonTwo.next()){
                    Map<String,Object> printTwo = new HashMap<String,Object>();
                    Integer idTwo = Util.getIntValue(sqlPersonTwo.getString("id"));
                    Integer zwTwo = Util.getIntValue(sqlPersonTwo.getString("zwx"));
                    String  yxTwo = Util.null2String(sqlPersonTwo.getString("yx"));
                    String  ryxmTwo = Util.null2String(sqlPersonTwo.getString("ryxm"));
                    printTwo.put("id",idTwo);
                    printTwo.put("zw",zwTwo);
                    printTwo.put("yx",yxTwo);
                    printTwo.put("ryxm",ryxmTwo);
                    listTwo.add(printTwo);
                }
            }
            //主持人员
            List<Map<String,Object>> listThree = new ArrayList<>();
            RecordSet sqlHyldThree = new RecordSet();
            sqlHyldThree.execute("SELECT hyzcr FROM UF_HYRY_DT3  WHERE mainid =  (SELECT id FROM uf_hyry WHERE hylx = "+hylxNew+")");
            while (sqlHyldThree.next()){
                Integer hyzcrThree = Util.getIntValue(sqlHyldThree.getString("hyzcr"));
                RecordSet sqlPersonThree = new RecordSet();
                sqlPersonThree.execute("SELECT id,zwx,yx,ryxm FROM uf_hyxx WHERE sfxs = 0 AND zwx = "+hyzcrThree);
                while (sqlPersonThree.next()){
                    Map<String,Object> printThree = new HashMap<String,Object>();
                    Integer idThree = Util.getIntValue(sqlPersonThree.getString("id"));
                    Integer zwThree = Util.getIntValue(sqlPersonThree.getString("zwx"));
                    String  yxThree = Util.null2String(sqlPersonThree.getString("yx"));
                    String  ryxmThree = Util.null2String(sqlPersonThree.getString("ryxm"));
                    printThree.put("id",idThree);
                    printThree.put("zw",zwThree);
                    printThree.put("yx",yxThree);
                    printThree.put("ryxm",ryxmThree);
                    listThree.add(printThree);
                }
            }

            jsonObject.put("code","200");
            jsonObject.put("msg","success");
            jsonObject.put("cxPerson",listOne);
            jsonObject.put("lxPerson",listTwo);
            jsonObject.put("zcPerson",listThree);
        }catch (Exception e) {
            e.printStackTrace();
            jsonObject.put("code","500");
            jsonObject.put("msg","fail");
        }
        return JSONObject.toJSONString(jsonObject);
    }







}
