package com.uas.platform.b2b.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uas.platform.b2b.model.PurchaseNotice;
import com.uas.platform.b2b.model.SaleSend;
import com.uas.platform.b2b.model.SaleSendItem;
import com.uas.platform.b2b.service.PurchaseNoticeService;
import com.uas.platform.b2b.support.SystemSession;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.PageParams;
import com.uas.platform.core.model.Status;
import com.uas.platform.core.util.serializer.FlexJsonUtils;
import com.uas.platform.core.web.bind.RequestState;

/**
 * 卖家对送货提醒的操作
 * 
 * @author yingp
 * 
 */
@Controller
@RequestMapping("/sale/notice")
public class SaleNoticeController {

	@Autowired
	private PurchaseNoticeService purchaseNoticeService;

	/**
	 * 作为卖家，收到的送货提醒
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseNotice> getReceivedPurchaseNotices(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseNoticeService.findAllByPageInfo(info);
	}

	/**
	 * 作为卖家，收到的送货提醒，对应id
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public PurchaseNotice getReceivedPurchaseNoticeById(@PathVariable("id") Long id) {
		return purchaseNoticeService.findById(id);
	}

	/**
	 * 作为卖家，收到的送货提醒，对应id
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/sendItems", method = RequestMethod.GET)
	@ResponseBody
	public List<SaleSendItem> getSendItemsNoticeById(@PathVariable("id") Long id) {
		return purchaseNoticeService.findSendById(id);
	}

	/**
	 * 作为卖家，收到的送货提醒(待发货)
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(params = RequestState.TODO, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseNotice> getTodoPurchaseNotices(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 询价单状态为未回复
		info.filter("status", Status.NOT_REPLY.value());
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseNoticeService.findAllByPageInfo(info);
	}
	
	/**
	 * 作为卖家，收到的送货提醒(已发货)
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(params = RequestState.DONE, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseNotice> getDonePurchaseNotices(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseNoticeService.findDoneByPageInfo(info);
	}

	/**
	 * 作为卖家，收到的送货提醒(已结案)
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(params = RequestState.END, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseNotice> getEndPurchaseNotices(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 询价单状态为已结案
		info.filter("end", (short) Constant.YES);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseNoticeService.findAllByPageInfo(info);
	}

	/**
	 * 作为卖家，针对送货提醒发货
	 * 
	 * @param noticeId
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/{noticeId}", method = RequestMethod.POST)
	public ResponseEntity<String> send(@PathVariable("noticeId") Long noticeId, @RequestBody String json) {
		SaleSend send = FlexJsonUtils.fromJson(json, SaleSend.class);
		purchaseNoticeService.send(noticeId, send);
		return new ResponseEntity<String>(HttpStatus.OK);
	}

}
