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.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.PrizeRecord; import com.hssx.entity.PrizeRecordExample; import com.hssx.entity.User; import com.hssx.entity.UserPrizeCountExample; import com.hssx.mapper.PrizeMapper; 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; /** * 小游戏抽奖 参数: userId:当前抽奖人id * * @return */ @RequestMapping(value = "/luckDraw") public void luckDraw(Integer userId, HttpServletResponse response) throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException { HttpRespMsg msg = new HttpRespMsg(); Integer count = -1; PrizeRecord prizeRecord = new PrizeRecord(); User user = usermapper.selectByPrimaryKey(userId); prizeRecord.setUid(user.getId()); prizeRecord.setType(user.getType()); prizeRecord.setVoucherId(user.getVoucherId()); UserPrizeCountExample uExp = new UserPrizeCountExample(); Integer luckDrawCount = UserPrizeCountMapper.selectCountByToday(); if (luckDrawCount < 2) { synchronized (this) { Random ran = new Random(); int num = ran.nextInt(9999); Prize prize = prizeMapper.selectByPrimaryKey(num); if (prize == null) { count = -1; } else { if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) { count = 0; // prize.setIsSelected(1); // prizeMapper.updateByPrimaryKeySelective(prize); prizeRecord.setPrizeId(prize.getPrizeLevel()); prizeRecord.setPrize(prize.getName()); prizeRecordMapper.insert(prizeRecord); } else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) { count = 1; // prize.setIsSelected(1); // prizeMapper.updateByPrimaryKeySelective(prize); prizeRecord.setPrizeId(prize.getPrizeLevel()); prizeRecord.setPrize(prize.getName()); prizeRecordMapper.insert(prizeRecord); } else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) { count = 2; // prize.setIsSelected(1); // prizeMapper.updateByPrimaryKeySelective(prize); prizeRecord.setPrizeId(prize.getPrizeLevel()); prizeRecord.setPrize(prize.getName()); prizeRecordMapper.insert(prizeRecord); } else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) { count = 3; // prize.setIsSelected(1); // prizeMapper.updateByPrimaryKeySelective(prize); prizeRecord.setPrizeId(prize.getPrizeLevel()); prizeRecord.setPrize(prize.getName()); prizeRecordMapper.insert(prizeRecord); } 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); } else { count = -1; } } } HashMap map = new HashMap(); map.put("prizeLevel", count); map.put("prizeRecord", prizeRecord); msg.data = map; }else{ } response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().println(msg.toJSONStr()); } /** * 小游戏抽奖列表 * * @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()); } /** * 小游戏抽奖填写用户信息 参数: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()); } }