parent
bda3bfe16e
commit
55b7f36048
@ -0,0 +1,55 @@ |
||||
package com.share.help.controller; |
||||
|
||||
import com.share.help.Constants; |
||||
import com.share.help.form.FindActivityForm; |
||||
import com.share.help.form.SendHelpForm; |
||||
import com.share.help.res.FindActivityRes; |
||||
import com.share.help.res.JSONResponse; |
||||
import com.share.help.res.Result; |
||||
import com.share.help.res.SendHelpRes; |
||||
import com.share.help.service.ActivityService; |
||||
import org.apache.commons.lang3.ObjectUtils; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PutMapping; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
@RestController |
||||
@RequestMapping(Constants.ACTIVITY_INTERFACE) |
||||
public class ActivityController { |
||||
|
||||
@Autowired |
||||
private ActivityService activityService; |
||||
|
||||
|
||||
/** |
||||
* 发布求助信息 |
||||
* @return 返回发布结果 |
||||
*/ |
||||
@PutMapping(Constants.ACTIVITY_INTERFACE_SEND_HELP) |
||||
public JSONResponse<SendHelpRes,Void> send(SendHelpForm sendHelpForm){ |
||||
if(StringUtils.isNoneEmpty(sendHelpForm.getTitle(),sendHelpForm.getContent(),sendHelpForm.getServiceAddress())&& |
||||
ObjectUtils.allNotNull(sendHelpForm.getActivityStartTime(),sendHelpForm.getActivityEndTime() |
||||
,sendHelpForm.getActivityImgFile())){ |
||||
return activityService.sendHelp(sendHelpForm); |
||||
}else{ |
||||
return new JSONResponse<>(SendHelpRes.fail,Result.FAIL); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 返回活动列表数据 |
||||
* @param findActivityForm 查询表单 |
||||
* @return 返回活动列表数据 |
||||
*/ |
||||
@GetMapping(Constants.ACTIVITY_INTERFACE_FIND) |
||||
public JSONResponse<FindActivityRes.Res,FindActivityRes> find(FindActivityForm findActivityForm){ |
||||
if(findActivityForm.getCurrentPage()==null){ |
||||
return new JSONResponse<>(FindActivityRes.Res.fail,Result.FAIL); |
||||
}else{ |
||||
return activityService.find(findActivityForm); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,52 @@ |
||||
package com.share.help.dao; |
||||
|
||||
import com.share.help.entity.ActivityEntity; |
||||
import com.share.help.res.ActivitySimple; |
||||
import com.share.help.res.FindActivityRes; |
||||
import org.apache.ibatis.annotations.*; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 活动数据接口 |
||||
*/ |
||||
public interface ActivityMapper { |
||||
|
||||
/** |
||||
* 保存活动信息 |
||||
* @param activityEntity 活动实体 |
||||
* @return 返回保存结果 |
||||
*/ |
||||
@Insert("insert into activity(activity_id,title,content,seek_help_time,activity_start_time,activity_end_time,activity_img,service_address)value(#{activityId},#{title},#{content},#{seekHelpTime},#{activityStartTime},#{activityEndTime},#{activityImg},#{serviceAddress})") |
||||
boolean insert(ActivityEntity activityEntity); |
||||
|
||||
/** |
||||
* 根据活动id删除活动信息 |
||||
* @param activityId 活动id |
||||
* @return 返回删除结果 |
||||
*/ |
||||
@Delete("delete from activity where activity_id=#{activityId}") |
||||
boolean delete(@Param("activityId") Long activityId); |
||||
|
||||
/** |
||||
* 根据活动id查找活动信息 |
||||
* @param activityId 活动id |
||||
* @return 返回活动信息 |
||||
*/ |
||||
@Select("select * from activity where activity_id=#{activityId}") |
||||
@ResultMap("activityMap") |
||||
ActivityEntity findOne(@Param("activityId") Long activityId); |
||||
|
||||
/** |
||||
* 分页查询活动 |
||||
* @param start 起始索引 |
||||
* @param count 查询个数 |
||||
* @return 返回活动列表 |
||||
*/ |
||||
@Select("select activity_id,title,content,activity_img from activity limit #{start},#{count}") |
||||
@ResultMap("activitySimpleMap") |
||||
List<ActivitySimple> getPage(@Param("start") Integer start, @Param("count") Integer count); |
||||
|
||||
@Select("select count(*) as count,ceil(count(*)/#{pageSize}) as totalPage from activity") |
||||
FindActivityRes count(@Param("pageSize") Integer pageSize); |
||||
} |
@ -0,0 +1,28 @@ |
||||
package com.share.help.form; |
||||
|
||||
/** |
||||
* 查找活动列表表单 |
||||
*/ |
||||
public class FindActivityForm { |
||||
//查找分页
|
||||
private Integer currentPage; |
||||
|
||||
//标题信息
|
||||
private String title; |
||||
|
||||
public String getTitle() { |
||||
return title; |
||||
} |
||||
|
||||
public void setTitle(String title) { |
||||
this.title = title; |
||||
} |
||||
|
||||
public Integer getCurrentPage() { |
||||
return currentPage; |
||||
} |
||||
|
||||
public void setCurrentPage(Integer currentPage) { |
||||
this.currentPage = currentPage; |
||||
} |
||||
} |
@ -0,0 +1,23 @@ |
||||
package com.share.help.form; |
||||
|
||||
import com.share.help.entity.ActivityEntity; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
/** |
||||
* 发布求助信息表单 |
||||
*/ |
||||
public class SendHelpForm extends ActivityEntity { |
||||
|
||||
/** |
||||
* 活动背景图 |
||||
*/ |
||||
private MultipartFile activityImgFile; |
||||
|
||||
public MultipartFile getActivityImgFile() { |
||||
return activityImgFile; |
||||
} |
||||
|
||||
public void setActivityImgFile(MultipartFile activityImgFile) { |
||||
this.activityImgFile = activityImgFile; |
||||
} |
||||
} |
@ -0,0 +1,43 @@ |
||||
package com.share.help.res; |
||||
|
||||
/** |
||||
* 活动列表实体 |
||||
*/ |
||||
public class ActivitySimple { |
||||
private Long activityId; |
||||
private String title; |
||||
private String content; |
||||
private String activityImg; |
||||
|
||||
public Long getActivityId() { |
||||
return activityId; |
||||
} |
||||
|
||||
public void setActivityId(Long activityId) { |
||||
this.activityId = activityId; |
||||
} |
||||
|
||||
public String getTitle() { |
||||
return title; |
||||
} |
||||
|
||||
public void setTitle(String title) { |
||||
this.title = title; |
||||
} |
||||
|
||||
public String getContent() { |
||||
return content; |
||||
} |
||||
|
||||
public void setContent(String content) { |
||||
this.content = content; |
||||
} |
||||
|
||||
public String getActivityImg() { |
||||
return activityImg; |
||||
} |
||||
|
||||
public void setActivityImg(String activityImg) { |
||||
this.activityImg = activityImg; |
||||
} |
||||
} |
@ -0,0 +1,16 @@ |
||||
package com.share.help.res; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 查找活动列表结果 |
||||
*/ |
||||
public class FindActivityRes extends Page<List<List<ActivitySimple>>>{ |
||||
|
||||
public enum Res{ |
||||
//成功
|
||||
ok, |
||||
//失败
|
||||
fail |
||||
} |
||||
} |
@ -0,0 +1,53 @@ |
||||
package com.share.help.res; |
||||
|
||||
public class Page<T> { |
||||
private Integer currentPage; |
||||
|
||||
private Integer totalPage; |
||||
|
||||
private Long count; |
||||
|
||||
private Integer pageSize; |
||||
|
||||
private T body; |
||||
|
||||
public Integer getCurrentPage() { |
||||
return currentPage; |
||||
} |
||||
|
||||
public void setCurrentPage(Integer currentPage) { |
||||
this.currentPage = currentPage; |
||||
} |
||||
|
||||
public Integer getTotalPage() { |
||||
return totalPage; |
||||
} |
||||
|
||||
public void setTotalPage(Integer totalPage) { |
||||
this.totalPage = totalPage; |
||||
} |
||||
|
||||
public Integer getPageSize() { |
||||
return pageSize; |
||||
} |
||||
|
||||
public void setPageSize(Integer pageSize) { |
||||
this.pageSize = pageSize; |
||||
} |
||||
|
||||
public Long getCount() { |
||||
return count; |
||||
} |
||||
|
||||
public void setCount(Long count) { |
||||
this.count = count; |
||||
} |
||||
|
||||
public T getBody() { |
||||
return body; |
||||
} |
||||
|
||||
public void setBody(T body) { |
||||
this.body = body; |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
package com.share.help.res; |
||||
|
||||
/** |
||||
* 发布求助信息响应 |
||||
*/ |
||||
public enum SendHelpRes { |
||||
//发布成功
|
||||
ok, |
||||
//发布失败请联系管理员
|
||||
fail |
||||
} |
@ -0,0 +1,86 @@ |
||||
package com.share.help.service; |
||||
|
||||
import com.share.help.dao.ActivityMapper; |
||||
import com.share.help.entity.ActivityEntity; |
||||
import com.share.help.form.FindActivityForm; |
||||
import com.share.help.form.SendHelpForm; |
||||
import com.share.help.res.*; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.sql.Timestamp; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 活动服务层 |
||||
*/ |
||||
@Service |
||||
public class ActivityService extends ImageService{ |
||||
|
||||
@Autowired |
||||
private ActivityMapper activityMapper; |
||||
|
||||
//活动分页配置
|
||||
@Value("${activity.page-size}") |
||||
private int pageSize; |
||||
|
||||
@Value("${activity.row-size}") |
||||
private int rowSize; |
||||
|
||||
|
||||
|
||||
|
||||
/** |
||||
* 发布求助信息 |
||||
* @param sendHelpForm 求助信息表单 |
||||
* @return 发布结果 |
||||
*/ |
||||
public JSONResponse<SendHelpRes,Void> sendHelp(SendHelpForm sendHelpForm){ |
||||
ActivityEntity activityEntity=new ActivityEntity(); |
||||
activityEntity.setTitle(sendHelpForm.getTitle()); |
||||
activityEntity.setContent(sendHelpForm.getContent()); |
||||
activityEntity.setSeekHelpTime(new Timestamp(System.currentTimeMillis())); |
||||
activityEntity.setActivityStartTime(sendHelpForm.getActivityStartTime()); |
||||
activityEntity.setActivityEndTime(sendHelpForm.getActivityEndTime()); |
||||
//保存活动背景图
|
||||
String imageName=saveImg(sendHelpForm.getActivityImgFile()); |
||||
if(imageName==null){ |
||||
return new JSONResponse<>(SendHelpRes.fail, Result.FAIL); |
||||
} |
||||
activityEntity.setActivityImg(imageName); |
||||
activityEntity.setServiceAddress(sendHelpForm.getServiceAddress()); |
||||
return activityMapper.insert(activityEntity)?new JSONResponse<>(SendHelpRes.ok,Result.OK):new JSONResponse<>(SendHelpRes.fail,Result.FAIL); |
||||
} |
||||
|
||||
/** |
||||
* 分页查询活动信息 |
||||
* @param findActivityForm 查询活动信息 |
||||
* @return 返回活动信息 |
||||
*/ |
||||
public JSONResponse<FindActivityRes.Res,FindActivityRes> find(FindActivityForm findActivityForm){ |
||||
FindActivityRes findActivityRes=activityMapper.count(pageSize); |
||||
findActivityRes.setPageSize(pageSize); |
||||
findActivityRes.setCurrentPage(findActivityForm.getCurrentPage()); |
||||
List<ActivitySimple> activitySimpleList=activityMapper.getPage((findActivityForm.getCurrentPage()-1)*pageSize,pageSize); |
||||
List<List<ActivitySimple>> dataList=new ArrayList<>(); |
||||
if(!activitySimpleList.isEmpty()){ |
||||
if(activitySimpleList.size()<rowSize){ |
||||
dataList.add(activitySimpleList); |
||||
}else { |
||||
for (int i = rowSize, j = 0; i < activitySimpleList.size(); j = i, i += rowSize) { |
||||
dataList.add(activitySimpleList.subList(j, i)); |
||||
if (i + rowSize >= activitySimpleList.size()) { |
||||
dataList.add(activitySimpleList.subList(i, activitySimpleList.size())); |
||||
} |
||||
} |
||||
} |
||||
findActivityRes.setBody(dataList); |
||||
return new JSONResponse<FindActivityRes.Res,FindActivityRes>(FindActivityRes.Res.ok,Result.OK).setBody(findActivityRes); |
||||
}else{ |
||||
return new JSONResponse<>(FindActivityRes.Res.fail, Result.FAIL); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,23 @@ |
||||
<?xml version="1.0" encoding="UTF-8" ?> |
||||
<!DOCTYPE mapper |
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
<mapper namespace="com.share.help.dao.ActivityMapper"> |
||||
<resultMap id="activityMap" type="com.share.help.entity.ActivityEntity"> |
||||
<id property="activityId" column="activity_id"/> |
||||
<result property="title" column="title"/> |
||||
<result property="content" column="content"/> |
||||
<result property="seekHelpTime" column="seek_help_time"/> |
||||
<result property="activityStartTime" column="activity_start_time"/> |
||||
<result property="activityEndTime" column="activity_end_time"/> |
||||
<result property="activityImg" column="activity_img"/> |
||||
<result property="serviceAddress" column="service_address"/> |
||||
</resultMap> |
||||
|
||||
<resultMap id="activitySimpleMap" type="com.share.help.res.ActivitySimple"> |
||||
<id property="activityId" column="activity_id"/> |
||||
<result property="title" column="title"/> |
||||
<result property="content" column="content"/> |
||||
<result property="activityImg" column="activity_img"/> |
||||
</resultMap> |
||||
</mapper> |
Loading…
Reference in new issue