package com.cnd3b.restcontroller.enterprise.webclient.phonebook;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.D3bException;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.RowsMap;
import com.cnd3b.common.data.SQLFactory;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

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();
    }

    /**
     * 通讯录新增、修改
     *
     * @return
     */
    public String insertOrModify() throws D3bException, P2Exception {
        long tenterprise_phonid = content.getLongValue("tenterprise_phonid"); //新增时默认传0
        String fname = content.getString("fname", "tenterprise_phonebook.fname", "姓名");//姓名
        String fdepname = content.getString("fdepname", "tenterprise_phonebook.fdepname", "部门");//部门名称
        String frole = content.getString("frole", "tenterprise_phonebook.frole", "职务");//角色
        String fphonenumber = content.getString("fphonenumber", "tenterprise_phonebook.fphonenumber", "手机号");//手机号
        String fgroupname = content.getString("fgroupname");//分组名称

        PaoSetRemote tenterprise_phonebookSet = getP2ServerSystemPaoSet("tenterprise_phonebook", "siteid='" + siteid + "' and tenterprise_phonid='" + tenterprise_phonid + "'");
        PaoRemote tenterprise_phonebook = null;
        if (tenterprise_phonid <= 0 || tenterprise_phonebookSet.isEmpty()) {
            tenterprise_phonebook = tenterprise_phonebookSet.addAtEnd();
            tenterprise_phonebook.setValue("siteid", siteid, 11L);//企业ID
            tenterprise_phonebook.setValue("createby", username, 11L);//录入人
            tenterprise_phonebook.setValue("createdate", sysdate, 11L);//录入时间
        } else {
            tenterprise_phonebook = tenterprise_phonebookSet.getPao(0);
        }
        tenterprise_phonebook.setValue("fname", fname, 11L);//姓名
        tenterprise_phonebook.setValue("fdepname", fdepname, 11L);//部门
        tenterprise_phonebook.setValue("frole", frole, 11L);//角色
        tenterprise_phonebook.setValue("fphonenumber", fphonenumber, 11L);//手机号
        tenterprise_phonebook.setValue("fgroupname", fgroupname, 11L);//分组名称
        tenterprise_phonebook.setValue("changeby", username, 11L);//修改人
        tenterprise_phonebook.setValue("changedate", sysdate, 11L);//修改时间
        saveDataLog(tenterprise_phonebook);
        tenterprise_phonebookSet.save();

        return getSucReturnObject().toString();
    }

    /**
     * 通讯录删除
     *
     * @return
     */
    public String delete() {
        SQLFactory sqlFactory = new SQLFactory(this, "通讯录删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tenterprise_phonid", content.getLongValue("tenterprise_phonid"));

        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }


    /**
     * 人员组列表查询
     *
     * @return
     */
    public String query_groupnameList() {
        SQLFactory sql = new SQLFactory(this, "通讯录分类查询");
        sql.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 人员组新增修改
     *
     * @return
     */
    public String insert_modify_groupname() throws D3bException, P2Exception {
        long ttypedetailid = content.getLongValue("ttypedetailid");//新增为0
        String fgroupname = content.getString("fgroupname", "ttypedetail.fvalue", "通讯录分类名称");
        boolean fisused = content.getBooleanValue("fisused");

        Rows rows = dbConnect.runSqlQuery("select * from ttypedetail where siteid='" + siteid + "' and ftype='通讯录人员分类' and fvalue='" + fgroupname + "'");
        if (!rows.isEmpty() && rows.get(0).getLong("ttypedetailid") != ttypedetailid) {
            return getErrReturnObject().setErrMsg("通讯录人员分类不可重复").toString();
        }

        PaoSetRemote ttypedetailSet = getP2ServerSystemPaoSet("ttypedetail", "siteid='" + siteid + "' and ttypedetailid='" + ttypedetailid + "'");
        PaoRemote ttypedetail = null;
        if (ttypedetailSet.isEmpty()) {
            ttypedetail = ttypedetailSet.addAtEnd();
            ttypedetail.setValue("siteid", siteid, 11L);//企业ID
            ttypedetail.setValue("ftype", "通讯录人员分类", 11L);
        } else {
            ttypedetail = ttypedetailSet.getPao(0);
        }
        ttypedetail.setValue("fvalue", fgroupname, 11L);
        ttypedetail.setValue("fisused", fisused, 11L);
        ttypedetailSet.save();

        return getSucReturnObject().toString();
    }

    /**
     * 来源渠道删除
     *
     * @return
     */
    public String delete_groupname() {
        SQLFactory sqlFactory = new SQLFactory(this, "通讯录分类删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("ttypedetailid", content.getLongValue("ttypedetailid"));
        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }
}
