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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.trade.request.*;
import com.neusoft.smart.pos.dto.trade.response.*;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.constants.CommonErrorCode;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.order.constants.TradeErrorCode;
import com.neusoft.smart.pos.order.persistent.domain.OrderMasterExample;
import com.neusoft.smart.pos.order.repository.OrderMasterRepository;
import com.neusoft.smart.pos.order.repository.OrderRefundRepository;
import com.neusoft.smart.pos.payorder.persistent.domain.PayTypeDict;
import com.neusoft.smart.pos.payorder.persistent.domain.TradeMchPayChannel;
import com.neusoft.smart.pos.payorder.repository.PayChannelRepository;
import com.neusoft.smart.pos.payorder.repository.TradeSettlementRepository;
import com.neusoft.smart.pos.trade.repository.*;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;

import lombok.val;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("tradeStatisticsService")
public class TradeStatisticsService {

    @Autowired
    private OrderMasterRepository orderMasterRepository;

    @Autowired
    private OrderRefundRepository orderRefundRepository;

    @Autowired
    private TradeSettlementRepository tradeSettlementRepository;

    @Autowired
    private PayChannelRepository payChannelRepository;

    @Autowired
    private PayTypeRepository payTypeRepository;

    private final static Logger logger = LoggerFactory.getLogger(TradeStatisticsService.class);
    /**
     * 支付渠道金额统计查询方法
     * @return
     */
    public GetPayChannelSummaryResponse queryPayChannelSummary(GetPayChannelSummaryRequest payChannelSummaryRequest) {
        //从header中获取集团ID
        Integer orgId = RequestHeaderUtil.getOrgIdByRole(payChannelSummaryRequest.getOrgId());
        boolean queryByOrg = StringUtils.isEmpty(payChannelSummaryRequest.getShopId());
        if(null == orgId){
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        //1.校验参数信息
        if(StringUtils.isEmpty(payChannelSummaryRequest.getTimeMode())){
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_TIME_MODE_EMPTY);
        }
        if(!payChannelSummaryRequest.getTimeMode().equals("1") && !payChannelSummaryRequest.getTimeMode().equals("2")){
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_TIME_MODE_ERROR);
        }
        if(payChannelSummaryRequest.getTimeMode().equals("1")){
            if(StringUtils.isEmpty(payChannelSummaryRequest.getStartTime()) && StringUtils.isEmpty(payChannelSummaryRequest.getEndTime())){

            }else if(StringUtils.isEmpty(payChannelSummaryRequest.getStartTime()) || StringUtils.isEmpty(payChannelSummaryRequest.getEndTime())){
                throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_START_END_TIME_EMPTY);
            }
        }
        if(payChannelSummaryRequest.getTimeMode().equals("2")){
            if(StringUtils.isEmpty(payChannelSummaryRequest.getDateTime())){
                throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_DATE_TIME_EMPTY);
            }
        }

        //1.查询门店结算方式
        Integer shopId = 0;
        if(!queryByOrg){
            val tradeSettlementInfo = tradeSettlementRepository.queryTradeSettlementInfo(orgId,payChannelSummaryRequest.getShopId());
            if(null == tradeSettlementInfo){
                return new GetPayChannelSummaryResponse();
            }

            if(DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())){
                shopId = payChannelSummaryRequest.getShopId();
            }
        }

        //2.查询门店支付渠道列表
        final List<TradeMchPayChannel> tradeMchPayChannelList = new ArrayList<>();
        if(queryByOrg) {
            val orgPayTypeList = payChannelRepository.queryOrgPayTypeList(orgId);
            orgPayTypeList.forEach(payType -> {
                val tradeMchPayChannel = new TradeMchPayChannel();
                tradeMchPayChannel.setPayType(payType);
                tradeMchPayChannelList.add(tradeMchPayChannel);
            });
        } else {
            tradeMchPayChannelList.addAll(payChannelRepository.queryShopPayChannels(orgId,shopId));
        }


        //6.根据支付方式查询对应聚合支付平台支付信息
        val tradePayTypeDicts = payTypeRepository.queryPayTypes(null,null);

        //3.查询门店订单汇总信息
        val example = new OrderMasterExample();
        val criteria = example.createCriteria();
        criteria.andOrgIdEqualTo(orgId)
                //sql里有条件 已付款 退款中 已退款
                //.andPayStatusEqualTo(DictionaryConstant.PAY_STATUS_YFK.getCode())
                .andOrderTypeNotEqualTo(DictionaryConstant.ORDER_TYPE_HYCZ.getCode());

        if(!queryByOrg) {
            criteria.andShopIdEqualTo(payChannelSummaryRequest.getShopId());
        }

        //3.1处理查询时间
        if(!StringUtils.isEmpty(payChannelSummaryRequest.getTimeMode())){
            Date startTime = null;
            Date endTime = null;
            //时间模式(@1:按开始及结束时间查询@2:按年月查询)
            if(("1").equals(payChannelSummaryRequest.getTimeMode())){
                if(StringUtils.isEmpty(payChannelSummaryRequest.getStartTime()) && StringUtils.isEmpty(payChannelSummaryRequest.getEndTime())){
                    startTime = DateUtils.getDayStartTime(new Date());
                    endTime = DateUtils.getDayEndTime(new Date());
                }else{
                    startTime = DateUtils.getDayStartTime(DateUtils.parse(payChannelSummaryRequest.getStartTime(),DateUtils.FORMAT_SHORT));
                    endTime = DateUtils.getDayEndTime(DateUtils.parse(payChannelSummaryRequest.getEndTime(),DateUtils.FORMAT_SHORT));
                }
            }else if(("2").equals(payChannelSummaryRequest.getTimeMode())){
                startTime = DateUtils.getMonthFirstDay(DateUtils.parse(payChannelSummaryRequest.getDateTime(),DateUtils.FORMAT_SHORTER));
                endTime = DateUtils.getDayEndTime(DateUtils.getMonthLastDay(DateUtils.parse(payChannelSummaryRequest.getDateTime(),DateUtils.FORMAT_SHORTER)));
            }
            example.getOredCriteria().get(0).andPayOrderCreatedAtGreaterThanOrEqualTo(startTime);
            example.getOredCriteria().get(0).andPayOrderCreatedAtLessThan(endTime);
        }
        val payOrderList = orderMasterRepository.queryShopPayOrderList(example);

        //4.整理返回数据
        GetPayChannelSummaryResponse payChannelSummary = new GetPayChannelSummaryResponse();
        List<GetPayChannelSummaryResponse.PayChannel> payChannels = new ArrayList<>();
        tradeMchPayChannelList.forEach(payChannel ->{
            //去掉富茂优惠券和折扣
            if(payChannel.getPayType().equals(DictionaryConstant.PAY_TYPE_FM_COUPON.getCode())) return;
            if(payChannel.getPayType().equals(DictionaryConstant.PAY_TYPE_DISCOUNT.getCode())) return;
            GetPayChannelSummaryResponse.PayChannel channel = new GetPayChannelSummaryResponse.PayChannel();
            channel.setPayType(payChannel.getPayType());
            tradePayTypeDicts.forEach(tradePayType->{
                if(payChannel.getPayType().equals(tradePayType.getPayType())){
                    channel.setPayTypeName(tradePayType.getName());
                    return;
                }
            });
            payOrderList.forEach(payOrder ->{
                if(payChannel.getPayType().equals(payOrder.get("payType"))){
                    channel.setPayAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("payAmount").toString())));
                    channel.setPayCount(Integer.parseInt(payOrder.get("payCount").toString()));
                    channel.setRefundAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("refundAmount").toString())));
                    channel.setRefundCount(Integer.parseInt(payOrder.get("refundCount").toString()));
                    channel.setAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("amount").toString())));
                    channel.setDiscountAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("discountAmount").toString())));
                    return;
                }
            });
            payChannels.add(channel);
        });
        payChannelSummary.setPayChannels(payChannels);
        return payChannelSummary;
    }

    /**
     * 集团门店交易日报查询接口
     * @param dailyRequest
     * @return  统计每日每个门店 real pay amount
     */
    public GetDailyResponse queryPayDaily(GetDailyRequest dailyRequest){
        //1.校验参数信息
        if(StringUtils.isEmpty(dailyRequest.getStartTime()) || StringUtils.isEmpty(dailyRequest.getEndTime())){
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_START_END_TIME_EMPTY);
        }
