package p2.application.tnotice;

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 tnotice extends PaoCust {
    public tnotice(PaoSet arg0) {
        super(arg0);
    }

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

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

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

        {
            /**
             * 制造浏览记录
             */
            PaoSetRemote tnotice_userdataSet = getPaoSet("tnotice_userdata", "tnotice_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 tnotice_userdata = tnotice_userdataSet.addAtEnd();
                tnotice_userdata.setValue("siteid", getString("siteid"), 11L);
                tnotice_userdata.setValue("tnoticeid", getUniqueIDValue(), 11L);
                tnotice_userdata.setValue("tenterprise_userid", userrow.getString("tenterprise_userid"), 11L);
                tnotice_userdata.setValue("createby", userrow.getString("fname"), 11L);
                Calendar calendar = Calendar.getInstance();
                tnotice_userdata.setValue("createdate", calendar.getTime(), 11L);
                calendar.add(Calendar.MINUTE, 5);
                tnotice_userdata.setValue("closedate", calendar.getTime(), 11L);
                tnotice_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;
    }
}
