完成活动奖励时间币

master
pan 5 years ago
parent bea12f27d8
commit 38d15997c3
  1. 9
      src/main/java/com/share/help/controller/ActivityController.java
  2. 8
      src/main/java/com/share/help/dao/ActivityHistoryMapper.java
  3. 26
      src/main/java/com/share/help/service/ActivityService.java
  4. 14
      src/main/resources/mapping/ActivityHistoryMapper.xml
  5. 2
      src/main/resources/mapping/ActivityMapper.xml
  6. 5
      src/main/resources/mapping/UserMapper.xml
  7. 17
      src/test/java/com/share/help/HelpApplicationTests.java

@ -11,6 +11,8 @@ import com.share.help.res.*;
import com.share.help.service.ActivityService; import com.share.help.service.ActivityService;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -23,6 +25,7 @@ public class ActivityController {
@Autowired @Autowired
private ActivityService activityService; private ActivityService activityService;
private Logger logger= LoggerFactory.getLogger(ActivityController.class);
/** /**
* 发布求助信息 * 发布求助信息
@ -77,7 +80,13 @@ public class ActivityController {
public JSONResponse<DefaultRes,Void> apply(UpdateActivityForm updateActivityForm,@CookieValue(Constants.USER_COOKIE) String userId){ public JSONResponse<DefaultRes,Void> apply(UpdateActivityForm updateActivityForm,@CookieValue(Constants.USER_COOKIE) String userId){
if(StringUtils.isNotEmpty(userId)&&ObjectUtils.allNotNull(updateActivityForm.getActivityId(),updateActivityForm.getActivityStatus())){ if(StringUtils.isNotEmpty(userId)&&ObjectUtils.allNotNull(updateActivityForm.getActivityId(),updateActivityForm.getActivityStatus())){
updateActivityForm.setUserId(userId); updateActivityForm.setUserId(userId);
try {
return activityService.apply(updateActivityForm); return activityService.apply(updateActivityForm);
} catch (RuntimeException e) {
e.printStackTrace();
logger.error(e.toString());
return new JSONResponse<>(DefaultRes.fail,Result.FAIL);
}
}else{ }else{
return new JSONResponse<>(DefaultRes.fail,Result.FAIL); return new JSONResponse<>(DefaultRes.fail,Result.FAIL);
} }

@ -4,7 +4,6 @@ import com.share.help.entity.ActivityHistoryEntity;
import com.share.help.entity.custorm.HelpRes; import com.share.help.entity.custorm.HelpRes;
import com.share.help.entity.custorm.UserWithName; import com.share.help.entity.custorm.UserWithName;
import com.share.help.res.Page; import com.share.help.res.Page;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update; import org.apache.ibatis.annotations.Update;
@ -20,10 +19,10 @@ public interface ActivityHistoryMapper {
* @param activityHistoryEntity 活动历史实体 * @param activityHistoryEntity 活动历史实体
* @return 返回添加结果 * @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); boolean insert(ActivityHistoryEntity activityHistoryEntity);
/** /**
* 获取志愿者列表 * 获取志愿者列表
* @param activityStatus 活动状态 * @param activityStatus 活动状态
@ -41,4 +40,7 @@ public interface ActivityHistoryMapper {
//活动评价 //活动评价
@Update("update activity_history set activity_score=#{score} where history_id=#{historyId}") @Update("update activity_history set activity_score=#{score} where history_id=#{historyId}")
boolean updateScore(@Param("historyId") Long historyId,@Param("score") Integer score); boolean updateScore(@Param("historyId") Long historyId,@Param("score") Integer score);
//获取时间币
Integer getTimeScore(@Param("activityId") Long activityId,@Param("userId") String userId);
} }

@ -4,9 +4,11 @@ import com.share.help.Util;
import com.share.help.dao.ActivityHistoryMapper; import com.share.help.dao.ActivityHistoryMapper;
import com.share.help.dao.ActivityMapper; import com.share.help.dao.ActivityMapper;
import com.share.help.dao.LeaveWordMapper; import com.share.help.dao.LeaveWordMapper;
import com.share.help.dao.UserMapper;
import com.share.help.entity.ActivityEntity; import com.share.help.entity.ActivityEntity;
import com.share.help.entity.ActivityHistoryEntity; import com.share.help.entity.ActivityHistoryEntity;
import com.share.help.entity.LeaveWordEntity; import com.share.help.entity.LeaveWordEntity;
import com.share.help.entity.UserEntity;
import com.share.help.entity.custorm.*; import com.share.help.entity.custorm.*;
import com.share.help.form.*; import com.share.help.form.*;
import com.share.help.res.*; import com.share.help.res.*;
@ -48,6 +50,8 @@ public class ActivityService{
@Value("${page-size.help}") @Value("${page-size.help}")
private int helpPageSize; private int helpPageSize;
@Autowired
private UserMapper userMapper;
private Logger logger = LoggerFactory.getLogger(ActivityService.class); private Logger logger = LoggerFactory.getLogger(ActivityService.class);
@ -113,16 +117,26 @@ public class ActivityService{
* @return 返回报名结果 * @return 返回报名结果
*/ */
@Transactional @Transactional
public JSONResponse<DefaultRes,Void> apply(UpdateActivityForm activityForm){ public JSONResponse<DefaultRes,Void> apply(UpdateActivityForm activityForm) throws RuntimeException{
// 保存到活动历史记录 // 保存到活动历史记录
ActivityHistoryEntity activityEntity=new ActivityHistoryEntity(); ActivityHistoryEntity activityEntity=new ActivityHistoryEntity();
activityEntity.setActivityId(activityForm.getActivityId()); activityEntity.setActivityId(activityForm.getActivityId());
activityEntity.setUserId(activityForm.getUserId()); activityEntity.setUserId(activityForm.getUserId());
activityEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); activityEntity.setCreateTime(new Timestamp(System.currentTimeMillis()));
activityEntity.setActivityStatus(activityForm.getActivityStatus().name()); 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)){ if(!activityHistoryMapper.insert(activityEntity)){
logger.error("保存活动历史记录失败"); throw new RuntimeException("保存活动历史记录失败");
return new JSONResponse<>(DefaultRes.fail,Result.FAIL);
} }
// 给求助用户发送留言 // 给求助用户发送留言
@ -131,15 +145,13 @@ public class ActivityService{
leaveWordEntity.setSourceUserId(activityForm.getUserId()); leaveWordEntity.setSourceUserId(activityForm.getUserId());
ActivityEntity activity=activityMapper.findOne(activityForm.getActivityId()); ActivityEntity activity=activityMapper.findOne(activityForm.getActivityId());
if(activity==null){ if(activity==null){
logger.error("查询活动记录失败"); throw new RuntimeException("查询活动记录失败");
return new JSONResponse<>(DefaultRes.fail,Result.FAIL);
} }
leaveWordEntity.setTargetUserId(activity.getUserId()); leaveWordEntity.setTargetUserId(activity.getUserId());
leaveWordEntity.setActivityId(activityForm.getActivityId()); leaveWordEntity.setActivityId(activityForm.getActivityId());
leaveWordEntity.setCreateTime(new Timestamp(System.currentTimeMillis())); leaveWordEntity.setCreateTime(new Timestamp(System.currentTimeMillis()));
if(!leaveWordMapper.insert(leaveWordEntity)){ if(!leaveWordMapper.insert(leaveWordEntity)){
logger.error("保存留言信息失败"); throw new RuntimeException("保存留言信息失败");
return new JSONResponse<>(DefaultRes.fail,Result.FAIL);
} }
return new JSONResponse<>(DefaultRes.ok,Result.OK); return new JSONResponse<>(DefaultRes.ok,Result.OK);
} }