//		if(null == dailyRequest.getShopIds() || dailyRequest.getShopIds().size() <= 0){
//			throw new BusinessCommonException(TradeErrorCode.SHOP_ID_LIST_EMPTY);
//		}

        //2.根据开始时间及结束时间生成表头信息
        List<String> headerList = getTableHeaders(dailyRequest.getStartTime(),dailyRequest.getEndTime());

        //3设置分页信息
        val totalPage = dailyRequest.getShopIds().size();
        List<Integer> shopIds = dailyRequest.getShopIds();
        if(dailyRequest.getIsPage()){
            val totalCount = dailyRequest.getOffset()+dailyRequest.getLimit();
            if(dailyRequest.getShopIds().size() <= totalCount){
                shopIds = dailyRequest.getShopIds().subList(dailyRequest.getOffset(),dailyRequest.getShopIds().size());
            }else{
                shopIds = dailyRequest.getShopIds().subList(dailyRequest.getOffset(),totalCount);
            }
        }

        //4.查询数据信息，按照时间及门店进行分组
        val example = new OrderMasterExample();
        example.createCriteria()
                .andOrgIdEqualTo(dailyRequest.getOrgId())
                //.andOrderStatusEqualTo(DictionaryConstant.ORDER_UPDATE_STATUS_ZFWC.getCode())
                .andOrderTypeNotEqualTo(DictionaryConstant.ORDER_TYPE_HYCZ.getCode())
                .andCreatedAtLessThanOrEqualTo(DateUtils.getDayEndTime(DateUtils.parse(dailyRequest.getEndTime(),DateUtils.FORMAT_SHORT)))
                .andCreatedAtGreaterThanOrEqualTo(DateUtils.getDayStartTime(DateUtils.parse(dailyRequest.getStartTime(),DateUtils.FORMAT_SHORT)));
        if(null != shopIds && shopIds.size() > 0){
            example.getOredCriteria().get(0).andShopIdIn(shopIds);
        }

        List<Map> shopDayOrderList = orderMasterRepository.queryShopDayOrder(example);
        List<Map<String,Object>> dailyDetailList = dailyDetailsHandle(shopIds,headerList,shopDayOrderList);
        PaginationExtendBean<Map<String,Object>> paginationExtendBean = new PaginationExtendBean<>();
        paginationExtendBean.setRows(dailyDetailList);
        paginationExtendBean.setTotal(totalPage);

        //5.整理返回数据信息
        GetDailyResponse dailyResponse = new GetDailyResponse();
        headerList.add(0,"门店名称");
        dailyResponse.setHeaders(headerList);
        dailyResponse.setPaginationExtendBean(paginationExtendBean);
        return dailyResponse;
    }

    /**
     * 根据开始时间及结束时间获取日期列表
     * @param startTime(含)
     * @param endTime(含)
     * @return
     */
    private List<String> getTableHeaders(String startTime,String endTime){
        int days = DateUtils.daysBetween(DateUtils.parse(startTime,DateUtils.FORMAT_SHORT),DateUtils.parse(endTime,DateUtils.FORMAT_SHORT));
        List<String> headerList = new ArrayList<>();
        for(int i=0;i<=days;i++){
            headerList.add(DateUtils.format(DateUtils.addDay(DateUtils.parse(startTime,DateUtils.FORMAT_SHORT),i),DateUtils.FORMAT_SHORT));
        }
        return headerList;
    }

    /**
     * 整理表格数据信息
     * @param headerList
     * @param dailyList
     * @return
     */
    private List<Map<String,Object>> dailyDetailsHandle(List<Integer> shopIds,List<String> headerList,List<Map> dailyList){
        List<Map<String,Object>> dailyDetailList = new ArrayList<>();
        shopIds.forEach(shopId->{
            Map<String,Object> dailyDetail = new HashMap<>();
            dailyDetail.put("shopId",shopId);
            dailyDetail.put("shopName","");
            //遍历表头信息
            for(int i=0;i<headerList.size();i++){
                dailyDetail.put(headerList.get(i),BigDecimal.valueOf(0));
                //遍历数据信息
                for(int j=0;j<dailyList.size();j++){
                    if(Integer.parseInt(dailyList.get(j).get("shopId").toString()) == shopId && String.valueOf(dailyList.get(j).get("createdAt")).equals(headerList.get(i))){
                        dailyDetail.put(headerList.get(i),BigDecimal.valueOf(Double.valueOf(String.valueOf(dailyList.get(j).get("orderAmount")))));
                        break;
                    }
                }
            }
            dailyDetailList.add(dailyDetail);
        });
        return dailyDetailList;
    }

    /**
     * 支付渠道支付占比统计查询方法
     * @return
     */
    public GetPayChannelProportionResponse queryPayChannelProportion(GetPayChannelProportionRequest payChannelProportionRequest) {
        Integer orgId = RequestHeaderUtil.getOrgId();
        Integer shopId = RequestHeaderUtil.getStoreIdByRole(payChannelProportionRequest.getShopId());
        if(null == orgId || null == shopId){
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        payChannelProportionRequest.setOrgId(orgId);
        payChannelProportionRequest.setShopId(shopId);

        //1.校验参数信息
        if(StringUtils.isEmpty(payChannelProportionRequest.getStartTime()) || StringUtils.isEmpty(payChannelProportionRequest.getEndTime())){
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_START_END_TIME_EMPTY);
        }
        if(null == payChannelProportionRequest.getShopId() || 0 == payChannelProportionRequest.getShopId()){
            throw new BusinessCommonException(TradeErrorCode.SHOP_ID_EMPTY);
        }if(null == payChannelProportionRequest.getOrgId() || 0 == payChannelProportionRequest.getOrgId()){
            throw new BusinessCommonException(TradeErrorCode.ORG_ID_EMPTY);
        }

        //2.根据集团及门店ID列表查询需要展示的支付方式集合
        Set<String> payTypeSet = getPayTypeSetByShopId(payChannelProportionRequest.getOrgId(),payChannelProportionRequest.getShopId());

        //3.根据支付方式查询对应聚合支付平台支付信息
        val tradePayTypeDicts = payTypeRepository.queryPayTypes(null,null);

        //4.查询门店订单汇总信息
        Date startTime = DateUtils.getDayStartTime(DateUtils.parse(payChannelProportionRequest.getStartTime(),DateUtils.FORMAT_SHORT));
        Date endTime = DateUtils.getDayEndTime(DateUtils.parse(payChannelProportionRequest.getEndTime(),DateUtils.FORMAT_SHORT));
        val example = new OrderMasterExample();
        example.createCriteria()
                .andOrgIdEqualTo(payChannelProportionRequest.getOrgId())
                .andShopIdEqualTo(payChannelProportionRequest.getShopId())
                //sql里有条件:已付款或退款中或已退款
                //.andPayStatusEqualTo(DictionaryConstant.PAY_STATUS_YFK.getCode())
                .andOrderTypeNotEqualTo(DictionaryConstant.ORDER_TYPE_HYCZ.getCode())
                .andPayOrderCreatedAtGreaterThanOrEqualTo(startTime)
                .andPayOrderCreatedAtLessThan(endTime);
        List<Map> payOrderList = orderMasterRepository.queryShopPayOrderList(example);

        //5.整理返回数据信息
        GetPayChannelProportionResponse payChannelProportion = new GetPayChannelProportionResponse();
        List<GetPayChannelProportionResponse.PayChannel> payChannels = new ArrayList<>();
        BigDecimal totalAmount = new BigDecimal(0.00);  //支付总实收金额
        BigDecimal totalPayAmount = new BigDecimal(0.00);  //支付总消费金额
        Integer totalPayCount = 0;  //总消费笔数
        Integer totalRefundCount = 0;  //总退款笔数
        for(String payType:payTypeSet){
            //去掉富茂优惠券和折扣
            if(!payType.equals(DictionaryConstant.PAY_TYPE_FM_COUPON.getCode())&&!payType.equals(DictionaryConstant.PAY_TYPE_DISCOUNT.getCode())) {
                GetPayChannelProportionResponse.PayChannel channel = new GetPayChannelProportionResponse.PayChannel();
                channel.setPayType(payType);
                tradePayTypeDicts.forEach(tradePayType -> {
                    if (payType.equals(tradePayType.getPayType())) {
                        channel.setPayTypeName(tradePayType.getName());
                        return;
                    }
                });
                for (Map payOrder : payOrderList) {
                    if (payType.equals(payOrder.get("payType"))) {
                        channel.setPayAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("payAmount").toString())));
                        channel.setPayCount(Integer.parseInt(payOrder.get("payCount").toString()));
                        channel.setRefundCount(Integer.parseInt(payOrder.get("refundCount").toString()));
                        channel.setAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("amount").toString())));
                        totalAmount = totalAmount.add(channel.getAmount());
                        totalPayAmount = totalPayAmount.add(channel.getPayAmount());
                        totalPayCount = totalPayCount + channel.getPayCount();
                        totalRefundCount = totalRefundCount + channel.getRefundCount();
                        break;
                    }
                }
                payChannels.add(channel);
            }
        }

        if(totalAmount.doubleValue() != 0){
            payTypeProportionHandle(payChannels,totalPayAmount);
        }

        //计算汇总行信息
        if(null != payChannels && payChannels.size() > 0){
            GetPayChannelProportionResponse.PayChannel channelSum = new GetPayChannelProportionResponse.PayChannel();
            channelSum.setPayType("");
            channelSum.setPayTypeName("汇总");
            channelSum.setPayAmount(totalPayAmount);
            channelSum.setPayCount(totalPayCount);
            channelSum.setRefundCount(totalRefundCount);
            channelSum.setAmount(totalAmount);
            channelSum.setProportion("");
            payChannels.add(channelSum);
        }

        payChannelProportion.setPayChannels(payChannels);

        return payChannelProportion;
    }

    /**
     * 根据门店Id列表查询支付方式ID集合
     * @param shopId
     * @return
     */
    private Set<String> getPayTypeSetByShopId(Integer orgId,Integer shopId){
        Set<String> payTypeSet = new HashSet<>();
        //1.2.1 获取该门店的结算方式
        val tradeSettlementInfo = tradeSettlementRepository.queryTradeSettlementInfo(orgId,shopId);
        if(null == tradeSettlementInfo){
            throw new BusinessCommonException(TradeErrorCode.TRADE_SETTLEMENT_INFO_EMPTY);
        }
        Integer shopIdTemp = 0;
        if(DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())){
            shopIdTemp = shopId;
        }
        //1.2.2 根据不同的结算方式获取门店支付方式列表
        val tradeMchPayChannelList = payChannelRepository.queryShopPayChannels(orgId,shopIdTemp);

        tradeMchPayChannelList.forEach(tradeMchPayChannel->{
            payTypeSet.add(tradeMchPayChannel.getPayType());
        });
        return payTypeSet;
    }

    /**
     * 支付方式支付金额占比计算方法
     * @param payChannels
     */
    private void payTypeProportionHandle(List<GetPayChannelProportionResponse.PayChannel> payChannels,BigDecimal totalAmount){
        NumberFormat nt = NumberFormat.getPercentInstance();
        //设置百分数精确度2即保留两位小数
        nt.setMinimumFractionDigits(2);
        payChannels.forEach(payChannel -> {
            Double amount = payChannel.getPayAmount().doubleValue();
            Double proportion =amount/totalAmount.doubleValue();
            payChannel.setProportion(nt.format(proportion));
        });
    }

    /**
     * POS端日报汇总统计查询方法
     * @param serialNo:POS机SN
     * @param dateTime:查询时间(2018-01-06)
     * @return
     */
    public GetPayChannelSummaryForPosResponse queryPayChannelSummaryForPos(String serialNo,String dateTime,String startTime,String endTime,String timeMode){
        //1.交易参数信息
        if(StringUtils.isEmpty(serialNo)){
            throw new BusinessCommonException(TradeErrorCode.POS_SN_EMPTY);
        }

        if(!timeMode.equals("1") && !timeMode.equals("2")){
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_TIME_MODE_ERROR);
        }
        if(timeMode.equals("1")){
            if(StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)){

            }else if(StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)){
                throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_START_END_TIME_EMPTY);
            }
        }
        if(timeMode.equals("2")){
            if(StringUtils.isEmpty(dateTime)){
                throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_DATE_TIME_EMPTY);
            }
        }

        //2.查询设备信息
        val deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(serialNo);
        if(null == deviceInfo){
            throw new BusinessCommonException(TradeErrorCode.DEVICE_ORG_EMPTY);
        }
        val orgId = deviceInfo.getOrganizationId();
        val shopId = deviceInfo.getStoreId();

        //3.查询门店结算方式
        val tradeSettlementInfo = tradeSettlementRepository.queryTradeSettlementInfo(orgId,shopId);
        Integer shopIdTemp = 0;
        if(DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())){
            shopIdTemp = shopId;
        }

        //4.查询门店支付渠道列表
        val tradeMchPayChannelList = payChannelRepository.queryShopPayChannels(orgId,shopIdTemp);

        //5.查询系统支付渠道信息
        val tradePayTypeDicts = payTypeRepository.queryPayTypes(null,null);

        //6.查询门店订单汇总信息
