package com.cnd3b.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.actionscore.ActionScore;
import com.cnd3b.common.actionscore.AgentActionScore;
import com.cnd3b.common.actionscore.EnterpriseActionScore;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.RowsMap;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DBConnect;
import com.cnd3b.common.data.db.DataPool;
import com.cnd3b.common.parameter.parameter;
import com.cnd3b.common.data.Row;
import p2.p2server.P2Server;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.security.ConnectionKey;
import p2.util.P2Exception;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.*;

public class Controller extends BaseClass {
    // 请求正文
    public JSONObject content;
    public int returnMaxCount = 1000;
    public D3BReturnObject returnObject;
    /**
     * 不要预加载数据，false表示需要预加载，true表示不需要预加载
     */
    public boolean donotpreloading = false;

    public int pageNumber = 1;
    public int pageSize = 20;
    public String $classname = "";
    public String $method = "";
    public String $accesstoken = "";

    public long userid = 0L;
    public String username = "";
    public String tstoreid = "";
    public long tstoreId = 0L;
    public String siteid = "";
    public String tagentsid = "";
    public long tagentsId = 0L;
    public String fusertype = "";
    public String fphonenumber = "";
    public DBConnect dbConnect = null;
    public boolean fissysadministrator = false;
    public boolean fisadministrator = false;
    public boolean fispromoters = false;//是否二级推广人
    public boolean fissingleagent = false;//是否独立经销商
    public String hostipport;//访问地址url 如http://localhost:8080 http://127.0.0.1:8080
    public Date sysdate;

    /**
     * 构造函数
     *
     * @param content
     */
    public Controller(JSONObject content) {
        this.dbConnect = new DBConnect();
        sysdate = getDateTime();
        this.content = content;
        if (content.containsKey("returnMaxCount")) {
            returnMaxCount = content.getIntValue("returnMaxCount");
        }
        returnObject = new D3BReturnObject(this);
        donotpreloading = content.containsKey("donotpreloading") && content.getBoolean("donotpreloading");
        this.content.remove("donotpreloading");
        if (this.content.containsKey("pageNumber")) {
            pageNumber = this.content.getIntValue("pageNumber");
        }

        if (this.content.containsKey("pageSize")) {
            pageSize = this.content.getIntValue("pageSize");
        }
        if (this.content.containsKey("$classname")) {
            $classname = this.content.getString("$classname");
        }
        if (this.content.containsKey("$method")) {
            $method = this.content.getString("$method");
        }
        if (this.content.containsKey("$requestHost")) {
            hostipport = this.content.getString("$requestHost");
        }
        if (this.content.containsKey("userid")) {
            userid = this.content.getLongValue("userid");
        } else if (this.content.containsKey("$accesstoken")) {
            $accesstoken = this.content.getString("$accesstoken");
            if(parameter.tokenlist.containsKey($accesstoken)){
                userid = parameter.tokenlist.get($accesstoken);
            }
        }
        if (parameter.userIdList.containsKey(userid)) {
            Row row = parameter.userIdList.get(userid);
            siteid = row.getString("siteid");
            tagentsid = row.getString("tagentsid");
            tstoreid = row.getString("tstoreid");
            fusertype = row.getString("fusertype");
            username = row.getString("fname");
            fphonenumber = row.getString("fphonenumber");
            fissysadministrator = row.getBoolean("fissysadministrator");
            fisadministrator = row.getBoolean("fisadministrator");
            fispromoters = "PROMOTERS".equalsIgnoreCase(siteid);
            fissingleagent = "AGENTS".equalsIgnoreCase(siteid);
            createRequestLog();
        }
        if (!"".equals(tstoreid)) {
            tstoreId = Long.parseLong(tstoreid);
        }
        if (!"".equals(tagentsid)) {
            tagentsId = Long.parseLong(tagentsid);
        }
    }


    public String getSort(String[] sortfield, String defaultsort) {
        String sort = "";
        if (content.containsKey("sort") && Arrays.asList(sortfield).contains(content.getString("sort").replace("desc", "").replace("asc", "").trim())) {
            sort = content.getString("sort");
        } else {
            sort = defaultsort;
        }
        return sort;
    }

