公告相关上传

Change-Id: I81fa2661ee5aefbb898697da43e57a83cc6cec35
diff --git a/src/main/java/com/pt5/pthouduan/controller/ActivityController.java b/src/main/java/com/pt5/pthouduan/controller/ActivityController.java
new file mode 100644
index 0000000..5a60a24
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/controller/ActivityController.java
@@ -0,0 +1,160 @@
+package com.pt5.pthouduan.controller;
+
+import com.pt5.pthouduan.entity.Activity;
+import com.pt5.pthouduan.service.ActivityService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+/**
+ * <p>
+ * 活动控制器
+ * </p>
+ *
+ * 功能:增删改查活动公告(支持上传图片)
+ *
+ * @author ym
+ * @since 2025-05-16
+ */
+@CrossOrigin(origins = {"http://localhost:5173", "http://localhost:3000"})
+@Controller
+@RequestMapping("/activity")
+public class ActivityController {
+
+    @Autowired
+    private ActivityService activityService;
+
+    // 获取所有 is_show == 0 的活动标题和图片(活动预览)
+    @GetMapping("/preview")
+    @ResponseBody
+    public List<Activity> getVisibleActivityPreviews() {
+        List<Activity> allVisible = activityService.findByIsShow(0);
+        return allVisible.stream().map(activity -> {
+            Activity preview = new Activity();
+            preview.setActivityid(activity.getActivityid());
+            preview.setContent(activity.getContent());
+            preview.setTitle(activity.getTitle());
+            preview.setAward(activity.getAward());
+            preview.setTime(activity.getTime());
+            preview.setPhoto(activity.getPhoto());
+            return preview;
+        }).collect(Collectors.toList());
+    }
+
+    // 获取所有 is_show == 0 的完整活动信息
+    @GetMapping("/full")
+    @ResponseBody
+    public List<Activity> getVisibleActivities() {
+        return activityService.findByIsShow(0);
+    }
+
+    // 创建新的公告(支持图片上传)
+    @PostMapping("/create")
+    @ResponseBody
+    public boolean createActivity(
+            @RequestParam("title") String title,
+            @RequestParam("content") String content,
+            @RequestParam(value = "photo", required = false) MultipartFile photoFile,
+            @RequestParam(value = "isShow", required = false, defaultValue = "0") Integer isShow,
+            @RequestParam(value = "award", required = false, defaultValue = "0") Integer award
+    ) {
+        Activity activity = new Activity();
+        activity.setTitle(title);
+        activity.setContent(content);
+        activity.setIs_show(isShow);
+        activity.setAward(award);
+        activity.setTime(LocalDateTime.now()); // 设置当前时间
+
+        // 处理图片上传
+        if (photoFile != null && !photoFile.isEmpty()) {
+            String uploadDir = "D:/activityuploads/";
+            File dir = new File(uploadDir);
+            if (!dir.exists()) dir.mkdirs();
+
+            String fileName = UUID.randomUUID() + "_" + photoFile.getOriginalFilename();
+            File dest = new File(uploadDir + fileName);
+            try {
+                photoFile.transferTo(dest);
+                // 设置图片访问路径(请确保 /images 映射到了 uploadDir)
+                activity.setPhoto("/activity/" + fileName);
+            } catch (IOException e) {
+                e.printStackTrace();
+                return false;
+            }
+        }
+
+        return activityService.save(activity);
+    }
+
+
+    // 删除公告(根据ID)
+    @DeleteMapping("/delete/{id}")
+    @ResponseBody
+    public boolean deleteActivity(@PathVariable Integer id) {
+        return activityService.removeById(id);
+    }
+
+    // 获取所有活动(无论展示状态)
+    @GetMapping("/all")
+    @ResponseBody
+    public List<Activity> getAllActivities() {
+        // 假设 activityService 有 list() 方法,返回所有活动
+        return activityService.list();
+    }
+
+    //根据题目搜索公告
+    @GetMapping("/search")
+    @ResponseBody
+    public List<Activity> searchActivitiesByTitle(@RequestParam("title") String title) {
+        return activityService.searchByTitle(title);
+    }
+
+    // 修改公告(根据ID)
+    @PutMapping("/update")
+    @ResponseBody
+    public boolean updateActivity(
+            @RequestParam("activityid") Integer id,
+            @RequestParam("title") String title,
+            @RequestParam("content") String content,
+            @RequestParam(value = "photo", required = false) MultipartFile photoFile,
+            @RequestParam(value = "isShow", required = false, defaultValue = "0") Integer isShow,
+            @RequestParam(value = "award", required = false, defaultValue = "0") Integer award
+    ) {
+        Activity activity = activityService.getById(id);
+        if (activity == null) {
+            return false;
+        }
+
+        activity.setTitle(title);
+        activity.setContent(content);
+        activity.setIs_show(isShow);
+        activity.setAward(award);
+
+        // 上传新图片(可选)
+        if (photoFile != null && !photoFile.isEmpty()) {
+            String uploadDir = "D:/activityuploads/";
+            File dir = new File(uploadDir);
+            if (!dir.exists()) dir.mkdirs();
+
+            String fileName = UUID.randomUUID() + "_" + photoFile.getOriginalFilename();
+            File dest = new File(uploadDir + fileName);
+            try {
+                photoFile.transferTo(dest);
+                activity.setPhoto("/activity/" + fileName);
+            } catch (IOException e) {
+                e.printStackTrace();
+                return false;
+            }
+        }
+
+        return activityService.updateById(activity);
+    }
+}
diff --git a/src/main/java/com/pt5/pthouduan/entity/Activity.java b/src/main/java/com/pt5/pthouduan/entity/Activity.java
new file mode 100644
index 0000000..089ecfa
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/entity/Activity.java
@@ -0,0 +1,110 @@
+package com.pt5.pthouduan.entity;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+import java.util.Date;
+
+/**
+ * <p>
+ *
+ * </p>
+ *
+ * @author 杨蔓
+ * @since 2025-05-16
+ */
+@TableName("activity")
+public class Activity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId("activityid")
+    private Integer activityid;
+
+    private String title;
+
+    private String content;
+
+    private String photo;
+
+    private LocalDateTime time;
+
+    private Integer is_show;
+
+    private Integer award;
+
+
+    public Integer getActivityid() {
+        return activityid;
+    }
+
+    public void setActivityid(Integer 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 getPhoto() {//获取标签
+        return photo;
+    }
+
+    public void setPhoto(String photo) {
+        this.photo = photo;
+    }
+
+    public LocalDateTime getTime() {
+        return time;
+    }
+
+    public void setTime(LocalDateTime time) {
+        this.time = time;
+    }
+
+    public Integer getIs_show() {
+        return is_show;
+    }
+
+    public void setIs_show(Integer is_show) {
+        this.is_show = is_show;
+    }
+
+    public Integer getAward() {
+        return award;
+    }
+
+    public void setAward(Integer award) {
+        this.award = award;
+    }
+
+
+
+
+    @Override
+    public String toString() {
+        return "Activity{" +
+                "activityid=" + activityid +
+                ", title=" + title  +
+                ", content=" + content +
+                ", photo=" + photo +
+                ", time=" + time +
+                ", is_show=" + is_show +
+                ", award=" + award +
+                '}';
+    }
+}
diff --git a/src/main/java/com/pt5/pthouduan/mapper/ActivityMapper.java b/src/main/java/com/pt5/pthouduan/mapper/ActivityMapper.java
new file mode 100644
index 0000000..c94aeb0
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/mapper/ActivityMapper.java
@@ -0,0 +1,38 @@
+package com.pt5.pthouduan.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.pt5.pthouduan.entity.Activity;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * <p>
+ *  活动 Mapper 接口
+ * </p>
+ *
+ * 功能:增、删、改、查(按是否展示)
+ *
+ * 作者:杨蔓
+ * @since 2025-05-16
+ */
+@Mapper
+public interface ActivityMapper extends BaseMapper<Activity> {
+
+    // 根据是否展示状态查询活动列表(0:展示,1:隐藏)
+    List<Activity> selectByIsShow(@Param("is_show") Integer isShow);
+
+    // 插入新的活动
+    int insert(Activity activity);
+
+    // 根据活动ID删除活动
+    int deleteById(@Param("activityid") Integer activityid);
+
+    // 根据活动ID更新活动内容
+    int updateById(Activity activity);
+
+    // ✅ 新增:根据活动ID查询活动详情
+    Activity selectById(@Param("activityid") Integer activityid);
+
+}
diff --git a/src/main/java/com/pt5/pthouduan/service/ActivityService.java b/src/main/java/com/pt5/pthouduan/service/ActivityService.java
new file mode 100644
index 0000000..67b51e1
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/ActivityService.java
@@ -0,0 +1,40 @@
+package com.pt5.pthouduan.service;
+
+import com.pt5.pthouduan.entity.Activity;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 活动服务接口
+ * </p>
+ *
+ * 功能:增、删、改、查(按是否显示状态)
+ * </p>
+ *
+ * @author ym
+ * @since 2025-05-16
+ */
+public interface ActivityService {
+
+    // 根据 is_show 状态查询活动列表(0:显示,1:隐藏等)
+    List<Activity> findByIsShow(Integer isShow);
+
+    // 创建活动
+    boolean save(Activity activity);
+
+    // 根据ID删除活动
+    boolean removeById(Integer activityid);
+
+    // 根据ID更新活动
+    boolean updateById(Activity activity);
+
+    // 根据ID获取活动
+    Activity getById(Integer activityid);
+
+    // 新增:获取所有活动(不分是否显示)
+    List<Activity> list();
+
+    //根据题目获得公告
+    List<Activity> searchByTitle(String title);
+}
diff --git a/src/main/java/com/pt5/pthouduan/service/impl/ActivityServiceImpl.java b/src/main/java/com/pt5/pthouduan/service/impl/ActivityServiceImpl.java
new file mode 100644
index 0000000..3173589
--- /dev/null
+++ b/src/main/java/com/pt5/pthouduan/service/impl/ActivityServiceImpl.java
@@ -0,0 +1,67 @@
+package com.pt5.pthouduan.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.pt5.pthouduan.entity.Activity;
+import com.pt5.pthouduan.mapper.ActivityMapper;
+import com.pt5.pthouduan.service.ActivityService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 活动服务实现类
+ * </p>
+ *
+ * 实现了活动的增删改查逻辑
+ * </p>
+ *
+ * @author ym
+ * @since 2025-05-16
+ */
+@Service
+public class ActivityServiceImpl implements ActivityService {
+
+    @Autowired
+    private ActivityMapper activityMapper;
+
+    @Override
+    public List<Activity> findByIsShow(Integer isShow) {
+        return activityMapper.selectByIsShow(isShow);
+    }
+
+    @Override
+    public boolean save(Activity activity) {
+        return activityMapper.insert(activity) > 0;
+    }
+
+    @Override
+    public boolean removeById(Integer activityid) {
+        return activityMapper.deleteById(activityid) > 0;
+    }
+
+    @Override
+    public boolean updateById(Activity activity) {
+        return activityMapper.updateById(activity) > 0;
+    }
+
+    @Override
+    public Activity getById(Integer activityid) {
+        return activityMapper.selectById(activityid);
+    }
+
+    // 新增:查询所有活动(不分是否显示)
+    @Override
+    public List<Activity> list() {
+        return activityMapper.selectList(null);
+    }
+
+    @Override
+    public List<Activity> searchByTitle(String title) {
+        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
+        // 模糊匹配 title 字段
+        queryWrapper.like("title", title);
+        return activityMapper.selectList(queryWrapper);
+    }
+}