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

import com.neusoft.smart.pos.dto.trade.request.GetMemberOrderDetailsRequest;
import com.neusoft.smart.pos.dto.trade.request.GetShopOrderDetailsRequest;
import com.neusoft.smart.pos.dto.trade.request.GetShopOrderSummaryRequest;
import com.neusoft.smart.pos.dto.trade.response.GetShopOrderDetailsResponse;
import com.neusoft.smart.pos.dto.trade.response.GetShopOrderSummaryResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteOrderStatisticsService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteShopService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderStatisticsService {
	
	@Autowired
	private RemoteOrderStatisticsService remoteOrderStatisticsService;

	@Autowired
	private RemoteShopService remoteShopService;

	/**
	 * 门店订单汇总查询方法
	 * @param shopOrderSummaryRequest
	 * @return
	 */
	public PaginationExtendBean<GetShopOrderSummaryResponse> queryShopOrderSummary(GetShopOrderSummaryRequest shopOrderSummaryRequest) {
		//1.根据集团及门店ID查询门店列表信息
		val shopListResponse = remoteShopService.queryShopListByCurrentUser();

		//2.交易管理服务查询门店订单信息
		List<Integer> shopIdList = new ArrayList<>();
		Integer orgId = 0;
		if(null != shopListResponse.getResult() && shopListResponse.getResult().size() > 0){
			shopListResponse.getResult().forEach(shop -> {
				shopIdList.add(shop.getId());
			});
			orgId = shopListResponse.getResult().get(0).getOrg();
		}
		if(null == shopIdList || shopIdList.size() == 0){
			return new PaginationExtendBean<>();
		}
		val shopOrderResponseData = remoteOrderStatisticsService.queryShopOrderSummary(orgId,shopIdList,shopOrderSummaryRequest.getOffset(),shopOrderSummaryRequest.getLimit(),shopOrderSummaryRequest.getIsPage(),shopOrderSummaryRequest.getStartTime(),shopOrderSummaryRequest.getEndTime());

		//3.判断返回信息是否错误
		if(null == shopOrderResponseData || !ApplicationConstants.SUCCESS_CODE.equals(shopOrderResponseData.getCode())){
			throw new BusinessCommonException(shopOrderResponseData);
		}

		//4.整理返回数据信息
		for(int i =0 ;i<shopOrderResponseData.getResult().getRows().size();i++){
			for(int j=0;j<shopListResponse.getResult().size();j++){
				if(shopOrderResponseData.getResult().getRows().get(i).getShopId() .intValue()== shopListResponse.getResult().get(j).getId().intValue()){
					shopOrderResponseData.getResult().getRows().get(i).setShopName(shopListResponse.getResult().get(j).getName());
					break;
				}
			}
		}
		return shopOrderResponseData.getResult();
	}

	/**
	 * 门店订单详情统计查询方法
	 * @param shopOrderDetailsRequest
	 * @return
	 */
	public GetShopOrderDetailsResponse queryShopOrderDetails(GetShopOrderDetailsRequest shopOrderDetailsRequest) {
		if(StringUtils.isEmpty(shopOrderDetailsRequest.getStartTime())&&StringUtils.isEmpty(shopOrderDetailsRequest.getEndTime())){
			shopOrderDetailsRequest.setStartTime(DateUtils.format(DateUtils.getDayStartTime(new Date())));
			shopOrderDetailsRequest.setEndTime(DateUtils.format(DateUtils.getDayEndTime(new Date())));
		}
		//1.查询门店订单详情统计列表信息
		List<Integer> shopIds = new ArrayList<>();
		shopIds.add(shopOrderDetailsRequest.getShopId());
		val shopOrderDetailsResponseData = remoteOrderStatisticsService.queryShopOrderDetails(
				shopIds,
				shopOrderDetailsRequest.getOrderNumber(),
				shopOrderDetailsRequest.getSerialNo(),
				shopOrderDetailsRequest.getDeviceName(),
				shopOrderDetailsRequest.getMemberCardNo(),
				shopOrderDetailsRequest.getMemberName(),
				shopOrderDetailsRequest.getCardNum(),
				shopOrderDetailsRequest.getCustomerNumber(),
				shopOrderDetailsRequest.getPayType(),
				shopOrderDetailsRequest.getOrderStatus(),
				shopOrderDetailsRequest.getStartTime(),
				shopOrderDetailsRequest.getEndTime(),
				shopOrderDetailsRequest.getDateTime(),
				shopOrderDetailsRequest.getTimeMode(),
				shopOrderDetailsRequest.getOrderType(),
				shopOrderDetailsRequest.getOffset(),
				shopOrderDetailsRequest.getLimit(),
				shopOrderDetailsRequest.getIsPage()
		);
		//2.判断返回信息是否错误
		if(null == shopOrderDetailsResponseData || !ApplicationConstants.SUCCESS_CODE.equals(shopOrderDetailsResponseData.getCode())){
			throw new BusinessCommonException(shopOrderDetailsResponseData);
		}
		return shopOrderDetailsResponseData.getResult();

	}

	/**
	 * 门店订单详情统计查询方法
	 * @param memberOrderDetailsRequest
	 * @return
	 */
	public GetShopOrderDetailsResponse queryMemberOrderDetails(GetMemberOrderDetailsRequest memberOrderDetailsRequest) {
		//1.查询门店订单详情统计列表信息
		val memberOrderDetailsResponseData = remoteOrderStatisticsService.queryShopOrderDetails(
				memberOrderDetailsRequest.getShopIds(),
				"",
				"",
				memberOrderDetailsRequest.getDeviceName(),
				memberOrderDetailsRequest.getMemberCardNo(),
				memberOrderDetailsRequest.getMemberName(),
				memberOrderDetailsRequest.getCardNum(),
				"",
				"",
				"",
				memberOrderDetailsRequest.getStartTime(),
				memberOrderDetailsRequest.getEndTime(),
				"",
				"1",
				memberOrderDetailsRequest.getOrderType(),
				memberOrderDetailsRequest.getOffset(),
				memberOrderDetailsRequest.getLimit(),
				memberOrderDetailsRequest.getIsPage()
		);
		//2.判断返回信息是否错误
		if(null == memberOrderDetailsResponseData || !ApplicationConstants.SUCCESS_CODE.equals(memberOrderDetailsResponseData.getCode())){
			throw new BusinessCommonException(memberOrderDetailsResponseData);
		}
		return memberOrderDetailsResponseData.getResult();

	}


}
