修改产品奖项

1 个父辈 a7a7b264
package com.w1hd.zzhnc.controller.pc;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
......@@ -11,8 +14,10 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.beust.jcommander.internal.Maps;
import com.w1hd.zzhnc.dao.LotteryLogDao;
import com.w1hd.zzhnc.model.Activity;
import com.w1hd.zzhnc.model.Prize;
import com.w1hd.zzhnc.service.ActivityService;
import com.w1hd.zzhnc.vo.Vo_msg;
......@@ -39,10 +44,33 @@ public class ActivityController extends BaseController {
}
// 修改活动配置
@RequestMapping(value = "/editActivityPrize/{type}", method = RequestMethod.POST)
@ResponseBody
public Object editActivityPrize(@PathVariable(value = "type") Integer type, @RequestBody Prize prize) {
try {
activityService.updateActivityPrize(type, prize);
} catch (Exception e) {
e.printStackTrace();
return new Vo_msg(-1, null, "error");
}
return new Vo_msg(0, null, "success");
}
// 修改活动配置
@RequestMapping(value = "/editActivitySetting", method = RequestMethod.POST)
@ResponseBody
public Object editActivitySetting(@RequestBody Activity activity) {
try {
Activity setting = activityService.getActivitySetting();
activity.setP1(setting.getP1());
activity.setP2(setting.getP2());
activity.setP3(setting.getP3());
activity.setP4(setting.getP4());
activity.setP5(setting.getP5());
activity.setP6(setting.getP6());
activity.setP7(setting.getP7());
activity.setP8(setting.getP8());
activityService.updateActivity(activity);
} catch (Exception e) {
e.printStackTrace();
......@@ -70,16 +98,19 @@ public class ActivityController extends BaseController {
public Object getActivitySetting() {
Activity activity = activityService.getActivitySetting();
if (null == activity) {
return new Vo_msg(-1, "获取失败,数据为空");
}
BigDecimal currentMny = lotteryLogDao.getSumMny(activity.getTurn(), 1);
activityService.updateCurrentMny(currentMny);
activity = activityService.getActivitySetting();
return new Vo_msg(0, activity);
activity.setCurrentMny(currentMny);
activityService.updateActivity(activity);
List<Prize> prizeList = activityService.getPrizeList();
Map<String,Object> result = Maps.newHashMap();
result.put("activity", activity);
result.put("prizeList", prizeList);
return new Vo_msg(0, result);
}
// 设置活动下一轮
......@@ -97,8 +128,7 @@ public class ActivityController extends BaseController {
// 中奖记录
@RequestMapping(value = "/lotterys", method = { RequestMethod.POST, RequestMethod.GET })
@ResponseBody
public Object lotterys(
@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
public Object lotterys(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
@RequestParam(value = "pagesize", defaultValue = "10", required = false) Integer pagesize,
@RequestParam(value = "turn", defaultValue = "1", required = false) Integer turn,
@RequestParam(value = "keyword", defaultValue = "", required = false) String keyword,
......
......@@ -25,8 +25,7 @@ import org.theyeasy.weixin.util.WxMiniUtil;
import com.beust.jcommander.internal.Maps;
import com.w1hd.zzhnc.controller.pc.BaseController;
import com.w1hd.zzhnc.dao.FansDao;
import com.w1hd.zzhnc.dao.LotteryLogDao;
import com.w1hd.zzhnc.model.Activity;
import com.w1hd.zzhnc.model.Articles;
import com.w1hd.zzhnc.model.Banner;
import com.w1hd.zzhnc.model.Fans;
......@@ -40,7 +39,6 @@ import com.w1hd.zzhnc.service.GoodsService;
import com.w1hd.zzhnc.service.PrizeService;
import com.w1hd.zzhnc.util.PageResults;
import com.w1hd.zzhnc.util.RedisUtil;
import com.w1hd.zzhnc.vo.MiniHomeVO;
import com.w1hd.zzhnc.vo.Vo_msg;
@Controller
......@@ -61,7 +59,7 @@ public class WxMiniController extends BaseController {
@Autowired
PrizeService prizeService;
@RequestMapping(value = "/wxmsg", method = RequestMethod.GET)
@RequestMapping(value = "/wxmsg")
public void WxMsg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
......@@ -158,7 +156,7 @@ public class WxMiniController extends BaseController {
@ResponseBody
public Object getArticlesList(Integer page, Integer pagesize) {
return new Vo_msg(0, articleService.getArticlesList(page, pagesize, "",null));
return new Vo_msg(0, articleService.getArticlesList(page, pagesize, "", null));
}
// 增加文章浏览数
......@@ -294,24 +292,50 @@ public class WxMiniController extends BaseController {
}
}
/** 查看我的奖品 */
@RequestMapping(value = "/prizes", method = RequestMethod.GET)
@ResponseBody
public Object prizes(@RequestParam(value = "openId") String openId) {
Fans fans = fansService.getFansByMiniOpenid(openId);
if(fans!=null ) {
if (fans != null) {
try {
List<Lotterylog> list = prizeService.getMyLotteryLog(fans.getId());
return new Vo_msg(0, list);
} catch (Exception e) {
e.printStackTrace();
}
}
return new Vo_msg(0, null);
return new Vo_msg(0, null);
}
/** 中奖 */
@RequestMapping(value = "/prizes/kill", method = RequestMethod.GET)
@ResponseBody
public Object prizesKill(@RequestParam(value = "openId") String openId) {
Fans fans = fansService.getFansByMiniOpenid(openId);
Integer logId = prizeService.randomKill(fans.getId());
if(logId == 0) {
return new Vo_msg(-1, null,"不好意思你没中奖");
}
if(logId ==-1) {
return new Vo_msg(-1, null,"你已经中过奖了");
}
Lotterylog log = prizeService.getLog(logId);
return new Vo_msg(0, log);
}
/** 获取奖品列表 */
@RequestMapping(value = "/prizes/all", method = RequestMethod.GET)
@ResponseBody
public Object prizesList(@RequestParam(value = "openId") String openId) {
Activity setting = activityService.getActivitySetting();
return setting;
}
/** 核销奖品 */
@RequestMapping(value = "/prizes/{id}", method = RequestMethod.GET)
@ResponseBody
public Object prizes(@RequestParam(value = "openId") String openId, @PathVariable(value = "id") Integer id) {
......
......@@ -47,8 +47,10 @@ public class Activity implements Serializable {
private Prize p2;
private Prize p3;
private Prize p4;
private Prize p5;
private Prize p6;
private Prize p7;
private Prize p8;
public Activity() {
super();
......@@ -261,4 +263,37 @@ public class Activity implements Serializable {
this.badWordsReply = badWordsReply;
}
public Prize getP5() {
return p5;
}
public void setP5(Prize p5) {
this.p5 = p5;
}
public Prize getP6() {
return p6;
}
public void setP6(Prize p6) {
this.p6 = p6;
}
public Prize getP7() {
return p7;
}
public void setP7(Prize p7) {
this.p7 = p7;
}
public Prize getP8() {
return p8;
}
public void setP8(Prize p8) {
this.p8 = p8;
}
}
\ No newline at end of file
package com.w1hd.zzhnc.model;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Id;
public class Lotterylog {
public class Lotterylog implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
private Integer id;
private Integer fansid;
......
......@@ -7,6 +7,7 @@ import java.util.List;
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;
......@@ -30,7 +31,7 @@ import com.w1hd.zzhnc.vo.Lotterylog_Vo;
@Service
public class ActivityService {
final String ACTIVITY_SETTING =RedisUtil.PROJECTNAME.concat("_").concat("ACTIVITY_SETTING");// 娲诲姩璁剧疆淇濆瓨key
final String ACTIVITY_SETTING = RedisUtil.PROJECTNAME.concat("_").concat("ACTIVITY_SETTING");// 娲诲姩璁剧疆淇濆瓨key
@Autowired
LotteryLogDao logDao;
......@@ -42,9 +43,10 @@ public class ActivityService {
Activity redisactivity = (Activity) redisTemplate.opsForValue().get(ACTIVITY_SETTING);
if (redisactivity == null) {
redisactivity = new Activity(new Date(2017, 12, 27), new Date(2018, 2, 1), 1, new BigDecimal(500), "zhenzhou",
"keyworks", "you'r lose", "replay String of activity finished ", "input title pls", "input subtitle pls", 0, "nochance replay", "waitting...", "welcome", "fail",
"success", "undefind");
redisactivity = new Activity(new Date(2017, 12, 27), new Date(2018, 2, 1), 1, new BigDecimal(500),
"zhenzhou", "keyworks", "you'r lose", "replay String of activity finished ", "input title pls",
"input subtitle pls", 0, "nochance replay", "waitting...", "welcome", "fail", "success",
"undefind");
Prize prize = new Prize();
prize.setIsMoney(true);
prize.setMaxMoney(200);
......@@ -77,7 +79,7 @@ public class ActivityService {
public String clearActivity() {
Activity activity = getActivitySetting();
activity.setTurn(activity.getTurn()+1);
activity.setTurn(activity.getTurn() + 1);
Prize prize = new Prize();
activity.setP1(prize);
activity.setP2(prize);
......@@ -89,7 +91,7 @@ public class ActivityService {
return "ok";
}
public String addLotteryLog(Integer fansid, BigDecimal mny, Integer status, Integer turn,String name) {
public String addLotteryLog(Integer fansid, BigDecimal mny, Integer status, Integer turn, String name) {
Lotterylog log = new Lotterylog();
log.setCreatedtime(CommonUtil.getTime());
log.setFansid(fansid);
......@@ -108,8 +110,7 @@ public class ActivityService {
int total = logDao.getLotteryLogCount(turn, status);
BigDecimal bMny = logDao.getSumMny(turn, status);
int mny = bMny == null ? 0 : bMny.intValue();
pageResults.setPage(page);
pageResults.setPageSize(pagesize);
pageResults.setRows(list);
......@@ -119,26 +120,62 @@ public class ActivityService {
}
public void updateCurrentMny(BigDecimal currentMny) {
try {
String activityjson = RedisUtil.get(ACTIVITY_SETTING);
if (Strings.isNullOrEmpty(activityjson))
return;
activityjson = CommonUtil.JsonToObject(activityjson);
Activity redisactivity = CommonUtil.getEntity(activityjson, Activity.class);
redisactivity.setCurrentMny(currentMny);
String json = CommonUtil.getJson(redisactivity);
System.out.println("activity json:" + json.toString());
RedisUtil.set(ACTIVITY_SETTING, json.toString());
} catch (Exception e) {
e.printStackTrace();
}
Activity activity = getActivitySetting();
activity.setCurrentMny(currentMny);
updateActivity(activity);
}
public void updateActivity(Activity activity) {
redisTemplate.opsForValue().set(ACTIVITY_SETTING, activity);
}
public void updateActivityPrize(Integer type, Prize prize) {
Activity setting = getActivitySetting();
switch (type) {
case 1:
setting.setP1(prize);
break;
case 2:
setting.setP2(prize);
break;
case 3:
setting.setP3(prize);
break;
case 4:
setting.setP4(prize);
break;
case 5:
setting.setP5(prize);
break;
case 6:
setting.setP6(prize);
break;
case 7:
setting.setP7(prize);
break;
case 8:
setting.setP8(prize);
break;
default:
break;
}
updateActivity(setting);
}
public List<Prize> getPrizeList() {
Activity setting = getActivitySetting();
List<Prize> newArrayList = Lists.newArrayList();
newArrayList.add(setting.getP1());
newArrayList.add(setting.getP2());
newArrayList.add(setting.getP3());
newArrayList.add(setting.getP4());
newArrayList.add(setting.getP5());
newArrayList.add(setting.getP6());
newArrayList.add(setting.getP7());
newArrayList.add(setting.getP8());
return newArrayList;
}
}
package com.w1hd.zzhnc.service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
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.Goods;
import com.w1hd.zzhnc.model.Activity;
import com.w1hd.zzhnc.model.Lotterylog;
import com.w1hd.zzhnc.model.Prize;
import com.w1hd.zzhnc.util.PageResults;
import com.w1hd.zzhnc.util.RedisUtil;
import com.w1hd.zzhnc.vo.Lotterylog_Vo;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
......@@ -22,14 +27,19 @@ import tk.mybatis.mapper.entity.Example.Criteria;
@Service
public class PrizeService {
final String LotteryLogID_ = RedisUtil.PROJECTNAME.concat("_").concat("lotteryLog_");//中奖记里
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;
}
......@@ -60,20 +70,93 @@ public class PrizeService {
public boolean delete(String id) {
return prizeDao.deleteByPrimaryKey(id) > 0;
}
/**获取我的奖品*/
public List<Lotterylog> getMyLotteryLog(Integer fansId){
/**
* 增加一条奖项记录
*
* @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;
}
activityService.updateActivity(setting);
Lotterylog lotterylog = new Lotterylog();
lotterylog.setCreatedtime(new Date());
lotterylog.setFansid(fansId);
lotterylog.setMny(mny);
lotterylog.setPrizeName(p.getName());
if("谢谢".equals(p.getName())) {
lotterylog.setStatus(3);
}else {
lotterylog.setStatus(1);
}
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);
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);
......@@ -81,4 +164,88 @@ public class PrizeService {
return lotterylog;
}
@Autowired
RedisTemplate redisTemplate;
@SuppressWarnings("unchecked")
public Integer randomKill(Integer fansId) {
Integer lotteryLog = (Integer) redisTemplate.opsForValue().get(PRIZE_KILL_FANSID + fansId);
if (lotteryLog == null) {
Activity activity = activityService.getActivitySetting();
int nextInt = RandomUtils.nextInt(0, 100);
System.out.println("幸运数字 ---》》》" + nextInt);
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();
nextInt = 51;
int mny = 0;
Integer logId = 0;
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 {
return -0;
}
redisTemplate.opsForValue().set(PRIZE_KILL_FANSID + fansId, logId);
return logId;
}
return -1;
}
public Lotterylog getLog(Integer logId) {
return (Lotterylog) redisTemplate.opsForValue().get(LotteryLogID_ + logId);
}
}
Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
请先完成此消息的编辑!