//		Date startTime = DateUtils.getDayStartTime(DateUtils.parse(dateTime,DateUtils.FORMAT_SHORT));
//		Date endTime = DateUtils.getDayEndTime(DateUtils.parse(dateTime,DateUtils.FORMAT_SHORT));
        val example = new OrderMasterExample();
        example.createCriteria()
                .andOrgIdEqualTo(orgId)
                .andShopIdEqualTo(shopId)
                //sql里有条件:已付款或退款中或已退款
                //.andPayStatusEqualTo(DictionaryConstant.PAY_STATUS_YFK.getCode())
                .andOrderTypeNotEqualTo(DictionaryConstant.ORDER_TYPE_HYCZ.getCode());

        //3.1处理查询时间
        if(!StringUtils.isEmpty(timeMode)){
            Date startTimeTemp = null;
            Date endTimeTemp = null;
            //时间模式(@1:按开始及结束时间查询@2:按年月查询)
            if(("1").equals(timeMode)){
                if(StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)){
                    startTimeTemp = DateUtils.getDayStartTime(new Date());
                    endTimeTemp = DateUtils.getDayEndTime(new Date());
                }else{
                    if(startTime.length()>10){
                        startTimeTemp = DateUtils.parse(startTime,DateUtils.FORMAT_LONG);
                        endTimeTemp = DateUtils.parse(endTime,DateUtils.FORMAT_LONG);
                    }else{
                        startTimeTemp = DateUtils.getDayStartTime(DateUtils.parse(startTime,DateUtils.FORMAT_SHORT));
                        endTimeTemp = DateUtils.getDayEndTime(DateUtils.parse(endTime,DateUtils.FORMAT_SHORT));
                    }
                }
            }else if(("2").equals(timeMode)){
                if(startTime.length()>10){
                    startTimeTemp = DateUtils.getDayStartTime(DateUtils.parse(dateTime,DateUtils.FORMAT_LONG));
                    endTimeTemp = DateUtils.getDayEndTime(DateUtils.parse(dateTime,DateUtils.FORMAT_LONG));
                }else{
                    startTimeTemp = DateUtils.getDayStartTime(DateUtils.parse(dateTime,DateUtils.FORMAT_SHORT));
                    endTimeTemp = DateUtils.getDayEndTime(DateUtils.parse(dateTime,DateUtils.FORMAT_SHORT));
                }
            }
            example.getOredCriteria().get(0).andPayOrderCreatedAtGreaterThanOrEqualTo(startTimeTemp);
            example.getOredCriteria().get(0).andPayOrderCreatedAtLessThan(endTimeTemp);
        }

        List<Map> payOrderList = orderMasterRepository.queryShopPayOrderList(example);

        //7.整理返回数据
        GetPayChannelSummaryForPosResponse payChannelSummaryForPos = new GetPayChannelSummaryForPosResponse();
        payChannelSummaryForPos.setShopId(shopId);

        BigDecimal totalAmount = new BigDecimal(0);
        List<GetPayChannelSummaryForPosResponse.PayChannel> payChannels = new ArrayList<>();
        for(TradeMchPayChannel tradeMchPayChannel:tradeMchPayChannelList){
            //去掉富茂优惠券和折扣
            if(!tradeMchPayChannel.getPayType().equals(DictionaryConstant.PAY_TYPE_FM_COUPON.getCode())
                    &&!tradeMchPayChannel.getPayType().equals(DictionaryConstant.PAY_TYPE_DISCOUNT.getCode())) {
                GetPayChannelSummaryForPosResponse.PayChannel channel = new GetPayChannelSummaryForPosResponse.PayChannel();
                channel.setPayType(tradeMchPayChannel.getPayType());
                //存入支付方式名称
                tradePayTypeDicts.forEach(tradePayType -> {
                    if (tradeMchPayChannel.getPayType().equals(tradePayType.getPayType())) {
                        channel.setPayTypeName(tradePayType.getName());
                        return;
                    }
                });
                for (Map payOrder : payOrderList) {
                    if (tradeMchPayChannel.getPayType().equals(payOrder.get("payType"))) {
                        channel.setPayAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("payAmount").toString())));
                        channel.setPayCount(Integer.parseInt(payOrder.get("payCount").toString()));
                        channel.setRefundAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("refundAmount").toString())));
                        channel.setRefundCount(Integer.parseInt(payOrder.get("refundCount").toString()));
                        channel.setAmount(BigDecimal.valueOf(Double.valueOf(payOrder.get("amount").toString())));
                        totalAmount = totalAmount.add(BigDecimal.valueOf(Double.valueOf(channel.getAmount())));
                        break;
                    }
                }
                payChannels.add(channel);
            }
        }
        payChannelSummaryForPos.setTotalAmount(totalAmount);
        payChannelSummaryForPos.setPayChannels(payChannels);
        return payChannelSummaryForPos;
    }

    /**
     * 集团交接班统计查询方法
     * @return
     */
    public GetShiftTradeSummaryResponse queryShiftTradeSummary(GetShopTradeSummaryRequest shopTradeSummaryRequest, String customerNumber) {
        logger.debug("queryShiftTradeSummary STARTING......:");
        logger.debug(shopTradeSummaryRequest.getOrgId().toString());
        logger.debug(shopTradeSummaryRequest.getShopIds().toString());
        logger.debug(customerNumber);
        if (null == shopTradeSummaryRequest.getOrgId()) {
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        //1.校验参数信息
        if (StringUtils.isEmpty(shopTradeSummaryRequest.getTimeMode())) {
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_TIME_MODE_EMPTY);
        }
        if (!shopTradeSummaryRequest.getTimeMode().equals("1") && !shopTradeSummaryRequest.getTimeMode().equals("2")) {
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_TIME_MODE_ERROR);
        }
        if (shopTradeSummaryRequest.getTimeMode().equals("1")) {
            if (StringUtils.isEmpty(shopTradeSummaryRequest.getStartTime()) && StringUtils.isEmpty(shopTradeSummaryRequest.getEndTime())) {

            } else if (StringUtils.isEmpty(shopTradeSummaryRequest.getStartTime()) || StringUtils.isEmpty(shopTradeSummaryRequest.getEndTime())) {
                throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_START_END_TIME_EMPTY);
            }
        }
        if (shopTradeSummaryRequest.getTimeMode().equals("2")) {
            if (StringUtils.isEmpty(shopTradeSummaryRequest.getDateTime())) {
                throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_DATE_TIME_EMPTY);
            }
        }

        Integer orgId = shopTradeSummaryRequest.getOrgId();


        //1.查询门店结算方式
        Integer shopIdTemp = 0;
        if(shopTradeSummaryRequest.getShopIds() != null && shopTradeSummaryRequest.getShopIds().size() > 0) {
            Integer shopId = shopTradeSummaryRequest.getShopIds().get(0);
            val tradeSettlementInfo = tradeSettlementRepository.queryTradeSettlementInfo(orgId, shopId);
            if (DictionaryConstant.SETTLEMENT_TYPE_MD.getCode().equals(tradeSettlementInfo.getSettlementType())) {
                shopIdTemp = shopId;
            }
        }

        //3.查询支付渠道信息
        final List<TradeMchPayChannel> payChannelList = new ArrayList<>();
        if(shopIdTemp == 0) {
            val orgPayTypeList = payChannelRepository.queryOrgPayTypeList(orgId);
            orgPayTypeList.forEach(payType -> {
                val tradeMchPayChannel = new TradeMchPayChannel();
                tradeMchPayChannel.setPayType(payType);
                payChannelList.add(tradeMchPayChannel);
            });
        } else {
            payChannelList.addAll(payChannelRepository.queryShopPayChannels(orgId,shopIdTemp));
        }
        //排除不统计的渠道
        Dictionary<String,String> typeColumn = new Hashtable<>();
        typeColumn.put("6","6payType");
        typeColumn.put("24","24payType");
        typeColumn.put("31","31payType");
        typeColumn.put("32","32payType");
        payChannelList.removeIf(o-> typeColumn.get(o.getPayType())==null);

        //4.查询系统支付渠道信息
        val tradePayTypeDicts = payTypeRepository.queryPayTypes(null,null);

        //3.1处理查询时间
        String timeMode = shopTradeSummaryRequest.getTimeMode();
        String startTime = shopTradeSummaryRequest.getStartTime();
        String endTime = shopTradeSummaryRequest.getEndTime();
        String dateTime = shopTradeSummaryRequest.getDateTime();
        Date startTimeTemp = null;
        Date endTimeTemp = null;
        //时间模式(@1:按开始及结束时间查询@2:按年月查询)
        if(("1").equals(timeMode)){
            if(StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)){
                startTimeTemp = DateUtils.getDayStartTime(new Date());
                endTimeTemp = DateUtils.getDayEndTime(new Date());
            }else{
                if(startTime.length()>10){
                    startTimeTemp = DateUtils.parse(startTime,DateUtils.FORMAT_LONG);
                    endTimeTemp = DateUtils.parse(endTime,DateUtils.FORMAT_LONG);
                }else{
                    startTimeTemp = DateUtils.getDayStartTime(DateUtils.parse(startTime,DateUtils.FORMAT_SHORT));
                    endTimeTemp = DateUtils.getDayEndTime(DateUtils.parse(endTime,DateUtils.FORMAT_SHORT));
                }
            }
        }else if(("2").equals(timeMode)){
                startTimeTemp = DateUtils.getDayStartTime(DateUtils.getMonthFirstDay(DateUtils.parse(dateTime,DateUtils.FORMAT_SHORTER)));
                endTimeTemp = DateUtils.getDayEndTime(DateUtils.getMonthLastDay(DateUtils.parse(dateTime,DateUtils.FORMAT_SHORTER)));
        }

        String operator = null;
        if(customerNumber != null && !customerNumber.equals("")) {
            Map<String, Object> param = new HashMap<>();
            param.put("id", customerNumber);
            Map userInfo = orderMasterRepository.queryUsernameByCustomerNumber(param);
            operator = userInfo.get("username").toString();
        }

        GetShiftTradeSummaryResponse getShiftTradeSummaryResponse = new GetShiftTradeSummaryResponse();
        getShiftTradeSummaryResponse.setTotalMemberPoints("0");
        BigDecimal totalAmount = new BigDecimal("0.00");
        List<ShiftTradeSummary> shiftTradeSummaryList = new ArrayList<>();

        List<Map> userLoginLogoutList = orderMasterRepository.selectLoginLogoutRecordList(orgId, startTimeTemp, endTimeTemp, operator);
        logger.info("userLoginLogoutList size="+userLoginLogoutList.size());

        if(userLoginLogoutList != null) {
            //查询交易记录
            //返回结果整理
            for(Map userLoginShiftRecord:userLoginLogoutList) {
                ShiftTradeSummary shiftTradeSummaryResponse = new ShiftTradeSummary();
                shiftTradeSummaryResponse.setShopId((Integer)userLoginShiftRecord.get("shop_id"));
                shiftTradeSummaryResponse.setOperator(userLoginShiftRecord.get("username").toString());
                shiftTradeSummaryResponse.setLoginTime(userLoginShiftRecord.get("loginTime").toString());
                shiftTradeSummaryResponse.setShiftTime(userLoginShiftRecord.get("shiftTime").toString());
                Date loginTime = DateUtils.parse(userLoginShiftRecord.get("loginTime").toString(),DateUtils.FORMAT_LONG);
                Date logoutTime = DateUtils.parse(userLoginShiftRecord.get("shiftTime").toString(),DateUtils.FORMAT_LONG);

                val example = new OrderMasterExample();
                example.createCriteria()
                        .andOdotOrgIdEqualTo(orgId)
                        //sql里有条件 paystatus =2 4 5
                        //.andPayStatusEqualTo(DictionaryConstant.PAY_STATUS_YFK.getCode())
                        //.andOrderTypeNotEqualTo(DictionaryConstant.ORDER_TYPE_HYCZ.getCode())
                        .andOdotCreatedAtGreaterThanOrEqualTo(loginTime)
                        .andOdotCreatedAtLessThan(logoutTime)
                        .andOdotCreatedByEqualTo(shiftTradeSummaryResponse.getOperator());

                List<Map> userWorkList = orderMasterRepository.selectTradeSummary(example);

                Map currentUserMap =null;
                if(userWorkList.size()>0)
                currentUserMap = userWorkList.get(0);
                else continue;
//                for (Map user:userWorkList
//                     ) {
//                    if (user.get("createdBy").toString().equals(userLoginShiftRecord.get("username"))) {
//                        currentUserMap = user;
//                        break;
//                    }
//                }
                List<GetPayChannelSummaryForPosResponse.PayChannel> payChannels = new ArrayList<>();
                for(TradeMchPayChannel tradeMchPayChannel:payChannelList) {
                    GetPayChannelSummaryForPosResponse.PayChannel channel = new GetPayChannelSummaryForPosResponse.PayChannel();
                    channel.setPayType(tradeMchPayChannel.getPayType());
                    //存入支付方式名称
                    tradePayTypeDicts.forEach(tradePayType -> {
                        if (tradeMchPayChannel.getPayType().equals(tradePayType.getPayType())) {
                            channel.setPayTypeName(tradePayType.getName());
                            return;
                        }
                    });
                    String typeColumnName = typeColumn.get(channel.getPayType());
                    BigDecimal paid = new BigDecimal(currentUserMap.get(typeColumnName).toString());
                    BigDecimal refund = new BigDecimal(currentUserMap.get(typeColumnName+"Refund").toString());
                    channel.setPayAmount(paid);
                    channel.setRefundAmount(refund);
                    channel.setAmount(paid.subtract(refund));
                    payChannels.add(channel);
                }
                logger.debug("--------------currentUserMap-----------");
                logger.debug(currentUserMap.get("amPaidAmount").toString());
                logger.debug(currentUserMap.get("pmPaidAmount").toString());
                logger.debug(currentUserMap.get("paidAmount").toString());
                shiftTradeSummaryResponse.setPayChannels(payChannels);
                shiftTradeSummaryResponse.setAmountAM(currentUserMap.get("amPaidAmount").toString());
                shiftTradeSummaryResponse.setAmountPM(currentUserMap.get("pmPaidAmount").toString());
                shiftTradeSummaryResponse.setTotalAmount(currentUserMap.get("paidAmount").toString());
                shiftTradeSummaryList.add(shiftTradeSummaryResponse);
                totalAmount = totalAmount.add(new BigDecimal(shiftTradeSummaryResponse.getTotalAmount()));
            }
        }
        getShiftTradeSummaryResponse.setTotalAmount(totalAmount.toString());
        getShiftTradeSummaryResponse.setCount(shiftTradeSummaryList.size());
        getShiftTradeSummaryResponse.setShiftTradeSummaryList(shiftTradeSummaryList);

        return getShiftTradeSummaryResponse;
    }

    /**
     * 门店交易统计查询方法
     * @return
     */
    public GetShopTradeSummaryResponse queryShopTradeSummary(GetShopTradeSummaryRequest shopTradeSummaryRequest) {
        //从header中获取集团ID
        Integer orgId = RequestHeaderUtil.getOrgIdByRole(shopTradeSummaryRequest.getOrgId());
        if(null == orgId){
            throw new BusinessCommonException(CommonErrorCode.PERMISSION_ERROR);
        }
        if(null == shopTradeSummaryRequest.getShopIds() || shopTradeSummaryRequest.getShopIds().size() == 0){
            shopTradeSummaryRequest.setShopIds(RequestHeaderUtil.getStoreIds());
        }

        //1.校验参数信息
        if(StringUtils.isEmpty(shopTradeSummaryRequest.getTimeMode())){
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_TIME_MODE_EMPTY);
        }
        if(!shopTradeSummaryRequest.getTimeMode().equals("1") && !shopTradeSummaryRequest.getTimeMode().equals("2")){
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_TIME_MODE_ERROR);
        }
        if(shopTradeSummaryRequest.getTimeMode().equals("1")){
            if(StringUtils.isEmpty(shopTradeSummaryRequest.getStartTime()) && StringUtils.isEmpty(shopTradeSummaryRequest.getEndTime())){

            }else if(StringUtils.isEmpty(shopTradeSummaryRequest.getStartTime()) || StringUtils.isEmpty(shopTradeSummaryRequest.getEndTime())){
                throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_START_END_TIME_EMPTY);
            }
        }
        if(shopTradeSummaryRequest.getTimeMode().equals("2")){
            if(StringUtils.isEmpty(shopTradeSummaryRequest.getDateTime())){
                throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_DATE_TIME_EMPTY);
            }
        }

        //3.查询门店订单汇总信息
        val example = new OrderMasterExample();
        example.createCriteria()
                .andOrgIdEqualTo(orgId)
                .andShopIdIn(shopTradeSummaryRequest.getShopIds())
                .andPayStatusEqualTo(DictionaryConstant.PAY_STATUS_YFK.getCode());
