package com.amms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.amms.domain.vo.Result;
import com.amms.domain.Announcement;
import com.amms.service.IAnnouncementService;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * 公告Controller
 */
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private IAnnouncementService announcementService;

    /**
     * 查询公告列表
     */
    @GetMapping("/list")
    public PageInfo list(Announcement announcement, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Announcement> announcements = announcementService.selectAnnouncementList(announcement);
        return new PageInfo(announcements);
    }

    /**
     * 查询全部公告列表
     */
    @GetMapping("/listAll")
    public List<Announcement> listAll(Announcement announcement) {
        return announcementService.selectAnnouncementList(announcement);
    }

    /**
     * 获取公告详细信息
     */
    @GetMapping(value = "/info/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return Result.success(announcementService.selectAnnouncementById(id));
    }

    /**
     * 新增公告
     */
    @PostMapping("/add")
    public Result add(@RequestBody Announcement announcement) {
        return announcementService.insertAnnouncement(announcement) > 0 ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改公告
     */
    @PutMapping
    public Result update(@RequestBody Announcement announcement) {
        return announcementService.updateAnnouncement(announcement) > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除公告
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        return announcementService.deleteAnnouncementById(id) > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 设为置顶（全局仅一个置顶）
     */
    @PutMapping("/setTop/{id}")
    public Result setTop(@PathVariable Long id) {
        return announcementService.setTop(id) > 0 ? Result.success("置顶成功") : Result.error("置顶失败");
    }

    /**
     * 游客端公告列表（置顶 + 普通分页）
     *
     * 返回：
     * topList - 置顶公告列表
     * list    - 普通公告当前页数据
     * total   - 普通公告总条数
     */
    @GetMapping("/touristList")
    public Result touristList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,
                              @RequestParam(value = "title", required = false) String title) {
        List<Announcement> topList = announcementService.selectTopAnnouncements();
        PageHelper.startPage(pageNum, pageSize);
        Announcement filter = new Announcement();
        filter.setTitle(title);
        List<Announcement> normals = announcementService.selectNormalAnnouncements(filter);
        PageInfo pageInfo = new PageInfo(normals);
        Map<String, Object> data = new HashMap<>();
        data.put("topList", topList);
        data.put("list", pageInfo.getList());
        data.put("total", pageInfo.getTotal());
        return Result.success(data);
    }
}
