package p2.application.tagentactivities;

import baseclass.PaoCust;
import com.cnd3b.common.data.Row;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.db.DBConnect;
import p2.pao.PaoRemote;
import p2.pao.PaoSet;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;
import p2.application.tagents.tagents;
import p2.application.tuseraccountdetails.tuseraccountdetails;

import java.util.Calendar;
import java.util.Random;

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

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

    @Override
    public void add() throws P2Exception {
        super.add();
        if (getOwner() instanceof tagents) {
            setValue("tagentsid", getOwner().getUniqueIDValue(), 11L);
        }
    }

    /**
     * 创建随机数据
     *
     * @throws P2Exception
     */
    public void createRandomData() throws P2Exception {
        DBConnect dbConnect = new DBConnect();

        String siteid = getString("siteid");
        String fcustfirstname[] = {"张", "李", "王", "夏", "胡", "沈", "周", "陆"};
        String fcustsecendname[] = {"晶", "静伟", "鑫晖", "爱军", "磊", "琪", "文华"};
        //推广人列表
        Rows tpromotersRows = dbConnect.runSqlQuery("select t1.tenterprise_userid as fsaler1id,t1.fname as fsaler1,t2.fsaler2id,t3.fname as fsaler2 from tenterprise_users t1 " +
                "left join tpromoters t2 on t1.siteid=t2.siteid and t1.tenterprise_userid=t2.fsaler1id and t1.tagentsid=t2.tagentsid and t2.fstatus=1 " +
                "left join tenterprise_users t3 on t2.fsaler2id=t3.tenterprise_userid where isnull(t1.fphonenumber,'')!='' and t1.siteid='" + getString("siteid") + "' and t1.tagentsid='" + getString("tagentsid") + "'");
        PaoSetRemote tagentactivities_shareSet = getPaoSet("tagentactivities_share", "tagentactivities_share");
        for (Row tpromoters : tpromotersRows) {
            String fsaler1id = tpromoters.getString("fsaler1id");
            String fsaler1 = tpromoters.getString("fsaler1");
            String fsaler2id = tpromoters.getString("fsaler2id");
            String fsaler2 = tpromoters.getString("fsaler2");

            //创建分享记录
            PaoRemote tagentactivities_share = tagentactivities_shareSet.addAtEnd();
            tagentactivities_share.setValue("siteid", siteid, 11L);//企业ID
            tagentactivities_share.setValue("fsaler1id", fsaler1id, 11L);//一级推广人ID
            tagentactivities_share.setValue("fsaler1", fsaler1, 11L);//一级推广人姓名
            if (fsaler2 != null && !fsaler2.equals("") && !fsaler2.equalsIgnoreCase("null")) {
                tagentactivities_share.setValue("fsaler2id", fsaler2id, 11L);//二级推广人ID
                tagentactivities_share.setValue("fsaler2", fsaler2, 11L);//二级推广人姓名
            }
            tagentactivities_share.setValue("tagentactivitiesid", getUniqueIDValue(), 11L);
            tagentactivities_share.setValue("createdate", getDateTime(), 11L);//推广时间
            tagentactivities_share.setValue("description", "随机数据");
            PaoSetRemote tagentactivities_custSet = tagentactivities_share.getPaoSet("tagentactivities_cust" + Calendar.getInstance(), "tagentactivities_cust");
            for (int o = 0; o < 10; o++) {
                String wechat_name = fcustfirstname[getRandomint(0, fcustfirstname.length)] + fcustsecendname[getRandomint(0, fcustsecendname.length)];
                //创建访客记录
                PaoRemote tagentactivities_cust = tagentactivities_custSet.addAtEnd();
                tagentactivities_cust.setValue("siteid", siteid, 11L);//企业ID
                tagentactivities_cust.setValue("tagentactivitiesid", getUniqueIDValue(), 11L);//活动ID
                tagentactivities_cust.setValue("tagentactivities_shareid", tagentactivities_share.getUniqueIDValue(), 11L);//分享ID
                tagentactivities_cust.setValue("fwechatname", wechat_name, 11L);//微信名
                tagentactivities_cust.setValue("createdate", getDateTime(), 11L);//分享时间
                tagentactivities_cust.setValue("fvisitors", 1, 11L);
                tagentactivities_cust.setValue("wechat_openid", "", 11L);
                tagentactivities_cust.setValue("changedate", getDateTime(), 11L);
                tagentactivities_cust.setValue("fvisitors", 1, 11L);
                tagentactivities_cust.setValue("description", "随机数据");
                if (fsaler2 != null && !fsaler2.equals("") && !fsaler2.equalsIgnoreCase("null")) {
                    tagentactivities_cust.setValue("famount", getInt("fsaler2rewardamount1"), 11L);
                    createUseraccountdetails("支出", "拉新", tagentactivities_cust, getInt("fsaler2rewardamount1"), "拉新：" + wechat_name, getLong("tenterprise_userid"));
                    createUseraccountdetails("收入", "拉新", tagentactivities_cust, getInt("fsaler2rewardamount1"), "拉新：" + wechat_name, Long.parseLong(fsaler2id));
                } else {
                    tagentactivities_cust.setValue("famount", getInt("fsaler1rewardamount1"), 11L);
                    createUseraccountdetails("支出", "拉新", tagentactivities_cust, getInt("fsaler1rewardamount1"), "拉新：" + wechat_name, getLong("tenterprise_userid"));
                    createUseraccountdetails("收入", "拉新", tagentactivities_cust, getInt("fsaler1rewardamount1"), "拉新：" + wechat_name, Long.parseLong(fsaler1id));
                }


                //创建表单记录
                if (getRandomint(0, 2) == 1) {
                    PaoSetRemote tagentactivities_formSet = tagentactivities_cust.getPaoSet("tagentactivities_form" + Calendar.getInstance(), "tagentactivities_form");
                    PaoRemote tagentactivities_form = tagentactivities_formSet.addAtEnd();
                    tagentactivities_form.setValue("createdate", getDateTime(), 11L);
                    tagentactivities_form.setValue("siteid", siteid, 11L);
                    tagentactivities_form.setValue("wechat_openid", "", 11L);
                    tagentactivities_form.setValue("tagentactivities_shareid", tagentactivities_share.getUniqueIDValue(), 11L);
                    tagentactivities_form.setValue("tagentactivitiesid", getUniqueIDValue(), 11L);
                    tagentactivities_form.setValue("description", "随机数据");
                    tagentactivities_form.setValue("fname", wechat_name, 11L);
                    tagentactivities_form.setValue("fphonenumber", "13732579910", 11L);
                    tagentactivities_form.setValue("faddress", "嘉兴", 11L);
                    if (fsaler2 != null && !fsaler2.equals("") && !fsaler2.equalsIgnoreCase("null")) {
                        tagentactivities_form.setValue("famount", getInt("fsaler2rewardamount2"), 11L);
                        createUseraccountdetails("支出", "线索", tagentactivities_form, getInt("fsaler2rewardamount2"), "线索：" + wechat_name, getLong("tenterprise_userid"));
                        createUseraccountdetails("收入", "线索", tagentactivities_cust, getInt("fsaler2rewardamount2"), "线索：" + wechat_name, Long.parseLong(fsaler2id));
                    } else {
                        tagentactivities_form.setValue("famount", getInt("fsaler2rewardamount1"), 11L);
                        createUseraccountdetails("支出", "线索", tagentactivities_form, getInt("fsaler2rewardamount1"), "线索：" + wechat_name, getLong("tenterprise_userid"));
                        createUseraccountdetails("收入", "线索", tagentactivities_cust, getInt("fsaler2rewardamount1"), "线索：" + wechat_name, Long.parseLong(fsaler1id));
                    }
                }
            }
        }
        setValue("description", "随机创建数据" + getDateTime_Str(), 11L);
    }

    public int getRandomint(int minvalue, int maxvalue) {

        // Row userrow = tpromotersRows.get(getRandomint(0, tpromotersRows.size()));
        return new Random().nextInt(maxvalue) % (maxvalue - minvalue + 1) + minvalue;
    }


    /**
     * 创建拉新支出明细
     */
    public void createUseraccountdetails(String ftype, String ftypemx, PaoRemote pao, int famount, String fnotes, long tenterprise_userid) throws P2Exception {
        if (famount != 0) {
            PaoSetRemote tuseraccountdetailsSet = pao.getPaoSet("$tuseraccountdetails" + Calendar.getInstance().getTime(), "tuseraccountdetails");

            tuseraccountdetails tuseraccountdetails = (tuseraccountdetails) tuseraccountdetailsSet.addAtEnd();
            String siteid = pao.getString("siteid");
            tuseraccountdetails.setValues(ftype, ftypemx, famount, fnotes, "系统", siteid, tenterprise_userid);
            /**
             * 账户余额通过tuseraccountdetails触发器实现
             */
        }
    }


}
