package com.hssx.controller; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Random; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.filefilter.FalseFileFilter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.hssx.entity.Prize; import com.hssx.entity.PrizeExample; import com.hssx.entity.PrizeOpportunity; import com.hssx.entity.PrizeRecord; import com.hssx.entity.PrizeRecordExample; import com.hssx.entity.User; import com.hssx.entity.UserPrizeCount; import com.hssx.entity.UserPrizeCountExample; import com.hssx.mapper.PrizeMapper; import com.hssx.mapper.PrizeOpportunityMapper; import com.hssx.mapper.PrizeRecordMapper; import com.hssx.mapper.UserMapper; import com.hssx.mapper.UserPrizeCountMapper; import com.hssx.utils.HttpRespMsg; import com.sun.javafx.collections.MappingChange.Map; @Controller @RequestMapping("/prize") public class PrizeController { @Autowired UserMapper usermapper; @Autowired PrizeMapper prizeMapper; @Autowired PrizeRecordMapper prizeRecordMapper; @Autowired UserPrizeCountMapper UserPrizeCountMapper; @Autowired PrizeOpportunityMapper prizeOpportunityMapper; /** * 小游戏抽奖 参数: userId:当前抽奖人id * * @return */ @RequestMapping(value = "/luckDraw") public void luckDraw(@RequestParam(required = false) Integer userId, HttpServletResponse response) throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException { HttpRespMsg msg = new HttpRespMsg(); userId = new Random().nextInt(6)+1;//测试用 Integer count = -1; PrizeRecord prizeRecord = new PrizeRecord(); User user = usermapper.selectByPrimaryKey(userId); prizeRecord.setUid(user.getId()); prizeRecord.setType(user.getType()); prizeRecord.setVoucherId(user.getVoucherId()); prizeRecord.setNickName(user.getNickName()); UserPrizeCountExample uExp = new UserPrizeCountExample(); Integer luckDrawCount = UserPrizeCountMapper.selectCountByTodayByUid(userId); HashMap map = new HashMap(); // 模拟无限次数抽奖 // 测试必定中奖账号 if ("o1L3L5vwFkGSUMpec3uwRgsI6A-M".equals(user.getVoucherId())) { Prize prize = prizeMapper.selectByPrimaryKey(1); // UserPrizeCount userPrizeCount = new UserPrizeCount(); // userPrizeCount.setUserId(userId); // UserPrizeCountMapper.insert(userPrizeCount); map.put("prizeLevel", 0); map.put("prizeRecord", prizeRecord); map.put("prize", prize); msg.data = map; response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(msg.toJSONStr()); return; } // if (luckDrawCount <= 1) { // 直接抽奖(每日默认2次抽奖机会) count = getLuckDraw(prizeRecord, count); // 添加抽奖记录 UserPrizeCount userPrizeCount = new UserPrizeCount(); userPrizeCount.setUserId(userId); UserPrizeCountMapper.insert(userPrizeCount); map.put("prizeLevel", count); map.put("prizeRecord", prizeRecord); msg.data = map; // } else if (luckDrawCount == 2) { // int opportunityCount = // prizeOpportunityMapper.selectCountByToday(userId); // if (opportunityCount > 0) { // // 直接抽奖 // count = getLuckDraw(prizeRecord, count); // // 添加抽奖记录 // UserPrizeCount userPrizeCount = new UserPrizeCount(); // userPrizeCount.setUserId(userId); // UserPrizeCountMapper.insert(userPrizeCount); // map.put("prizeLevel", count); // map.put("prizeRecord", prizeRecord); // msg.data = map; // }else{ // msg.setError("抽奖次数已达上限,去分享即可获得抽奖机会"); // } // // } else { // msg.setError("抽奖次数已达上限"); // } response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(msg.toJSONStr()); } public synchronized Integer getLuckDraw(PrizeRecord prizeRecord, Integer count) { Random ran = new Random(); int num = ran.nextInt(9999); Prize prize = prizeMapper.selectByPrimaryKey(num); if (prize == null) { count = -1; } else { if (prize.getPrizeLevel() == 5 || prize.getIsSelected() == 1) { count = -1; } else { if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) { count = 0; } else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) { count = 1; } else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) { count = 2; } else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) { count = 3; } else if (prize.getPrizeLevel() == 4 && prize.getIsSelected() == 0) { count = 4; } // prize.setIsSelected(1); // prizeMapper.updateByPrimaryKeySelective(prize); prizeRecord.setPrizeId(prize.getPrizeLevel()); prizeRecord.setPrize(prize.getName()); prizeRecordMapper.insert(prizeRecord); } } return count; } /** * 小游戏抽奖列表 * * @return */ @RequestMapping(value = "/luckDrawList") public void luckDrawList(HttpServletResponse response) throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException { HttpRespMsg msg = new HttpRespMsg(); List list = prizeRecordMapper.selectByExample(new PrizeRecordExample()); msg.data = list; response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(msg.toJSONStr()); } /** * 小游戏抽奖列表后台查看 * * @return */ @RequestMapping(value = "/luckDrawListBack") public void luckDrawListBack(HttpServletResponse response) throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException { HttpRespMsg msg = new HttpRespMsg(); PrizeRecordExample example = new PrizeRecordExample(); example.createCriteria().andUidIsNotNull(); List list = prizeRecordMapper.selectByExample(example); msg.data = list; response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(msg.toJSONStr()); } /** * 小游戏抽奖填写用户信息 参数:phone:电话 username:姓名 id:中奖纪录id * * @return */ @RequestMapping(value = "/luckDrawAddInfo") public void luckDrawAddInfo(PrizeRecord prizeRecord, HttpServletResponse response) throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException { HttpRespMsg msg = new HttpRespMsg(); prizeRecordMapper.updateByPrimaryKeySelective(prizeRecord); msg.data = prizeRecord; response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(msg.toJSONStr()); } /** * 转发链接添加抽奖次数 参数: uid:用户id * * @return */ @RequestMapping(value = "/luckDrawAddShare") public void luckDrawAddCount(PrizeRecord prizeRecord, HttpServletResponse response) throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException { HttpRespMsg msg = new HttpRespMsg(); int count = prizeOpportunityMapper.selectCountByToday(prizeRecord.getUid()); if (count == 0) { PrizeOpportunity prizeOpportunity = new PrizeOpportunity(); prizeOpportunity.setIsShare(1); prizeOpportunity.setUid(prizeRecord.getUid()); prizeOpportunityMapper.insert(prizeOpportunity); } response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(msg.toJSONStr()); } /** * 查看个人获奖信息 * * 传递的参数: userId:用户id message:寄语 * * @return * @throws IOException */ @RequestMapping("/getluckDrawByUserId") public void getluckDrawByUserId(Integer UserId, HttpServletResponse response) throws IOException { HttpRespMsg msg = new HttpRespMsg(); PrizeRecordExample exp = new PrizeRecordExample(); exp.createCriteria().andUidEqualTo(UserId); List list = prizeRecordMapper.selectByExample(exp); msg.data = list; response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(msg.toJSONStr()); } }