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

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

import java.util.ArrayList;
import java.util.HashMap;

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

    /**
     * 权限组列表查询
     *
     * @return
     */
    public String query_authgrouplist() {
        Rows rows = dbConnect.runSqlQuery("select tauthgroupid,fauthgroupname,fisused from tauthgroup where siteid='" + siteid + "'");
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 权限组功能明细查询
     *
     * @return
     */
    public String query_tauthgroupMain() {
        String tauthgroupid = content.getString("tauthgroupid");
        Rows rows = dbConnect.runSqlQuery("select tauthgroupid,fauthgroupname,fisused from tauthgroup where siteid='" + siteid + "' and tauthgroupid=" + tauthgroupid);
        for (Row row : rows) {
            Rows mxrows = dbConnect.runSqlQuery("select t1.tauthgroup_appsid,t1.tauthgroupid,t1.system_appsid,t2.systemid,t2.moduleid,t2.id,t2.fappname,t2.fnote from tauthgroup_apps t1 inner join system_apps t2 on t1.system_appsid=t2.system_appsid and t2.fisused=1 where t1.siteid='" + siteid + "' and tauthgroupid=" + tauthgroupid);
            row.put("apps", mxrows);
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 权限组新增、修改
     *
     * @return
     */
    public String insert_modify_tauthgroup() throws D3bException, P2Exception {
        long tauthgroupid = content.getLongValue("tauthgroupid");//新增为0
        String fauthgroupname = content.getString("fauthgroupname", "tauthgroup.fauthgroupname", "权限名称");//权限名称
        Boolean fisused = content.getBoolean("fisused");//是否启用
        JSONArray apps = content.getJSONArray("apps");

        PaoSetRemote tauthgroupSet = getP2ServerSystemPaoSet("tauthgroup", "siteid='" + siteid + "' and tauthgroupid='" + tauthgroupid + "'");
        PaoRemote tauthgroup = null;
        if (tauthgroupSet.isEmpty()) {
            tauthgroup = tauthgroupSet.addAtEnd();
            tauthgroup.setValue("siteid", siteid, 11L);//企业ID
        } else {
            tauthgroup = tauthgroupSet.getPao(0);
        }
        tauthgroup.setValue("fauthgroupname", fauthgroupname, 11L);
        tauthgroup.setValue("fisused", fisused, 11L);

        PaoSetRemote tauthgroup_appsSet = tauthgroup.getPaoSet("$tauthgroup_apps", "tauthgroup_apps", "siteid=:siteid and tauthgroupid=:tauthgroupid ");
        HashMap<String, PaoRemote> paoMap = getPaoMap(tauthgroup_appsSet, "tauthgroup_appsid");

        /**
         * 用以判断是否需要将表体行删除
         */
        ArrayList<String> donotdeleteid = new ArrayList<>();
        for (Object o : apps) {
            JSONObject app = (JSONObject) o;
            String tauthgroup_appsid = app.getString("tauthgroup_appsid");
            String system_appsid = app.getString("system_appsid");

            PaoRemote apppao = null;
            if ("0".equals(tauthgroup_appsid) || !paoMap.containsKey(tauthgroup_appsid)) {
                apppao = tauthgroup_appsSet.addAtEnd();
                apppao.setValue("siteid", siteid, 11L);//企业ID
                apppao.setValue("tauthgroupid", tauthgroup.getUniqueIDValue(), 11L);
            } else {
                donotdeleteid.add(tauthgroup_appsid);
                apppao = paoMap.get(tauthgroup_appsid);
            }
            apppao.setValue("system_appsid", system_appsid, 11L);
        }
        /**
         * 删除多余的行
         */
        for (String id : paoMap.keySet()) {
            if (!donotdeleteid.contains(id)) {
                if (!paoMap.get(id).isModified()) {
                    paoMap.get(id).delete();
                }
            }
        }
        content.put("tauthgroupid", tauthgroup.getString("tauthgroupid"));
        tauthgroupSet.save();

        return query_tauthgroupMain();
    }

    /**
     * 权限组删除
     *
     * @return
     */
    public String authgroupDelete() {
        String tauthgroupid = content.getString("tauthgroupid");
        ArrayList<String> SQLlist = new ArrayList<>();
        SQLlist.add("delete from tauthgroup where siteid='" + siteid + "' and tauthgroupid='" + tauthgroupid + "'");
        SQLlist.add("delete from tauthgroup_apps where siteid='" + siteid + "' and tauthgroupid='" + tauthgroupid + "'");
        SQLlist.add("update tenterprise_users set tauthgroupid=null where siteid='" + siteid + "' and tauthgroupid='" + tauthgroupid + "'");
        String result = dbConnect.runSqlUpdate(SQLlist);
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }

    /**
     * 系统功能选择
     *
     * @return
     */
    public String query_systemapps() {
        String tauthgroupid = content.getString("tauthgroupid");
        SQLFactory factory = new SQLFactory(this, "权限组系统功能选择");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tauthgroupid", tauthgroupid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 管理员列表查询
     *
     * @return
     */
    public String query_administrator() {
        SQLFactory factory = new SQLFactory(this, "管理员列表查询");
        factory.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 新增修改管理员
     *
     * @return
     */
    public String modify_administrator() {
        String tenterprise_userid = content.getString("tenterprise_userid");
        String tauthgroupid = content.getString("tauthgroupid");
        if ("".equals(tauthgroupid)) {
            dbConnect.runSqlUpdate("update tenterprise_users set fisadministrator=0, tauthgroupid=null where siteid='" + siteid + "' and tenterprise_userid='" + tenterprise_userid + "'");
        } else {
            dbConnect.runSqlUpdate("update tenterprise_users set fisadministrator=1, tauthgroupid='" + tauthgroupid + "' where siteid='" + siteid + "' and tenterprise_userid='" + tenterprise_userid + "'");
        }
        return query_administrator();
    }

    /**
     * 查询当前账号权限
     *
     * @return
     */
    public String query_myappauth() {
        if (fissysadministrator) {
            SQLFactory sqlFactory = new SQLFactory(this, "查询系统总管理员账号权限");
            sqlFactory.addParameter("siteid", siteid);
            Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
            return getSucReturnObject().setData(rows).saveToDataPool().toString();
        } else {
            SQLFactory sqlFactory = new SQLFactory(this, "查询账号权限");
            sqlFactory.addParameter("siteid", siteid);
            sqlFactory.addParameter("tenterprise_userid", userid);
            Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
            return getSucReturnObject().setData(rows).saveToDataPool().toString();
        }
    }

}
