package com.sv.oms.controller;
import com.sv.service.oms.AnnouncementService;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.oms.controller.OmsController;
import com.sv.entity.Announcement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Controller - 公告
 *
 * @author yechanglin
 * @since 2018-08-11
 */
@RestController
public class AnnouncementController extends OmsController {

	private final Logger logger = LoggerFactory.getLogger(AnnouncementController.class);
	
	@Resource
	private AnnouncementService announcementService;

	/**
	 * 分页查询公告
	 */
	@RequestMapping(value = "/announcements", method = RequestMethod.GET)
	public ResponseDTO findPage(Pagination pagination,
								@RequestParam(value = "starTime",required = false) String starTime,
								@RequestParam(value = "endTime",required = false) String endTime) {
		return ResponseDTO.ok()
				.setPagination(announcementService.findPage(pagination, starTime, endTime));
	}

	/**
	 * 查询公告信息
	 */
	@RequestMapping(value = "/announcement/{id}", method = RequestMethod.GET)
	public ResponseDTO findById(@PathVariable("id") Integer id) {
		return ResponseDTO.ok()
				.addAttribute("announcement", announcementService.findById(id));
	}

	/**
	 * 保存公告
	 */
	@RequestMapping(value = "/announcement", method = RequestMethod.POST)
	public ResponseDTO save(Announcement announcement) {
		announcementService.save(announcement);
		return ResponseDTO.ok("保存成功");
	}

	/**
	 * 删除公告
	 */
	@RequestMapping(value = "/announcement/delete", method = RequestMethod.POST)
	public ResponseDTO delete(@RequestParam("id[]") Integer[] ids) {
		announcementService.deleteByIds(ids);
		return ResponseDTO.ok("删除成功");
	}

}
