package com.yida.data.school.news.controller.h5;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.notice.EduNoticeSchool;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.news.service.EduNoticeSchoolService;
import com.yida.data.school.vo.news.SchollNoticeTeacherDraftVO;
import com.yida.data.school.vo.news.SchollNoticeTeacherPublishVO;
import com.yida.data.school.vo.news.SchoolNoticeInfoByParentVO;
import com.yida.data.school.vo.news.SchoolNoticeReceiveParentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.time.LocalDateTime;
import java.util.Collections;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "学校公告(前台)")
@Slf4j
@Validated
@RestController
@RequestMapping("/h5/news/school")
@RequiredArgsConstructor
public class H5EduNoticeSchoolController {

    private final EduNoticeSchoolService eduNoticeSchoolService;

    @ApiOperation("分页查询教职工发布的通知列表")
    @GetMapping("/listPublishNoticeByStaffPage")
    public ResultBean<IPage<SchollNoticeTeacherPublishVO>> listPublishNoticeByStaffPage(
        @ApiParam("公告标题") @RequestParam(required = false) String title,
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        return ResultBean.buildSuccess(eduNoticeSchoolService
            .listPublishNoticeByStaffPage(currentUser.getDeptId(), currentUser.getUserId(), title, pageNum, pageSize));
    }

    @ApiOperation("分页查询教职工创建的草稿")
    @GetMapping("/listDraftNoticeByStaffPage")
    public ResultBean<IPage<SchollNoticeTeacherDraftVO>> listDraftNoticeByStaffPage(
        @ApiParam("公告标题提") @RequestParam(required = false) String title,
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        return ResultBean.buildSuccess(eduNoticeSchoolService
            .listDraftNoticeByStaffPage(currentUser.getDeptId(), currentUser.getUserId(), title, pageNum, pageSize));
    }

    @ApiOperation("查询通知详情")
    @GetMapping("/getInfo")
    public ResultBean<EduNoticeSchool> getInfo(Long id) {
        return ResultBean.buildSuccess(eduNoticeSchoolService.getNoticeInfo(id));
    }


    @ApiOperation("保存公告")
    @PostMapping("/saveNotice")
    public ResultBean saveNotice(@RequestBody EduNoticeSchool noticeSchool) {
        noticeSchool.setLastEditTime(LocalDateTime.now());
        eduNoticeSchoolService.saveNotice(noticeSchool, FebsUtil.getCurrentUser());
        return ResultBean.buildSuccess();
    }

    @ApiOperation("提醒未确认家长")
    @GetMapping("/retrySendNotice")
    public ResultBean retrySendNotice(Long noticeId) {
        eduNoticeSchoolService.retrySendNotice(noticeId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询待接收家长列表")
    @GetMapping("/listReceiveParent")
    public ResultBean<IPage<SchoolNoticeReceiveParentVO>> listReceiveParent(Long noticeId,
        @ApiParam("家长名称") @RequestParam(required = false) String name,
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return ResultBean.buildSuccess(eduNoticeSchoolService.listReceiveParent(noticeId, name, pageNum, pageSize));
    }

    @ApiOperation("撤回通知")
    @GetMapping("/recallNotice")
    public ResultBean<Long> recallNotice(Long noticeId) {
        return ResultBean.buildSuccess(eduNoticeSchoolService.recallNotice(noticeId));
    }

    @ApiOperation("删除学校通知")
    @GetMapping("/delNotice")
    public ResultBean delNotice(Long noticeId) {
        eduNoticeSchoolService.removeNotice(Collections.singletonList(noticeId));
        return ResultBean.buildSuccess();
    }

    @ApiOperation("家长获取通知详情(阅读埋点)")
    @GetMapping("/getNoticeInfoByParent")
    public ResultBean<SchoolNoticeInfoByParentVO> getNoticeInfoByParent(Long id,
        @ApiParam("家长id") @RequestParam(required = false) Long parentId) {
        return ResultBean.buildSuccess(eduNoticeSchoolService.getNoticeInfoByParent(id, parentId));
    }

}
