package p2.application.tarchives_ad;

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 java.util.Calendar;
import java.util.Random;

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

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

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

    @Override
    public void fieldAction(Object paoField, String fieldname) throws P2Exception {
        super.fieldAction(paoField, fieldname);
    }


    public void createRandomData() throws P2Exception {
        DBConnect dbConnect = new DBConnect();
        Rows userRows = dbConnect.runSqlQuery("select t1.tenterprise_userid,t1.fname,t1.tagentsid from tenterprise_users t1  where t1.siteid='" + getString("siteid") + "'");
        Rows docRows = dbConnect.runSqlQuery("select tattachmentid from tattachment where siteid='" + getString("siteid") + "' and ownertable='" + getName() + "' and ownerid='" + getUniqueIDValue() + "'");

        {
            /**
             * 制造浏览记录
             */
            PaoSetRemote tarchives_ad_userdataSet = getPaoSet("tarchives_ad_userdata", "tarchives_ad_userdata");
            PaoSetRemote tattachment_downlogSet = getPaoSet("tattachment_downlog", "tattachment_downlog");
            PaoSetRemote tarchives_ad_shareSet = getPaoSet("tarchives_ad_share", "tarchives_ad_share");
            PaoSetRemote tarchives_ad_shareformSet = getPaoSet("tarchives_ad_shareform", "tarchives_ad_shareform");
            for (int i = 0; i < userRows.size(); i++) {
                Row userrow = userRows.get(getRandomint(0, userRows.size()));

                PaoRemote tarchives_ad_userdata = tarchives_ad_userdataSet.addAtEnd();
                tarchives_ad_userdata.setValue("siteid", getString("siteid"), 11L);
                tarchives_ad_userdata.setValue("tarchives_adid", getUniqueIDValue(), 11L);
                tarchives_ad_userdata.setValue("tenterprise_userid", userrow.getString("tenterprise_userid"), 11L);
                tarchives_ad_userdata.setValue("createby", userrow.getString("fname"), 11L);
                Calendar calendar = Calendar.getInstance();
                tarchives_ad_userdata.setValue("createdate", calendar.getTime(), 11L);
                calendar.add(Calendar.MINUTE, 5);
                tarchives_ad_userdata.setValue("closedate", calendar.getTime(), 11L);
                tarchives_ad_userdata.setValue("description", "随机数据");

                if (docRows.size() > 0 && getRandomint(0, 2) == 1) {
                    //下载记录
                    Row docrow = docRows.get(getRandomint(0, docRows.size()));

                    PaoRemote tattachment_downlog = tattachment_downlogSet.addAtEnd();
                    tattachment_downlog.setValue("siteid", getString("siteid"));
                    tattachment_downlog.setValue("createby", userrow.getString("fname"));
                    tattachment_downlog.setValue("createdate", calendar.getTime());
                    tattachment_downlog.setValue("tagentsid", userrow.getString("tagentsid"));
                    tattachment_downlog.setValue("tenterprise_userid", userrow.getString("tenterprise_userid"));
                    tattachment_downlog.setValue("tattachmentid", docrow.getString("tattachmentid"));
                    tattachment_downlog.setValue("description", "随机数据");
                }

                if (getRandomint(0, 2) == 1) {
                    String fchannels[] = {"微信", "微信朋友圈", "新浪微博", "腾讯微博"};
                    String fchannel = fchannels[(getRandomint(0, fchannels.length))];

                    PaoRemote tarchives_ad_share = tarchives_ad_shareSet.addAtEnd();
                    tarchives_ad_share.setValue("siteid", getString("siteid"), 11L);//企业ID
                    tarchives_ad_share.setValue("tenterprise_userid", userrow.getString("tenterprise_userid"), 11L);//用户ID
                    tarchives_ad_share.setValue("tarchives_adid", getUniqueIDValue(), 11L);//推广素材ID
                    tarchives_ad_share.setValue("fchannel", fchannel, 11L);//分享渠道
                    tarchives_ad_share.setValue("createby", userrow.getString("fname"), 11L);//分享人
                    tarchives_ad_share.setValue("createdate", getDateTime(), 11L);//分享时间
                    tarchives_ad_share.setValue("description", "随机数据");

                    String fcustfirstname[] = {"张", "李", "王", "夏", "胡", "沈", "周", "陆"};
                    String fcustsecendname[] = {"晶", "静伟", "鑫晖", "爱军", "磊", "琪", "文华"};
                    for (int o = 0; o < 10; o++) {
                        if (getRandomint(0, 2) == 1) {
                            String custname = fcustfirstname[getRandomint(0, fcustfirstname.length)] + fcustsecendname[getRandomint(0, fcustsecendname.length)];

                            PaoRemote tarchives_ad_shareform = tarchives_ad_shareformSet.addAtEnd();
                            tarchives_ad_shareform.setValue("siteid", getString("siteid"), 11L);//企业ID
                            tarchives_ad_shareform.setValue("tenterprise_userid", userrow.getString("tenterprise_userid"), 11L);//分享用户ID
                            tarchives_ad_shareform.setValue("tarchives_adid", getUniqueIDValue(), 11L);//推广素材ID
                            tarchives_ad_shareform.setValue("fchannel", fchannel, 11L);//分享渠道
                            tarchives_ad_shareform.setValue("createdate", getDateTime(), 11L);//分享时间
                            tarchives_ad_shareform.setValue("fvisitors", 1, 11L);

                            tarchives_ad_shareform.setValue("fprovince", "浙江省", 11L);
                            tarchives_ad_shareform.setValue("fcity", "嘉兴市", 11L);
                            tarchives_ad_shareform.setValue("fcounty", "南湖区", 11L);
                            tarchives_ad_shareform.setValue("fname", custname, 11L);
                            tarchives_ad_shareform.setValue("fphonenumber", "13732458910", 11L);
                            tarchives_ad_shareform.setValue("changedate", getDateTime(), 11L);

                            tarchives_ad_shareform.setValue("description", "随机数据");
                        }
                    }
                }
            }
            setValue("description", "随机创建数据" + getDateTime_Str(), 11L);
        }
    }

    public int getRandomint(int minvalue, int maxvalue) {
        return new Random().nextInt(maxvalue) % (maxvalue - minvalue + 1) + minvalue;
    }

}