//				.andOrderTypeNotEqualTo(DictionaryConstant.ORDER_TYPE_HYCZ.getCode());

		//3.1处理查询时间
		if(!StringUtils.isEmpty(shopTradeSummaryRequest.getTimeMode())){
			Date startTime = null;
			Date endTime = null;
			//时间模式(@1:按开始及结束时间查询@2:按年月查询)
			if(("1").equals(shopTradeSummaryRequest.getTimeMode())){
				if(StringUtils.isEmpty(shopTradeSummaryRequest.getStartTime()) && StringUtils.isEmpty(shopTradeSummaryRequest.getEndTime())){
					startTime = DateUtils.getDayStartTime(new Date());
					endTime = DateUtils.getDayEndTime(new Date());
				}else{
					startTime = DateUtils.getDayStartTime(DateUtils.parse(shopTradeSummaryRequest.getStartTime(),DateUtils.FORMAT_SHORT));
					endTime = DateUtils.getDayEndTime(DateUtils.parse(shopTradeSummaryRequest.getEndTime(),DateUtils.FORMAT_SHORT));
				}
			}else if(("2").equals(shopTradeSummaryRequest.getTimeMode())){
				startTime = DateUtils.getMonthFirstDay(DateUtils.parse(shopTradeSummaryRequest.getDateTime(),DateUtils.FORMAT_SHORTER));
				endTime = DateUtils.getDayEndTime(DateUtils.getMonthLastDay(DateUtils.parse(shopTradeSummaryRequest.getDateTime(),DateUtils.FORMAT_SHORTER)));
			}
			example.getOredCriteria().get(0).andCreatedAtGreaterThanOrEqualTo(startTime);
			example.getOredCriteria().get(0).andCreatedAtLessThanOrEqualTo(endTime);
		}
		List<Map> shopTradeOrderList = orderMasterRepository.queryShopTradeOrderList(example);

		//4.整理返回数据
