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


import com.neusoft.smart.pos.dto.org.Shop;
import com.neusoft.smart.pos.dto.org.response.PostShopResponse;
import com.neusoft.smart.pos.dto.trade.request.GetDailyRequest;
import com.neusoft.smart.pos.dto.trade.request.GetPayTypeTradeSummaryRequest;
import com.neusoft.smart.pos.dto.trade.request.GetShopTradeSummaryRequest;
import com.neusoft.smart.pos.dto.trade.response.GetDailyResponse;
import com.neusoft.smart.pos.dto.trade.response.GetPayChannelSummaryForPosResponse;
import com.neusoft.smart.pos.dto.trade.response.GetPayTypeTradeSummaryResponse;
import com.neusoft.smart.pos.dto.trade.response.GetShopTradeSummaryResponse;
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.service.feignService.RemoteShopService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteTradeStatisticsService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("tradeStatisticsService")
public class TradeStatisticsService {

	@Autowired
	private RemoteTradeStatisticsService remoteTradeStatisticsService;

	@Autowired
	private RemoteShopService remoteShopService;

	/**
	 * 集团门店交易日报查询接口
	 * @param dailyRequest
	 * @return
	 */
	public GetDailyResponse queryPayDaily(GetDailyRequest dailyRequest){
		//1.根据集团及门店ID查询门店列表信息
		ResponseData<List<PostShopResponse>> shopListResponse = remoteShopService.queryShopListByCurrentUser();

		if(null == shopListResponse || !ApplicationConstants.SUCCESS_CODE.equals(shopListResponse.getCode())){
			throw new BusinessCommonException(shopListResponse);
		}
		List<Integer> shopIds = new ArrayList<>();
		Integer orgId = 0;
		if(null != shopListResponse.getResult() && shopListResponse.getResult().size() > 0){
			orgId = shopListResponse.getResult().get(0).getOrg();
			shopListResponse.getResult().forEach(shop->{
				shopIds.add(shop.getId());
			});
		}

		val tradeDailyResponse = remoteTradeStatisticsService.queryPayDaily(orgId,shopIds,dailyRequest.getStartTime(),dailyRequest.getEndTime(),dailyRequest.getOffset(),dailyRequest.getLimit(),dailyRequest.getIsPage());
		//2.判断返回信息是否错误
		if(null == tradeDailyResponse || !ApplicationConstants.SUCCESS_CODE.equals(tradeDailyResponse.getCode())){
			throw new BusinessCommonException(tradeDailyResponse);
		}

		//3.整理返回数据信息
		for(int i =0 ;i<tradeDailyResponse.getResult().paginationExtendBean.getRows().size();i++){
			for(int j=0;j<shopListResponse.getResult().size();j++){
				if(tradeDailyResponse.getResult().paginationExtendBean.getRows().get(i).get("shopId").toString().equals(shopListResponse.getResult().get(j).getId().toString())){
					tradeDailyResponse.getResult().paginationExtendBean.getRows().get(i).put("shopName",shopListResponse.getResult().get(j).getName());
					break;
				}
			}
		}
		return tradeDailyResponse.getResult();
	}

	/**
	 * POS端日报汇总统计查询方法
	 * @param serialNo:POS机SN
	 * @param dateTime:查询时间(2018-01-06)
	 * @return
	 */
	public GetPayChannelSummaryForPosResponse queryPayChannelSummaryForPos(String serialNo,String dateTime,String endTime,String startTime,String timeMode){
		val tradeStatisticsResponse = remoteTradeStatisticsService.queryPayChannelSummaryForPos(serialNo,dateTime,endTime,startTime,timeMode);
		if(null == tradeStatisticsResponse || !ApplicationConstants.SUCCESS_CODE.equals(tradeStatisticsResponse.getCode())){
			throw new BusinessCommonException(tradeStatisticsResponse);
		}
		val shopResponse = remoteShopService.getShopDetail(tradeStatisticsResponse.getResult().getShopId());
		tradeStatisticsResponse.getResult().setShopName(shopResponse.getResult().getName());
		return tradeStatisticsResponse.getResult();
	}

