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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.notice.EduNoticeBuy;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.news.service.EduNoticeBuyService;
import com.yida.data.school.vo.news.BuyNoticePageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author ccl
 */
@Api(tags = "通知咨询-购买通知")
@AllArgsConstructor
@RestController
@RequestMapping("/news/buy")
public class EduNoticeBuyController {
    private final EduNoticeBuyService eduNoticeBuyService;


    @ApiOperation("分页查询购买通知")
    @GetMapping("/listBuyNoticePage")
    public ResultBean<IPage<BuyNoticePageVO>> listBuyNoticePage(@ApiParam("区域id") Long areaId,
                                                                @ApiParam("学校id") Long schoolId,
                                                                @ApiParam("校区id") Long campsId,
                                                                @ApiParam("通知状态，0-待发送，1-已发送，2-已撤回") Integer status,
                                                                @ApiParam("通知标题") String title,
                                                                @ApiParam("发送时间") String sendDate,
                                                                @RequestParam(defaultValue = "1") Integer current,
                                                                @RequestParam(defaultValue = "10") Integer size) {
        Page<EduNoticeBuy> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        IPage<BuyNoticePageVO> eduNoticeBuyPage = eduNoticeBuyService.listBuyNoticePage(page, areaId, schoolId, campsId, status, title, sendDate, FebsUtil.getCurrentUser());
        return ResultBean.buildSuccess(eduNoticeBuyPage);
    }

    @ApiOperation("新增或修改购买通知")
    @PostMapping("/saveBuyNotice")
    public ResultBean saveBuyNotice(@RequestBody EduNoticeBuy eduNoticeBuy) {

        CurrentUser currentUser = FebsUtil.getCurrentUser();
        eduNoticeBuy.setCreateName(Objects.requireNonNull(currentUser).getUsername());
        eduNoticeBuyService.saveBuyNotice(eduNoticeBuy);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("定时发送购买通知")
    @GetMapping("/updateBuyNoticeById")
    public ResultBean updateBuyNoticeById(@RequestParam Long eduNoticeBuyId) {
        eduNoticeBuyService.updateBuyNoticeById(eduNoticeBuyId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询通知详情")
    @GetMapping("/getBuyNoticeById")
    public ResultBean<EduNoticeBuy> getBuyNoticeById(@RequestParam Long eduNoticeBuyId) {
        EduNoticeBuy buyNoticeById = eduNoticeBuyService.getBuyNoticeById(eduNoticeBuyId);

        return ResultBean.buildSuccess(buyNoticeById);
    }

    @ApiOperation("删除通知")
    @GetMapping("/delBuyNoticeBatch")
    public ResultBean delBuyNoticeBatch(@RequestParam Long eduNoticeBuyId) {

        eduNoticeBuyService.delBuyNotice(eduNoticeBuyId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("再次通知")
    @GetMapping("/sendBuyNoticeAgain")
    public ResultBean sendBuyNoticeAgain(@RequestParam Long eduNoticeBuyId) {

        eduNoticeBuyService.sendBuyNoticeAgain(eduNoticeBuyId);
        return ResultBean.buildSuccess();
    }
}
