package com.cnd3b.restcontroller.enterprise.salerclient.mainpage.phonebook;

import com.alibaba.fastjson.JSONArray;
import com.cnd3b.common.Controller;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.RowsMap;
import com.cnd3b.common.data.SQLFactory;
import com.alibaba.fastjson.JSONObject;

public class phonebook extends Controller {
    public phonebook(JSONObject content) {
        super(content);
    }

    public String query() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.fgroupname", "t1.fdepname"};
        String sort = getSort(sortfield, "t1.fgroupname,t1.fdepname");
        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (content.containsKey("where")) {
            JSONObject whereObject = content.getJSONObject("where");
            if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
                where.append(" and(");
                where.append("t1.fname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fdepname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.frole like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fgroupname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fphonenumber like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
        }
        SQLFactory costhead = new SQLFactory(this, "通讯录查询");
        costhead.addParameter("siteid", siteid);
        costhead.addParameter_SQL("where", where);
        RowsMap rowsMap = dbConnect.runSqlQuery(costhead.getSQL()).toRowsMap("fgroupname");
        JSONArray returnarray = new JSONArray();
        for (String key : rowsMap.keySet()) {
            Rows rows = rowsMap.get(key);
            JSONObject object = new JSONObject();
            object.put("fgroupname", key);
            object.put("phonebooklist", rows.toJsonArray());
            returnarray.add(object);
        }

        return getSucReturnObject().setData(returnarray).saveToDataPool().toString();
    }


}
