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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.entity.school.EduSchoolManuscript;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.dto.news.ListNoticeDTO;
import com.yida.data.school.news.service.EduNoticeSchoolService;
import com.yida.data.school.vo.news.SchoolNoticePageVO;
import com.yida.data.system.feign.RemoteDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.search.fetch.subphase.FetchDocValuesContext.FieldAndFormat;
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 = "通知咨询-学校通知")
@RestController
@RequestMapping("/news/school")
@RequiredArgsConstructor
public class EduNoticeSchoolController {

    private final EduNoticeSchoolService eduNoticeSchoolService;

    private final RemoteDeptService remoteDeptService;

    @ApiOperation("保存、编辑学校通知")
    @PostMapping("/saveNotice")
    public ResultBean saveNotice(@RequestBody EduNoticeSchool noticeSchool) {
        eduNoticeSchoolService.saveNotice(noticeSchool, FebsUtil.getCurrentUser());
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除学校通知")
    @PostMapping("/delNoticeBatch")
    public ResultBean delNoticeBatch(@RequestBody List<Long> noticeIds) {
        eduNoticeSchoolService.removeNotice(noticeIds);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("分页查询学校通知")
    @GetMapping("/listNoticePage")
    public ResultBean<IPage<SchoolNoticePageVO>> listNoticePage(ListNoticeDTO dto) {
        if (Objects.isNull(dto.getSchoolId())) {
            dto.setSchoolId(FebsUtil.getCurrentUser().getDeptId());
        }
        return ResultBean.buildSuccess(eduNoticeSchoolService.listNoticePage(dto));
    }

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

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

    @ApiOperation("确认通知")
    @GetMapping("/confirmNotice")
    public ResultBean confirmNotice(Long parentId, Long noticeId) {
        eduNoticeSchoolService.confirmNotice(parentId, noticeId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("个人查询显示的通知列表")
    @GetMapping("/listShowNoticePage")
    public ResultBean<IPage<SchoolNoticePageVO>> listShowNoticePage(@ApiParam("家长id") Long parentId,
        @ApiParam("0-个人公告，1-公开公告") Integer isPublic,
        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return ResultBean.buildSuccess(eduNoticeSchoolService
            .listShowNoticePage(FebsUtil.getCurrentUser().getDeptId(), parentId, isPublic, pageNum, pageSize));
    }

    @ApiOperation("查询公告列表")
    @GetMapping("/listNotice")
    public ResultBean<List> listNotice(ListNoticeDTO dto) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getDeptType() == 0) {
            dto.setSchoolId(currentUser.getDeptId());
        }
        return ResultBean.buildSuccess(eduNoticeSchoolService.listNotice(dto));
    }

    @ApiOperation("查询公告数量")
    @GetMapping("/countNotice")
    public ResultBean<Integer> countNotice(String startDate, String endDate) {
        List<Long> schoolIds = remoteDeptService.getSchoolByArea(null, null).getData().stream().map(Dept::getDeptId).collect(
            Collectors.toList());
        return ResultBean.buildSuccess(
            CollUtil.isNotEmpty(schoolIds) ? (int) eduNoticeSchoolService.count(Wrappers.<EduNoticeSchool>lambdaQuery()
                .eq(EduNoticeSchool::getIsDraft, 0)
                .in(EduNoticeSchool::getSchoolId, Arrays.asList(schoolIds))
                .between(EduNoticeSchool::getCreateDate, LocalDateTimeUtil.parseDate(startDate).atStartOfDay(),
                    LocalDateTimeUtil.parseDate(endDate).atTime(LocalTime.MAX))) : 0);
    }
}
