package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.common.Result;
import com.zhentao.entity.Announcement;
import com.zhentao.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告管理控制器
 */
@RestController
@RequestMapping("/api/admin/announcement")
public class AnnouncementController {
    
    @Autowired
    private AnnouncementService announcementService;
    
    /**
     * 分页查询公告列表
     */
    @GetMapping("/list")
    public Result getAnnouncementList(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Integer status) {
        Page<Announcement> page = announcementService.getAnnouncementList(current, size, status);
        return Result.success(page);
    }
    
    /**
     * 获取所有启用的公告
     */
    @GetMapping("/active")
    public Result getActiveAnnouncements() {
        List<Announcement> announcements = announcementService.getActiveAnnouncements();
        return Result.success(announcements);
    }
    
    /**
     * 根据ID获取公告详情
     */
    @GetMapping("/{id}")
    public Result getAnnouncementById(@PathVariable Long id) {
        Announcement announcement = announcementService.getAnnouncementById(id);
        if (announcement == null) {
            return Result.error("公告不存在");
        }
        return Result.success(announcement);
    }
    
    /**
     * 创建公告
     */
    @PostMapping
    public Result createAnnouncement(@RequestBody Announcement announcement) {
        if (announcement.getContent() == null || announcement.getContent().trim().isEmpty()) {
            return Result.error("公告内容不能为空");
        }
        if (announcement.getStatus() == null) {
            announcement.setStatus(1); // 默认启用
        }
        if (announcement.getPriority() == null) {
            announcement.setPriority(0); // 默认优先级为0
        }
        
        boolean success = announcementService.createAnnouncement(announcement);
        if (success) {
            return Result.success("创建成功");
        }
        return Result.error("创建失败");
    }
    
    /**
     * 更新公告
     */
    @PutMapping("/{id}")
    public Result updateAnnouncement(@PathVariable Long id, @RequestBody Announcement announcement) {
        announcement.setId(id);
        boolean success = announcementService.updateAnnouncement(announcement);
        if (success) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }
    
    /**
     * 删除公告
     */
    @DeleteMapping("/{id}")
    public Result deleteAnnouncement(@PathVariable Long id) {
        boolean success = announcementService.deleteAnnouncement(id);
        if (success) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
    
    /**
     * 批量删除公告
     */
    @DeleteMapping("/batch")
    public Result batchDeleteAnnouncements(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("请选择要删除的公告");
        }
        boolean success = announcementService.batchDeleteAnnouncements(ids);
        if (success) {
            return Result.success("批量删除成功");
        }
        return Result.error("批量删除失败");
    }
    
    /**
     * 更新公告状态
     */
    @PutMapping("/{id}/status")
    public Result updateAnnouncementStatus(@PathVariable Long id, @RequestParam Integer status) {
        if (status != 0 && status != 1) {
            return Result.error("状态值无效");
        }
        boolean success = announcementService.updateAnnouncementStatus(id, status);
        if (success) {
            return Result.success("状态更新成功");
        }
        return Result.error("状态更新失败");
    }
}

