package com.ks.daylucky.controller.api.client; import com.ks.daylucky.pojo.DO.UserInfo; import com.ks.daylucky.pojo.VO.AcceptData; import com.ks.daylucky.pojo.VO.SimpleUser; import com.ks.daylucky.pojo.VO.UserActivityUnRecievedAwardVO; import com.ks.daylucky.service.UserInfoService; import com.ks.daylucky.util.Constant; import com.ks.daylucky.util.UserInfoUtil; import com.ks.lucky.exception.LuckyActivityAwardException; import com.ks.lucky.exception.LuckyActivityAwardResultException; import com.ks.lucky.exception.LuckyActivityException; import com.ks.lucky.pojo.DO.LuckyActivityAwardResult; import com.ks.lucky.pojo.DO.LuckyActivityAwards; import com.ks.lucky.remote.service.LuckyActivityAwardResultService; import com.ks.lucky.remote.service.LuckyActivityAwardService; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.dubbo.config.annotation.Reference; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.yeshi.utils.JsonUtil; import org.yeshi.utils.TimeUtil; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @Controller @RequestMapping("api/client/activity/awards") public class ActivityAwardsController { @Reference(version = "1.0") private LuckyActivityAwardResultService luckyActivityAwardResultService; @Reference(version = "1.0") private LuckyActivityAwardService luckyActivityAwardService; @Resource private UserInfoService userInfoService; /** * 获取中奖信息 * * @param acceptData * @return */ @ResponseBody @RequestMapping("getDrawnInfo") public String getDrawnInfo(AcceptData acceptData, Long activityId, int count) { List list = luckyActivityAwardService.getAwardList(activityId); JSONArray array = new JSONArray(); for (LuckyActivityAwards award : list) { JSONObject item = new JSONObject(); List awardResults = luckyActivityAwardResultService.getResultListWithAwardInfo(activityId, award.getId(), 1, count); List userList = new ArrayList<>(); Map userMap = userInfoService.listUserInfoAsMap(UserInfoUtil.getBaseUserList(awardResults)); for (LuckyActivityAwardResult info : awardResults) { UserInfo user = userMap.get(info.getUid()); if (user != null) { userList.add(new SimpleUser(user.getId(), user.getNickName(), user.getPortrait())); } } item.put("userList", userList); item.put("award", JsonUtil.getApiCommonGson().toJson(award)); array.add(item); } JSONObject data = new JSONObject(); data.put("data", array); Date updateTime = luckyActivityAwardResultService.getLatestUpdateTime(activityId); if (updateTime != null) { data.put("updateTime", TimeUtil.getGernalTime(updateTime.getTime(), "yyyy.MMM.dd HH:mm")); } return JsonUtil.loadTrueResult(data); } /** * 获取活动的某个奖项的中奖列表 * * @param acceptData * @param activityId * @param awardId * @param page * @return */ @ResponseBody @RequestMapping("getDrawnJoinerList") public String getDrawnJoinerList(AcceptData acceptData, Long activityId, Long awardId, int page) { List awardResults = luckyActivityAwardResultService.getResultListWithAwardInfo(activityId, awardId, page, Constant.PAGE_SIZE); long count = luckyActivityAwardResultService.countResultWithAwardInfo(activityId, awardId); List userList = new ArrayList<>(); Map userMap = userInfoService.listUserInfoAsMap(UserInfoUtil.getBaseUserList(awardResults)); for (LuckyActivityAwardResult info : awardResults) { UserInfo user = userMap.get(info.getUid()); if (user != null) { userList.add(new SimpleUser(user.getId(), user.getNickName(), user.getPortrait())); } } JSONObject data = new JSONObject(); data.put("data", userList); data.put("count", count); return JsonUtil.loadTrueResult(data); } /** * 获取未领奖的奖励信息 * * @param acceptData * @return */ @ResponseBody @RequestMapping("getUnRecieveAward") public String getUnRecieveAward(AcceptData acceptData) { List resultList = luckyActivityAwardResultService.getUnRecievedAward(acceptData.getApp().getId(), acceptData.getUid() + "", 1, 1); if (resultList != null && resultList.size() > 0) { LuckyActivityAwardResult result = resultList.get(0); UserActivityUnRecievedAwardVO vo = new UserActivityUnRecievedAwardVO(); LuckyActivityAwards award = luckyActivityAwardService.getAwardDetail(result.getAwardId()); vo.setAwardName(award.getAwardName()); vo.setAwardId(award.getId()); vo.setPicture(award.getAwardPoster()); vo.setTitle("活动期号:NO." + award.getActivityId()); return JsonUtil.loadTrueResult(vo); } return JsonUtil.loadFalseResult(""); } /** * 领取奖品 * * @param acceptData * @param activityId * @return */ @ResponseBody @RequestMapping("recieveAward") public String recieveAward(AcceptData acceptData, Long activityId) { LuckyActivityAwardResult result = luckyActivityAwardResultService.getResult(acceptData.getApp().getId(), acceptData.getUid() + "", activityId); if (result == null) { return JsonUtil.loadFalseResult("未找到中奖记录"); } try { luckyActivityAwardResultService.recieveAward(acceptData.getApp().getId(), acceptData.getUid() + "", activityId); return JsonUtil.loadTrueResult("领取成功"); } catch (LuckyActivityException e) { e.printStackTrace(); } catch (LuckyActivityAwardException e) { e.printStackTrace(); } catch (LuckyActivityAwardResultException e) { e.printStackTrace(); } return JsonUtil.loadFalseResult("领取失败"); } }