package com.neusoft.smart.pos.gateway.service;

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.inventory.request.SellAndWarehouseOutRequest;
import com.neusoft.smart.pos.dto.org.request.PostOrgRequest;
import com.neusoft.smart.pos.dto.org.response.PostOrgResponse;
import com.neusoft.smart.pos.dto.trade.request.PostOrderDetailRequest;
import com.neusoft.smart.pos.dto.trade.request.PostSettlementRequest;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.gateway.constants.InventoryConstant;
import com.neusoft.smart.pos.gateway.constants.MemberErrorCode;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteInventoryService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteMemberService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteOrgService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteTradeSettlementService;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class InventoryService {
	
	@Autowired
	private RemoteInventoryService remoteInventoryService;
	
	/**
	 * 锁定库存接口
	 */
	public void lockInventory(String serialNo,String orderNumber,List<PostOrderDetailRequest> orderDetails){
		val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(serialNo);
		if(null == deviceInfo){
			throw new BusinessCommonException(MemberErrorCode.DEVICE_INFO_NOT_FOUND);
		}
		val orgId = deviceInfo.getOrganizationId();
		val shopId = deviceInfo.getStoreId();
		if(null == orderDetails){
			return;
		}
		lockInventory(orgId,shopId,orderNumber,orderDetails);
	}
	
	/**
	 * 锁定库存接口
	 */
	public void lockInventory(Integer orgId,Integer shopId,String orderNumber,List<PostOrderDetailRequest> orderDetails) {
		SellAndWarehouseOutRequest sellAndWarehouseOutRequest = new SellAndWarehouseOutRequest();
		sellAndWarehouseOutRequest.setOrderNum(orderNumber);
		sellAndWarehouseOutRequest.setOrgId(orgId);
		sellAndWarehouseOutRequest.setShopId(shopId);
		
		if(null != orderDetails && orderDetails.size() > 0){
			List<SellAndWarehouseOutRequest.OrderItem> orderItems = new ArrayList<>();
			orderDetails.forEach(orderDetail->{
				SellAndWarehouseOutRequest.OrderItem orderItem = new SellAndWarehouseOutRequest.OrderItem();
				orderItem.setOrgId(orgId);
				orderItem.setShopId(shopId);
				if(!StringUtils.isEmpty(orderDetail.getItemNumber())){
					orderItem.setGoodsId(Integer.parseInt(orderDetail.getItemNumber()));
				}else{
					orderItem.setGoodsId(0);
				}
				orderItem.setStorageNum(orderDetail.getQuantity());
				orderItems.add(orderItem);
			});
			sellAndWarehouseOutRequest.setOrderItems(orderItems);
		}
		lockerInventoryHandle(InventoryConstant.LOCKER_DEFAULT,sellAndWarehouseOutRequest);
	}
	
	/**
	 * 根据状态改变库存接口
	 */
	public void lockInventoryByStatus(String serialNo,String orderNumber,Integer lockStatus){
		val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(serialNo);
		if(null == deviceInfo){
			throw new BusinessCommonException(MemberErrorCode.DEVICE_INFO_NOT_FOUND);
		}
		val orgId = deviceInfo.getOrganizationId();
		val shopId = deviceInfo.getStoreId();
		lockInventoryByStatus(orgId,shopId,orderNumber,lockStatus);
	}
	
	/**
	 * 根据状态改变库存接口
	 */
	public void lockInventoryByStatus(Integer orgId,Integer shopId,String orderNumber,Integer lockStatus) {
		SellAndWarehouseOutRequest sellAndWarehouseOutRequest = new SellAndWarehouseOutRequest();
		sellAndWarehouseOutRequest.setOrderNum(orderNumber);
		sellAndWarehouseOutRequest.setOrgId(orgId);
		sellAndWarehouseOutRequest.setShopId(shopId);
		lockerInventoryHandle(lockStatus,sellAndWarehouseOutRequest);
	}
	
	/**
	 * 库存处理接口
	 */
	private void lockerInventoryHandle(Integer lockerStatus,SellAndWarehouseOutRequest sellAndWarehouseOutRequest) {
		//1.调用集团门店管理服务，新建集团信息
		val responseData = remoteInventoryService.orderChangeStatus(lockerStatus,sellAndWarehouseOutRequest);
		
		if (!ApplicationConstants.SUCCESS_CODE.equals(responseData.getCode()) || null == responseData) {
			throw new BusinessCommonException(responseData);
		}
	}
}
