package com.iamberry.wechat.service.drp;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.PageBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.admin.ShopSystemRule;
import com.iamberry.wechat.core.entity.drp.PlaceOrder;
import com.iamberry.wechat.core.entity.product.Product;
import com.iamberry.wechat.face.admin.SystemService;
import com.iamberry.wechat.face.drp.PlaceOrderService;
import com.iamberry.wechat.service.mapper.PlaceOrderMapper;
import com.iamberry.wechat.service.mapper.ProductInfoMapper;
import com.iamberry.wechat.tools.OrderNOUtil;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.StaticInfo;

/**
 * 
 * @author 欧阳明
 * @Description: 代理者订货处理业务
 * @Date:2016年4月19日
 *
 */
@Service("placeOrderServiceImpl")
public class PlaceOrderServiceImpl implements PlaceOrderService {

	@Autowired
	private PlaceOrderMapper placeOrderMapper;
	@Autowired
	private ProductInfoMapper productInfoMapper;
	
	@Autowired
	private SystemService  systemService;
	
	@Autowired
	private OrderNOUtil orderNOUtil;
	
	
	//获取订购单的paper,价格跟普通的价格不一样
	public Product getOrderPaper(int productId){
		Product product=productInfoMapper.getProductByProductId(productId);
		ShopSystemRule sr=systemService.selectOneShopRuleById(20);
		product.setProductDiscount(sr.getRuleNum().intValue());
		return product;
	}
	

	//通过id获取订货单
	public PlaceOrder getOrderPaperById(String id) {
		return placeOrderMapper.getOnePlaceOrder(id);
	}
	
	/**
	 * 提交订货订单
	 */
	public PlaceOrder submitOrder(PlaceOrder placeOrder, Integer placeId) {
		placeOrder.setId(orderNOUtil.createOrderCode(placeId));
		placeOrder.setCreateDate(new Date());
		placeOrder.setStatus(1);
		placeOrderMapper.insert(placeOrder);
		
		return placeOrder;
	}
	
	/**
	 * 对于待支付的订单可以用这个方法支付订单
	 */
	public ResultMsg payOrder(PlaceOrder placeOrder) {
		
		//这里需要调用其他的逻辑支付逻辑进行处理
		ResultMsg remsg=new ResultMsg();
		remsg.setResultCode(ResultInfo.SUCCESSCODE);
		remsg.setMessage(ResultInfo.SUCCESSINFO);
		return remsg;
	}



	/**
	 * 通过实体类placeOrder做为查询条件查询订单情况
	 * placeOrder 作为一个查询条件 已付的status为2；未付的为1；发货的为3；
	 */
	public List<PlaceOrder> getSearchPaidOrder(PlaceOrder placeOrder) {
		PageBean page=new PageBean();
		page.initRecordBegin();
		placeOrder.setPage(page);
	
		List<PlaceOrder>  placeOrders=placeOrderMapper.selectAll(placeOrder);
		//需要获取订单的商品信息
		for(PlaceOrder po:placeOrders){

			Product product=productInfoMapper.getProductByProductId(new Integer(po.getProductId()));

			product.setProductAccessnum(null);
			product.setProductParameterImg(null);
			product.setProductCreateDate(null);
			product.setProductSalesnum(null);
			po.setProduct(product);

		}
		return placeOrders.size()==0?null:placeOrders;
	}

	//获取分页的下一页信息
	public Map getSearchPaidOrderNext(PlaceOrder placeOrder) {
		HashMap map=new HashMap();
		
		PageBean page=placeOrder.getPage();
		placeOrder.getPage().initRecordBegin();
		List<PlaceOrder>  placeOrders=placeOrderMapper.selectAll(placeOrder);
		int discountMoney=systemService.selectOneShopRuleById(20).getRuleNum().intValue();
		
		//需要获取订单的商品信息
		for(PlaceOrder po:placeOrders){
			Product produt=productInfoMapper.getProductByProductId(new Integer(po.getProductId()));
			produt.setProductDiscount(discountMoney);
			po.setProduct(produt);
		}
		page.setCount(placeOrderMapper.getAllCount(placeOrder));
		map.put("list", placeOrders.size()==0?null:placeOrders);
		map.put("pageCount", page.getPageCount());
		return map;
	}

	 //更新订单的状态等信息
	public Integer UpdateOrder(PlaceOrder placeOrder) {
	     return	placeOrderMapper.update(placeOrder);
	}


	//后台管理-订货管理分页
	public Map getSearchPaidOrderAdmin(PageBean page) {
		Map hm=new HashMap();
		PageBean pageIndex=new PageBean();
		PlaceOrder placeOrder=(PlaceOrder)page.getCondition();
		String str=placeOrder.getEndDate();
		if(str!=null && !"".equals(str)){
			placeOrder.setEndDate(str+StaticInfo.endDateC);
		}
		
		pageIndex.setPageNumber(page.getPageNumber());
		pageIndex.setPageSize(page.getPageSize());
		pageIndex.initRecordBegin();
		
		placeOrder.setPage(pageIndex);
		if(page.getPageCount()>0){
			pageIndex.setCount(page.getPageCount());
		}else{
			pageIndex.setCount(placeOrderMapper.getAllCount(placeOrder));
		}
		
		pageIndex.initRecordBegin();
		List<PlaceOrder> lists=placeOrderMapper.selectAllMore(placeOrder);
        hm.put("data",lists);
		hm.put("page",pageIndex);
		return hm;
	
	}




	
}
