package com.yanzhen.service; import com.yanzhen.entity.NoticeReceive; import com.yanzhen.mapper.NoticeMapper; import com.yanzhen.entity.Notice; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.yanzhen.mapper.NoticeReceiveMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import java.util.List; @Service // 标记为Spring的服务组件 public class NoticeService { @Autowired // 自动注入NoticeMapper依赖 private NoticeMapper noticeMapper; @Autowired // 自动注入NoticeReceiveMapper依赖 private NoticeReceiveMapper noticeReceiveMapper; // 创建通知,并关联到多个建筑 public int create(Notice notice) { noticeMapper.create(notice); // 在数据库中创建通知记录 List buildingIds = notice.getBuildingIds(); // 获取通知关联的建筑ID列表 for (Integer buildingId : buildingIds) { // 遍历每个建筑ID NoticeReceive noticeReceive = new NoticeReceive(); // 创建新的NoticeReceive对象 noticeReceive.setBuildingId(buildingId); // 设置建筑ID noticeReceive.setNoticeId(notice.getId()); // 设置通知ID noticeReceiveMapper.create(noticeReceive); // 在数据库中创建通知接收记录 } return 1; // 返回成功标志 } // 根据逗号分隔的ID字符串批量删除通知 public int delete(String ids) { String[] arr = ids.split(","); // 将ID字符串分割成数组 int row = 0; // 初始化受影响行数计数器 for (String s : arr) { // 遍历每个ID if(!StringUtils.isEmpty(s)){ // 如果ID不为空 noticeReceiveMapper.deleteByNoticeId(Integer.parseInt(s)); // 删除相关的通知接收记录 noticeMapper.delete(Integer.parseInt(s)); // 删除通知记录 row++; // 增加受影响行数计数器 } } return row; // 返回受影响行数 } // 根据单个ID删除通知 public int delete(Integer id) { noticeReceiveMapper.deleteByNoticeId(id); // 删除相关的通知接收记录 return noticeMapper.delete(id); // 删除通知记录并返回结果 } // 更新通知信息 public int update(Notice notice) { return noticeMapper.update(notice); // 更新通知记录并返回结果 } // 选择性更新通知信息,同时重新关联建筑 public int updateSelective(Notice notice) { noticeMapper.updateSelective(notice); // 选择性更新通知记录 noticeReceiveMapper.deleteByNoticeId(notice.getId()); // 删除旧的通知接收记录 List buildingIds = notice.getBuildingIds(); // 获取新的通知关联的建筑ID列表 for (Integer buildingId : buildingIds) { // 遍历每个建筑ID NoticeReceive noticeReceive = new NoticeReceive(); // 创建新的NoticeReceive对象 noticeReceive.setBuildingId(buildingId); // 设置建筑ID noticeReceive.setNoticeId(notice.getId()); // 设置通知ID noticeReceiveMapper.create(noticeReceive); // 在数据库中创建新的通知接收记录 } return 1; // 返回成功标志 } // 分页查询通知 public PageInfo query(Notice notice) { if(notice != null && notice.getPage() != null){ // 如果通知对象和分页信息不为空 PageHelper.startPage(notice.getPage(), notice.getLimit()); // 启动分页 } return new PageInfo(noticeMapper.query(notice)); // 执行查询并返回分页结果 } // 根据建筑ID分页查询通知 public PageInfo queryByBuildingId(Notice notice){ if(notice != null && notice.getPage() != null){ // 如果通知对象和分页信息不为空 PageHelper.startPage(notice.getPage(), notice.getLimit()); // 启动分页 } return new PageInfo(noticeMapper.queryByBuildingId(notice)); // 执行查询并返回分页结果 } // 根据ID获取通知详情 public Notice detail(Integer id) { return noticeMapper.detail(id); // 查询并返回通知详情 } // 统计符合条件的通知数量 public int count(Notice notice) { return noticeMapper.count(notice); // 查询并返回符合条件的通知数量 } }