PrizeService.java 11.2 KB
package com.w1hd.zzhnc.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.RandomUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.testng.collections.Lists;
import org.testng.util.Strings;

import com.w1hd.zzhnc.dao.LotteryLogDao;
import com.w1hd.zzhnc.dao.PrizeDao;
import com.w1hd.zzhnc.model.Activity;
import com.w1hd.zzhnc.model.Lotterylog;
import com.w1hd.zzhnc.model.Prize;
import com.w1hd.zzhnc.util.CommonUtil;
import com.w1hd.zzhnc.util.JsonMapper;
import com.w1hd.zzhnc.util.PageResults;
import com.w1hd.zzhnc.util.RedisUtil;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class PrizeService {

	private static final int MAX_CHANGES = 4; // 无条件最大可抽奖次数

	final String LotteryLogID_ = RedisUtil.PROJECTNAME.concat("_").concat("lotteryLog_");// 中奖记录

	final String PRIZE_KILL_FANSID = RedisUtil.PROJECTNAME.concat("_").concat("PRIZE_KILL_FANSID_");// 保存粉丝中奖的日志

	@Autowired
	PrizeDao prizeDao;

	@Autowired
	LotteryLogDao lotteryLogDao;

	@Autowired
	ActivityService activityService;

	public boolean add(Prize prize) {
		return prizeDao.insert(prize) > 0;
	}

	public boolean update(Prize p) {
		return prizeDao.updateByPrimaryKeySelective(p) > 0;
	}

	public PageResults<Prize> search(String key, Integer page, Integer size) {

		Example ex = new Example(Prize.class);
		Criteria c = ex.createCriteria();
		if (!Strings.isNullOrEmpty(key)) {
			c.andCondition(" (name like \"%" + key + "%\" )");
		}
		c.andEqualTo("deleted", false);
		RowBounds row = new RowBounds((page - 1) * size, size);
		List<Prize> list = prizeDao.selectByExampleAndRowBounds(ex, row);
		int count = prizeDao.selectCountByExample(ex);
		PageResults<Prize> pageResults = new PageResults<>();
		pageResults.setTotal(count);
		pageResults.setPage(page);
		pageResults.setPageSize(size);
		pageResults.setRows(list);
		return pageResults;
	}

	public boolean delete(String id) {
		return prizeDao.deleteByPrimaryKey(id) > 0;
	}

	/**
	 * 增加一条奖项记录
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Integer insert(Integer fansId, Integer pType, BigDecimal mny) {
		Activity setting = activityService.getActivitySetting();
		Prize p = null;
		switch (pType) {
		case 1:
			p = setting.getP1();
			p.setNum(p.getNum() - 1);
			setting.setP1(p);
			break;
		case 2:
			p = setting.getP2();
			p.setNum(p.getNum() - 1);
			setting.setP2(p);
			break;
		case 3:
			p = setting.getP3();
			p.setNum(p.getNum() - 1);
			setting.setP4(p);
			break;
		case 4:
			p = setting.getP4();
			p.setNum(p.getNum() - 1);
			setting.setP4(p);
			break;
		case 5:
			p = setting.getP5();
			p.setNum(p.getNum() - 1);
			setting.setP5(p);
			break;
		case 6:
			p = setting.getP6();
			p.setNum(p.getNum() - 1);
			setting.setP6(p);
			break;
		case 7:
			p = setting.getP7();
			p.setNum(p.getNum() - 1);
			setting.setP7(p);
			break;
		case 8:
			p = setting.getP8();
			p.setNum(p.getNum() - 1);
			setting.setP8(p);
			break;

		default:
			break;
		}
		setting.setCurrentMny(setting.getCurrentMny().add(mny));
		activityService.updateActivity(setting);
		Lotterylog lotterylog = new Lotterylog();
		lotterylog.setCreatedtime(new Date());
		lotterylog.setFansid(fansId);
		lotterylog.setMny(mny);
		lotterylog.setPrizeName(p.getName());
		lotterylog.setPrizeId(pType);
		if ("谢谢".equals(p.getName())) {
			lotterylog.setStatus(3);
		} else {
			lotterylog.setStatus(1);
			if (mny.doubleValue() > 1.0) {
				lotterylog.setStatus(2);
				String order = creatOrder(fansId, (int) (mny.doubleValue() * 100));
				System.out.println("抽中一个红包 >" + order);
				lotterylog.setPrizeUrl(order);
			}

		}
		lotterylog.setTurn(setting.getTurn());
		lotteryLogDao.insert(lotterylog);
		redisTemplate.opsForValue().set(LotteryLogID_ + lotterylog.getId(), lotterylog);
		return lotterylog.getId();
	}

	/** 获取我的奖品 */
	public List<Lotterylog> getMyLotteryLog(Integer fansId) {

		Example ex = new Example(Lotterylog.class);
		Criteria c = ex.createCriteria();
		c.andEqualTo("fansid", fansId);
		List<Integer> status = Lists.newArrayList();
		status.add(1);
		status.add(2);
		c.andIn("status", status);
		List<Lotterylog> list = lotteryLogDao.selectByExample(ex);
		return list;
	}

	/** 核销一个奖品记录 */
	public Lotterylog updateLotteryLog(Integer id, Integer fansId) {
		Lotterylog lotterylog = lotteryLogDao.selectByPrimaryKey(id);
		lotterylog.setStatus(2);
		lotterylog.setUpdatetime(new Date());
		lotteryLogDao.updateByPrimaryKey(lotterylog);
		return lotterylog;
	}

	@Autowired
	FansService fansService;

	@Autowired
	RedisTemplate redisTemplate;

	public Integer changes(Integer fansId) {
		Integer lotteryLog = (Integer) redisTemplate.opsForValue().get(PRIZE_KILL_FANSID + fansId);
		if (lotteryLog == null) {
			return 1;
		} else {
			Activity activitySetting = activityService.getActivitySetting();
			int fansCount = fansService.getCountParentFansId(fansId);
			Example ex = new Example(Lotterylog.class);
			ex.createCriteria().andEqualTo("fansid", fansId);
			int lotteryCount = lotteryLogDao.selectCountByExample(ex);
			if (lotteryCount > MAX_CHANGES) {
				return 0;
			}
			int shareCount = activitySetting.getShareCount();
			if (shareCount == 0)
				shareCount = 1;
			int changes = fansCount / shareCount;
			changes = changes - lotteryCount;
			return changes + 1;
		}
	}

	@SuppressWarnings("unchecked")
	public Integer randomKill(Integer fansId, Integer count) {

		if (count == 8) {
			return 0;
		}
		redisTemplate.delete(PRIZE_KILL_FANSID + fansId);
		Activity activity = activityService.getActivitySetting();
		int nextInt = RandomUtils.nextInt(0, 100);
		System.out.println("幸运数字 ---》》》" + nextInt);
		int sumNum = 0;
		int sumPro = 0;
		Prize tmp = null;
		List<Prize> prizeList = activityService.getPrizeList();
		for (Prize p : prizeList) {
			if (p != null) {
				sumNum += p.getNum();
				sumPro += p.getProbability();
				if (p.getName().contains("谢谢")) {
					tmp = p;
				}
			}
		}
		int mny = 0;
		Integer logId = 0;
		if (nextInt > sumPro) {
			System.out.println("随机数大于总的中奖概率  随机数:" + nextInt + ",中奖总概率" + sumPro);
			if (tmp != null) {
				if (tmp.getIsMoney()) {
					mny = RandomUtils.nextInt(tmp.getMixMoney(), tmp.getMaxMoney());
				}
				logId = insert(fansId, tmp.getId(), new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			}
		} else {
			Prize p8 = activity.getP8();
			Prize p7 = activity.getP7();
			Prize p6 = activity.getP6();
			Prize p5 = activity.getP5();
			Prize p4 = activity.getP4();
			Prize p3 = activity.getP3();
			Prize p2 = activity.getP2();
			Prize p1 = activity.getP1();

			if (p1 != null && nextInt < p1.getProbability() && p1.getNum() > 1) {
				if (p1.getIsMoney()) {
					mny = RandomUtils.nextInt(p1.getMixMoney(), p1.getMaxMoney());
				}
				logId = insert(fansId, 1, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			} else if (p2 != null && nextInt < (p2.getProbability() + p1.getProbability()) && p2.getNum() > 1) {
				if (p2.getIsMoney()) {
					mny = RandomUtils.nextInt(p2.getMixMoney(), p2.getMaxMoney());
				}
				logId = insert(fansId, 2, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			} else if (p3 != null && nextInt < (p3.getProbability() + p2.getProbability() + p1.getProbability())
					&& p3.getNum() > 1) {
				if (p3.getIsMoney()) {
					mny = RandomUtils.nextInt(p3.getMixMoney(), p3.getMaxMoney());
				}
				logId = insert(fansId, 3, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			} else if (p4 != null
					&& nextInt < (p4.getProbability() + p3.getProbability() + p2.getProbability() + p1.getProbability())
					&& p4.getNum() > 1) {
				if (p4.getIsMoney()) {
					mny = RandomUtils.nextInt(p4.getMixMoney(), p4.getMaxMoney());
				}
				logId = insert(fansId, 4, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			} else if (p5 != null && nextInt < (p5.getProbability() + p4.getProbability() + p3.getProbability()
					+ p2.getProbability() + p1.getProbability()) && p5.getNum() > 1) {
				if (p5.getIsMoney()) {
					mny = RandomUtils.nextInt(p5.getMixMoney(), p5.getMaxMoney());
				}
				logId = insert(fansId, 5, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			} else if (p6 != null && nextInt < (p6.getProbability() + p5.getProbability() + p4.getProbability()
					+ p3.getProbability() + p2.getProbability() + p1.getProbability()) && p6.getNum() > 1) {
				if (p6.getIsMoney()) {
					mny = RandomUtils.nextInt(p6.getMixMoney(), p6.getMaxMoney());
				}
				logId = insert(fansId, 6, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			} else if (p7 != null && nextInt < (p7.getProbability() + p6.getProbability() + p5.getProbability()
					+ p4.getProbability() + p3.getProbability() + p2.getProbability() + p1.getProbability())
					&& p7.getNum() > 1) {
				if (p7.getIsMoney()) {
					mny = RandomUtils.nextInt(p7.getMixMoney(), p7.getMaxMoney());
				}
				logId = insert(fansId, 7, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			} else if (p8 != null
					&& nextInt < (p8.getProbability() + p7.getProbability() + p6.getProbability() + p5.getProbability()
							+ p4.getProbability() + p3.getProbability() + p2.getProbability() + p1.getProbability())
					&& p8.getNum() > 1) {
				if (p8.getIsMoney()) {
					mny = RandomUtils.nextInt(p7.getMixMoney(), p7.getMaxMoney());
				}
				logId = insert(fansId, 8, new BigDecimal(mny * 0.01).setScale(2, RoundingMode.HALF_UP));
			} else {
				if (sumNum > 0) {
					return randomKill(fansId, count++);
				}
			}
		}

		redisTemplate.opsForValue().set(PRIZE_KILL_FANSID + fansId, logId);
		return logId;
	}

	public Lotterylog getLog(Integer logId) {
		return (Lotterylog) redisTemplate.opsForValue().get(LotteryLogID_ + logId);
	}

	public static String getAuthorizeUrl(String orderId, String activityId, int fansId) {

		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd06aec668fe359d1&redirect_uri=http://www.w1hd.com/api/wx/wxd06aec668fe359d1/payWxMpUser2/"
				+ orderId + "/" + activityId + "/nmamtf&response_type=code&scope=snsapi_userinfo&state=" + fansId
				+ "&component_appid=wx79ad5a35526f26fb&connect_redirect=1#wechat_redirect";
		return url;
	}

	public static String creatOrder(Integer fansId, Integer mny) {
//		/** 测试的 */
//		String merchatId = "161";
//		String activityId = "1290";
		/** 正式的 */
		 String merchatId = "228";
		 String activityId = "1444";

		String sendGet = CommonUtil.sendGet("http://www.w1hd.com/api/wx/createOrder",
				"merchantid=" + merchatId + "&wxactivityId=" + activityId + "&mny=" + mny);

		Object fromJsonString = JsonMapper.fromJsonString(sendGet, Map.class);
		Map<String, Object> map = (Map<String, Object>) JsonMapper.fromJsonString(sendGet, Map.class);
		System.out.println("订单生成请求结果:" + sendGet);

		if (!map.get("code").equals(0) || !map.containsKey("data")) {
			return "fail";
		}

		return getAuthorizeUrl(map.get("data").toString(), activityId, fansId);

	}

}