//		BigDecimal orderAmount = new BigDecimal(0.00);  //支付总实收金额
//		BigDecimal totalPayAmount = new BigDecimal(0.00);  //支付总消费金额
//		Integer totalPayCount = 0;  //总消费笔数
//		Integer totalRefundCount = 0;  //总退款笔数
		GetShopTradeSummaryResponse shopTradeSummaryResponse = new GetShopTradeSummaryResponse();
		List<GetShopTradeSummaryResponse.ShopTradeInfo> shopTradeInfos = new ArrayList<>();
		for(Integer shopId:shopTradeSummaryRequest.getShopIds()){
			GetShopTradeSummaryResponse.ShopTradeInfo shopTradeInfo = new GetShopTradeSummaryResponse.ShopTradeInfo();
			shopTradeInfo.setShopId(shopId);
			for(Map shopTradeOrder:shopTradeOrderList){
				if(shopTradeOrder.get("shopId").toString().equals(shopId.toString())){
					shopTradeInfo.setPayAmount(BigDecimal.valueOf(Double.valueOf(shopTradeOrder.get("payAmount").toString())));
					shopTradeInfo.setPayCount(Integer.parseInt(shopTradeOrder.get("payCount").toString()));
					shopTradeInfo.setRefundAmount(BigDecimal.valueOf(Double.valueOf(shopTradeOrder.get("refundAmount").toString())));
					shopTradeInfo.setRefundCount(Integer.parseInt(shopTradeOrder.get("refundCount").toString()));
					shopTradeInfo.setAmount(BigDecimal.valueOf(Double.valueOf(shopTradeOrder.get("amount").toString())));
					shopTradeInfo.setRechargeCount(Integer.parseInt(shopTradeOrder.get("rechargeCount").toString()));
					shopTradeInfo.setRechargeAmount(BigDecimal.valueOf(Double.valueOf(shopTradeOrder.get("rechargeAmount").toString())));
//					orderAmount = orderAmount.add(shopTradeInfo.getAmount());

//					totalPayAmount = totalPayAmount.add(shopTradeInfo.getPayAmount());
//					totalPayCount = totalPayCount+shopTradeInfo.getPayCount();
//					totalRefundCount = totalRefundCount+shopTradeInfo.getRefundCount();
                    break;
                }
            }
            shopTradeInfos.add(shopTradeInfo);
        }


        shopTradeSummaryResponse.setShopTradeInfoList(shopTradeInfos);
        return shopTradeSummaryResponse;
    }

    /**
     * 门店交易统计查询方法
     * @return
     */
    public GetPayTypeTradeSummaryResponse queryPayTypeTradeSummary(GetPayTypeTradeSummaryRequest payTypeTradeSummaryRequest) {
        Integer orgId = payTypeTradeSummaryRequest.getOrgId();
        //1.校验参数信息
        if(StringUtils.isEmpty(payTypeTradeSummaryRequest.getStartTime()) || StringUtils.isEmpty(payTypeTradeSummaryRequest.getEndTime())){
            throw new BusinessCommonException(TradeErrorCode.ORDER_STATISTICS_START_END_TIME_EMPTY);
        }

        //2.查询集团和门店支付方式合集
        List<String> tradeMchPayChannelList = payChannelRepository.queryOrgPayTypeList(orgId);
        List<PayTypeDict> tradePayTypeDicts = payTypeRepository.queryPayTypes(null,null);
        List<GetPayTypeResponse> payTypeList = new ArrayList<>();
        for(int i=0;i<tradeMchPayChannelList.size();i++){
            GetPayTypeResponse payType = new GetPayTypeResponse();
            payType.setPayType(tradeMchPayChannelList.get(i));
            //存入支付方式名称
            tradePayTypeDicts.forEach(tradePayType->{
                if(payType.getPayType().equals(tradePayType.getPayType())){
                    payType.setPayTypeName(tradePayType.getName());
                    return;
                }
            });
            payTypeList.add(payType);
        }


        //4.整理返回数据
        List<GetShopSummaryResponse> shopSummaryList = new ArrayList<>();

        BigDecimal payAmount = BigDecimal.ZERO;
        if(null != payTypeTradeSummaryRequest.getShopIds() && payTypeTradeSummaryRequest.getShopIds().size() > 0){
            //遍历门店ID列表，查询各个门店支付信息
            for(Integer shopId:payTypeTradeSummaryRequest.getShopIds()){
                BigDecimal shopPayAmount = BigDecimal.ZERO;
                Integer shopPayCount = 0;
                GetShopSummaryResponse shopSummary = new GetShopSummaryResponse();
                shopSummary.setShopId(shopId);
                List<Map> shopPayTypeTradeList = queryShopPayTypeTradeList(orgId,shopId,payTypeTradeSummaryRequest.getStartTime(),payTypeTradeSummaryRequest.getEndTime());
                List<GetPayTypeResponse> payTypeTradeList = new ArrayList<>();
                //1.遍历支付方式列表
                for(int i=0;i<payTypeList.size();i++){
                    GetPayTypeResponse payTypeTemp = new GetPayTypeResponse();
                    payTypeTemp.setPayType(payTypeList.get(i).getPayType());
                    payTypeTemp.setPayTypeName(payTypeList.get(i).getPayTypeName());
                    //统计报表不计算折扣和富茂优惠券
                    if(payTypeTemp.getPayType().equals(DictionaryConstant.PAY_TYPE_DISCOUNT.getCode())
                            ||payTypeTemp.getPayType().equals(DictionaryConstant.PAY_TYPE_FM_COUPON.getCode()))
                    {
                        continue;
                    }
                    //2.遍历门店支付方式收入金额
                    for(Map tempMap:shopPayTypeTradeList){
                        if(tempMap.get("payType").toString().equals(payTypeList.get(i).getPayType())){
                            payTypeTemp.setPayAmount(BigDecimal.valueOf(Double.parseDouble(tempMap.get("payAmount").toString())));
                            shopPayAmount = shopPayAmount.add(payTypeTemp.getPayAmount());
                            payTypeTemp.setPayCount(payTypeTemp.getPayCount()+Integer.parseInt(tempMap.get("payCount").toString()));  //支付方式总笔数
                            shopPayCount = shopPayCount+payTypeTemp.getPayCount();
                            break;
                        }
                    }
                    payTypeTradeList.add(payTypeTemp);
                }
                shopSummary.setPayTypeList(payTypeTradeList);
                shopSummary.setPayAmount(shopPayAmount);
                shopSummary.setPayCount(shopPayCount);  //门店消费总笔数
                shopSummaryList.add(shopSummary);
                payAmount = payAmount.add(shopSummary.getPayAmount());
            }
        }

        GetPayTypeTradeSummaryResponse payTypeTradeSummaryResponse = new GetPayTypeTradeSummaryResponse();
        payTypeTradeSummaryResponse.setShopSummaryList(shopSummaryList);
        payTypeTradeSummaryResponse.setTotalAmount(payAmount);
        return payTypeTradeSummaryResponse;
    }

    /**
     * 查询门店支付方式列表
     * @param orgId
     * @param shopId
     * @param startDate
     * @param endDate
     * @return
     */
    private List<Map> queryShopPayTypeTradeList(Integer orgId,Integer shopId,String startDate,String endDate){
        val example = new OrderMasterExample();
        example.createCriteria()
                .andOrgIdEqualTo(orgId)
                .andShopIdEqualTo(shopId)
                //sql里有条件 paystatus =2 4 5
                //.andOrderStatusEqualTo(DictionaryConstant.ORDER_UPDATE_STATUS_YZF.getCode())
                .andOrderTypeNotEqualTo(DictionaryConstant.ORDER_TYPE_HYCZ.getCode());
        //3.1处理查询时间
        example.getOredCriteria().get(0).andPayOrderCreatedAtGreaterThanOrEqualTo(DateUtils.parse(startDate));
        example.getOredCriteria().get(0).andPayOrderCreatedAtLessThan(DateUtils.parse(endDate));
        return orderMasterRepository.queryShopPayTypeTradeList(example);
    }

}
