package com.fuzamei.service.serviceImpl;

import java.util.ArrayList;
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.alibaba.fastjson.JSON;
import com.fuzamei.constant.OperationType;
import com.fuzamei.constant.Role;
import com.fuzamei.constant.Statuses;
import com.fuzamei.entity.DeliverOrder;
import com.fuzamei.entity.IssueOrder;
import com.fuzamei.entity.Orders;
import com.fuzamei.entity.Params;
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;
import com.fuzamei.util.blockchain.BlockChainUtil;

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

	@Override
	public PageDTO queryOrdersByPlanner(Params params) {
		PageDTO pageDto=new PageDTO();
		List<IssueOrder> orderList = ordersIssueMapper.queryOrdersByPlanner(params);
		int count = ordersIssueMapper.findQueryCountByPlanner(params);
		pageDto.setRows(orderList);
		pageDto.setTotal(count);
		return pageDto;
	}
	
	@Override
	public PageDTO queryOrdersBySupplier(Params params) {
		PageDTO pageDto=new PageDTO();
		List<IssueOrder> orderList = ordersIssueMapper.queryOrdersBySupplier(params);
		int count = ordersIssueMapper.findQueryCountBySupplier(params);
		pageDto.setRows(orderList);
		pageDto.setTotal(count);
		return pageDto;
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public void consignment(Params params) {
		String sign = params.getSign();
		@SuppressWarnings("unchecked")
		Map<String,Object> map = JSON.parseObject(sign, Map.class);
		String hash = (String) map.get("sid");
		Long currentTime = System.currentTimeMillis();
		//更新【订单表】
		params.setSendTime(currentTime);
		params.setUpdateTime(currentTime);
		params.setStatusId(Statuses.UNDELIVER);//待运输状态
		ordersIssueMapper.consignment(params);
		//插入【操作记录】
		params.setOperationTypeId(OperationType.SEND);
		params.setOperatorId(params.getUserId());
		params.setOperationTime(currentTime);
		operationHistoryMapper.addToHistory(params);
		params.setHash(hash);
		boolean flag = blockChainUtil.sendBlockChain(params.getSign());
		if(!flag) throw new RuntimeException("区块链操作失败");
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void addOrder(Params params) {
		String sign = params.getSign();
		@SuppressWarnings("unchecked")
		Map<String,Object> map = JSON.parseObject(sign, Map.class);
		String hash = (String) map.get("sid");
		Long currentTime = System.currentTimeMillis();
		//插入【订单表】
		params.setPlannerId(params.getUserId());
		params.setRequireTime(currentTime);
		params.setCreateTime(currentTime);
		params.setUpdateTime(currentTime);
		params.setBoxQty(1);//暂时将箱子数量设置为1=======================>>TODO
		params.setStatusId(Statuses.UNSEND);
		ordersIssueMapper.addOrder(params);
		//插入【操作记录】
		params.setOperationTypeId(OperationType.ADD_ORDER);
		params.setOperatorId(params.getUserId());
		params.setOperationTime(currentTime);
		params.setHash(hash);
		operationHistoryMapper.addToHistory(params);
		boolean flag = blockChainUtil.sendBlockChain(params.getSign());
		if(!flag) throw new RuntimeException("区块链操作失败");
	}

	@Override
	public List<Object> orderTracking(Params params) {
		Orders order = params.getOrder();
		return dispatchOrderInfo(order);
	}
	
	/**
	 * 
	* @Title: dispatchOrderInfo
	* @Description: TODO(将order表中的所有数据分发给各个小订单表中，然后根据当时订单的状态包装成map)
	* @params @params order
	* @params @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月25日 下午9:11:39
	* @throws
	 */
	public static final List<Object> dispatchOrderInfo(Orders order){
		List<Object> list=new ArrayList<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());
		list.add(issueOrder);
		if(!Statuses.UNSEND.equals(order.getStatusId())){
			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());
			list.add(sendOrder);
		}
		if(Statuses.DELIVERING.equals(order.getStatusId())||Statuses.TALLIED.equals(order.getStatusId())||Statuses.REJECTED.equals(order.getStatusId())){
			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());
			list.add(deliverOrder);
		}
		if(Statuses.TALLIED.equals(order.getStatusId())||Statuses.REJECTED.equals(order.getStatusId())){
			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());
			list.add(tallyOrder);
		}
		return list;
	}

	
}
