package p2.application.tagents;

import baseclass.PaoCust;
import p2.pao.PaoRemote;
import p2.pao.PaoSet;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.util.HashMap;

public class tagents extends PaoCust {
    public tagents(PaoSet arg0) {
        super(arg0);
    }

    @Override
    public void add() throws P2Exception {
        super.add();
    }

    @Override
    protected void save() throws P2Exception {
        super.save();
        PaoRemote sysvar = getSysVars();
        //经销商账号数量
        int fagentaccountqty = sysvar.getInt("fagentaccountqty");
        PaoSetRemote tenterprise_users = getPaoSet("tenterprise_users");
        int createUserCount = 0;
        if ((createUserCount = (fagentaccountqty - tenterprise_users.count())) > 0) {
            for (int i = 0; i < createUserCount; i++) {
                if (tenterprise_users.isEmpty() && i == 0) {
                    PaoRemote tenterprise_user = tenterprise_users.addAtEnd();
                    tenterprise_user.setValue("fisadministrator", true, 11L);
                    tenterprise_user.setValue("frole", "管理员", 11L);
                    tenterprise_user.setValue("fname", getString("fcontact"), 11L);
                    tenterprise_user.setValue("fphonenumber", getString("fphonenumber"), 11L);
                    tenterprise_user.setValue("fbirthdate", getString("fbirthdate"), 11L);
                    tenterprise_user.setValue("femail", getString("femail"), 11L);
                    tenterprise_user.setValue("fwechatno", getString("fwechatno"), 11L);
                } else {
                    tenterprise_users.addAtEnd();
                }
            }
        }
        if (getString("description").contains("复制")) {
            String fcopyagentid = getString("description").replace("复制", "");
            copydata(Long.parseLong(fcopyagentid));
        }
    }

