package com.engine.htsc.GwRelated.fzjg.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.workflow.ld.DeptWHUtil;
import com.weaver.general.Util;
import org.apache.commons.collections.map.HashedMap;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.authority.manager.HrmDepartmentManager;
import weaver.hrm.company.DepartmentComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.POST;
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.HashMap;
import java.util.Map;

public class Gettypetovalue {

    @POST
    @Path("/Getissxzx")
    @Produces({MediaType.TEXT_PLAIN})
    public String Getissxzx(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        //   String requestid = Util.null2String(request.getParameter("requestid"));
        JSONObject jsonObject=new JSONObject();
        BaseBean bb = new BaseBean();
        PropBean pb = new PropBean();
        String deptid = Util.null2String(request.getParameter("deptid"));
        String sql="";
        if("".equals(deptid)){
            jsonObject.put("result","no");
        }
        deptid= JAVATools.getpreventsql(deptid);
        sql="select  sfsxzx from hrmdepartmentdefined where deptid="+deptid; //内控联络人
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        if(rs.getCounts()<=0){
            jsonObject.put("result","no");
        }
        if(rs.next()){
            String value=Util.null2String(rs.getString("sfsxzx"));
            jsonObject.put("result","yes");
            if("0".equals(value)){
                jsonObject.put("data","0");
            }else{
                jsonObject.put("data","1");
            }
        }

        return jsonObject.toString();
    }


    //过去三级营业部
    @POST
    @Path("/GetthlevelSalesdp")
    @Produces({MediaType.TEXT_PLAIN})
    public String GetthlevelSalesdp(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        //   String requestid = Util.null2String(request.getParameter("requestid"));
        JSONObject jsonObject=new JSONObject();
        JSONObject jsonValue=new JSONObject();
        BaseBean bb = new BaseBean();
        PropBean pb = new PropBean();
        String value="";
        String deptname="";
        String deptid = Util.null2String(request.getParameter("deptid"));
        String sql="";
        if("".equals(deptid)){
            jsonObject.put("data","");
        }
        deptid= JAVATools.getpreventsql(deptid);
        sql="select parentsalecode from hrmdepartmentdefined where deptid="+deptid; //内控联络人
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        rs.execute(sql);
        if(rs.getCounts()<=0){
            Map<String,String> map= getparentdeptid(deptid);
            jsonValue.put("deptid",map.get("deptid"));
            jsonValue.put("deptvalue",map.get("deptvalue"));
            jsonObject.put("data",jsonValue);
        }
        if(rs.next()){
             String departmentcode=Util.null2String(rs.getString("parentsalecode"));
            String sql2="select id,departmentname from hrmdepartment where departmentcode='"+departmentcode+"'";
            rs1.execute(sql2);
            if(rs1.getCounts()<=0){

                Map<String,String> map= getparentdeptid(deptid);
                jsonValue.put("deptid",map.get("deptid"));
                jsonValue.put("deptvalue",map.get("deptvalue"));
                jsonObject.put("data",jsonValue);
            }
            if(rs1.next()){
                value=Util.null2String(rs1.getString("id"));
                deptname=Util.null2String(rs1.getString("departmentname"));
                if("".equals(value)){
                    Map<String,String> map= getparentdeptid(deptid);
                    jsonValue.put("deptid",map.get("deptid"));
                    jsonValue.put("deptvalue",map.get("deptvalue"));

                    jsonObject.put("data",jsonValue);
                }else{
                    jsonValue.put("deptid",value);
                    jsonValue.put("deptvalue",deptname);
                    jsonObject.put("data",jsonValue);
                }

            }


        }

        return jsonObject.toString();
    }


    public Map<String,String> getparentdeptid(String deptid){
        String value="";
        String deptname="";
        Map<String,String> map=new HashMap<>();
        //DepartmentComInfo departmentComInfo=new DepartmentComInfo();
        //value=  departmentComInfo.getAllSupDepId(Util.getIntValue(deptid));
        String sql="select supdepid,departmentname from HrmDepartment where id = "+deptid;
        RecordSet recordSet=new RecordSet();
        recordSet.equals(sql);
        if(recordSet.next()){
            value=Util.null2String(recordSet.getString("supdepid"));
            deptname=Util.null2String(recordSet.getString("departmentname"));
        }
        map.put("deptid",value);
        map.put("deptname",deptname);

        return  map;

    }


}
