package com.uas.platform.b2b.controller;

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.PurchaseOrderChange;
import com.uas.platform.b2b.service.PurchaseOrderChangeService;
import com.uas.platform.b2b.support.SystemSession;
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/changes")
public class SaleOrderChangeController {

	@Autowired
	private PurchaseOrderChangeService purchaseOrderChangeService;

	/**
	 * 作为卖家，收到的采购变更单（全部）
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseOrderChange> getReceivedPurchaseOrderChanges(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("order.vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderChangeService.findAllByPageInfo(info);
	}

	/**
	 * 作为卖家，收到的采购变更单（待处理）
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(params = RequestState.TODO, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseOrderChange> getTodoPurchaseOrderChanges(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 变更单状态为未回复
		info.filter("status", Status.NOT_REPLY.value());
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("order.vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderChangeService.findAllByPageInfo(info);
	}
	
	/**
	 * 作为卖家，收到的采购变更单（已处理）
	 * 
	 * @param json
	 * @return
	 */
	@RequestMapping(params = RequestState.DONE, method = RequestMethod.GET)
	@ResponseBody
	public Page<PurchaseOrderChange> getDonePurchaseOrderChanges(PageParams params) {
		PageInfo info = new PageInfo(params);
		// 变更单状态为已回复
		info.filter("status", Status.REPLIED.value());
		// 我作为卖家，把我的企业ID作为供应商ID传入
		info.filter("order.vendUU", SystemSession.getUser().getEnterprise().getUu());
		return purchaseOrderChangeService.findAllByPageInfo(info);
	}

	/**
	 * 作为卖家，根据变更单ID查找采购变更单(含明细)
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public PurchaseOrderChange getReceivedPurchaseOrderItemById(@PathVariable("id") Long id) {
		return purchaseOrderChangeService.findById(id);
	}

	/**
	 * 作为卖家，回复收到的采购变更单
	 * 
	 * @param json
	 * @param changeId
	 * @return
	 */
	@RequestMapping(value = "/{changeId}/reply", method = RequestMethod.POST)
	public ResponseEntity<String> replyOrderItem(@RequestBody String json, @PathVariable("changeId") Long changeId) {
		PurchaseOrderChange change = FlexJsonUtils.fromJson(json, PurchaseOrderChange.class);
		purchaseOrderChangeService.reply(change);
		return new ResponseEntity<String>(HttpStatus.OK);
	}

}
