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

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.apply.EduProductNotice;
import com.yida.data.school.transaction.service.EduProductNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品催缴通知策略表 Controller
 *
 * @author wjm
 * @date 2021-09-01 18:23:14
 */
@Api(tags = "交易-商品催缴策略")
@Slf4j
@Validated
@RestController
@RequestMapping("/transaction/productNotice")
@RequiredArgsConstructor
public class EduProductNoticeController {

    private final EduProductNoticeService eduProductNoticeService;

    @ApiOperation("新增或修改商品催缴策略")
    @PostMapping("/insertNotice")
    public ResultBean insertNotice(@RequestBody List<EduProductNotice> eduProductNotice) {
        eduProductNoticeService.insertNotice(eduProductNotice);

        return ResultBean.buildSuccess();
    }

    @ApiOperation("清空商品催缴策略")
    @GetMapping("/removeNotice")
    public ResultBean removeNotice(@RequestParam Long productId) {

        eduProductNoticeService.remove(Wrappers.lambdaQuery(new EduProductNotice())
                .eq(EduProductNotice::getProductId, productId).isNotNull(EduProductNotice::getSendIntervalDate));

        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询商品催缴策略")
    @GetMapping("/selectNotice")
    public ResultBean<List<EduProductNotice>> selectNotice(@RequestParam Long productId) {

        return ResultBean.buildSuccess(eduProductNoticeService.list(Wrappers.lambdaQuery(new EduProductNotice())
                .eq(EduProductNotice::getProductId, productId).isNotNull(EduProductNotice::getSendIntervalDate)));
    }

    @ApiOperation("查询催缴列表")
    @GetMapping("/findNotice")
    public ResultBean<IPage<EduProductNotice>> findNotice(@RequestParam Long productId,
                                                          @RequestParam(defaultValue = "10") Integer size,
                                                          @RequestParam(defaultValue = "1") Integer pageNum) {
        Page page = new Page();
        page.setSize(size);
        page.setCurrent(pageNum);
        return ResultBean.buildSuccess(eduProductNoticeService.page(page, Wrappers.lambdaQuery(new EduProductNotice())
                .eq(EduProductNotice::getProductId, productId)));
    }
}