	/**
	 * 沈阳食堂交易月报统计
	 * @param shopTradeSummaryRequest
	 * @return
	 */
	public GetShopTradeSummaryResponse queryShopTradeSummary(GetShopTradeSummaryRequest shopTradeSummaryRequest){
		//1.根据集团及门店ID查询门店列表信息
		ResponseData<List<PostShopResponse>> shopListResponse = remoteShopService.queryShopListByCurrentUser();

		val shopTradeSummaryResponseData =remoteTradeStatisticsService.queryShopTradeSummary(
				shopTradeSummaryRequest.getOrgId(),
				shopTradeSummaryRequest.getShopIds(),
				shopTradeSummaryRequest.getStartTime(),
				shopTradeSummaryRequest.getEndTime(),
				shopTradeSummaryRequest.getDateTime(),
				shopTradeSummaryRequest.getTimeMode()
		);
		//2.判断返回信息是否错误
		if(null == shopTradeSummaryResponseData || !ApplicationConstants.SUCCESS_CODE.equals(shopTradeSummaryResponseData.getCode())){
			throw new BusinessCommonException(shopTradeSummaryResponseData);
		}

		//3.整理返回数据信息
		for(int i =0 ;i<shopTradeSummaryResponseData.getResult().getShopTradeInfoList().size();i++){
			for(int j=0;j<shopListResponse.getResult().size();j++){
				if(shopTradeSummaryResponseData.getResult().getShopTradeInfoList().get(i).getShopId().toString().equals(shopListResponse.getResult().get(j).getId().toString())){
					shopTradeSummaryResponseData.getResult().getShopTradeInfoList().get(i).setShopName(shopListResponse.getResult().get(j).getName());
					break;
				}
			}
		}
		return shopTradeSummaryResponseData.getResult();
	}
	
	/**
	 * 门店支付方式交易统计
	 * @return
	 */
	public GetPayTypeTradeSummaryResponse queryPayTypeTradeSummary(GetPayTypeTradeSummaryRequest payTypeTradeSummaryRequest){
		//1.根据集团及门店ID查询门店列表信息
		ResponseData<List<PostShopResponse>> shopListResponse = remoteShopService.queryShopListByCurrentUser();
		
		if(null == shopListResponse || !ApplicationConstants.SUCCESS_CODE.equals(shopListResponse.getCode())){
			throw new BusinessCommonException(shopListResponse);
		}
		List<Integer> shopIds = new ArrayList<>();
		Integer orgId = 0;
		if(null != shopListResponse.getResult() && shopListResponse.getResult().size() > 0){
			orgId = shopListResponse.getResult().get(0).getOrg();
			if(null == payTypeTradeSummaryRequest.getShopIds() || payTypeTradeSummaryRequest.getShopIds().size() == 0){
				shopListResponse.getResult().forEach(shop->{
					shopIds.add(shop.getId());
				});
			}else{
				shopIds.addAll(payTypeTradeSummaryRequest.getShopIds());
			}
		}
		
		val shopTradeSummaryResponseData =remoteTradeStatisticsService.queryPayTypeTradeSummary(
				orgId,
				shopIds,
				payTypeTradeSummaryRequest.getStartTime(),
				payTypeTradeSummaryRequest.getEndTime()
		);
		//2.判断返回信息是否错误
		if(null == shopTradeSummaryResponseData || !ApplicationConstants.SUCCESS_CODE.equals(shopTradeSummaryResponseData.getCode())){
			throw new BusinessCommonException(shopTradeSummaryResponseData);
		}
		
		//3.整理返回数据信息
		for(int i =0 ;i<shopTradeSummaryResponseData.getResult().getShopSummaryList().size();i++){
			for(int j=0;j<shopListResponse.getResult().size();j++){
				if(shopTradeSummaryResponseData.getResult().getShopSummaryList().get(i).getShopId().toString().equals(shopListResponse.getResult().get(j).getId().toString())){
					shopTradeSummaryResponseData.getResult().getShopSummaryList().get(i).setShopName(shopListResponse.getResult().get(j).getName());
					break;
				}
			}
		}
		return shopTradeSummaryResponseData.getResult();
	}
}
