package com.fuzamei.service.serviceImpl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

import com.fuzamei.constant.OperationType;
import com.fuzamei.constant.Role;
import com.fuzamei.constant.Status;
import com.fuzamei.entity.DeliverOrder;
import com.fuzamei.entity.IssueOrder;
import com.fuzamei.entity.Orders;
import com.fuzamei.entity.Param;
import com.fuzamei.entity.SendOrder;
import com.fuzamei.entity.TallyOrder;
import com.fuzamei.entity.UserDetail;
import com.fuzamei.mapperInterface.OperationHistoryMapper;
import com.fuzamei.mapperInterface.OrdersIssueMapper;
import com.fuzamei.service.OrdersIssueService;
import com.fuzamei.util.PageDTO;

@Service
public class OrdersIssueServiceImpl implements OrdersIssueService {
	
	@Autowired
	private OrdersIssueMapper ordersIssueMapper;
	
	@Autowired
	private OperationHistoryMapper operationHistoryMapper;

	@Override
	public PageDTO queryOrdersByPlanner(Param param) {
		PageDTO pageDto=new PageDTO();
		List<IssueOrder> orderList = ordersIssueMapper.queryOrdersByPlanner(param);
		int count = ordersIssueMapper.findQueryCountByPlanner(param);
		pageDto.setRows(orderList);
		pageDto.setTotal(count);
		return pageDto;
	}
	
	@Override
	public PageDTO queryOrdersBySupplier(Param param) {
		PageDTO pageDto=new PageDTO();
		List<IssueOrder> orderList = ordersIssueMapper.queryOrdersBySupplier(param);
		int count = ordersIssueMapper.findQueryCountBySupplier(param);
		pageDto.setRows(orderList);
		pageDto.setTotal(count);
		return pageDto;
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public void consignment(Param param) {
		Long currentTime = System.currentTimeMillis();
		//更新【订单表】
		param.setSendTime(currentTime);
		param.setUpdateTime(currentTime);
		param.setStatusId(Status.UNDELIVER);//待运输状态
		ordersIssueMapper.consignment(param);
		//插入【操作记录】
		param.setOperationTypeId(OperationType.SEND);
		param.setOperatorId(param.getUserId());
		param.setOperationTime(currentTime);
		operationHistoryMapper.addToHistory(param);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void addOrder(Param param) {
		Long currentTime = System.currentTimeMillis();
		//插入【订单表】
		param.setPlannerId(param.getUserId());
		param.setRequireTime(currentTime);
		param.setCreateTime(currentTime);
		param.setUpdateTime(currentTime);
		param.setBoxQty(1);//暂时将箱子数量设置为1=======================>>TODO
		param.setStatusId(Status.UNSEND);
		ordersIssueMapper.addOrder(param);
		//插入【操作记录】
		param.setOperationTypeId(OperationType.ADD_ORDER);
		param.setOperatorId(param.getUserId());
		param.setOperationTime(currentTime);
		operationHistoryMapper.addToHistory(param);
	}

	@Override
	public Map<String, Object> orderTracking(Param param) {
		Orders order = param.getOrder();
		return dispatchOrderInfo(order);
	}
	
	/**
	 * 
	* @Title: dispatchOrderInfo
	* @Description: TODO(将order表中的所有数据分发给各个小订单表中，然后根据当时订单的状态包装成map)
	* @param @param order
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月25日 下午9:11:39
	* @throws
	 */
	public static final Map<String, Object> dispatchOrderInfo(Orders order){
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		IssueOrder issueOrder=new IssueOrder();
		issueOrder.setOrderId(order.getOrderId());
		issueOrder.setBoxNo(order.getBoxNo());
		issueOrder.setCreateTime(order.getCreateTime());
		issueOrder.setInboxQty(order.getInboxQty());
		issueOrder.setPartNo(order.getPartNo());
		issueOrder.setPlannerName(order.getPlannerName());
		issueOrder.setRequireTime(order.getRequireTime());
		issueOrder.setUpdateTime(order.getUpdateTime());
		issueOrder.setStatusName(order.getStatusName());
		issueOrder.setSupplierName(order.getSupplierName());
		map.put("issueOrder", issueOrder);
		if(!Status.UNSEND.equals(order.getStatusName())){
			SendOrder sendOrder=new SendOrder();
			sendOrder.setOrderId(order.getOrderId());
			sendOrder.setBoxNo(order.getBoxNo());
			sendOrder.setCarrierName(order.getCarrierName());
			sendOrder.setCreateTime(order.getSendTime());
			sendOrder.setUpdateTime(order.getUpdateTime());
			sendOrder.setInboxQty(order.getInboxQty());
			sendOrder.setPartNo(order.getPartNo());
			sendOrder.setSendTime(order.getSendTime());
			sendOrder.setStatusName(order.getStatusName());
			sendOrder.setSupplierName(order.getSupplierName());
			map.put("sendOrder", sendOrder);
		}
		if(Status.DELIVERING.equals(order.getStatusName())||Status.TALLIED.equals(order.getStatusName())||Status.REJECTED.equals(order.getStatusName())){
			DeliverOrder deliverOrder = new DeliverOrder();
			deliverOrder.setBoxNo(order.getBoxNo());
			deliverOrder.setBoxQty(order.getBoxQty());
			deliverOrder.setCarNo(order.getCarNo());
			deliverOrder.setCarrierName(order.getCarrierName());
			deliverOrder.setCreateTime(order.getTransportTime());
			deliverOrder.setOrderId(order.getOrderId());
			deliverOrder.setStatusName(order.getStatusName());
			deliverOrder.setTransportTime(order.getTransportTime());
			deliverOrder.setUpdateTime(order.getUpdateTime());
			map.put("deliverOrder", deliverOrder);
		}
		if(Status.TALLIED.equals(order.getStatusName())||Status.REJECTED.equals(order.getStatusName())){
			TallyOrder tallyOrder=new TallyOrder();
			tallyOrder.setBoxNo(order.getBoxNo());
			tallyOrder.setBoxQty(order.getBoxQty());
			tallyOrder.setCarNo(order.getCarNo());
			tallyOrder.setCreateTime(order.getTallyTime());
			tallyOrder.setOrderId(order.getOrderId());
			tallyOrder.setPartNo(order.getPartNo());
			tallyOrder.setReceiverName(order.getReceiverName());
			tallyOrder.setStatusName(order.getStatusName());
			tallyOrder.setTallyTime(order.getTallyTime());
			map.put("tallyOrder", tallyOrder);
		}
		return map;
	}

	
}
