package com.zsc.edu.gateway.modules.message.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.message.dto.BulletinDto;
import com.zsc.edu.gateway.modules.message.entity.Bulletin;
import com.zsc.edu.gateway.modules.message.query.BulletinQuery;
import com.zsc.edu.gateway.modules.message.service.BulletinService;
import com.zsc.edu.gateway.modules.message.vo.BulletinVo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告Controller
 *
 * @author harry_yao
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/rest/bulletin")
public class BulletinController {

    private final BulletinService service;
    /**
     * 普通用户查看公告详情
     *
     * @param id ID
     * @return 公告
     */
    @GetMapping("/self/{id}")
    public BulletinVo selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
        return service.detail(userDetails,id, Bulletin.State.publish);
    }

    /**
     * 普通用户分页查询公告
     *
     * @param query    查询表单
     * @return 分页数据
     */
    @GetMapping("/self")
    public Page<Bulletin> getBulletins(Page<Bulletin> page, BulletinQuery query) {
        query.setState(Bulletin.State.publish);
        return service.page(page, query.wrapper());
    }

    /**
     * 管理查询公告详情
     *
     * @param id ID
     * @return 公告
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('message:bulletin:query')")
    public BulletinVo detail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
        return service.detail(userDetails,id, null);
    }

    /**
     * 管理员分页查询公告
     *
     * @param query    查询参数
     * @return 分页数据
     */
    @GetMapping()
    @PreAuthorize("hasAuthority('message:bulletin:query')")
    public Page<Bulletin> query(Page<Bulletin> page, BulletinQuery query) {
        return service.page(page, query.wrapper());
    }

    /**
     * 创建公告
     *
     * @param userDetails 操作用户
     * @param dto         表单数据
     * @return 公告
     */
    @PostMapping
    @PreAuthorize("hasAuthority('message:bulletin:create')")
    public Bulletin create(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody BulletinDto dto) {
        return service.create(userDetails, dto);
    }

    /**
     * 更新公告，只能修改"编辑中"或"已发布"的公告，"已发布"的公告修改后会改为"编辑中"
     *
     * @param userDetails 操作用户
     * @param dto         表单数据
     * @param id          ID
     * @return 公告
     */
    @PatchMapping("/{id}")
    @PreAuthorize("hasAuthority('message:bulletin:update')")
    public Boolean update(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody BulletinDto dto, @PathVariable("id") Long id) {
        return service.update(userDetails, dto, id);
    }

    /**
     * 切换公告置顶状态
     *
     * @param id ID
     * @return 公告
     */
    @PatchMapping("/{id}/toggle-top")
    @PreAuthorize("hasAuthority('message:bulletin:update')")
    public Boolean toggleTop(@PathVariable("id") Long id) {
        return service.toggleTop(id);
    }

    /**
     * 发布公告，只能发布"编辑中"的公告
     *
     * @param userDetails 操作用户
     * @param ids          IDs
     * @return 公告
     */
    @PatchMapping("/publish")
    @PreAuthorize("hasAuthority('message:bulletin:update')")
    public Boolean publish(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody List<Long> ids) {
        return service.publish(userDetails, ids);
    }

    /**
     * 关闭公告，只能关闭"已发布"的公告
     *
     * @param userDetails 操作用户
     * @param id          ID
     * @return 公告
     */
    @PatchMapping("/{id}/toggleClose")
    @PreAuthorize("hasAuthority('message:bulletin:update')")
    public Boolean toggleClose(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
        return service.close(userDetails, id);
    }

    /**
     * 删除公告，只能删除"编辑中"的公告
     *
     * @param id ID
     * @return 公
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('message:bulletin:delete')")
    public Boolean delete(@PathVariable("id") Long id) {
        return service.delete(id);
    }

}
