package com.cnd3b.restcontroller.enterprise.agentclient.usercenter.teammsg;

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

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

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

    /**
     * 经销商账号列表查询
     *
     * @return
     */
    public String query_userList() {
        SQLFactory sqlFactory = new SQLFactory(this, "经销商账号列表查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());

        SQLFactory subusersql = new SQLFactory(this, "下级账号查询");
        subusersql.addParameter("siteid", siteid);
        subusersql.addParameter_in("fparentid", rows.toArray("tenterprise_userid"));

        RowsMap map = dbConnect.runSqlQuery(subusersql.getSQL()).toRowsMap("fparentid");
        for (Row row : rows) {
            row.put("subusers", map.get(row.getString("tenterprise_userid")));
        }
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 经销商账号信息修改
     *
     * @return
     */
    public String update_userMsg() throws D3bException, P2Exception {
        long tenterprise_userid = content.getLongValue("tenterprise_userid");//账号ID
        String fname = content.getString("fname", "tenterprise_users.fname", "姓名");//姓名
        String tstoreid = content.getString("tstoreid");//门店
        String frole = content.getString("frole", "tenterprise_users.frole", "职位");//职位
        String fphonenumber = content.getString("fphonenumber", "tenterprise_users.fphonenumber", "手机号码");//手机号

        JSONArray subusersArray = content.getJSONArray("subusers");

        PaoSetRemote tenterprise_usersSet = getP2ServerSystemPaoSet("tenterprise_users", "siteid='" + siteid + "' and tagentsid='" + tagentsid + "' and tenterprise_userid='" + tenterprise_userid + "'");
        PaoRemote pao = null;
        if (tenterprise_userid <= 0 || tenterprise_usersSet.isEmpty()) {
            pao = tenterprise_usersSet.addAtEnd();
            pao.setValue("tagentsid", tagentsid, 11L);
            pao.setValue("siteid", siteid, 11L);
            pao.setValue("fusertype", "经销商", 11L);
            tenterprise_userid = pao.getUniqueIDValue();
        } else {
            pao = tenterprise_usersSet.getPao(0);
        }
        pao.setValue("fname", fname, 11L);
        pao.setValue("tstoreid", tstoreid, 11L);
        pao.setValue("frole", frole, 11L);
        pao.setValue("fphonenumber", fphonenumber, 11L);
        pao.setValue("changeby", username, 11L);
        pao.setValue("changedate", getDateTime(), 11L);

        /**
         * 查询下级账号，如不存在，则新增下级账号
         */
        PaoSetRemote tenterprise_userdetailSet = pao.getPaoSet("$tenterprise_userdetail", "tenterprise_userdetail", "siteid=:siteid and fparentid=:tenterprise_userid");
        HashMap<String, PaoRemote> paoMap = getPaoMap(tenterprise_userdetailSet, "fsubid");

        /**
         * 用以判断是否需要将表体行删除
         */
        ArrayList<String> donotdeleteid = new ArrayList<>();

        Iterator subuserlistit = subusersArray.iterator();
        ArrayList<String> distinctid = new ArrayList<>();
        while (subuserlistit.hasNext()) {
            JSONObject subuser = (JSONObject) subuserlistit.next();
            String fsubid = subuser.getString("fsubid");
            if (distinctid.contains(fsubid)) {
                continue;
            } else {
                distinctid.add(fsubid);
            }
            PaoRemote subuserpao = null;
            if (!paoMap.containsKey(fsubid)) {
                subuserpao = tenterprise_userdetailSet.addAtEnd();
                subuserpao.setValue("siteid", siteid, 11L);//企业ID
                subuserpao.setValue("fparentid", tenterprise_userid, 11L);//账号ID
                subuserpao.setValue("fsubid", fsubid, 11L);//下级账号ID
                subuserpao.setValue("createby", username, 11L);//录入人
                subuserpao.setValue("createdate", getDateTime(), 11L);//录入时间
            } else {
                donotdeleteid.add(fsubid);
                subuserpao = paoMap.get(fsubid);
            }
            subuserpao.setValue("changeby", username, 11L);//修改人
            subuserpao.setValue("changedate", getDateTime(), 11L);//修改时间
        }
        /**
         * 删除多余的行
         */
        for (String fsubid : paoMap.keySet()) {
            if (!donotdeleteid.contains(fsubid)) {
                if (!paoMap.get(fsubid).isModified()) {
                    paoMap.get(fsubid).delete();
                }
            }
        }
        tenterprise_usersSet.save();
        return getSucReturnObject().toString();
    }

    /**
     * 账号绑定门店选择
     *
     * @return
     */
    public String selectStore() {
        SQLFactory sqlFactory = new SQLFactory(this, "账号绑定门店选择");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tagentsid", tagentsid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }
}
