Commit 0e239b71 by hzc

首次提交公告管理代码

parent 4f8a85e4
package com.keymobile.indicators.api.hytobacco;
import com.keymobile.indicators.model.entity.indicators.AfficheInfo;
import com.keymobile.indicators.result.Result;
import com.keymobile.indicators.service.AfficheInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@Api(tags = {"公告信息管理"})
@RestController
@RequestMapping("/affiche")
public class AfficheInfoCtrl {
@Autowired
private AfficheInfoService afficheInfoService;
@ApiOperation(value = "发布公告")
@PostMapping("/save")
public Result save(@ApiParam(value = "公告内容",name = "content",required = true) @RequestParam("content") String content,
@ApiParam(value = "用户名",name = "userName",required = true) @RequestParam ("userName")String userName,
@ApiParam(value = "用户ID",name = "currentUserId",required = true) @RequestParam ("currentUserId")String currentUserId){
//saveAfficheInfo(String content,String userName,String currentUserId);
afficheInfoService.saveAfficheInfo(content,userName,currentUserId);
return Result.genOkResult();
}
@ApiOperation("根据条件搜索公告")
@GetMapping("/findPage")
public Page<AfficheInfo> findPage(@ApiParam("关键字搜索(公告内容)") @RequestParam(value = "keyword", required = false) String keyword,
@ApiParam("页码,从1开始") @RequestParam("pageNo") int pageNo,
@ApiParam("每页条数") @RequestParam("pageSize") int pageSize){
pageNo--;
PageRequest request = PageRequest.of(pageNo, pageSize);
List<AfficheInfo> list = new ArrayList<>();
long total = afficheInfoService.findByPageCount(keyword);
if (total > 0) {
list = afficheInfoService.findByPage(keyword,request.getOffset(),pageSize);
}
return new PageImpl<>(list, request, total);
}
@ApiOperation(value = "查询所以公告",notes = "查询的同时清除该用户的未读公告数")
@GetMapping("/findAll")
public List<AfficheInfo> findAll( @ApiParam(value = "用户ID",name = "currentUserId",required = true) @RequestParam ("currentUserId")String currentUserId){
return afficheInfoService.findAllAfficheInfo(currentUserId);
}
@ApiOperation(value = "通过主键逻辑删除")
@GetMapping("/deleteById")
public Result deleteById(@ApiParam(value = "公告主键",required = true) @RequestParam("id") Integer id){
afficheInfoService.deleteById(id);
return Result.genOkResult();
}
@ApiOperation(value = "通过主键集合逻辑删除")
@PostMapping("/deleteByIds")
public Result deleteByIds(@ApiParam(value = "公告主键集合",required = true)@RequestBody List<Integer> ids,
@ApiParam(value = "用户名",required = true)@RequestParam("userName") String userName){
afficheInfoService.deleteByIds(ids,userName);
return Result.genOkResult();
}
@ApiOperation(value = "修改公告内容")
@PostMapping("/update")
public Result update(@ApiParam(value = "公告主键",name = "id",required = true) @RequestParam("id") Integer id,
@ApiParam(value = "公告内容",name = "content",required = true) @RequestParam("content") String content ,
@ApiParam(value = "用户名(记录删除者)",name = "userName",required = true) @RequestParam("content")String userName){
afficheInfoService.updateAfficheInfoById(id,content, userName);
return Result.genOkResult();
}
@ApiOperation(value = "查询我未读的公告数")
@GetMapping("/findUnReadCount")
public long findUnReadCount(@ApiParam(value = "用户ID",name = "currentUserId",required = true)@RequestParam("currentUserId") String currentUserId){
//String currentUserId = SystemUserUtil.getCurrentUserId();
return afficheInfoService.findUnReadCountById(currentUserId);
}
}
package com.keymobile.indicators.model.entity.indicators;
import com.keymobile.indicators.model.entity.BaseModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel("公告实体类")
public class AfficheInfo extends BaseModel {
@ApiModelProperty(value = "主键")
private Integer id;
@ApiModelProperty(value = "公告内容")
private String content;
}
package com.keymobile.indicators.model.entity.indicators;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel
public class AfficheinfoUser {
@ApiModelProperty("主键-自增")
private Long id;
@ApiModelProperty("公告ID")
private Integer affcheId;
@ApiModelProperty("用户ID")
private String userId;
}
package com.keymobile.indicators.model.mapper.indicators;
import com.keymobile.indicators.model.entity.indicators.AfficheInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface AfficheInfoMapper {
@Insert("INSERT INTO affiche_info(content, creator,updater,state,create_time,update_time)VALUES (#{content}, #{creator}, #{updater}, #{state},#{updateTime},#{createTime})")
@Options(useGeneratedKeys = true, keyProperty = "id")
void insertAfficheInfo(AfficheInfo afficheInfo);
@Select("SELECT * FROM affiche_info where state=1")
List<AfficheInfo> selectAllAfficheInfo();
@Update("UPDATE affiche_info SET state=0,update_time=NOW() WHERE id=#{id}")
void deleteById(Integer id);
@Update("UPDATE affiche_info SET content=#{content},updater=#{updater},update_time=NOW() WHERE id=#{id}")
void updateAfficheInfoById(@Param("id") Integer id,@Param("content") String content,@Param("updater") String updater);
void deleteByIds(@Param("ids") List<Integer> ids,@Param("updater") String updater);
long findByPageCount(String keyword);
//查询加分页
List<AfficheInfo> findByPage(@Param("keyword") String keyword,@Param("start") long start,@Param("pageSize") int pageSize);
//通过用户id查询未读的公告数
@Select("select count(*) from affiche_user where user_id=#{currentUserId}")
long findUnReadCountById(String currentUserId);
void insertAfficheInfoToAllUser(@Param("affcheId") Integer affcheId, @Param("userIds") List<String> userIds);
@Delete("DELETE FROM affiche_user WHERE user_id=#{currentUserId}")
void deleteAfficheUserByUserId(String currentUserId);
@Delete("DELETE FROM affiche_user WHERE affche_id=#{id}")
void deleteAfficheUserByAfficheId(Integer id);
void deleteAfficheUserByAffcheIds(@Param("affcheIds") List<Integer> affcheIds);
}
package com.keymobile.indicators.service;
import com.keymobile.indicators.model.entity.indicators.AfficheInfo;
import java.util.List;
public interface AfficheInfoService {
void saveAfficheInfo(String content,String userName,String currentUserId);
List<AfficheInfo> findAllAfficheInfo(String currentUserId);
void deleteById(Integer id);
void updateAfficheInfoById(Integer id, String content,String userName);
void deleteByIds(List<Integer> ids,String userName);
long findByPageCount(String keyword);
List<AfficheInfo> findByPage(String keyword, long offset, int pageSize);
long findUnReadCountById(String currentUserId);
}
package com.keymobile.indicators.service.impl;
import com.keymobile.indicators.model.entity.indicators.AfficheInfo;
import com.keymobile.indicators.model.mapper.indicators.AfficheInfoMapper;
import com.keymobile.indicators.service.AfficheInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class AfficheInfoServiceImpl implements AfficheInfoService {
@Autowired
private AfficheInfoMapper afficheInfoMapper;
@Override
public void saveAfficheInfo(String content,String userName,String currentUserId) {
System.out.println("saveAfficheInfo");
Date date = new Date();
// String userName = SystemUserUtil.getCurrentUserName();
AfficheInfo afficheInfo = new AfficheInfo();
afficheInfo.setCreateTime(date);
afficheInfo.setUpdateTime(date);
afficheInfo.setContent(content);
afficheInfo.setCreator(userName);
afficheInfo.setUpdater(userName);
afficheInfo.setState(1);
afficheInfoMapper.insertAfficheInfo(afficheInfo);
Integer id = afficheInfo.getId();
this.insertAfficheInfoToAllUser(id,currentUserId);
}
@Override
public List<AfficheInfo> findAllAfficheInfo(String currentUserId) {
//删除未查看记录
this.deleteAfficheUserByUserId(currentUserId);
return afficheInfoMapper.selectAllAfficheInfo();
}
@Override
public void deleteById(Integer id) {
this.deleteAfficheUserByAfficheId(id);
afficheInfoMapper.deleteById(id);
}
@Override
public void updateAfficheInfoById(Integer id, String content,String userName) {
//String userName = SystemUserUtil.getCurrentUserName();
afficheInfoMapper.updateAfficheInfoById(id,content,userName);
}
@Async
@Override
public void deleteByIds(List<Integer> ids,String userName) {
// String userName = SystemUserUtil.getCurrentUserName();
afficheInfoMapper.deleteByIds(ids,userName);
afficheInfoMapper.deleteAfficheUserByAffcheIds(ids);
//代写清除未查看记录
}
@Override
public long findByPageCount(String keyword) {
return afficheInfoMapper.findByPageCount(keyword);
}
@Override
public List<AfficheInfo> findByPage(String keyword, long offset, int pageSize) {
return afficheInfoMapper.findByPage(keyword,offset,pageSize);
}
@Override
public long findUnReadCountById(String currentUserId) {
return afficheInfoMapper.findUnReadCountById(currentUserId);
}
@Async
public void insertAfficheInfoToAllUser(Integer id,String currentUserId){
// String currentUserId = SystemUserUtil.getCurrentUserId();
System.out.println("userid::"+currentUserId);
List<String> userIds = new ArrayList<>();
//currentUserId只是测试数据
userIds.add(currentUserId);
afficheInfoMapper.insertAfficheInfoToAllUser(id,userIds);
}
@Async
public void deleteAfficheUserByUserId(String currentUserId){
afficheInfoMapper.deleteAfficheUserByUserId(currentUserId);
}
@Async
public void deleteAfficheUserByAfficheId(Integer id){
afficheInfoMapper.deleteAfficheUserByAfficheId(id);
}
}
......@@ -62,5 +62,5 @@ mybatis:
logging:
level:
com.keymobile.indicators: info
com.keymobile.indicators: debug
config: classpath:logback-custom.xml
\ No newline at end of file
<?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.keymobile.indicators.model.mapper.indicators.AfficheInfoMapper">
<select id="findByPage" resultType="com.keymobile.indicators.model.entity.indicators.AfficheInfo">
select *
from affiche_info
<include refid="findWhereSql"></include>
order by update_time desc
limit #{start}, #{pageSize}
</select>
<select id="findByPageCount" resultType="long">
select count(id)
from affiche_info
<include refid="findWhereSql"></include>
</select>
<sql id="findWhereSql">
where state = 1
<if test="keyword != null and keyword != ''">
and content like concat('%', #{keyword}, '%')
</if>
</sql>
<update id="deleteByIds">
update affiche_info
set
state = 0,updater=#{updater}
where id in (
<foreach collection="ids" item="item" separator=",">
#{item}
</foreach>
)
</update>
<insert id="insertAfficheInfoToAllUser">
insert into affiche_user
(affche_id,user_id)
values
<foreach collection="userIds" item="user" separator=",">
(
#{affcheId}, #{user}
)
</foreach>
</insert>
<delete id="deleteAfficheUserByAffcheIds">
delete FROM affiche_user
where affche_id in (
<foreach collection="affcheIds" item="item" separator=",">
#{item}
</foreach>
)
</delete>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment