From 38d15997c3c19dea858dbf8ac25bb7509649e139 Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Sat, 30 May 2020 01:52:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=B4=BB=E5=8A=A8=E5=A5=96?= =?UTF-8?q?=E5=8A=B1=E6=97=B6=E9=97=B4=E5=B8=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../help/controller/ActivityController.java | 11 +++++++- .../share/help/dao/ActivityHistoryMapper.java | 8 +++--- .../share/help/service/ActivityService.java | 26 ++++++++++++++----- .../mapping/ActivityHistoryMapper.xml | 14 ++++++++++ src/main/resources/mapping/ActivityMapper.xml | 2 +- src/main/resources/mapping/UserMapper.xml | 5 +++- .../com/share/help/HelpApplicationTests.java | 17 +++++++++++- 7 files changed, 69 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/share/help/controller/ActivityController.java b/src/main/java/com/share/help/controller/ActivityController.java index 4b03d4b..919b862 100644 --- a/src/main/java/com/share/help/controller/ActivityController.java +++ b/src/main/java/com/share/help/controller/ActivityController.java @@ -11,6 +11,8 @@ import com.share.help.res.*; import com.share.help.service.ActivityService; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -23,6 +25,7 @@ public class ActivityController { @Autowired private ActivityService activityService; + private Logger logger= LoggerFactory.getLogger(ActivityController.class); /** * 发布求助信息 @@ -77,7 +80,13 @@ public class ActivityController { public JSONResponse apply(UpdateActivityForm updateActivityForm,@CookieValue(Constants.USER_COOKIE) String userId){ if(StringUtils.isNotEmpty(userId)&&ObjectUtils.allNotNull(updateActivityForm.getActivityId(),updateActivityForm.getActivityStatus())){ updateActivityForm.setUserId(userId); - return activityService.apply(updateActivityForm); + try { + return activityService.apply(updateActivityForm); + } catch (RuntimeException e) { + e.printStackTrace(); + logger.error(e.toString()); + return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + } }else{ return new JSONResponse<>(DefaultRes.fail,Result.FAIL); } diff --git a/src/main/java/com/share/help/dao/ActivityHistoryMapper.java b/src/main/java/com/share/help/dao/ActivityHistoryMapper.java index 17e8048..e12f432 100644 --- a/src/main/java/com/share/help/dao/ActivityHistoryMapper.java +++ b/src/main/java/com/share/help/dao/ActivityHistoryMapper.java @@ -4,7 +4,6 @@ import com.share.help.entity.ActivityHistoryEntity; import com.share.help.entity.custorm.HelpRes; import com.share.help.entity.custorm.UserWithName; import com.share.help.res.Page; -import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Update; @@ -20,10 +19,10 @@ public interface ActivityHistoryMapper { * @param activityHistoryEntity 活动历史实体 * @return 返回添加结果 */ - @Insert("insert into activity_history(activity_id,user_id,activity_status,create_time,activity_score,time_score)value" + - "(#{activityId},#{userId},#{activityStatus},#{createTime},#{activityScore},#{timeScore})") boolean insert(ActivityHistoryEntity activityHistoryEntity); + + /** * 获取志愿者列表 * @param activityStatus 活动状态 @@ -41,4 +40,7 @@ public interface ActivityHistoryMapper { //活动评价 @Update("update activity_history set activity_score=#{score} where history_id=#{historyId}") boolean updateScore(@Param("historyId") Long historyId,@Param("score") Integer score); + + //获取时间币 + Integer getTimeScore(@Param("activityId") Long activityId,@Param("userId") String userId); } diff --git a/src/main/java/com/share/help/service/ActivityService.java b/src/main/java/com/share/help/service/ActivityService.java index 80e0c4d..dc11122 100644 --- a/src/main/java/com/share/help/service/ActivityService.java +++ b/src/main/java/com/share/help/service/ActivityService.java @@ -4,9 +4,11 @@ import com.share.help.Util; import com.share.help.dao.ActivityHistoryMapper; import com.share.help.dao.ActivityMapper; import com.share.help.dao.LeaveWordMapper; +import com.share.help.dao.UserMapper; import com.share.help.entity.ActivityEntity; import com.share.help.entity.ActivityHistoryEntity; import com.share.help.entity.LeaveWordEntity; +import com.share.help.entity.UserEntity; import com.share.help.entity.custorm.*; import com.share.help.form.*; import com.share.help.res.*; @@ -48,6 +50,8 @@ public class ActivityService{ @Value("${page-size.help}") private int helpPageSize; + @Autowired + private UserMapper userMapper; private Logger logger = LoggerFactory.getLogger(ActivityService.class); @@ -113,16 +117,26 @@ public class ActivityService{ * @return 返回报名结果 */ @Transactional - public JSONResponse apply(UpdateActivityForm activityForm){ + public JSONResponse apply(UpdateActivityForm activityForm) throws RuntimeException{ // 保存到活动历史记录 ActivityHistoryEntity activityEntity=new ActivityHistoryEntity(); activityEntity.setActivityId(activityForm.getActivityId()); activityEntity.setUserId(activityForm.getUserId()); activityEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); activityEntity.setActivityStatus(activityForm.getActivityStatus().name()); + //完成活动结算时间币 + if(ActivityStatus.complete==activityForm.getActivityStatus()){ + int timeScore=activityHistoryMapper.getTimeScore(activityForm.getActivityId(),activityForm.getUserId()); + logger.info("奖励时间币:"+timeScore); + activityEntity.setTimeScore(timeScore); + UserEntity userEntity=userMapper.findOne(activityEntity.getUserId()); + userEntity.setTimeScore(userEntity.getTimeScore()+timeScore); + if(!userMapper.update(userEntity)){ + throw new RuntimeException("结算时间币失败"); + } + } if(!activityHistoryMapper.insert(activityEntity)){ - logger.error("保存活动历史记录失败"); - return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + throw new RuntimeException("保存活动历史记录失败"); } // 给求助用户发送留言 @@ -131,15 +145,13 @@ public class ActivityService{ leaveWordEntity.setSourceUserId(activityForm.getUserId()); ActivityEntity activity=activityMapper.findOne(activityForm.getActivityId()); if(activity==null){ - logger.error("查询活动记录失败"); - return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + throw new RuntimeException("查询活动记录失败"); } leaveWordEntity.setTargetUserId(activity.getUserId()); leaveWordEntity.setActivityId(activityForm.getActivityId()); leaveWordEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); if(!leaveWordMapper.insert(leaveWordEntity)){ - logger.error("保存留言信息失败"); - return new JSONResponse<>(DefaultRes.fail,Result.FAIL); + throw new RuntimeException("保存留言信息失败"); } return new JSONResponse<>(DefaultRes.ok,Result.OK); } diff --git a/src/main/resources/mapping/ActivityHistoryMapper.xml b/src/main/resources/mapping/ActivityHistoryMapper.xml index b5d19b1..b66c464 100644 --- a/src/main/resources/mapping/ActivityHistoryMapper.xml +++ b/src/main/resources/mapping/ActivityHistoryMapper.xml @@ -43,4 +43,18 @@ activity,activity_history where activity_history.user_id=#{userId} and activity.activity_id=activity_history.activity_id + + + insert into activity_history(activity_id,user_id,activity_status,create_time,activity_score,time_score)value + (#{activityId},#{userId},#{activityStatus},#{createTime},#{activityScore},#{timeScore}) + + + + \ No newline at end of file diff --git a/src/main/resources/mapping/ActivityMapper.xml b/src/main/resources/mapping/ActivityMapper.xml index a9a93f2..9e275b4 100644 --- a/src/main/resources/mapping/ActivityMapper.xml +++ b/src/main/resources/mapping/ActivityMapper.xml @@ -28,7 +28,7 @@ diff --git a/src/main/resources/mapping/UserMapper.xml b/src/main/resources/mapping/UserMapper.xml index d241209..9b22d6d 100644 --- a/src/main/resources/mapping/UserMapper.xml +++ b/src/main/resources/mapping/UserMapper.xml @@ -105,7 +105,10 @@ password=#{password}, - chat_status=#{chatStatus} + chat_status=#{chatStatus}, + + + time_score=#{timeScore} where user_id=#{userId} diff --git a/src/test/java/com/share/help/HelpApplicationTests.java b/src/test/java/com/share/help/HelpApplicationTests.java index 67555d1..13ad264 100644 --- a/src/test/java/com/share/help/HelpApplicationTests.java +++ b/src/test/java/com/share/help/HelpApplicationTests.java @@ -2,6 +2,7 @@ package com.share.help; import com.share.help.dao.*; import com.share.help.entity.ActivityEntity; +import com.share.help.entity.ActivityHistoryEntity; import com.share.help.entity.UserEntity; import com.share.help.entity.custorm.*; import com.share.help.form.ActivityStatus; @@ -250,5 +251,19 @@ class HelpApplicationTests { mvc.perform(builder).andDo(MockMvcResultHandlers.print()); } - + /** + * 测试活动记录 + */ + @Test + public void testAddHistory(){ + ActivityHistoryEntity activityHistoryEntity=new ActivityHistoryEntity(); + activityHistoryEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); + activityHistoryEntity.setActivityStatus(ActivityStatus.complete.name()); + activityHistoryEntity.setActivityId(57L); + activityHistoryEntity.setUserId("123"); + Integer score=activityHistoryMapper.getTimeScore(57L,"123"); + activityHistoryEntity.setTimeScore(score); + + Assertions.assertTrue(activityHistoryMapper.insert(activityHistoryEntity)); + } }