package weaver.interfaces.htsc.util;

import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


public class OrgUtil {


    /**
     * 获取用户的所有账号id
     * @param userid 需要查询的用户
     * @return 用户所有账号id
     */
    public static String getAllUserids(int userid){
        String alluserids = userid+"";
        User user = User.getUser(userid,0);
        String belongtoids = user.getBelongtoids();
        if(!"".equals(belongtoids)){
            alluserids += ","+belongtoids;
        }
        return alluserids;
    }



    /**
     * 取得选择字段选择项的数据
     * @param fieldid 字段id
     * @return 下拉框键值对
     */
    public static Map<String,String> getSelectItem(String fieldid) {
        Map<String,String> data = new HashMap<String,String>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("SELECT SELECTVALUE, SELECTNAME, LISTORDER FROM HRM_SELECTITEM " +
                "WHERE CANCEL =0 AND FIELDID=? ORDER BY LISTORDER",fieldid);
        while(rs.next()){
            String selectName = Util.null2String(rs.getString("SELECTNAME"));
            String selectValue = Util.null2String(rs.getString("SELECTVALUE"));
            data.put(selectName,selectValue);
        }
        return data;
    }

    /**
     * 查询自定义下拉框选项
     * @param fieldid 字段id
     * @return 自定义下拉框键值对
     */
    public static Map<String,String> getCusSelectItem(int fieldid) {
        Map<String,String> data = new HashMap<String,String>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("SELECT SELECTVALUE, SELECTNAME, FIELDORDER FROM CUS_SELECTITEM " +
                "WHERE FIELDID=? ORDER BY FIELDORDER",fieldid);
        while(rs.next()){
            String selectName = Util.null2String(rs.getString("SELECTNAME"));
            String selectValue = Util.null2String(rs.getString("SELECTVALUE"));
            data.put(selectName,selectValue);
        }
        return data;
    }

    /**
     * 获取当前登录用户
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return 当前登录用户
     */
    public static User getUser(HttpServletRequest request, HttpServletResponse response){
        return getUser(request,response);
    }


    /**
     * 根据人员id，用逗号隔开，返回带链接的显示名
     * @param resourceids 人员id字符串
     * @return 人员显示名
     */
    public static String getHrmShowNameHref(String resourceids) {
        String showname = "";
        try {
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            String hrmids[] = Util.TokenizerString2(resourceids, ",");
            for(int i=0;i<hrmids.length;i++){
                String hrmid = Util.null2String(hrmids[i]);
                if(!hrmid.equals("")){
                    showname+="<a href='javaScript:openhrm("+hrmid+");' onclick='pointerXY(event);'>"+resourceComInfo.getResourcename(hrmid)+"</a>&nbsp";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return showname;
    }


    /**
     * 根据人员的id，用逗号隔开，返回人员名称
     * @param resourceids 人员id字符串
     * @return 人员显示名称
     */
    public static String getHrmName(String resourceids) {
        String showname = "";
        try {
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            String hrmids[] = Util.TokenizerString2(resourceids, ",");
            for(int i=0;i<hrmids.length;i++){
                String hrmid = Util.null2String(hrmids[i]);
                if(!hrmid.equals("")){
                    showname+=","+resourceComInfo.getResourcename(hrmid);
                }
            }
            showname = showname.replaceFirst(",", "");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return showname;
    }


    /**
     * 根据人员ID，用逗号隔开，返回人员部门id
     * @param resourceids 人员id字符串
     * @return 人员部门id
     */
    public static String getResourcesDepartmentID(String resourceids) {
        String showname = "";
        try {
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            String hrmids[] = Util.TokenizerString2(resourceids, ",");
            for(int i=0;i<hrmids.length;i++){
                String hrmid = Util.null2String(hrmids[i]);
                if(!hrmid.equals("")){
                    showname+=","+resourceComInfo.getDepartmentID(hrmid);
                }
            }
            showname = showname.replaceFirst(",", "");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return showname;
    }

    /**
     * 根据部门id，逗号隔开
     * @param deptpids 部门id字符串
     * @return 部门名称
     */
    public static String getDepartmentName(String deptpids){
        String showname = "";
        try {
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String deptArr[] = Util.TokenizerString2(deptpids, ",");
            for(int i=0;i<deptArr.length;i++){
                String deptid = Util.null2String(deptArr[i]);
                if(!deptid.equals("")){
                    showname+=","+ departmentComInfo.getDepartmentname(deptid);
                }
            }
            showname = showname.replaceFirst(",", "");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return showname;
    }


    /**
     * 生成新的人员id
     * @return 新的人员id
     */
    public synchronized static int getNewResourceId(){
        RecordSet rs = new RecordSet();
        rs.executeProc("HrmResourceMaxId_Get","");
        rs.next();
        return rs.getInt(1);
    }




}
