package com.xhkjedu.sapi.controller.notice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sapi.model.notice.TNotice;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

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

    @PostMapping("/add")
    public ResultVo add(@RequestBody TNotice tNotice) {
        Integer id = noticeService.save(tNotice);
        return new ResultVo(0, "保存成功",id);
    }

    @PostMapping("/delete")
    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")
    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);
    }
}
