package com.fuzamei.service.serviceImpl;

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

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

import com.fuzamei.constant.OperationType;
import com.fuzamei.constant.Status;
import com.fuzamei.entity.Orders;
import com.fuzamei.entity.Param;
import com.fuzamei.entity.SendOrder;
import com.fuzamei.entity.TallyOrder;
import com.fuzamei.mapperInterface.CargoTallyMapper;
import com.fuzamei.mapperInterface.OperationHistoryMapper;
import com.fuzamei.service.CargoTallyService;
import com.fuzamei.util.PageDTO;

@Service
public class CargoTallyServiceImpl implements CargoTallyService {

	@Autowired
	private CargoTallyMapper cargoTallyMapper;
	
	@Autowired
	private OperationHistoryMapper operationHistoryMapper;

	@Override
	public PageDTO queryOrdersByReceiver(Param param) {
		PageDTO pageDto=new PageDTO();
		List<TallyOrder> orderList = cargoTallyMapper.queryOrdersByReceiver(param);
		int count = cargoTallyMapper.findQueryCountByReceiver(param);
		pageDto.setRows(orderList);
		pageDto.setTotal(count);
		return pageDto;
	}

	@Override
	public TallyOrder searchNewOrderByOrderId(Param param) {
		Orders order = param.getOrder();
		TallyOrder tallyOrder=new TallyOrder();
		tallyOrder.setOrderId(order.getOrderId());
		tallyOrder.setPartNo(order.getPartNo());
		tallyOrder.setBoxNo(order.getBoxNo());
		tallyOrder.setBoxQty(order.getBoxQty());
		tallyOrder.setCarNo(order.getCarNo());
		return tallyOrder;
	}

	@Override
	public void confirmOrReject(Param param) {
		Long currentTime = System.currentTimeMillis();
		//更新【订单表】
		param.setTallyTime(currentTime);
		param.setUpdateTime(currentTime);
		param.setReceiverId(param.getUserId());
		if(param.getConfirmId()==1)	param.setStatusId(Status.TALLIED);
		if(param.getConfirmId()==0)	param.setStatusId(Status.REJECTED);
		cargoTallyMapper.confirmOrReject(param);
		//插入【操作记录】
		if(param.getConfirmId()==1) param.setOperationTypeId(OperationType.CONFIRM);
		if(param.getConfirmId()==0) param.setOperationTypeId(OperationType.REJECT);
		param.setOperatorId(param.getUserId());
		param.setOperationTime(currentTime);
		operationHistoryMapper.addToHistory(param);
	}

	@Override
	public Map<String, Object> orderTracking(Param param) {
		Orders order = param.getOrder();
		return OrdersIssueServiceImpl.dispatchOrderInfo(order);
	}
}
