package p2.application.tarchives_sc;

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_sc extends PaoCust {
    public tarchives_sc(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_sc_userdataSet = getPaoSet("tarchives_sc_userdata", "tarchives_sc_userdata");
            PaoSetRemote tattachment_downlogSet = getPaoSet("tattachment_downlog", "tattachment_downlog");
            for (int i = 0; i < userRows.size(); i++) {
                Row userrow = userRows.get(getRandomint(0, userRows.size()));

                PaoRemote tarchives_sc_userdata = tarchives_sc_userdataSet.addAtEnd();
                tarchives_sc_userdata.setValue("siteid", getString("siteid"), 11L);
                tarchives_sc_userdata.setValue("tarchives_scid", getUniqueIDValue(), 11L);
                tarchives_sc_userdata.setValue("tenterprise_userid", userrow.getString("tenterprise_userid"), 11L);
                tarchives_sc_userdata.setValue("createby", userrow.getString("fname"), 11L);
                Calendar calendar = Calendar.getInstance();
                tarchives_sc_userdata.setValue("createdate", calendar.getTime(), 11L);
                calendar.add(Calendar.MINUTE, 5);
                tarchives_sc_userdata.setValue("closedate", calendar.getTime(), 11L);
                tarchives_sc_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", "随机数据");
                }
            }
            setValue("description", "随机创建数据" + getDateTime_Str(), 11L);
        }
    }

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