    /**
     * 经销商数据复制
     *
     * @return
     * @throws P2Exception
     */
    public void copydata(long tagentsid) throws P2Exception {
        //表名，老ID，新ID
        HashMap<String, HashMap<Long, Long>> idmap = new HashMap<>();

        saveid(idmap, getName(), tagentsid, getUniqueIDValue());
        {
            /**
             * 复制门店
             */
            PaoSetRemote sourcePaoSet = getPaoSet("tstore_source", "tstore", "tagentsid=" + tagentsid);
            PaoSetRemote newPaoset = getPaoSet("tstore_new", "tstore", "1=2");
            sourcePaoSet.copy(newPaoset);
            int i = 0;
            PaoRemote newpao = null;
            PaoRemote sourcepao = null;
            while ((newpao = newPaoset.getPao(i)) != null && (sourcepao = sourcePaoSet.getPao(i)) != null) {
                newpao.setValue("tagentsid", idmap.get("TAGENTS").get(sourcepao.getLong("tagentsid")));

                saveid(idmap, sourcePaoSet.getName(), sourcepao.getUniqueIDValue(), newpao.getUniqueIDValue());
                i++;
            }
        }

        {
            /**
             * 复制门店改建记录
             */
            PaoSetRemote sourcePaoSet = getPaoSet("tstore_rebuildlog_source", "tstore_rebuildlog", "tagentsid=" + tagentsid);
            PaoSetRemote newPaoset = getPaoSet("tstore_rebuildlog_new", "tstore_rebuildlog", "1=2");
            sourcePaoSet.copy(newPaoset);
            int i = 0;
            PaoRemote newpao = null;
            PaoRemote sourcepao = null;
            while ((newpao = newPaoset.getPao(i)) != null && (sourcepao = sourcePaoSet.getPao(i)) != null) {
                newpao.setValue("tagentsid", idmap.get("TAGENTS").get(sourcepao.getLong("tagentsid")));
                newpao.setValue("tstoreid", idmap.get("TSTORE").get(sourcepao.getLong("tstoreid")));

                saveid(idmap, sourcePaoSet.getName(), sourcepao.getUniqueIDValue(), newpao.getUniqueIDValue());
                i++;
            }
        }
        {
            /**
             * 复制客户档案
             */
            PaoSetRemote sourcePaoSet = getPaoSet("tcustomer_source", "tcustomer", "tagentsid=" + tagentsid);
            PaoSetRemote newPaoset = getPaoSet("tcustomer_new", "tcustomer", "1=2");
            sourcePaoSet.copy(newPaoset);
            int i = 0;
            PaoRemote newpao = null;
            PaoRemote sourcepao = null;
            while ((newpao = newPaoset.getPao(i)) != null && (sourcepao = sourcePaoSet.getPao(i)) != null) {
                newpao.setValue("tagentsid", idmap.get("TAGENTS").get(sourcepao.getLong("tagentsid")));
                newpao.setValue("tstoreid", idmap.get("TSTORE").get(sourcepao.getLong("tstoreid")));

                saveid(idmap, sourcePaoSet.getName(), sourcepao.getUniqueIDValue(), newpao.getUniqueIDValue());
                i++;
            }
        }
        {
            /**
             * 复制销售接待记录
             */
            PaoSetRemote sourcePaoSet = getPaoSet("tcustomer_reception_source", "tcustomer_reception", "tagentsid=" + tagentsid);
            PaoSetRemote newPaoset = getPaoSet("tcustomer_reception_new", "tcustomer_reception", "1=2");
            sourcePaoSet.copy(newPaoset);
            int i = 0;
            PaoRemote newpao = null;
            PaoRemote sourcepao = null;
            while ((newpao = newPaoset.getPao(i)) != null && (sourcepao = sourcePaoSet.getPao(i)) != null) {
                newpao.setValue("tagentsid", idmap.get("TAGENTS").get(sourcepao.getLong("tagentsid")));
                newpao.setValue("tstoreid", idmap.get("TSTORE").get(sourcepao.getLong("tstoreid")));
                newpao.setValue("tcustomerid", idmap.get("TCUSTOMER").get(sourcepao.getLong("tcustomerid")));

                saveid(idmap, sourcePaoSet.getName(), sourcepao.getUniqueIDValue(), newpao.getUniqueIDValue());
                i++;
            }
        }
        {
            /**
             * 复制销售订单明细
             */
            PaoSetRemote sourcePaoSet = getPaoSet("tcustomer_order_source", "tcustomer_order", "tagentsid=" + tagentsid);
            PaoSetRemote newPaoset = getPaoSet("tcustomer_order_new", "tcustomer_order", "1=2");
            sourcePaoSet.copy(newPaoset);
            int i = 0;
            PaoRemote newpao = null;
            PaoRemote sourcepao = null;
            while ((newpao = newPaoset.getPao(i)) != null && (sourcepao = sourcePaoSet.getPao(i)) != null) {
                newpao.setValue("tagentsid", idmap.get("TAGENTS").get(sourcepao.getLong("tagentsid")));
                newpao.setValue("tcustomerid", idmap.get("TCUSTOMER").get(sourcepao.getLong("tcustomerid")));
                newpao.setValue("tcustomer_receptid", idmap.get("TCUSTOMER_RECEPTION").get(sourcepao.getLong("tcustomer_receptid")));

                saveid(idmap, sourcePaoSet.getName(), sourcepao.getUniqueIDValue(), newpao.getUniqueIDValue());
                i++;
            }
        }

        {
            /**
             * 复制营销事件
             */
            PaoSetRemote sourcePaoSet = getPaoSet("tsaleevent_source", "tsaleevent", "tagentsid=" + tagentsid);
            PaoSetRemote newPaoset = getPaoSet("tsaleevent_new", "tsaleevent", "1=2");
            sourcePaoSet.copy(newPaoset);
            int i = 0;
            PaoRemote newpao = null;
            PaoRemote sourcepao = null;
            while ((newpao = newPaoset.getPao(i)) != null && (sourcepao = sourcePaoSet.getPao(i)) != null) {
                newpao.setValue("tagentsid", idmap.get("TAGENTS").get(sourcepao.getLong("tagentsid")));

                saveid(idmap, sourcePaoSet.getName(), sourcepao.getUniqueIDValue(), newpao.getUniqueIDValue());
                i++;
            }
        }

        {
            /**
             * 复制费用
             */
            PaoSetRemote sourcePaoSet = getPaoSet("tagents_cost_source", "tagents_cost", "tagentsid=" + tagentsid);
            PaoSetRemote newPaoset = getPaoSet("tagents_cost_new", "tagents_cost", "1=2");
            sourcePaoSet.copy(newPaoset);
            int i = 0;
            PaoRemote newpao = null;
            PaoRemote sourcepao = null;
            while ((newpao = newPaoset.getPao(i)) != null && (sourcepao = sourcePaoSet.getPao(i)) != null) {
                newpao.setValue("tagentsid", idmap.get("TAGENTS").get(sourcepao.getLong("tagentsid")));

                saveid(idmap, sourcePaoSet.getName(), sourcepao.getUniqueIDValue(), newpao.getUniqueIDValue());
                i++;
            }
        }
        {
            /**
             * 复制费用明细
             */
            PaoSetRemote sourcePaoSet = getPaoSet("tagents_costmx_source", "tagents_costmx", "tagentsid=" + tagentsid);
            PaoSetRemote newPaoset = getPaoSet("tagents_costmx_new", "tagents_costmx", "1=2");
            sourcePaoSet.copy(newPaoset);
            int i = 0;
            PaoRemote newpao = null;
            PaoRemote sourcepao = null;
            while ((newpao = newPaoset.getPao(i)) != null && (sourcepao = sourcePaoSet.getPao(i)) != null) {
                newpao.setValue("tagentsid", idmap.get("TAGENTS").get(sourcepao.getLong("tagentsid")));
                newpao.setValue("tagents_costid", idmap.get("TAGENTS_COST").get(sourcepao.getLong("tagents_costid")));

                saveid(idmap, sourcePaoSet.getName(), sourcepao.getUniqueIDValue(), newpao.getUniqueIDValue());
                i++;
            }
        }
        setValue("description", "", 11L);
    }

    public void saveid(HashMap<String, HashMap<Long, Long>> idmap, String tablename, long oldid, long newid) {
        HashMap<Long, Long> agentidMap = null;
        if (idmap.containsKey(tablename)) {
            agentidMap = idmap.get(tablename);
        } else {
            agentidMap = new HashMap<>();
        }
        agentidMap.put(oldid, newid);
        idmap.put(tablename, agentidMap);
    }


}
