package com.xhkjedu.sapi.controller.notice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sapi.model.notice.TNotice;
import com.xhkjedu.sapi.model.notice.TNoticeUser;
import com.xhkjedu.sapi.service.notice.NoticeService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.sapi.vo.notice.NoticeVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author ywx
* @classname TNoticeController
* @description
* @date 2020/06/12 17:45
**/
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    @PostMapping("/add")
    @Action("通知管理-增加")
    public ResultVo add(@RequestBody TNotice tNotice) {
        Integer id = noticeService.save(tNotice);
        return new ResultVo(0, "保存成功",id);
    }

    @PostMapping("/delete")
    @Action("通知管理-删除")
    public ResultVo delete(@RequestBody TNotice tNotice) {
        N_Utils.validation(new Object[]{tNotice.getNoticeid(),"通知id",1});
        noticeService.deleteById(tNotice.getNoticeid());
        return new ResultVo(0,"删除成功");
    }

    @PostMapping("/update")
    @Action("通知管理-修改")
    public ResultVo update(@RequestBody TNotice tNotice) {
        N_Utils.validation(new Object[]{tNotice.getNoticeid(),"通知id",1});
        noticeService.update(tNotice);
        return new ResultVo(0,"修改成功");
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TNotice tNotice) {
        N_Utils.validation(new Object[]{tNotice.getNoticeid(),"通知id",1});
        TNotice notice = noticeService.findById(tNotice.getNoticeid());
        return new ResultVo(0,"获取详情成功",notice);
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody TNotice tNotice) {
        PageHelper.startPage(tNotice.getPage(), tNotice.getPageSize());
        List<NoticeVo> list = noticeService.listAll(tNotice);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取管理端通知列表成功",pageResult);
    }

    @PostMapping("/list_my")
    public ResultVo listMy(@RequestBody TNotice tNotice) {
        Integer pageSize = tNotice.getPageSize();
        Integer createid = tNotice.getCreateid();
        Integer schoolid = tNotice.getSchoolid();
        N_Utils.validation(new Object[]{pageSize,"显示条数",1,createid,"用户id",1,schoolid,"学校id",1});
        List<NoticeVo> list = noticeService.listMy(tNotice);
        return new ResultVo(0,"获取用户通知列表成功",list);
    }

    //通知列表
    @PostMapping("/list_my_all")
    public ResultVo listMyAll(@RequestBody TNotice tNotice) {
        Integer page = tNotice.getPage();
        Integer pageSize = tNotice.getPageSize();
        Integer createid = tNotice.getCreateid();
        Integer schoolid = tNotice.getSchoolid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,createid,"用户id",1,schoolid,"学校id",1});
        PageHelper.startPage(page, pageSize);
        List<NoticeVo> list = noticeService.listMy(tNotice);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取我的所有通知列表成功",pageResult);
    }

    @PostMapping("/list_student")
    public ResultVo listStudent(@RequestBody TNotice tNotice) {
        Integer page = tNotice.getPage();
        Integer pageSize = tNotice.getPageSize();
        Integer createid = tNotice.getCreateid();
        Integer schoolid = tNotice.getSchoolid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,createid,"学生id",1,schoolid,"学校id",1});
        PageHelper.startPage(page, pageSize);
        List<NoticeVo> list = noticeService.listStudent(tNotice);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取学生通知列表成功",pageResult);
    }

    @PostMapping("/readed")
    public ResultVo readed(@RequestBody TNotice tNotice) {
        Integer noticeid = tNotice.getNoticeid();
        Integer createid = tNotice.getCreateid();
        N_Utils.validation(new Object[]{noticeid,"通知id",1,createid,"用户id",1});
        TNotice notice = noticeService.reade(noticeid, createid);
        return new ResultVo(0,"用户查看成功", notice);
    }

    @PostMapping("/student_readed")
    public ResultVo studentReaded(@RequestBody TNotice tNotice) {
        Integer noticeid = tNotice.getNoticeid();
        N_Utils.validation(new Object[]{noticeid,"通知id",1});
        TNotice notice = noticeService.findById2(noticeid);
        return new ResultVo(0,"学生查看成功", notice);
    }

    @PostMapping("/no_read_num")
    public ResultVo noReadNum(@RequestBody TNotice tNotice) {
        Integer createid = tNotice.getCreateid();
        N_Utils.validation(new Object[]{createid,"用户id",1});
        Integer num = noticeService.noReadNum(createid);
        return new ResultVo(0,"获取用户未看通知数量成功",num);
    }

    /**
     *功能描述 学生--网页--列表
     * @author WN
     * @date 2022/2/22
     * @param  * @param notice
     * @return com.xhkjedu.vo.ResultVo
     */
    @PostMapping("/stw_notice")
    public ResultVo listNoticeStuWeb(@RequestBody TNotice notice){
        Integer page = notice.getPage();
        Integer pageSize = notice.getPageSize();
        Integer createid = notice.getCreateid();
        Integer schoolid = notice.getSchoolid();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1,createid,"学生id",1,schoolid,"学校id",1});
        PageHelper.startPage(page, pageSize);
        List<NoticeVo> list = noticeService.listNoticeStuWeb(notice);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取学生通知列表成功",pageResult);
    }

    /**
     * @Description 通知对象列表
     * @Date 2023/12/22 9:39
     * @Author YWX
     * @Param [nu]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_user")
    public ResultVo listUser(@RequestBody TNoticeUser nu) {
        Integer noticeid = nu.getNoticeid();
        N_Utils.validation(new Object[]{noticeid, "通知id", 1});
        List<Map> list = noticeService.listUser(nu);
        return new ResultVo(0, "获取通知对象成功", list);
    }

    /**
     * @Description 通知对象分页列表
     * @Date 2023/12/22 9:39
     * @Author YWX
     * @Param [nu]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_userp")
    public ResultVo listUserPaging(@RequestBody TNoticeUser nu) {
        Integer noticeid = nu.getNoticeid();
        Integer page = nu.getPage();
        Integer pageSize = nu.getPageSize();
        N_Utils.validation(new Object[]{noticeid, "通知id", 1, page, "显示页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map> list = noticeService.listUser(nu);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        List<Map> list1 = (List<Map>) pageResult.getList();
        Map<Object, List<Map>> gMap = list1.stream().collect(Collectors.groupingBy(m -> m.get("gradeid")));
        List<Map<String, Object>> grades = new ArrayList<>();
        for (Map.Entry<Object, List<Map>> entry : gMap.entrySet()) {
            Map<String, Object> g = new LinkedHashMap<>();
            g.put("gradeid", entry.getKey());
            g.put("subjectClass", entry.getValue());
            grades.add(g);
        }
        pageResult.setList(grades);
        return new ResultVo(0, "获取通知对象成功", pageResult);
    }

    /**
     * @Description 获取通知科目和班级
     * @Date 2023/12/25 14:20
     * @Author YWX
     * @Param [nu]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_sc")
    public ResultVo listSubjectAndClass(@RequestBody TNoticeUser nu) {
        Integer noticeid = nu.getNoticeid();
        N_Utils.validation(new Object[]{noticeid, "通知id", 1});
        Map map = noticeService.listSubjectAndClass(noticeid);
        return new ResultVo(0, "获取通知科目和班级成功", map);
    }

    /*
     * @Description 处理班级通知到学生个人
     * @Date 2023/12/18 17:41:09
     * @Author WN
     * @Param []
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @GetMapping("/chandle_nuser")
    public ResultVo chandleNoticeUser() {
        noticeService.chandleNoticeUser();
        return new ResultVo(0, "处理成功");
    }

    @PostMapping("/isexits")
    public ResultVo isExits(@RequestBody TNotice notice) {
        N_Utils.validation(new Object[]{notice.getNoticeid(),"通知id",1});
        return new ResultVo(0, "获取成功",noticeService.getNoticeid(notice.getNoticeid()));
    }

    @PostMapping("/read_teacher")
    public ResultVo noticeReadTeacher(@RequestBody TNoticeUser noticeUser) {
        N_Utils.validation(new Object[]{noticeUser.getNoticeid(),"通知id",1});
        return new ResultVo(0, "获取成功",noticeService.noticeReadTeacher(noticeUser));
    }

    @PostMapping("/read_student")
    public ResultVo noticeReadStudent(@RequestBody TNoticeUser noticeUser) {
        N_Utils.validation(new Object[]{noticeUser.getNoticeid(),"通知id",1});
        return new ResultVo(0, "获取成功",noticeService.noticeReadStudent(noticeUser));
    }

    /*
     * @Description 学生通知科目
     * @Date 2024/7/9 16:37:52
     * @Author WN
     * @Param [noticeUser]
     * @Return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/subject_student")
    public ResultVo noticeSubjectStudent(@RequestBody TNoticeUser noticeUser) {
        N_Utils.validation(new Object[]{noticeUser.getStudentid(),"学生id",1});
        return new ResultVo(0, "获取成功",noticeService.listNoticeSubjects(noticeUser.getStudentid()));
    }


}
