package com.uas.platform.b2b.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.HttpStatus;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.uas.platform.b2b.model.Notice;
import com.uas.platform.b2b.service.NoticeService;
import com.uas.platform.b2b.service.PurchaseInquiryService;
import com.uas.platform.b2b.service.PurchaseNoticeService;
import com.uas.platform.b2b.service.PurchaseOrderChangeService;
import com.uas.platform.b2b.service.PurchaseOrderService;
import com.uas.platform.core.model.PageInfo;

/**
 * 平台消息、任务快照
 * 
 * @author yingp
 *
 */
@RestController
@RequestMapping("/snapshot")
public class SnapshotController {

	@Autowired
	private PurchaseInquiryService purchaseInquiryService;

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	@Autowired
	private PurchaseOrderChangeService purchaseOrderChangeService;

	@Autowired
	private PurchaseNoticeService purchaseNoticeService;

	@Autowired
	private NoticeService noticeService;

	/**
	 * 统计待处理事项
	 * 
	 * @return
	 */
	@RequestMapping(value = "/todo", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	@ResponseStatus(value = HttpStatus.OK)
	public ModelMap getTodo() {
		ModelMap modelMap = new ModelMap();
		modelMap.put("inquiry", purchaseInquiryService.getOnhandCount());
		modelMap.put("order", purchaseOrderService.getOnhandCount());
		modelMap.put("orderChange", purchaseOrderChangeService.getOnhandCount());
		modelMap.put("notice", purchaseNoticeService.getOnhandCount());
		return modelMap;
	}

	/**
	 * 系统公告（取前count条）
	 * 
	 * @param count
	 * @return
	 */
	@RequestMapping(value = "/notice", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	@ResponseStatus(value = HttpStatus.OK)
	public List<Notice> getNoticesSnapshot(@RequestParam("count") int count) {
		PageInfo info = new PageInfo(1, count, 0);
		info.sorting("date", Direction.DESC);
		return noticeService.findAllByPageInfo(info).getContent();
	}

}
