Search in sources :

Example 1 with CmsLotteryUser

use of com.publiccms.entities.cms.CmsLotteryUser in project PublicCMS-preview by sanluan.

the class LotteryDirective method execute.

@Override
public void execute(RenderHandler handler, SysApp app, SysUser user) throws IOException, Exception {
    Long lotteryId = handler.getLong("lotteryId");
    CmsLottery lottery = lotteryService.getEntity(lotteryId);
    SysSite site = getSite(handler);
    handler.put("result", "failure");
    if (null != lottery && site.getId() == lottery.getSiteId() && !lottery.isDisabled()) {
        if (lotteryUserService.getPage(lotteryId, user.getId(), true, null, null, null, null, null).getTotalCount() == 0) {
            if (lottery.getLotteryCount() - lotteryUserService.getPage(lotteryId, user.getId(), null, null, null, null, null, null).getTotalCount() > 0) {
                CmsLotteryUser entity = new CmsLotteryUser(lotteryId, user.getId(), false, false, RequestUtils.getIpAddress(handler.getRequest()), CommonUtils.getDate());
                entity.setUserId(user.getId());
                if (lottery.getFractions() > random.nextInt(lottery.getNumerator()) && lotteryService.updateLastGift(lotteryId) && lottery.getLotteryCount() > lotteryUserService.getPage(lotteryId, null, true, null, null, null, null, null).getTotalCount()) {
                    entity.setWinning(true);
                }
                lotteryUserService.save(entity);
                handler.put("result", "success");
                handler.put("winning", entity.isWinning());
            }
        }
    }
}
Also used : CmsLotteryUser(com.publiccms.entities.cms.CmsLotteryUser) CmsLottery(com.publiccms.entities.cms.CmsLottery) SysSite(com.publiccms.entities.sys.SysSite)

Aggregations

CmsLottery (com.publiccms.entities.cms.CmsLottery)1 CmsLotteryUser (com.publiccms.entities.cms.CmsLotteryUser)1 SysSite (com.publiccms.entities.sys.SysSite)1