package weaver.interfaces.htsc.GlobatTools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import jxl.read.biff.Record;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.Util;

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

public class trealReimTools  {



    public static String  getregionStringtoId(String value){

//        String sql="select t0.id as sjid,t1.id as sid from uf_city t0,uf_city t1 where t1.parentcode = t0.code ";
        String sql = "select t0.code as sjid,t1.code as sid from travel_area t0,travel_area t1 where t1.parent_code = t0.code ";
        if("".equals(value)){
            return "";
        }
        String where="";
        String sjvalue= "";
        HashMap<String,String> cityMap = getCityUtil(value);
        if(cityMap.size()>0){
            where+=" and t0.name like '%"+cityMap.get("area")+"%'";
        }
        if(cityMap.size()>1){
            where+=" and t1.name like '%"+cityMap.get("city")+"%'";
        }
        if(!"".equals(sql)){
            sql+=where;
        }
        RecordSetDataSource rsdM = new RecordSetDataSource("Travel");
        rsdM.execute(sql);
        if (rsdM.next()){
            String sjid = Util.null2String(rsdM.getString("sjid"));
            String sid = Util.null2String(rsdM.getString("sid"));
            sjvalue=sjid;
            if(!"".equals(sid)){
                sjvalue+=","+sid;
            }
        }
        return sjvalue;
    }
    //城市入参格式化
    public static HashMap<String,String> getCityUtil(String cityReq){
        HashMap<String,String> cityMap = new HashMap<>();
        String[] cityArray = cityReq.split("-");
        if (cityArray.length<2){
            cityArray = cityReq.split(" ");
        }
        String area = cityReq.substring(0,2);
        String city = cityArray[1].replace("市","");
        cityMap.put("area",area);
        cityMap.put("city",city);
        return cityMap;
    }


    public static JSONObject Gethrmdeptid(String userids){
        JSONObject jsreturn=new JSONObject();
        if("".equals(userids)){
            return jsreturn;
        }

        JSONArray jsonArray=new JSONArray();
        String value="";
        RecordSet rs=new RecordSet();
        String sql="select hr.departmentid,(select departmentname from hrmdepartment where id = hr.departmentid) as deptname from hrmresource  hr where hr.id in ({0})";
        String sqlpars= MessageFormat.format(sql,userids);
        rs.execute(sqlpars);
        if(rs.getCounts()<=0){
            return jsreturn;
        }
        while(rs.next()){
            JSONObject jsonObject=new JSONObject();
            value+=value.equals("")?Util.null2String(rs.getString("departmentid")):","+Util.null2String(rs.getString("departmentid"));
            jsonObject.put("id",Util.null2String(rs.getString("departmentid")));
            jsonObject.put("name",Util.null2String(rs.getString("deptname")));
            jsonArray.add(jsonObject);
        }
        if(!value.equals("")){
            jsreturn.put("value",value);
            jsreturn.put("specialobj",jsonArray);
        }
        return jsreturn;
    }


    public static JSONObject Gethrmsupdepid(String userids){
        JSONObject jsreturn=new JSONObject();
        if("".equals(userids)){
            return jsreturn;
        }

        JSONArray jsonArray=new JSONArray();
        String value="";
        RecordSet rs=new RecordSet();
        String sql="select supdepid,(select departmentname from hrmdepartment where id = hd.supdepid) as deptname,supdepid, " +
                " hd.id, hd.departmentname,(select deptdepth from hrmdepartmentdefined hf where hf.deptid=hd.id) as deptdepth " +
                " from hrmdepartment  hd join hrmresource hr on  hd.id=hr.departmentid where hr.id in ({0})";
        String sqlpars= MessageFormat.format(sql,userids);
        rs.execute(sqlpars);
        if(rs.getCounts()<=0){
            return jsreturn;
        }
        while(rs.next()){
            JSONObject jsonObject=new JSONObject();
            if(Util.null2String(rs.getString("deptdepth")).equalsIgnoreCase("2")){
                value+=value.equals("")?Util.null2String(rs.getString("supdepid")):","+Util.null2String(rs.getString("supdepid"));
                jsonObject.put("id",Util.null2String(rs.getString("supdepid")));
                jsonObject.put("name",Util.null2String(rs.getString("deptname")));

            }else{
                value+=value.equals("")?Util.null2String(rs.getString("id")):","+Util.null2String(rs.getString("id"));
                jsonObject.put("id",Util.null2String(rs.getString("id")));
                jsonObject.put("name",Util.null2String(rs.getString("departmentname")));

            }
              jsonArray.add(jsonObject);
        }
        if(!value.equals("")){
            jsreturn.put("value",value);
            jsreturn.put("specialobj",jsonArray);
        }
        return jsreturn;
    }
}
