package com.cnd3b.utility.qywechat.tools;

import p2.p2server.P2Server;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

public class GetP2Id {
    public String[] gethrids(String fwechatuserid) {
        String[] id = null;
        PaoSetRemote twechatusersdetail = null;
        try {
            twechatusersdetail = P2Server.getP2Server().getPaoSet("twechatusersdetail",
                    P2Server.getP2Server().getSystemUserInfo());
            twechatusersdetail.setWhere("fwechatuserid='" + fwechatuserid + "'");
            twechatusersdetail.reset();
            id = new String[twechatusersdetail.count()];
            int i = 0;
            while (twechatusersdetail.getPao(i) != null) {
                id[i] = twechatusersdetail.getPao(i).getString("hrid");
                i++;
            }
            twechatusersdetail.close();
        } catch (P2Exception e) {
            e.printStackTrace();
        } finally {
            try {
                twechatusersdetail.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return id;
    }

    public PaoSetRemote getppuserpaoset(String FWECHATUSERID) {
        String[] id = null;
        PaoSetRemote pp_users = null;
        PaoSetRemote twechatusersdetail = null;
        try {
            twechatusersdetail = P2Server.getP2Server().getPaoSet("TWECHATUSERSDETAIL",
                    P2Server.getP2Server().getSystemUserInfo());
            twechatusersdetail.setWhere("FWECHATUSERID='" + FWECHATUSERID + "'");
            twechatusersdetail.reset();
            id = new String[twechatusersdetail.count()];
            int i = 0;
            while (twechatusersdetail.getPao(i) != null) {
                id[i] = twechatusersdetail.getPao(i).getString("hrid");
                i++;
            }

            StringBuilder where = new StringBuilder(" 1=2 ");
            for (String a : id) {
                where.append(" or hrid='").append(a).append("'");
            }

            pp_users = P2Server.getP2Server().getPaoSet("pp_users", P2Server.getP2Server().getSystemUserInfo());
            pp_users.setWhere(where.toString());
            pp_users.reset();
        } catch (P2Exception e) {
            e.printStackTrace();
        } finally {
            try {
                pp_users.close();
                twechatusersdetail.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return pp_users;
    }

    public String getagentnum(String fwechatuserid) {
        String fagentnum = "";
        PaoSetRemote twechatusersdetail = null;
        try {
            twechatusersdetail = P2Server.getP2Server().getPaoSet("twechatusersdetail",
                    P2Server.getP2Server().getSystemUserInfo());
            twechatusersdetail.setWhere("fwechatuserid='" + fwechatuserid + "' and fismain=1");
            twechatusersdetail.reset();
            if (twechatusersdetail.count() == 1) {
                PaoSetRemote pp_users = twechatusersdetail.getPao(0).getPaoSet("pp_users");
                fagentnum = pp_users.getPao(0).getString("fagentnum");
            }

        } catch (P2Exception e) {
            e.printStackTrace();
        } finally {
            try {
                twechatusersdetail.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fagentnum;
    }
}