    /**
     * 每次从数据库里获取数据时，后台自动加载一定数据的数据至缓存中
     *
     * @param loadcount
     */
    public void preloading(int loadcount) {
        if (!donotpreloading && loadcount > 0) {
            /**
             * 获得当前页码
             */
            int pageNumber = this.pageNumber;
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    for (int i = 1; i <= loadcount; i++) {
                        /**
                         * 先将是否需要缓存数据的key移除
                         */
                        content.remove("donotpreloading");
                        /**
                         * 设定需要缓存的页码。
                         */
                        if (content.get("pageNumber") instanceof String) {
                            content.put("pageNumber", String.valueOf(pageNumber + i));
                        } else {
                            content.put("pageNumber", (pageNumber + i));
                        }

                        /**
                         * 判断该数据是否存在于缓存中，如果不存在则查询一条
                         */
                        if (!DataPool.datapoolCtrl.containsKey(content.toString())) {
                            /**
                             * 缓存时，强制命令下次不再进行缓存
                             */
                            content.put("donotpreloading", true);
                            try {
                                /**
                                 * 执行请求方法
                                 */
                                Class clz = Class.forName("com.cnd3b.restcontroller." + $classname);
                                Constructor cla = clz.getDeclaredConstructor(JSONObject.class);
                                Object obj = cla.newInstance(content);
                                Method method = obj.getClass().getDeclaredMethod($method);
                                method.invoke(obj);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }.start();
        }
    }

    public long getUniqueIDValue(String tablename, String tableidname) {
        ConnectionKey locala = null;
        try {
            locala = P2Server.getP2Server().getSystemUserInfo()
                    .getConnectionKey();
            Connection localConnection = P2Server.getP2Server()
                    .getDBConnProvider().getConnection(locala);
            return p2.pao.m
                    .a(localConnection, tablename, tableidname);
        } catch (Exception e) {
            return 0;
        } finally {
            P2Server.getP2Server().getDBConnProvider().freeConnection(locala);
        }
    }


    public void saveDataLog(PaoRemote pao) throws P2Exception {
        if (content.containsKey("datalog")) {
            JSONArray datalogArray = content.getJSONArray("datalog");
            PaoSetRemote tdatalog = pao.getPaoSet("$tdatalog", "tdatalog");
            for (Object o : datalogArray) {
                JSONObject datalogObject = (JSONObject) o;
                PaoRemote datalog = tdatalog.addAtEnd();
                datalog.setValue("siteid", siteid, 11L);
                datalog.setValue("ftablename", pao.getName(), 11L);
                datalog.setValue("ftableid", pao.getUniqueIDValue(), 11L);
                datalog.setValue("ftype", datalogObject.getString("ftype"), 11L);
                datalog.setValue("flog", datalogObject.getString("flog"), 11L);
                datalog.setValue("changeby", username, 11L);
                datalog.setValue("changedate", getDateTime(), 11L);
                datalog.setValue("tenterprise_userid", userid, 11L);
            }
        }
    }

    public Rows getDataLog(String ftablename, String ftableid) {
        return dbConnect.runSqlQuery("select changeby,changedate,ftype,flog,tenterprise_userid from tdatalog where siteid='" + siteid
                + "' and ftablename='" + ftablename + "' and ftableid='" + ftableid + "'");
    }

    public Rows getDataLog(String ftablename, long ftableid) {
        return getDataLog(ftablename, String.valueOf(ftableid));
    }


    /**
     * 增加行动力值
     *
     * @param fsourcetype 行动累类型
     * @param fsourceid   来源ID
     * @param fnotes      加分备注
     */
    public void addActionScore(ActionScore.fsourcetype fsourcetype, long fsourceid, String fnotes) {
        new Thread() {
            @Override
            public void run() {
                switch (fusertype) {
                    case "经销商":
                        new AgentActionScore(siteid, tagentsid, userid, username, fsourcetype, fsourceid, fnotes).add();
                        break;
                    case "企业":
                        new EnterpriseActionScore(siteid, userid, username, fsourcetype, fsourceid, fnotes).add();
                        break;
                    default:
                        break;
                }
            }

        }.start();
    }

    /**
     * 增加行动力值
     *
     * @param fsourcetype 行动累类型
     * @param fsourceid   来源ID
     * @param fnotes      加分备注
     */
    public void addActionScore(ActionScore.fsourcetype fsourcetype, long userid, String username, long fsourceid, String fnotes) {
        new Thread() {
            @Override
            public void run() {
                if ("企业".equals(fusertype)) {
                    new EnterpriseActionScore(siteid, userid, username, fsourcetype, fsourceid, fnotes).add();
                }
            }

        }.start();
    }


    //userid:tuserrequestlogid
    public static HashMap<Long, Long> userRequestLogMap = new HashMap<>();
    //userid:count
    public static HashMap<Long, Long> userRequestLogcountMap = new HashMap<>();
    public static String lastday = "";

    /**
     * 创建请求日志
     */
    private void createRequestLog() {
        new Thread() {
            @Override
            public void run() {
                String nowday = getDate_Str();
                if (!lastday.equals(nowday)) {
                    lastday = nowday;
                    userRequestLogMap.clear();
                }
                if (!userRequestLogMap.containsKey(userid)) {
                    Rows rows = dbConnect.runSqlQuery("select tuserrequestlogid from tuserrequestlog where siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and convert(varchar(10),fdate,120)=convert(varchar(10),getdate(),120) ");
                    if (rows.isEmpty()) {
                        long tuserrequestlogid = createTableID("tuserrequestlog", "tuserrequestlogid");
                        SQLFactory sqlFactory = new SQLFactory(this, "请求日志插入");
                        sqlFactory.addParameter("tuserrequestlogid", tuserrequestlogid);
                        sqlFactory.addParameter("siteid", siteid);
                        sqlFactory.addParameter("tenterprise_userid", userid);
                        sqlFactory.addParameter("tagentsid", tagentsid);
                        String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
                        if ("true".equals(status)) {
                            userRequestLogMap.put(userid, tuserrequestlogid);
                        }
                    } else {
                        userRequestLogMap.put(userid, rows.get(0).getLong("tuserrequestlogid"));
                    }
                    userRequestLogcountMap.put(userid, 1L);
                } else {
                    long count = userRequestLogcountMap.get(userid) + 1;
                    if (count > 9) {
                        userRequestLogcountMap.put(userid, 1L);
                        long tuserrequestlogid = userRequestLogMap.get(userid);
                        dbConnect.runSqlUpdate("update tuserrequestlog set flastrequestdate=getdate(),frequesttimes=frequesttimes+" + count + " where siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and tuserrequestlogid=" + tuserrequestlogid);
                    } else {
                        userRequestLogcountMap.put(userid, count);
                    }
                }
            }
        }.start();
    }


    /**
     * 获取一个新的表数据ID
     */
    private static HashMap<String, Long> tableidmap = new HashMap<>();

    public long createTableID(String tablename, String fieldname) {
        synchronized (Controller.class) {
            tablename = tablename.toLowerCase();
            fieldname = fieldname.toLowerCase();
            if (!tableidmap.containsKey(tablename)) {
                Rows rows = dbConnect.runSqlQuery("select convert(bigint,isnull(max(" + fieldname + "),0)) as " + fieldname + " from " + tablename);
                if (!rows.isEmpty()) {
                    long id = rows.get(0).getLong(fieldname);
                    tableidmap.put(tablename, id);
                }
            }
            tableidmap.put(tablename, tableidmap.get(tablename) + 1);
            return tableidmap.get(tablename);
        }
    }

    public long createTableID(String tablename, String fieldname, int count) {
        synchronized (Controller.class) {
            tablename = tablename.toLowerCase();
            fieldname = fieldname.toLowerCase();
            if (!tableidmap.containsKey(tablename)) {
                Rows rows = dbConnect.runSqlQuery("select convert(bigint,isnull(max(" + fieldname + "),0)) as " + fieldname + " from " + tablename);
                if (!rows.isEmpty()) {
                    long id = rows.get(0).getLong(fieldname);
                    tableidmap.put(tablename, id);
                }
            }
            tableidmap.put(tablename, tableidmap.get(tablename) + 1 + count);
            return tableidmap.get(tablename) - count;
        }
    }


    public Row getUser(String userid) {
        return dbConnect.runSqlQuery("select  * from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid=" + userid).get(0);
    }

    public Row getUser(String userid, String siteid) {
        return dbConnect.runSqlQuery("select  * from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid=" + userid).get(0);
    }

    public RowsMap getUser(String userids[]) {
        StringBuilder where = new StringBuilder("''");
        for (String userid : userids) {
            where.append(",'").append(userid).append("'");
        }
        return dbConnect.runSqlQuery("select  * from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid in(" + where + ")").toRowsMap("tenterprise_userid");
    }

    /**
     * 获取用户账户余额
     *
     * @return
     */
    public long getUserBalance() {
        Rows balancerows = dbConnect.runSqlQuery("select faccountbalance from tenterprise_users where siteid='" + siteid + "' and tenterprise_userid=" + userid);
        if (!balancerows.isEmpty()) {
            return balancerows.get(0).getLong("faccountbalance");
        } else {
            return 0L;
        }
    }

    /**
     * 获取用户账户冻结金额
     *
     * @return
     */
    public long getUserFreez() {
        Rows freezrows = dbConnect.runSqlQuery("select ffreezamount from accountfreez_view where siteid='" + siteid + "' and tenterprise_userid=" + userid);
        if (!freezrows.isEmpty()) {
            return freezrows.get(0).getLong("ffreezamount");
        } else {
            return 0L;
        }
    }

    /**
     * 获取账户可用余额
     *
     * @return
     */
    public long getUserCanUseAmount() {
        Rows canuseamountrows = dbConnect.runSqlQuery("select (isnull(t1.faccountbalance,0)-isnull(t2.ffreezamount,0))as fcanuseamount from tenterprise_users t1 left join accountfreez_view t2 on t1.siteid=t2.siteid and t1.tenterprise_userid=t2.tenterprise_userid where t1.siteid='" + siteid + "' and t1.tenterprise_userid='" + userid + "'");
        if (!canuseamountrows.isEmpty()) {
            return canuseamountrows.get(0).getLong("fcanuseamount");
        } else {
            return 0L;
        }
    }

    /**
     * 获取商学院功能说明
     *
     * @return
     */
    public long getAppHelpNotes(String ftype) {
        long tarchives_scid = 0L;
        if (parameter.isdebug()) {
            if ("销售系统".equals(ftype)) {
                tarchives_scid = 2305L;
            }
        } else {
            if ("销售系统".equals(ftype)) {
                tarchives_scid = 2305L;
            }
        }
        return tarchives_scid;
    }

    public D3BReturnObject_Err getErrReturnObject() {
        return returnObject.getErrObject();
    }

    public D3BReturnObject_Suc getSucReturnObject() {
        return returnObject.getSucObject();
    }


    /**
     * 获取当前及下属的账号列表，返回一个账号列表字符串
     *
     * @return
     */
    public String[] getSubUsers() {
        if (content.containsKey("fsalerid") && !"".equals(content.getString("fsalerid"))) {
            return getSubUsers(content.getString("fsalerid"));
        } else {
            Rows rows = getSubUserRows();
            return rows.toArray("tenterprise_userid");
        }
    }

    /**
     * 获取当前及下属的账号列表，返回一个账号列表字符串
     *
     * @return
     */
    public String[] getSubUsers(String userid) {
        Rows rows = getSubUserRows(siteid, userid);
        return rows.toArray("tenterprise_userid");
    }

    /**
     * 获取当前及下属的账号列表，返回一个账号列表字符串
     *
     * @return
     */
    public String[] getSubUsers(String siteid, String userid) {
        Rows rows = getSubUserRows(siteid, userid);
        return rows.toArray("tenterprise_userid");
    }

    public Rows getSubUserRows() {
        String key = siteid + userid;
        Object object = DataPool.get(key);
        if (object != null) {
            return (Rows) object;
        }
        SQLFactory sqlFactory = new SQLFactory("下属账号列表获取");
        sqlFactory.addParameter("userid", userid);
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        DataPool.put(key, rows, 60);
        return rows;
    }

    public Rows getSubUserRows(String siteid, String userid) {
        String key = siteid + userid;
        Object object = DataPool.get(key);
        if (object != null) {
            return (Rows) object;
        }
        SQLFactory sqlFactory = new SQLFactory("下属账号列表获取");
        sqlFactory.addParameter("userid", userid);
        sqlFactory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        DataPool.put(key, rows, 60);
        return rows;
    }

    public RowsMap getAttachmentUrl(String ownertable, String[] ownerids) {
        return getAttachmentUrl(ownertable, ownerids, "default");
    }

    public RowsMap getAttachmentUrl(String ownertable, ArrayList<String> ownerids) {
        return getAttachmentUrl(ownertable, ownerids, "default");
    }

    public Rows getAttachmentUrl(String ownertable, String ownerids) {
        return getAttachmentUrl(ownertable, ownerids, "default");
    }

    public RowsMap getAttachmentUrl(String ownertable, ArrayList<String> ownerids, String ftype) {
        return getAttachmentUrl(ownertable, ownerids.toArray(new String[0]), ftype);
    }

    public RowsMap getAttachmentUrl(String ownertable, String[] ownerids, String ftype) {
        SQLFactory docfactory = new SQLFactory("附件查询");
        docfactory.addParameter("siteid", siteid);
        docfactory.addParameter("ownertable", ownertable);
        docfactory.addParameter_in("ownerid", ownerids);
        docfactory.addParameter("ftype", ftype);
        return dbConnect.runSqlQuery(docfactory.getSQL()).toRowsMap("ownerid");
    }

    public Rows getAttachmentUrl(String ownertable, String ownerids, String ftype) {
        SQLFactory docfactory = new SQLFactory("附件查询");
        docfactory.addParameter("siteid", siteid);
        docfactory.addParameter("ownertable", ownertable);
        docfactory.addParameter_in("ownerid", ownerids);
        docfactory.addParameter("ftype", ftype);
        return dbConnect.runSqlQuery(docfactory.getSQL());
    }

    public RowsMap getAttachmentQty(String ownertable, String[] ownerids) {
        return getAttachmentQty(ownertable, ownerids, "default");
    }

    public RowsMap getAttachmentQty(String ownertable, ArrayList<String> ownerids) {
        return getAttachmentQty(ownertable, ownerids, "default");
    }

    public Rows getAttachmentQty(String ownertable, String ownerids) {
        return getAttachmentQty(ownertable, ownerids, "default");
    }

    public RowsMap getAttachmentQty(String ownertable, ArrayList<String> ownerids, String ftype) {
        return getAttachmentQty(ownertable, ownerids.toArray(new String[0]), ftype);
    }

    public RowsMap getAttachmentQty(String ownertable, String[] ownerids, String ftype) {
        SQLFactory docfactory = new SQLFactory("附件数量查询");
        docfactory.addParameter("siteid", siteid);
        docfactory.addParameter("ownertable", ownertable);
        docfactory.addParameter_in("ownerid", ownerids);
        docfactory.addParameter("ftype", ftype);
        return dbConnect.runSqlQuery(docfactory.getSQL()).toRowsMap("ownerid");
    }

    public Rows getAttachmentQty(String ownertable, String ownerids, String ftype) {
        SQLFactory docfactory = new SQLFactory("附件数量查询");
        docfactory.addParameter("siteid", siteid);
        docfactory.addParameter("ownertable", ownertable);
        docfactory.addParameter_in("ownerid", ownerids);
        docfactory.addParameter("ftype", ftype);
        Rows rows = dbConnect.runSqlQuery(docfactory.getSQL());
        return rows;
    }


    public static HashMap<String, String> uniquecolumnnamemap = new HashMap<>();

    public String getuniquecolumnname(String tablename) {
        tablename = tablename.toLowerCase();
        if (!uniquecolumnnamemap.containsKey(tablename)) {
            SQLFactory sqlFactory = new SQLFactory("表ID字段查询");
            sqlFactory.addParameter("tablename", tablename);
            Rows rows = dbConnect.runSqlQuery(sqlFactory);
            for (Row row : rows) {
                uniquecolumnnamemap.put(row.getString("tablename").toLowerCase(), row.getString("uniquecolumnname").toLowerCase());
            }
        }
        return uniquecolumnnamemap.get(tablename);
    }

    public Rows addAttachmentUrl(Rows rows, String tablename, String fdoctype, String jsonkeyname) {
        String uniquecolumnname = getuniquecolumnname(tablename);
        RowsMap rowsMap = getAttachmentUrl(tablename, rows.toArrayList(uniquecolumnname), fdoctype);
        for (Row row : rows) {
            String ownerid = row.getString(uniquecolumnname);
            if (rowsMap.containsKey(ownerid)) {
                row.put(jsonkeyname, rowsMap.get(ownerid));
            } else {
                row.put(jsonkeyname, "[]");
            }
        }
        return rows;
    }

    /**
     * 获取请求访问地址数据key
     *
     * @return
     */
    public long getRequestAuthorDataKey() throws P2Exception {
        long datakey = 0L;
        PaoSetRemote trequestauthor = getP2ServerSystemPaoSet("trequestauthor");
        PaoRemote pao = trequestauthor.addAtEnd();
        pao.setValue("siteid", siteid, 11L);
        Calendar calendar = Calendar.getInstance();
        pao.setValue("createdate", calendar.getTime(), 11L);
        calendar.add(Calendar.SECOND, 20);
        pao.setValue("closedate", calendar.getTime(), 11L);
        pao.setValue("tenterprise_userid", userid, 11L);
        datakey = pao.getUniqueIDValue();
        trequestauthor.save();
        return datakey;
    }

    //  public static HashMap<String,HashMap<String,Integer>>table_field_length_Map=new HashMap<>();

//    /**
//     * 文本字段验证
//     *
//     * @param fieldvalue
//     * @return  true表示超出长度，false表示未超出长度
//     */
//    public void fieldValueCheck_Str(String fieldname, String fieldtitle, String fieldvalue)throws Exception {
//        String[]split=fieldname.toUpperCase().split("[.]");
//        String tablename=split[0];
//       if(!table_field_length_Map.containsKey(tablename)){
//            HashMap<String,Integer>map=new HashMap<>();
//            Rows rows = dbConnect.runSqlQuery("select fieldname,length from ps_objectcols where objectname='"+tablename+"'");
//            for(Row row:rows){
//                map.put(row.getString("fieldname"),row.getInteger("length"));
//            }
//           table_field_length_Map.put(tablename,map);
//       }
//       int maxlength= table_field_length_Map.get(tablename).get(split[1])/2;
//        if (fieldvalue!=null&&fieldvalue.length() >maxlength) {
//            throw new Exception(fieldtitle + " 内容超过最大字数" + maxlength + "限制");
//        }
//    }
//    /**
//     * 文本字段验证
//     *
//     * @param fieldvalue
//     * @return  true表示超出长度，false表示未超出长度
//     */
//    public void fieldValueCheck_Str(String fieldname, String fieldtitle, String fieldvalue,int length)throws Exception {
//        String[]split=fieldname.toUpperCase().split("[.]");
//        String tablename=split[0];
//        if(!table_field_length_Map.containsKey(tablename)){
//            HashMap<String,Integer>map=new HashMap<>();
//            Rows rows = dbConnect.runSqlQuery("select fieldname,length from ps_objectcols where objectname='"+tablename+"'");
//            for(Row row:rows){
//                map.put(row.getString("fieldname"),row.getInteger("length"));
//            }
//            table_field_length_Map.put(tablename,map);
//        }
//        int maxlength= table_field_length_Map.get(tablename).get(split[1])/2;
//        if (fieldvalue!=null&&(fieldvalue.length() >maxlength||fieldvalue.length()>length)) {
//            throw new Exception(fieldtitle + " 内容超过最大字数" + maxlength + "限制");
//        }
//    }

//    /**
//     * 字段验证
//     *
//     * @param object
//     * @param fieldname
//     * @return 错误信息
//     */
//    public void isNumeric(JSONObject object, String fieldname, String fieldtitle) throws Exception{
//        try {
//            object.getFloatValue(fieldname);
//        } catch (Exception e) {
//            throw new Exception(fieldtitle + "为数值项，请填入正确的数值");
//        }
//    }

    public void p2ServerSystemPaoSetClose() {
        for (PaoSetRemote paoset : PaoSetRemoteList) {
            try {
                paoset.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        PaoSetRemoteList.clear();
    }
}
