package jnpf.notice.controller.web;


import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.model.notice.domain.FtbNoticeManager;
import jnpf.model.notice.dto.FtbNoticeManagerDto;
import jnpf.model.notice.dto.NoticeManagerAuthorityDto;
import jnpf.model.notice.req.manager.AddNoticeManagerReq;
import jnpf.model.notice.req.manager.QueryNoticeManagerReq;
import jnpf.notice.service.FtbNoticeManagerService;
import jnpf.util.FtbUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * web公告管理员模块
 *
 * @author makejava
 * @since 2024-05-08 09:51:36
 */
@RestController
@RequestMapping("/web/noticeManager")
public class FtbNoticeManagerController {
    /**
     * 服务对象
     */
    @Autowired
    private FtbNoticeManagerService ftbNoticeManagerService;


    /**
     * 查询管理员列表
     *
     * @param req
     * @return {@link ActionResult}<{@link FtbNoticeManagerDto}>
     */
    @GetMapping("/queryList")
    public ActionResult<PageListVO<FtbNoticeManagerDto>> pageLists(@Validated QueryNoticeManagerReq req) {
        PageInfo<FtbNoticeManagerDto> pageVo = ftbNoticeManagerService.getPageLists(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }


    /**
     * 添加管理员
     *
     * @param req 参数
     * @return {@link ActionResult}<{@link String}>
     */
    @PostMapping("/add")
    public ActionResult<Boolean> add(@Validated @RequestBody AddNoticeManagerReq req) {
        ftbNoticeManagerService.insertData(req);
        return ActionResult.success("添加成功");
    }


    /**
     * 删除管理员
     *
     * @param id 管理员表主键id
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult<Boolean> delete(@PathVariable("id") String id) {
        ftbNoticeManagerService.deleteData(id);
        return ActionResult.success("删除成功", true);
    }


    /**
     * 查询当前用户是否是通知公告管理员
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/queryMyNoticeManagerAuthority")
    public ActionResult<NoticeManagerAuthorityDto> queryMyNoticeManagerAuthority() {
        UserInfo userInfo = UserProvider.getUser();
        NoticeManagerAuthorityDto noticeManagerAuthorityDto = new NoticeManagerAuthorityDto();
        if (StringUtil.isEmpty(userInfo.getUserId())) {
            noticeManagerAuthorityDto.setIsNoticeManager(false);
            return ActionResult.success("成功", noticeManagerAuthorityDto);
        }
        if (userInfo.getIsAdministrator()) {
            noticeManagerAuthorityDto.setIsNoticeManager(true);
            return ActionResult.success("成功", noticeManagerAuthorityDto);
        }


        //非系统管理员 判断是否添加了通知公告权限
        FtbNoticeManager ftbNoticeManager = ftbNoticeManagerService.queryByUserId(userInfo.getUserId());
        if (null != ftbNoticeManager) {
            noticeManagerAuthorityDto.setIsNoticeManager(true);
            return ActionResult.success("成功", noticeManagerAuthorityDto);
        }

        noticeManagerAuthorityDto.setIsNoticeManager(false);
        return ActionResult.success("成功", noticeManagerAuthorityDto);

    }


}

