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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.notice.dto.BulletinDto;
import com.zsc.edu.gateway.modules.notice.entity.Bulletin;
import com.zsc.edu.gateway.modules.notice.query.BulletinQuery;
import com.zsc.edu.gateway.modules.notice.service.BulletinService;
import com.zsc.edu.gateway.modules.notice.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;
import java.util.Set;

/**
 * 公告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 List<BulletinVo> selfDetail(@AuthenticationPrincipal UserDetailsImpl userDetails, @PathVariable("id") Long id) {
        return service.detail(userDetails,id, Bulletin.State.publish);
    }

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

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

    /**
     * 管理员分页查询公告
     *
     * @param query    查询参数
     * @return 分页数据
     */
    @GetMapping()
    @PreAuthorize("hasAuthority('BULLETIN_QUERY')")
    public IPage<BulletinVo> query( BulletinQuery query) {
        Page<BulletinVo> page = new Page<>(query.getPageNum(), query.getPageSize());
        return service.selectPageByConditions(page, query);
    }

    /**
     * 创建公告
     *
     * @param userDetails 操作用户
     * @param dto         表单数据
     * @return 公告
     */
    @PostMapping
    @PreAuthorize("hasAuthority('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('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('BULLETIN_UPDATE')")
    public Boolean toggleTop(@PathVariable("id") Long id) {
        return service.toggleTop(id);
    }

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

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

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

    /**
     *为公告添加附件
     */
    @PostMapping("/{id}/add")
    @PreAuthorize("hasAuthority('BULLETIN_CREATE')")
    public Boolean insertInto(@PathVariable Long id,@RequestBody Set<String> attachments){
        return service.insertInto(id, attachments);
    }
}
