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

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.school.EduSchoolManuscript;
import com.yida.data.school.news.service.EduSchoolManuscriptService;
import com.yida.data.school.vo.index.ManuscriptPageInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalTime;

@Api(tags = "通知咨询-稿件")
@Slf4j
@RestController
@RequestMapping("/out/news/manuscript")
@RequiredArgsConstructor
public class OutEduManuscriptController {

    private final EduSchoolManuscriptService eduSchoolManuscriptService;

    @ApiOperation("查询校园资讯数量")
    @GetMapping("/countManuscript")
    public ResultBean<Integer> countManuscript(String startDate, String endDate, Long[] schoolIds, Integer type) {
        return ResultBean.buildSuccess(
            ArrayUtil.isNotEmpty(schoolIds) ? (int) eduSchoolManuscriptService.count(Wrappers.<EduSchoolManuscript>lambdaQuery()
                .between(EduSchoolManuscript::getCreateDate, LocalDateTimeUtil.parseDate(startDate).atStartOfDay(),
                    LocalDateTimeUtil.parseDate(endDate).atTime(LocalTime.MAX))
                .in(EduSchoolManuscript::getSchoolId, schoolIds)
                .eq(EduSchoolManuscript::getType, type)) : 0);
    }

    @ApiOperation("分页查询稿件列表")
    @GetMapping("/listManuscriptPage")
    public ResultBean<IPage<ManuscriptPageInfoVO>> listManuscriptPage(
        @ApiParam("学校id") @RequestParam(required = false) Long deptId,
        @ApiParam("关键词") @RequestParam(required = false) String keyword,
        @ApiParam("发布人") @RequestParam(required = false) String author,
        @ApiParam("发布时间(yyyy-MM-dd)") @RequestParam(required = false) String publishDate,
        @ApiParam("发布途径，0-入学早知道(城市书房)，1-第二课堂(精彩推荐)，2-公众号") @RequestParam(required = false) Integer path,
        @ApiParam("稿件类型，0-存草稿，1-待发送，2-已发送") Integer status,
        @ApiParam("不查询得稿件Id") @RequestParam(required = false) Long notShowId,
        @ApiParam("0-资讯，1-安全教育，默认0") @RequestParam(required = false, defaultValue = "0") Integer type,
        @ApiParam("标签") @RequestParam(required = false) Integer label,
        @ApiParam("当前页码，默认1") @RequestParam(defaultValue = "1") Integer pageNum,
        @ApiParam("页面大小，默认10") @RequestParam(defaultValue = "10") Integer pageSize) {
        return ResultBean.buildSuccess(eduSchoolManuscriptService.listManuscriptPage(new Page(pageNum,
            pageSize), deptId, keyword, author, publishDate, path, notShowId, type, status,label));
    }

    @ApiOperation("稿件详情")
    @GetMapping("/getManuscript")
    public ResultBean<EduSchoolManuscript> saveManuscript(@ApiParam("稿件id") Long manuscriptId) {
        return ResultBean.buildSuccess(eduSchoolManuscriptService.getInfo(manuscriptId));
    }
}