@ -43,4 +43,18 @@
activity,activity_history where activity_history.user_id=#{userId} and activity.activity_id=activity_history.activity_id activity,activity_history where activity_history.user_id=#{userId} and activity.activity_id=activity_history.activity_id
</select> </select>
<!-- 添加活动历史记录-->
<insert id="insert">
insert into activity_history(activity_id,user_id,activity_status,create_time,activity_score,time_score)value
(#{activityId},#{userId},#{activityStatus},#{createTime},#{activityScore},#{timeScore})
</insert>
<!-- 计算时间币-->
<select id="getTimeScore" resultType="int">
select TIMESTAMPDIFF(HOUR, create_time, now()) as time_score
from activity_history
where activity_id = #{activityId}
and user_id = #{userId}
and activity_status = 'join'
</select>
</mapper> </mapper>

@ -28,7 +28,7 @@
<!-- 获取活动列表--> <!-- 获取活动列表-->
<select id="getPage" resultMap="activitySimpleMap"> <select id="getPage" resultMap="activitySimpleMap">
select activity_id,user_id,title,content,activity_img,(activity_id in(select distinct activity_id from activity_history select activity_id,user_id,title,content,activity_img,(activity_id in(select distinct activity_id from activity_history
where activity_history.user_id=#{userId})) as hasApply,now()>activity_start_time as hasStart from activity where activity_history.user_id=#{userId})) as hasApply,now()>activity_start_time as hasStart from activity order by seek_help_time desc
limit #{start},#{count} limit #{start},#{count}
</select> </select>

@ -105,7 +105,10 @@
password=#{password}, password=#{password},
</if> </if>
<if test="chatStatus!=null"> <if test="chatStatus!=null">
chat_status=#{chatStatus} chat_status=#{chatStatus},
</if>
<if test="timeScore!=null">
time_score=#{timeScore}
</if> </if>
</set> </set>
where user_id=#{userId} where user_id=#{userId}

@ -2,6 +2,7 @@ package com.share.help;
import com.share.help.dao.*; import com.share.help.dao.*;
import com.share.help.entity.ActivityEntity; import com.share.help.entity.ActivityEntity;
import com.share.help.entity.ActivityHistoryEntity;
import com.share.help.entity.UserEntity; import com.share.help.entity.UserEntity;
import com.share.help.entity.custorm.*; import com.share.help.entity.custorm.*;
import com.share.help.form.ActivityStatus; import com.share.help.form.ActivityStatus;
@ -250,5 +251,19 @@ class HelpApplicationTests {
mvc.perform(builder).andDo(MockMvcResultHandlers.print()); 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));
}
} }

Loading…
Cancel
Save