package com.jiayue.ipfcst.console.controller;

import com.jiayue.ipfcst.aop.SaveValidate;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.HolidayCalendar;
import com.jiayue.ipfcst.console.service.HolidayCalendarService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 假期日历测试restful接口
 *
 * @author tl
 * @version 3.0
 * @since 2020/6/28 10:12
 */
@RestController
@RequestMapping(value = "holidayCalendar")
@Slf4j
public class HolidayCalendarController {
  private final HolidayCalendarService holidayCalendarService;

  @Autowired
  public HolidayCalendarController(HolidayCalendarService holidayCalendarService) {
    this.holidayCalendarService = holidayCalendarService;
  }

  /**
   * 新增假日日历
   * @param holidayCalendar 假期实体参数
   * @return 状态
   */
  @SneakyThrows
  @PostMapping()
  @SaveValidate
  public ResponseVO add(@RequestBody
                          final HolidayCalendar holidayCalendar) {
    this.holidayCalendarService.save(holidayCalendar);
    return ResponseVO.success(1);
  }

  /**
   * 修改假期日历
   * @param holidayCalendar 假期实体参数
   * @return 状态
   */
  @SneakyThrows
  @PutMapping()
  @SaveValidate
  public ResponseVO update(@RequestBody
                             HolidayCalendar holidayCalendar) {
    this.holidayCalendarService.update(holidayCalendar);
    return ResponseVO.success(1);
  }

  /**
   * 删除假期日历
   * @param holidayCalendar 假期实体参数
   * @return 状态
   */
  @SneakyThrows
  @DeleteMapping()
  @SaveValidate
  public ResponseVO delete(@RequestBody
                             HolidayCalendar holidayCalendar) {
    this.holidayCalendarService.delete(holidayCalendar.getId());
    return ResponseVO.success(1);
  }

  /**
   * 分页获取假期日历
   *
   * @param page 页码
   * @param size 条数
   * @return 分页数据
   */
  @SneakyThrows
  @GetMapping(value = "/{page}/{size}")
  public ResponseVO get(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
    HolidayCalendar holidayCalendar = new HolidayCalendar();
    Page<HolidayCalendar> holidayCalendarPage = this.holidayCalendarService.get(holidayCalendar, page, size);
    return ResponseVO.success(holidayCalendarPage);
  }

  /**
   * 获取所有假期日历信息
   *
   * @return 假期日历信息
   */
  @SneakyThrows
  @GetMapping()
  public ResponseVO get() {
    List<HolidayCalendar> list = this.holidayCalendarService.getAll();
    return ResponseVO.success(list);
  }
}
