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

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.SQLFactory;
import com.cnd3b.common.data.db.DataPool;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.util.ArrayList;
import java.util.Iterator;

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

    /**
     * 人员组列表查询
     *
     * @return
     */
    public String query_usergroupList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.createdate desc"};
        String sort = getSort(sortfield, "t1.createdate desc");
        /**
         * 过滤条件设置
         */
        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.fgroupname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
        }
        SQLFactory sqlFactory = new SQLFactory(this, "人员组列表查询", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 人员组主界面查询
     *
     * @return
     */
    public String query_usergroupMain() {
        String tusergroupid = content.getString("tusergroupid");
        SQLFactory sqlFactory = new SQLFactory(this, "人员组主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tusergroupid", tusergroupid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 人员组主界面人员明细查询
     *
     * @return
     */
    public String query_usergroupDetail() {
        String tusergroupid = content.getString("tusergroupid");
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.tgroupusersid"};
        String sort = getSort(sortfield, "t1.tgroupusersid");
        /**
         * 过滤条件设置
         */
        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.fgroupname like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
        }
        SQLFactory sqlFactory = new SQLFactory(this, "人员组主界面人员明细查询", pageSize, pageNumber, sort);
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tusergroupid", tusergroupid);
        sqlFactory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 人员组新增、修改
     *
     * @return
     */
    public String insertOrModifyMain() throws D3bException, P2Exception {
        long tusergroupid = content.getLongValue("tusergroupid"); //新增时默认传0
        String fgroupname = content.getString("fgroupname", "tusergroup.fgroupname", "小组名称");//组名
        String fnotes = content.getString("fnotes", "tusergroup.fnotes", "备注");//备注

        PaoSetRemote tusergroupSet = getP2ServerSystemPaoSet("tusergroup", "siteid='" + siteid + "' and tusergroupid='" + tusergroupid + "'");
        PaoRemote tusergroup = null;
        if (tusergroupid <= 0 || tusergroupSet.isEmpty()) {
            tusergroup = tusergroupSet.addAtEnd();
            tusergroup.setValue("siteid", siteid, 11L);//企业ID
            tusergroup.setValue("createby", username, 11L);//录入人
            tusergroup.setValue("createdate", sysdate, 11L);//录入时间
        } else {
            tusergroup = tusergroupSet.getPao(0);
        }
        tusergroup.setValue("fgroupname", fgroupname, 11L);//组名
        tusergroup.setValue("fnotes", fnotes, 11L);//备注
        tusergroup.setValue("changeby", username, 11L);//修改人
        tusergroup.setValue("changedate", sysdate, 11L);//修改时间
        content.put("tusergroupid", tusergroup.getUniqueIDValue());
        saveDataLog(tusergroup);
        tusergroupSet.save();

        return query_usergroupMain();
    }


    /**
     * 人员组明细新增
     *
     * @return
     */
    public String insertusers() {
        String tusergroupid = content.getString("tusergroupid"); //新增时默认传0
        JSONArray userarray = content.getJSONArray("tenterprise_userid");
        Iterator<Object> it = userarray.iterator();
        ArrayList<String> sqllist = new ArrayList<>();
        while (it.hasNext()) {
            String tenterprise_userid = (String) it.next();
            SQLFactory sqlFactory = new SQLFactory(this, "人员组人员添加");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("tgroupusersid", createTableID("tgroupusers", "tgroupusersid"));
            sqlFactory.addParameter("tenterprise_userid", tenterprise_userid);
            sqlFactory.addParameter("tusergroupid", tusergroupid);
            sqllist.add(sqlFactory.getSQL());
        }
        String status = dbConnect.runSqlUpdate(sqllist);
        if ("true".equals(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 人员组明细删除
     *
     * @return
     */
    public String deleteusers() {
        String tusergroupid = content.getString("tusergroupid"); //新增时默认传0
        JSONArray userarray = content.getJSONArray("tenterprise_userid");
        ArrayList<String> sqllist = new ArrayList<>();

        for (Object o : userarray) {
            String tenterprise_userid = (String) o;
            sqllist.add("delete from tgroupusers where siteid='" + siteid + "' and tusergroupid='" + tusergroupid + "' and tenterprise_userid='" + tenterprise_userid + "'");
        }
        String status = dbConnect.runSqlUpdate(sqllist);
        if ("true".equals(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }

    /**
     * 人员组删除
     *
     * @return
     */
    public String delete() {
        SQLFactory sqlFactory = new SQLFactory(this, "人员组删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tusergroupid", content.getString("tusergroupid"));

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