package p2.application.tenterprise;

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

import java.util.Calendar;

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

    @Override
    protected void save() throws P2Exception {
        super.save();

        if (!toBeAdded()) {
            PaoRemote sysvar = getSysVars();
            //企业账号数量
            int fenterpriseaccountqty = sysvar.getInt("fenterpriseaccountqty");
            int createUserCount = 0;
            if ((createUserCount = (fenterpriseaccountqty - getPaoSet("$tenterprise_users", "tenterprise_users","siteid=:siteid and fusertype='企业'").count())) > 0) {
                PaoSetRemote tenterprise_users = getPaoSet("tenterprise_users");
                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("fissysadministrator", true, 11L);
                        tenterprise_user.setValue("frole", "管理员", 11L);
                    } else {
                        tenterprise_users.addAtEnd();
                    }
                }
            }
            //经销商账号数量
            int fagentaccountqty = sysvar.getInt("fagentaccountqty");
            PaoSetRemote tagents = getPaoSet("tagents");
            int n = 0;
            PaoRemote agentpao = null;
            while ((agentpao = tagents.getPao(n)) != null) {
                PaoSetRemote tagent_users = agentpao.getPaoSet("tenterprise_users");
                int createagentUserCount = 0;
                if ((createagentUserCount = (fagentaccountqty - tagent_users.count())) > 0) {
                    for (int i = 0; i < createagentUserCount; i++) {
                        if (tagent_users.isEmpty() && i == 0) {
                            PaoRemote tenterprise_user = tagent_users.addAtEnd();
                            tenterprise_user.setValue("fisadministrator", true, 11L);
                            tenterprise_user.setValue("frole", "管理员", 11L);
                            tenterprise_user.setValue("fname", agentpao.getString("fcontact"), 11L);
                            tenterprise_user.setValue("fphonenumber", agentpao.getString("fphonenumber"), 11L);
                            tenterprise_user.setValue("fbirthdate", agentpao.getString("fbirthdate"), 11L);
                            tenterprise_user.setValue("femail", agentpao.getString("femail"), 11L);
                            tenterprise_user.setValue("fwechatno", agentpao.getString("fwechatno"), 11L);
                        } else {
                            tagent_users.addAtEnd();
                        }
                    }
                }
                n++;
            }

            //自动复制销售动作
            PaoSetRemote ttypedetailSet = getPaoSet("ttypedetail1", "ttypedetail", "ftype='销售动作' and siteid='" + getString("siteid") + "'");
            if (ttypedetailSet.isEmpty()) {
                PaoSetRemote ttypedetaildefaultSet = getPaoSet("ttypedetail2", "ttypedetail", "ftype='销售动作' and siteid is null");
                int i = 0;
                PaoRemote defaultpao = null;
                while ((defaultpao = ttypedetaildefaultSet.getPao(i)) != null) {
                    PaoRemote newpao = ttypedetailSet.addAtEnd();
                    newpao.setValue("ftype", defaultpao.getString("ftype"));
                    newpao.setValue("ftypemx", defaultpao.getString("ftypemx"));
                    newpao.setValue("fvalue", defaultpao.getString("fvalue"));
                    newpao.setValue("siteid", getString("siteid"));
                    newpao.setValue("fisused", defaultpao.getBoolean("fisused"));
                    newpao.setValue("sequence", defaultpao.getInt("sequence"));
                    i++;
                }
            }

            //自动复制标准工作模板
            PaoSetRemote tworktemplateSet = getPaoSet("tworktemplate1", "tworktemplate", "siteid='" + getString("siteid") + "'");
            if (tworktemplateSet.isEmpty()) {
                PaoSetRemote tworktemplatedefaultSet = getPaoSet("tworktemplate2", "tworktemplate", "siteid is null");
                int i = 0;
                PaoRemote defaultpao = null;
                while ((defaultpao = tworktemplatedefaultSet.getPao(i)) != null) {
                    PaoRemote newpao = tworktemplateSet.addAtEnd();
                    newpao.setValue("fdocqty", defaultpao.getInt("fdocqty"));
                    newpao.setValue("fnotes", defaultpao.getString("fnotes"));
                    newpao.setValue("ftype", defaultpao.getString("ftype"));
                    newpao.setValue("fworkname", defaultpao.getString("fworkname"));
                    newpao.setValue("siteid", getString("siteid"));
                    newpao.setValue("fisused", defaultpao.getBoolean("fisused"));
                    newpao.setValue("fisimage", defaultpao.getBoolean("fisimage"));
                    newpao.setValue("fiseditnote", defaultpao.getBoolean("fiseditnote"));
                    i++;
                }
            }
        }
    }


    public void createdefaultpayinfovalidate() throws P2Exception {
        PaoSetRemote tagentsSet = getPaoSet("tagents");
        createdefaultpayinfovalidate(tagentsSet);
    }

    public void createdefaultpayinfovalidate(PaoSetRemote tagentsSet) throws P2Exception {
        PaoSetRemote tappmodelauthSet = getPaoSet("tappmodelauth");
        tappmodelauthSet.setWhere("fisdefault=1");
        tappmodelauthSet.reset();
        if (tappmodelauthSet.isEmpty()) {
            throw new P2AppException("", "请对该企业进行应用版本授权，且要有一个默认版本授权");
        }

        PaoRemote tappmodelauth = tappmodelauthSet.getPao(0);
        double fagentamount = tappmodelauth.getDouble("fagentamount");
        if (fagentamount <= 0) {
            throw new P2AppException("", "默认版本授权经销商实际支付金额小于等于0，不可生成付款记录");
        }
        for (PaoRemote tagentpao : (Iterable<PaoRemote>) tagentsSet.getSelection()) {
            PaoSetRemote tpayinfoSet = tagentpao.getPaoSet("tpayinfo", "tpayinfo", "siteid='" + getString("siteid") + "' and tagentsid='" + getUniqueIDValue() + "' and convert(varchar(10),fbegdate,120)<=convert(varchar(10),getdate(),120) and convert(varchar(10),fenddate,120)>=convert(varchar(10),getdate(),120)");
            if (!tpayinfoSet.isEmpty()) {
                throw new P2AppException("", "当前时间已存在一个有效的付款记录，不可重复生成");
            }
            PaoSetRemote tenterprise_usersSet = tagentpao.getPaoSet("tenterprise_users");
            tenterprise_usersSet.setWhere("fisadministrator=1");
            tenterprise_usersSet.reset();
            if (tenterprise_usersSet.isEmpty()) {
                throw new P2AppException("", "经销商" + tagentpao.getString("fagentname") + "不存在管理员账号,请检查");
            }
        }
    }

    public void createdefaultpayinfo() throws P2Exception {
        PaoSetRemote tagentsSet = getPaoSet("tagents");
        createdefaultpayinfo(tagentsSet);
    }

    public void createdefaultpayinfo(PaoSetRemote tagentsSet) throws P2Exception {
        PaoSetRemote tappmodelauthSet = getPaoSet("tappmodelauth");
        tappmodelauthSet.setWhere("fisdefault=1");
        tappmodelauthSet.reset();
        if (!tappmodelauthSet.isEmpty()) {
            PaoRemote tappmodelauth = tappmodelauthSet.getPao(0);
            String fappmodel = tappmodelauth.getString("fappmodel");
            double fagentamount = tappmodelauth.getDouble("fagentamount");
            for (PaoRemote tagentpao : (Iterable<PaoRemote>) tagentsSet.getSelection()) {
                PaoSetRemote tenterprise_usersSet = tagentpao.getPaoSet("tenterprise_users");
                tenterprise_usersSet.setWhere("fisadministrator=1");
                tenterprise_usersSet.reset();
                if (!tenterprise_usersSet.isEmpty()) {
                    PaoSetRemote tpayinfoSet = tagentpao.getPaoSet("$tpayinfo", "tpayinfo");
                    PaoRemote tpayinfo = tpayinfoSet.addAtEnd();
                    tpayinfo.setValue("siteid", getString("siteid"), 11L);
                    tpayinfo.setValue("famount", fagentamount, 11L);
                    tpayinfo.setValue("fbegdate", getDate(), 11L);
                    Calendar calendar = Calendar.getInstance();
                    calendar.add(Calendar.YEAR, 1);
                    tpayinfo.setValue("fenddate", calendar.getTime(), 11L);
                    tpayinfo.setValue("ftype", "线下", 11L);
                    tpayinfo.setValue("tagentsid", tagentpao.getUniqueIDValue(), 11L);
                    tpayinfo.setValue("tenterprise_userid", tenterprise_usersSet.getPao(0).getUniqueIDValue(), 11L);
                    tpayinfo.setValue("fappmodel", fappmodel, 11L);
                    tpayinfo.setValue("fsourceobject", "TAGENTS", 11L);
                    tpayinfo.setValue("fsourceid", tagentpao.getUniqueIDValue(), 11L);
                }
            }
        }
    }
}
