package com.ruoyi.bar.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.bar.domain.*;
import com.ruoyi.bar.service.*;
import com.ruoyi.brewery.domain.*;
import com.ruoyi.brewery.service.*;
import com.ruoyi.common.constant.CoinObtainType;
import com.ruoyi.common.constant.RewardStatusConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WyPlatformActivity;
import com.ruoyi.system.domain.WyPlatformActivityBeerView;
import com.ruoyi.system.domain.WyPlatformActivityReward;
import com.ruoyi.system.domain.vo.WyActivityVO;
import com.ruoyi.system.domain.vo.WyPlatformActivityVO;
import com.ruoyi.system.service.IWyPlatformActivityBeerService;
import com.ruoyi.system.service.IWyPlatformActivityRewardService;
import com.ruoyi.system.service.IWyPlatformActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bar.mapper.WyBeerQrcodeScanLogMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 酒品二维码扫码记录Service业务层处理
 *
 * @author kxl
 * @date 2025-01-27
 */
@Service
public class WyBeerQrcodeScanLogServiceImpl implements IWyBeerQrcodeScanLogService {
    @Resource
    private WyBeerQrcodeScanLogMapper wyBeerQrcodeScanLogMapper;
    @Resource
    private IWyBeerQrcodeService wyBeerQrcodeService;
    @Resource
    private IWyPlatformActivityBeerService wyPlatformActivityBeerService;
    @Resource
    private IWyPlatformActivityService wyPlatformActivityService;
    @Autowired
    private WyBeerQrcodeScanLogServiceImpl wyBeerQrcodeScanLogService;
    @Autowired
    private IWyActivityService wyActivityService;
    @Resource
    private IWyActivityBeerService wyActivityBeerService;
    @Resource
    private IWyPlatformActivityRewardService wyPlatformActivityRewardService;
    @Resource
    private IWyActivityRewardService wyActivityRewardService;
    @Resource
    private IWyBarService wyBarService;
    @Resource
    private IWyBarIconService wyBarIconService;
    @Resource
    private IWyBarIconLogService wyBarIconLogService;
    @Resource
    private IWyActivityJoinBatchService wyActivityJoinBatchService;

    @Resource
    private IWyBeerService beerService;

    /**
     * 查询酒品二维码扫码记录
     *
     * @param id 酒品二维码扫码记录主键
     * @return 酒品二维码扫码记录
     */
    @Override
    public WyBeerQrcodeScanLog selectWyBeerQrcodeScanLogById(Long id) {
        return wyBeerQrcodeScanLogMapper.selectWyBeerQrcodeScanLogById(id);
    }

    /**
     * 查询酒品二维码扫码记录列表
     *
     * @param wyBeerQrcodeScanLog 酒品二维码扫码记录
     * @return 酒品二维码扫码记录
     */
    @Override
    public List<WyBeerQrcodeScanLog> selectWyBeerQrcodeScanLogList(WyBeerQrcodeScanLog wyBeerQrcodeScanLog) {
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogMapper.selectWyBeerQrcodeScanLogList(wyBeerQrcodeScanLog);
        wyBeerQrcodeScanLogs.forEach(log -> {
            final WyBar wyBar = wyBarService.selectWyBarById(log.getBarId());
            if (wyBar != null) {
                log.setBarNumber(wyBar.getBarName());
            }
            final WyBeer beer = beerService.selectWyBeerById(log.getBeerId());
            if (beer != null) {
                log.setBeerName(beer.getBeerName());
            }
        });
        return wyBeerQrcodeScanLogs;
    }

    /**
     * 新增酒品二维码扫码记录
     *
     * @param wyBeerQrcodeScanLog 酒品二维码扫码记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWyBeerQrcodeScanLog(WyBeerQrcodeScanLog wyBeerQrcodeScanLog) {
        WyBeerQrcode wyBeerQrcode = wyBeerQrcodeService.selectWyBeerQrcodeByCode(wyBeerQrcodeScanLog.getSkuSn());
        if (wyBeerQrcode == null) {
            throw new RuntimeException("未找到对应的酒品二维码");
        }
        if (wyBeerQrcode.getScanTimes() == null) {
            wyBeerQrcode.setScanTimes(1L);
        } else {
            wyBeerQrcode.setScanTimes(wyBeerQrcode.getScanTimes() + 1); // 扫码次数加1
        }
        //核销
        if (wyBeerQrcodeScanLog.getScanStatus() == 1) {
            wyBeerQrcode.setQrcodeStatus(1); // 状态改为已核销
            wyBeerQrcode.setVerifyTime(DateUtils.getNowDate());
        }
        wyBeerQrcodeService.updateWyBeerQrcode(wyBeerQrcode);
        // 核销动作
        if (wyBeerQrcodeScanLog.getScanStatus() == 1) {
            //检查是否发过核销奖励（啤酒币）
            final WyBarIconLog wyBarIconLog = new WyBarIconLog();
            wyBarIconLog.setBarId(wyBeerQrcodeScanLog.getBarId());
            wyBarIconLog.setBeerId(wyBeerQrcode.getBeerId());
            wyBarIconLog.setCreateTime(DateUtils.getNowDate());
            wyBarIconLog.setCodeId(wyBeerQrcode.getId());
            wyBarIconLog.setObtainType(CoinObtainType.Consume); // 扫码获得
            final List<WyBarIconLog> wyBarIconLogs = wyBarIconLogService.selectWyBarIconLogList(wyBarIconLog);
            //没发过核销奖励
            if (wyBarIconLogs.size() == 0) {
                //发放核销奖励
                rewardBeerIcon(wyBeerQrcodeScanLog.getBreweryId(), "核销奖励啤酒币",
                        wyBeerQrcodeScanLog.getBarId(),
                        wyBeerQrcode.getVerifyIconAmount(),
                        CoinObtainType.Consume, wyBeerQrcodeScanLog.getCodeId()); //核销奖励
            }
            //存在要参加的活动
            if (wyBeerQrcodeScanLog.getActivityId() != null) {
                // ********** 处理活动 **********
                recordActivityJoined(wyBeerQrcodeScanLog);
                checkBatchScanCount(wyBeerQrcodeScanLog);//检查是否超限
            }

        }
        wyBeerQrcodeScanLog.setCreateTime(DateUtils.getNowDate());
        return wyBeerQrcodeScanLogMapper.insertWyBeerQrcodeScanLog(wyBeerQrcodeScanLog);
    }

    /**
     * 记录活动参与记录
     */
    @Transactional
    public void recordActivityJoined(WyBeerQrcodeScanLog qrcodeScanLog) {
        if (qrcodeScanLog.getActivitySupplier() == 1) {
            //处理品牌方奖励
            final WyActivity activity = wyActivityService.selectWyActivityById(qrcodeScanLog.getActivityId());
            //检查活动参与次数
            final WyActivityJoinBatch query = new WyActivityJoinBatch();
            query.setActivityId(qrcodeScanLog.getActivityId());
            query.setBarId(qrcodeScanLog.getBarId());
            query.setActivitySupplier(qrcodeScanLog.getActivitySupplier());
            final List<WyActivityJoinBatch> wyActivityJoinBatches = wyActivityJoinBatchService.selectWyActivityJoinBatchList(query);
            final int joinCount = wyActivityJoinBatches.size();
            if (activity.getActivityType() == 1) { //定向活动,判断参与次数
                if (joinCount > 0 && wyActivityJoinBatches.get(0).getJoinStatus() != 1) {
                    throw new RuntimeException("定向活动已达到最大参与次数");
                }
            }
            if (wyActivityJoinBatches.size() > 0 && wyActivityJoinBatches.get(0).getJoinStatus() == 1) {//如果存在进行中的活动批次，直接使用该批次
                qrcodeScanLog.setJoinBatchId(wyActivityJoinBatches.get(0).getBatchId());
            } else {
                final WyActivityJoinBatch joinBatch = createQrcodeBatch(qrcodeScanLog);
                qrcodeScanLog.setJoinBatchId(joinBatch.getBatchId());
            }
            final int batchScanCount = getEffectiveScanCount(qrcodeScanLog.getBeerId(), qrcodeScanLog.getBarId(), qrcodeScanLog.getActivityId(), 1, qrcodeScanLog.getJoinBatchId());
            if (batchScanCount >= (activity.getActivityTarget() - 1)) {
                implementBreweryAward(qrcodeScanLog); //发放奖励
                //
                // 处理批次参与状态
                //
                final WyActivityJoinBatch wyActivityJoinBatch = wyActivityJoinBatchService.selectWyActivityJoinBatchByBatchId(qrcodeScanLog.getJoinBatchId());
                wyActivityJoinBatch.setJoinStatus(2); // 已完成
                wyActivityJoinBatchService.updateWyActivityJoinBatch(wyActivityJoinBatch);
            }
            //更新活动参与次数
            wyActivityService.updateActivityPopularity(qrcodeScanLog.getActivityId());
        } else if (qrcodeScanLog.getActivitySupplier() == 0) {
            //处理平台的奖励
            final WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(qrcodeScanLog.getActivityId());
            //检查活动参与次数
            final WyActivityJoinBatch query = new WyActivityJoinBatch();
            query.setActivityId(qrcodeScanLog.getActivityId());
            query.setBarId(qrcodeScanLog.getBarId());
            query.setActivitySupplier(qrcodeScanLog.getActivitySupplier());
            final List<WyActivityJoinBatch> wyActivityJoinBatches = wyActivityJoinBatchService.selectWyActivityJoinBatchList(query);
            if (wyActivityJoinBatches.size() > 0 && wyActivityJoinBatches.get(0).getJoinStatus() == 1) {//如果存在进行中的活动批次，直接使用该批次
                qrcodeScanLog.setJoinBatchId(wyActivityJoinBatches.get(0).getBatchId());
            } else {
                final WyActivityJoinBatch joinBatch = createQrcodeBatch(qrcodeScanLog);
                qrcodeScanLog.setJoinBatchId(joinBatch.getBatchId());
            }
            final int batchScanCount = getEffectiveScanCount(qrcodeScanLog.getBeerId(), qrcodeScanLog.getBarId(), qrcodeScanLog.getActivityId(), 0, qrcodeScanLog.getJoinBatchId());
            if (batchScanCount >= (platformActivity.getActivityTarget() - 1)) {
                implementPlatformAward(qrcodeScanLog); //发放奖励
                //
                // 处理批次参与状态
                //
                final WyActivityJoinBatch wyActivityJoinBatch = wyActivityJoinBatchService.selectWyActivityJoinBatchByBatchId(qrcodeScanLog.getJoinBatchId());
                wyActivityJoinBatch.setJoinStatus(2); // 已完成
                wyActivityJoinBatchService.updateWyActivityJoinBatch(wyActivityJoinBatch);
            }
            wyPlatformActivityService.updateActivityPopularity(qrcodeScanLog.getActivityId());
        } else {
            throw new RuntimeException("活动供应商类型错误");
        }

    }

    /**
     * 创建新批次
     *
     * @param qrcodeScanLog 扫码记录
     * @return 批次
     */
    private WyActivityJoinBatch createQrcodeBatch(WyBeerQrcodeScanLog qrcodeScanLog) {
        final WyActivityJoinBatch joinBatch = new WyActivityJoinBatch();
        joinBatch.setBreweryId(qrcodeScanLog.getBreweryId());
        joinBatch.setActivityId(qrcodeScanLog.getActivityId());
        joinBatch.setBarId(qrcodeScanLog.getBarId());
        joinBatch.setActivitySupplier(qrcodeScanLog.getActivitySupplier());
        joinBatch.setJoinStatus(1); // 参与状态
        String remark = "";
        if (qrcodeScanLog.getActivitySupplier() == 0) {
            final WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(qrcodeScanLog.getActivityId());
            remark = platformActivity.getActivityName();
        }
        if (qrcodeScanLog.getActivitySupplier() == 1) {
            final WyActivity activity = wyActivityService.selectWyActivityById(qrcodeScanLog.getActivityId());
            remark = activity.getActivityName();
        }
        joinBatch.setRemark(remark);
        final List<WyActivityJoinBatch> batches = wyActivityJoinBatchService.selectWyActivityJoinBatchList(joinBatch);
        if (batches.size() > 0) {
            throw new RuntimeException("该活动已存在进行中的批次,请勿重复创建");
        }
        wyActivityJoinBatchService.insertWyActivityJoinBatch(joinBatch);
        return joinBatch;
    }

    /**
     * 检查扫码记录数是否达标
     *
     * @param qrcodeScanLog 核销记录
     */
    private void checkBatchScanCount(WyBeerQrcodeScanLog qrcodeScanLog) {
        WyBeerQrcodeScanLog query = new WyBeerQrcodeScanLog();
        query.setJoinBatchId(qrcodeScanLog.getJoinBatchId());
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = this.selectWyBeerQrcodeScanLogList(query);
        if (qrcodeScanLog.getActivitySupplier() == 0) {
            final WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(qrcodeScanLog.getActivityId());
            if (wyBeerQrcodeScanLogs.size() >= platformActivity.getActivityTarget()) {
                throw new RuntimeException("当前批次核销记录已达标，无法再次累计");
            }
        }
        if (qrcodeScanLog.getActivitySupplier() == 1) {
            final WyActivity activity = wyActivityService.selectWyActivityById(qrcodeScanLog.getActivityId());
            if (wyBeerQrcodeScanLogs.size() >= activity.getActivityTarget()) {
                throw new RuntimeException("当前批次核销记录已达标，无法再次累计");
            }
        }
    }

    /**
     * 某款酒扫码核销次数
     */
    public int getBeerScanCount(Long beerId, HashMap<String, Object> params) {
        final WyBeerQrcodeScanLog wyBeerQrcodeScanLogQuery = new WyBeerQrcodeScanLog();
        wyBeerQrcodeScanLogQuery.setBeerId(beerId);
        wyBeerQrcodeScanLogQuery.setScanStatus(1);
        wyBeerQrcodeScanLogQuery.setParams(params);
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogList(wyBeerQrcodeScanLogQuery);
        return wyBeerQrcodeScanLogs.size();
    }

    /**
     * 获取有效扫码数量
     */
    public int getEffectiveScanCount(Long beerId, Long barId, Long activityId, Integer supplier, Long batchId) {
        if (batchId == null) {
            throw new RuntimeException("批次id不能为空");
        }
        final WyBeerQrcodeScanLog wyBeerQrcodeScanLogQuery = new WyBeerQrcodeScanLog();
        wyBeerQrcodeScanLogQuery.setBeerId(beerId);
        wyBeerQrcodeScanLogQuery.setBarId(barId);
        wyBeerQrcodeScanLogQuery.setActivityId(activityId);
        wyBeerQrcodeScanLogQuery.setScanStatus(1);
        wyBeerQrcodeScanLogQuery.setActivitySupplier(supplier);
        wyBeerQrcodeScanLogQuery.setJoinBatchId(batchId);
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanLogList(wyBeerQrcodeScanLogQuery);
        return wyBeerQrcodeScanLogs.size();
    }

    /**
     * 获取有效扫码数量
     */
    public int getEffectiveScanCount(Long beerId, Long barId, Long activityId, Integer supplier) {
        Long batchId = null;
        final WyActivityJoinBatch batchQuery = new WyActivityJoinBatch();
        batchQuery.setActivityId(activityId);
        batchQuery.setActivitySupplier(supplier);
        batchQuery.setBarId(barId);
        batchQuery.setJoinStatus(1);
        final List<WyActivityJoinBatch> wyActivityJoinBatches = wyActivityJoinBatchService.selectWyActivityJoinBatchList(batchQuery);
        if (wyActivityJoinBatches.size() > 0) {
            batchId = wyActivityJoinBatches.get(0).getBatchId();
            return getEffectiveScanCount(beerId, barId, activityId, supplier, batchId);
        } else {
//            wyActivityJoinBatchService.insertWyActivityJoinBatch(batchQuery);
//            batchId = batchQuery.getBatchId();
            return 0;
        }
    }

    /**
     * 查询平台方活动列表
     *
     * @param beerId 酒款id
     * @param barId  酒吧id
     * @return 活动列表
     */
    @Override
    public List<WyPlatformActivityVO> getPlatformActivityByBeerId(Long beerId, Long barId) {
        final WyBar barInfo = wyBarService.selectWyBarById(barId);
        List<WyPlatformActivityVO> platformActivities = new ArrayList<>();
        //校验是否符合无门槛活动条件
        final WyPlatformActivity wyPlatformActivityNoLimit = new WyPlatformActivity();
        wyPlatformActivityNoLimit.setBeerScope(0);
        wyPlatformActivityNoLimit.setActivityStatus(1);
        final List<WyPlatformActivity> wyPlatformActivities = wyPlatformActivityService.selectWyPlatformActivityList(wyPlatformActivityNoLimit);
        if (wyPlatformActivities != null && wyPlatformActivities.size() > 0) {
            wyPlatformActivities.forEach(item -> {
                boolean joinFlag = true;
                if (item.getCity() != null && !item.getCity().equals("全国")) {//判断活动区域限制
                    if (checkCity(barInfo, item.getCity())) {
                        System.out.println("活动地区不匹配");
                        System.out.println("barInfo.getCity()=" + barInfo.getCity());
                        System.out.println("item.getCity()=" + item.getCity());
                        joinFlag = false;
                    }
                }
                if (item.getStartDate().after(new Date())) {//判断活动是否未开始
                    System.out.println("活动已未开始");
                    joinFlag = false;
                }
                if (item.getEndDate().before(new Date())) {//判断活动是否已经结束
                    System.out.println("活动已结束");
                    joinFlag = false;
                }
                if (joinFlag) {
                    //最后参加的批次
                    final WyActivityJoinBatch query = new WyActivityJoinBatch();
                    query.setActivityId(item.getId());
                    query.setBarId(barId);
                    query.setActivitySupplier(0);
                    final WyActivityJoinBatch lastJoinBatch = wyActivityJoinBatchService.selectLastJoinBatch(query);
                    //
                    final WyPlatformActivityVO vo = new WyPlatformActivityVO();
                    BeanUtils.copyProperties(item, vo);
                    vo.setBarAwardStatus(false);
                    if (lastJoinBatch == null) {
                        vo.setBarCount(0);
                        vo.setRemainingBeerCount(Math.toIntExact(item.getActivityTarget()));
                    } else {
                        //获取有效扫码数量
                        vo.setBarCount(getEffectiveScanCount(beerId, barId, item.getId(), 0, lastJoinBatch.getBatchId()));
                        vo.setRemainingBeerCount((int) (item.getActivityTarget() - vo.getBarCount()));
                        //判断是否已经获取过奖励
                        final WyPlatformActivityReward wyPlatformActivityReward = wyPlatformActivityRewardService.selectWyPlatformActivityRewardByActivityIdAndBarId(item.getId(), barId, lastJoinBatch.getBatchId());
                        if (wyPlatformActivityReward != null) {
                            vo.setBarAwardStatus(true);
                        }
                    }
                    platformActivities.add(vo);
                }
            });
        }
        //通过酒款id检查是否符合有门槛活动条件
        final WyPlatformActivityBeerView wyPlatformActivityBeerView = new WyPlatformActivityBeerView();
        wyPlatformActivityBeerView.setBeerId(beerId);
        wyPlatformActivityBeerView.setActivityStatus(1);
        final List<WyPlatformActivityBeerView> wyPlatformActivityBeerViews = wyPlatformActivityBeerService.selectWyPlatformActivityBeerViewList(wyPlatformActivityBeerView);
        if (wyPlatformActivityBeerViews != null && wyPlatformActivityBeerViews.size() > 0) {
            wyPlatformActivityBeerViews.forEach(item -> {
                WyPlatformActivity wyPlatformActivity = wyPlatformActivityService.selectWyPlatformActivityById(item.getActivityId());
                if (wyPlatformActivity != null) {
                    boolean joinFlag = true;
                    if (wyPlatformActivity.getCity() != null && !wyPlatformActivity.getCity().equals("全国")) {//判断活动区域限制
//                        if (barInfo.getCity() == null || barInfo.getCity().equals("") || !wyPlatformActivity.getCity().equals(barInfo.getCity())) {
                        if (checkCity(barInfo, wyPlatformActivity.getCity())) {
                            System.out.println("活动地区不匹配");
                            joinFlag = false;
                        }
                    }
                    if (wyPlatformActivity.getStartDate().after(new Date())) {//判断活动是否未开始
                        System.out.println("活动已未开始");
                        joinFlag = false;
                    }
                    if (wyPlatformActivity.getEndDate().before(new Date())) {//判断活动是否已经结束
                        System.out.println("活动已结束");
                        joinFlag = false;
                    }
                    if (joinFlag) {
                        //最后参加的批次
                        final WyActivityJoinBatch query = new WyActivityJoinBatch();
                        query.setActivityId(item.getActivityId());
                        query.setBarId(barId);
                        query.setActivitySupplier(0);
                        final WyActivityJoinBatch lastJoinBatch = wyActivityJoinBatchService.selectLastJoinBatch(query);
                        //
                        final WyPlatformActivityVO vo = new WyPlatformActivityVO();
                        BeanUtils.copyProperties(wyPlatformActivity, vo);
                        vo.setBarAwardStatus(false);
                        if (lastJoinBatch == null) {
                            vo.setBarCount(0);
                            vo.setRemainingBeerCount(Math.toIntExact(wyPlatformActivity.getActivityTarget()));
                        } else {
                            //获取有效扫码数量
                            vo.setBarCount(getEffectiveScanCount(beerId, barId, item.getActivityId(), 0, lastJoinBatch.getBatchId()));
                            vo.setRemainingBeerCount((int) (wyPlatformActivity.getActivityTarget() - vo.getBarCount()));
                            //判断是否已经获取过奖励
                            final WyPlatformActivityReward wyPlatformActivityReward = wyPlatformActivityRewardService.selectWyPlatformActivityRewardByActivityIdAndBarId(item.getActivityId(), barId, lastJoinBatch.getBatchId());
                            if (wyPlatformActivityReward != null) {
                                vo.setBarAwardStatus(true);
                            }
                        }
                        platformActivities.add(vo);
                    }
                }
            });
        }
        return platformActivities;
    }

    /**
     * 判断是否符合活动地区
     */
    private static boolean checkCity(WyBar barInfo, String city) {
        return barInfo.getCity() == null || barInfo.getCity().equals("") || barInfo.getProvince() == null || barInfo.getProvince().equals("") || !city.equals(barInfo.getProvince() + "/" + barInfo.getCity());
    }

    /**
     * 查询品牌方活动列表
     *
     * @param beerId 酒款id
     * @param barId  酒吧id
     * @return 活动列表
     */
    @Override
    public List<WyActivityVO> getBreweryActivityByBeerId(Long beerId, Long barId) {
        final WyBar barInfo = wyBarService.selectWyBarById(barId);
        List<WyActivityVO> activities = new ArrayList<>();
        //处理无门槛品牌方活动
        final WyActivity activity = new WyActivity();
        activity.setBeerScope(0);
        activity.setActivityStatus(1);
        final List<WyActivity> wyActivities = wyActivityService.selectWyActivityList(activity);
        if (wyActivities != null && wyActivities.size() > 0) {
            wyActivities.forEach(item -> {
                Boolean joinFlag = true;
                if (item.getActivityType() == 1) { //定向活动,判断能否参与
                    final List<WyActivityBar> wyActivityBars = wyActivityService.selectWyActivityBarList(new WyActivityBar(item.getId(), barId));
                    if (wyActivityBars == null || wyActivityBars.size() == 0) {
                        System.out.println("定向活动，该酒吧不参与该活动");
                        joinFlag = false;
                    }
                } else {
                    if (item.getCity() != null && !item.getCity().equals("全国")) {//判断活动区域限制
//                        if (barInfo.getCity() == null || barInfo.getCity().equals("") || !item.getCity().equals(barInfo.getCity())) {
                        if (checkCity(barInfo, item.getCity())) {
                            System.out.println("活动地区不匹配");
                            joinFlag = false;
                        }
                    }
                }
                if (item.getStartDate().after(new Date())) {//判断活动是否未开始
                    System.out.println("活动已结束，该酒吧不参与该活动");
                    joinFlag = false;
                }
                if (item.getEndDate().before(new Date())) {//判断活动是否已经结束
                    System.out.println("活动已结束，该酒吧不参与该活动");
                    joinFlag = false;
                }
                if (joinFlag) {
                    //最后参加的批次
                    final WyActivityJoinBatch query = new WyActivityJoinBatch();
                    query.setActivityId(item.getId());
                    query.setBarId(barId);
                    query.setActivitySupplier(1);
                    final WyActivityJoinBatch lastJoinBatch = wyActivityJoinBatchService.selectLastJoinBatch(query);
                    //
                    final WyActivityVO vo = new WyActivityVO();
                    BeanUtils.copyProperties(item, vo);
                    vo.setBarAwardStatus(false);
                    if (lastJoinBatch == null) {
                        vo.setBarCount(0);
                        vo.setRemainingBeerCount(item.getActivityTarget());
                    } else {
                        //获取有效扫码数量
                        vo.setBarCount(getEffectiveScanCount(beerId, barId, item.getId(), 1, lastJoinBatch.getBatchId()));
                        vo.setRemainingBeerCount(item.getActivityTarget() - vo.getBarCount());
                        //判断是否已经获取过奖励
                        final WyActivityReward wyActivityReward = wyActivityRewardService.selectWyActivityRewardByActivityIdAndBarId(item.getId(), barId, lastJoinBatch.getBatchId());
                        if (wyActivityReward != null) {
                            vo.setBarAwardStatus(true);
                        }
                    }
                    vo.setBrandLogo(item.getBrandLogo());
                    vo.setBreweryName(item.getBreweryName());
                    activities.add(vo);
                }
            });
        }
        //处理有门槛品牌方活动
        final WyActivityBeer wyActivityBeer = new WyActivityBeer();
        wyActivityBeer.setBeerId(beerId);
        final List<WyActivityBeer> wyActivityBeers = wyActivityBeerService.selectWyActivityBeerList(wyActivityBeer);
        if (wyActivityBeers != null && wyActivityBeers.size() > 0) {
            wyActivityBeers.forEach(item -> {
                WyActivity wyActivity = wyActivityService.selectWyActivityById(item.getActivityId());
                if (wyActivity != null) {
                    Boolean joinFlag = true;
                    if (wyActivity.getActivityType() == 1) { //定向活动,判断能否参与
                        final List<WyActivityBar> wyActivityBars = wyActivityService.selectWyActivityBarList(new WyActivityBar(wyActivity.getId(), barId));
                        if (wyActivityBars == null || wyActivityBars.size() == 0) {
                            System.out.println("定向活动，该酒吧不参与该活动");
                            joinFlag = false;
                        }
                    } else {
                        if (wyActivity.getCity() != null && !wyActivity.getCity().equals("全国")) {//判断活动区域限制
//                            if (barInfo.getCity() == null || barInfo.getCity().equals("") || !wyActivity.getCity().equals(barInfo.getCity())) {
                            if (checkCity(barInfo, wyActivity.getCity())) {
                                System.out.println("活动地区不匹配");
                                joinFlag = false;
                            }
                        }
                    }
                    if (wyActivity.getStartDate().after(new Date())) {//判断活动是否未开始
                        System.out.println("活动已结束，该酒吧不参与该活动");
                        joinFlag = false;
                    }
                    if (wyActivity.getEndDate().before(new Date())) {//判断活动是否已经结束
                        System.out.println("活动已结束，该酒吧不参与该活动");
                        joinFlag = false;
                    }
                    if (joinFlag) {
                        //最后参加的批次
                        final WyActivityJoinBatch query = new WyActivityJoinBatch();
                        query.setActivityId(item.getActivityId());
                        query.setBarId(barId);
                        query.setActivitySupplier(1);
                        final WyActivityJoinBatch lastJoinBatch = wyActivityJoinBatchService.selectLastJoinBatch(query);
                        //
                        final WyActivityVO vo = new WyActivityVO();
                        BeanUtils.copyProperties(wyActivity, vo);
                        vo.setBarAwardStatus(false);
                        if (lastJoinBatch == null) {
                            vo.setBarCount(0);
                            vo.setRemainingBeerCount(wyActivity.getActivityTarget());
                        } else {
                            //获取有效扫码数量
                            vo.setBarCount(getEffectiveScanCount(beerId, barId, item.getActivityId(), 1, lastJoinBatch.getBatchId()));
                            vo.setRemainingBeerCount(wyActivity.getActivityTarget() - vo.getBarCount());
                            //判断是否已经获取过奖励
                            final WyActivityReward wyActivityReward = wyActivityRewardService.selectWyActivityRewardByActivityIdAndBarId(item.getActivityId(), barId, lastJoinBatch.getBatchId());
                            if (wyActivityReward != null) {
                                vo.setBarAwardStatus(true);
                            }
                        }
                        vo.setBrandLogo(wyActivity.getBrandLogo());
                        vo.setBreweryName(wyActivity.getBreweryName());
                        activities.add(vo);
                    }
                }
            });
        }
        return activities;
    }

    /**
     * 门店参与的活动列表
     *
     * @param barId  酒吧id
     * @param status 活动参与状态
     * @return 门店参与的活动列表
     */
    @Override
    public HashMap<String, Object> getActivityByBarId(Long barId, Integer status, Long breweryId) {
        if (status != null) {
            if (status != 1 && status != 2) {
                throw new RuntimeException("活动参与状态错误");
            }
        }
        final WyActivityJoinBatch wyActivityJoinBatchQuery = new WyActivityJoinBatch();
        wyActivityJoinBatchQuery.setBarId(barId);
        if (status != null) {
            wyActivityJoinBatchQuery.setJoinStatus(status);
        }
        final List<WyActivityJoinBatch> joinBatches = wyActivityJoinBatchService.selectWyActivityJoinBatchList(wyActivityJoinBatchQuery);
        List<WyActivityVO> activities = new ArrayList<>();
        List<WyPlatformActivityVO> platformActivities = new ArrayList<>();
        //按批次处理参加过的活动
        joinBatches.forEach(item -> {
            final BatchJoined batchJoined = selectActivityByBatchId(item.getBatchId());
            int remainingBeerCount = 0;
            //平台活动
            if (item.getActivitySupplier() == 0) {
                if (breweryId == null || breweryId == 0) { //不限制品牌方
                    final WyActivityBeer wyActivityBeer = new WyActivityBeer();
                    wyActivityBeer.setActivityId(item.getActivityId());
                    final WyPlatformActivity wyPlatformActivity = wyPlatformActivityService.selectWyPlatformActivityById(item.getActivityId());
                    if (wyPlatformActivity != null) {
                        final WyPlatformActivityVO vo = new WyPlatformActivityVO();
                        BeanUtils.copyProperties(wyPlatformActivity, vo);
                        vo.setBarCount(batchJoined.getScanNum());     //获取有效扫码数量
                        remainingBeerCount = (int) (wyPlatformActivity.getActivityTarget() - batchJoined.getScanNum());
                        if (remainingBeerCount < 0) {
                            remainingBeerCount = 0;
                        }
                        vo.setBeerIds(wyPlatformActivity.getBeerIds());
                        vo.setBeers(wyPlatformActivity.getBeers());
                        vo.setRemainingBeerCount(remainingBeerCount);
                        vo.setBarAwardStatus(item.getJoinStatus() == 2);
                        vo.setBatchId(item.getBatchId());
                        System.out.println("门店参与的活动列表:platformActivities:" + remainingBeerCount);
                        platformActivities.add(vo);
                    }
                }
            } else {//品牌活动
                final WyActivity wyActivity = wyActivityService.selectWyActivityById(item.getActivityId());
                if (wyActivity != null) {
                    Boolean joinFlag = true;
                    if (breweryId != null && breweryId != 0 && !breweryId.equals(wyActivity.getBreweryId())) {
                        joinFlag = false; //限制品牌方
                    }
                    if (joinFlag) {
                        final WyActivityVO vo = new WyActivityVO();
                        BeanUtils.copyProperties(wyActivity, vo);
                        vo.setBarCount(batchJoined.getScanNum());     //获取有效扫码数量
                        remainingBeerCount = wyActivity.getActivityTarget() - batchJoined.getScanNum();
                        if (remainingBeerCount < 0) {
                            remainingBeerCount = 0;
                        }
                        vo.setBeerIds(wyActivity.getBeerIds());
                        vo.setBeers(wyActivity.getBeers());
                        vo.setRemainingBeerCount(remainingBeerCount);
                        vo.setBarAwardStatus(item.getJoinStatus() == 2);
                        vo.setBatchId(item.getBatchId());
                        System.out.println("门店参与的活动列表:activities:" + remainingBeerCount);
                        activities.add(vo);
                    }
                }
            }
        });
        platformActivities.sort(Comparator.comparing(WyPlatformActivityVO::getRemainingBeerCount)); //升序排列，剩余数量最少的活动排在前面
        activities.sort(Comparator.comparing(WyActivityVO::getRemainingBeerCount)); //升序排列，剩余数量最少的活动排在前面
        //合并两个活动列表
        HashMap<String, Object> map = new HashMap<>();
        map.put("platformActivities", platformActivities);
        map.put("activities", activities);
        return map;
    }

    /**
     * 发布平台活动奖励
     */
    @Transactional
    public void implementPlatformAward(WyBeerQrcodeScanLog qrcodeScanLog) {
        final Long activityId = qrcodeScanLog.getActivityId();
        final Long barId = qrcodeScanLog.getBarId();
        final Long codeId = qrcodeScanLog.getCodeId();
        final WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(activityId);
        //判断是否已经获取过奖励
        final WyPlatformActivityReward wyPlatformActivityReward = new WyPlatformActivityReward();
        wyPlatformActivityReward.setActivityId(activityId);
        wyPlatformActivityReward.setBarId(barId);
        wyPlatformActivityReward.setJoinBatchId(qrcodeScanLog.getJoinBatchId());
        //奖励未发放
        //step3 记录奖励发放记录
        wyPlatformActivityReward.setRewardStatus(RewardStatusConstants.Waiting); //奖励未领取
        wyPlatformActivityRewardService.insertWyPlatformActivityReward(wyPlatformActivityReward);

        //奖励啤酒币，立即发放
        if (platformActivity.getActivityRewardType() == 2) {
            claimPlatformAward(activityId, barId, codeId, qrcodeScanLog.getJoinBatchId());
        }
    }

    /**
     * 发布品票方活动奖励
     */
    @Transactional
    public void implementBreweryAward(WyBeerQrcodeScanLog qrcodeScanLog) {
        final Long activityId = qrcodeScanLog.getActivityId();
        final Long barId = qrcodeScanLog.getBarId();
        final Long breweryId = qrcodeScanLog.getBreweryId();
        final Long codeId = qrcodeScanLog.getCodeId();
        //判断是否已经获取过奖励
        final WyActivityReward wyActivityReward = new WyActivityReward();
        wyActivityReward.setActivityId(activityId);
        wyActivityReward.setBarId(barId);
        wyActivityReward.setBreweryId(breweryId);
        wyActivityReward.setJoinBatchId(qrcodeScanLog.getJoinBatchId());
        final List<WyActivityReward> wyActivityRewards = wyActivityRewardService.selectWyActivityRewardList(wyActivityReward);
        if (wyActivityRewards != null && wyActivityRewards.size() > 0) {
            //奖励已发放
            System.out.println("奖励已发放,无需重复发放");
        } else {
            //奖励未发放
            //step3 记录奖励发放记录
            wyActivityReward.setRewardStatus(RewardStatusConstants.Waiting); //奖励未领取
            wyActivityRewardService.insertWyActivityReward(wyActivityReward);

            WyActivity wyActivity = wyActivityService.selectWyActivityById(activityId);
            //奖励啤酒币，立即发放
            if (wyActivity.getActivityRewardType() == 2) {
                claimBreweryAward(activityId, barId, codeId, qrcodeScanLog.getJoinBatchId());
            }
        }
    }

    /**
     * 领取平台活动奖励
     *
     * @param activityId 活动id
     * @param barId      酒吧id
     */
    public void claimPlatformAward(Long activityId, Long barId, Long codeId, Long joinBatchId) {
        final WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(activityId);
        if (platformActivity == null) {
            throw new RuntimeException("未找到平台活动");
        }
        //判断奖励类型,如果奖励商品
        if (platformActivity.getActivityRewardType() == 1) {
            //奖励商品
            //TODO 添加发送提醒
        } else if (platformActivity.getActivityRewardType() == 2) { //如果奖励啤酒币
            //奖励啤酒币
            final Long activityRewardCount = platformActivity.getActivityRewardCount(); //奖励数量
            rewardBeerIcon(0L, "平台活动奖励,平台活动编号：" + activityId, barId, activityRewardCount, 1, codeId); //活动奖励
            //更新奖励状态
            final WyPlatformActivityReward wyPlatformActivityReward = wyPlatformActivityRewardService.selectWyPlatformActivityRewardByActivityIdAndBarId(activityId, barId, joinBatchId);
            wyPlatformActivityReward.setRewardStatus(RewardStatusConstants.Redeemed); //奖励已领取
            wyPlatformActivityReward.setClaimTime(DateUtils.getNowDate());
            wyPlatformActivityRewardService.updateWyPlatformActivityReward(wyPlatformActivityReward);
        } else {
            throw new RuntimeException("奖励类型错误");
        }
    }

    /**
     * 领取品牌活动奖励
     *
     * @param activityId 活动id
     * @param barId      酒吧id
     */
    @Transactional
    public void claimBreweryAward(Long activityId, Long barId, Long codeId, Long joinBatchId) {
        final WyActivity activity = wyActivityService.selectWyActivityById(activityId);
        if (activity == null) {
            throw new RuntimeException("未找到品牌方活动");
        }
        //判断奖励类型,如果奖励商品
        if (activity.getActivityRewardType() == 1) {
            //奖励商品
            //TODO 添加发送提醒
        } else if (activity.getActivityRewardType() == 2) { //如果奖励啤酒币
            //奖励啤酒币
            final Integer activityRewardCount = activity.getActivityRewardCount(); //奖励数量
            final String remark = "活动奖励,品牌方活动编号：" + activity.getId() + ",品牌方id：" + activity.getBreweryId();
            rewardBeerIcon(activity.getBreweryId(), remark, barId, Long.valueOf(activityRewardCount), 1, codeId); //活动奖励
            //更新奖励状态
            final WyActivityReward wyActivityReward = wyActivityRewardService.selectWyActivityRewardByActivityIdAndBarId(activityId, barId, joinBatchId);
            wyActivityReward.setRewardStatus(RewardStatusConstants.Redeemed); //奖励已领取
            wyActivityReward.setClaimTime(DateUtils.getNowDate());
            wyActivityRewardService.updateWyActivityReward(wyActivityReward);


        } else {
            throw new RuntimeException("奖励类型错误");
        }
    }

    /**
     * 奖励啤酒币
     */
    private void rewardBeerIcon(Long breweryId, String remark, Long barId, Long activityRewardCount, Integer obtainType, Long codeId) {
        WyBarIcon wyBarIcon = wyBarIconService.selectWyBarIconByBreweryIdAndBarId(breweryId, barId);
        if (wyBarIcon == null) {
            //钱包不存在,创建钱包
            wyBarIcon = new WyBarIcon();
            wyBarIcon.setBreweryId(breweryId);
            wyBarIcon.setBarId(barId);
            wyBarIcon.setBalance(activityRewardCount);
            wyBarIconService.insertWyBarIcon(wyBarIcon);
        } else {
            if (wyBarIcon.getBalance() == null) {
                wyBarIcon.setBalance(0L);
            }
            //钱包存在,增加余额
            wyBarIcon.setBalance(wyBarIcon.getBalance() + activityRewardCount);
            wyBarIconService.updateWyBarIcon(wyBarIcon);
        }
        final WyBarIconLog wyBarIconLog = new WyBarIconLog();    //记录钱包变动日志
        wyBarIconLog.setBarId(barId);
        wyBarIconLog.setBreweryId(breweryId);
        wyBarIconLog.setBalance(wyBarIcon.getBalance());
        wyBarIconLog.setAmount(activityRewardCount);//变动数量
        wyBarIconLog.setRemark(remark);
        wyBarIconLog.setObtainType(obtainType);
        wyBarIconLog.setCodeId(codeId);
        wyBarIconLogService.insertWyBarIconLog(wyBarIconLog);
    }

    /**
     * 修改酒品二维码扫码记录
     *
     * @param wyBeerQrcodeScanLog 酒品二维码扫码记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWyBeerQrcodeScanLog(WyBeerQrcodeScanLog wyBeerQrcodeScanLog) {
        final WyBeerQrcodeScanLog log = this.selectWyBeerQrcodeScanLogById(wyBeerQrcodeScanLog.getBarId());
        if (log.getScanStatus() != 0 && wyBeerQrcodeScanLog.getScanStatus() == 0) {
            //扫码状态变更为未扫码,撤销奖励
            final WyActivityReward wyActivityReward = wyActivityRewardService.selectWyActivityRewardByActivityIdAndBarId(log.getActivityId(), log.getBarId(), log.getJoinBatchId());
            if (wyActivityReward != null) {
                if (Objects.equals(wyActivityReward.getRewardStatus(), RewardStatusConstants.Redeemed)) {
                    throw new RuntimeException("该记录已经领取过奖励，无法撤销");
                } else {
                    wyActivityRewardService.deleteWyActivityRewardById(wyActivityReward.getId());
                }
            }
        }
        wyBeerQrcodeScanLog.setUpdateTime(DateUtils.getNowDate());
        return wyBeerQrcodeScanLogMapper.updateWyBeerQrcodeScanLog(wyBeerQrcodeScanLog);
    }

    /**
     * 批量删除酒品二维码扫码记录
     *
     * @param ids 需要删除的酒品二维码扫码记录主键
     * @return 结果
     */
    @Override
    public int deleteWyBeerQrcodeScanLogByIds(Long[] ids) {
        return wyBeerQrcodeScanLogMapper.deleteWyBeerQrcodeScanLogByIds(ids);
    }

    /**
     * 删除酒品二维码扫码记录信息
     *
     * @param id 酒品二维码扫码记录主键
     * @return 结果
     */
    @Override
    public int deleteWyBeerQrcodeScanLogById(Long id) {
        return wyBeerQrcodeScanLogMapper.deleteWyBeerQrcodeScanLogById(id);
    }

    /**
     * 获取门店参与的活动列表
     *
     * @param barId 酒吧id
     * @return 门店参与的平台活动列表
     */
    @Override
    public List<ActivityJoined> selectActivityByBarId(Long barId) {
        return wyBeerQrcodeScanLogMapper.selectActivityByBarId(barId);
    }

    @Override
    public BatchJoined selectActivityByBatchId(Long batchId) {
        return wyBeerQrcodeScanLogMapper.selectActivityByBatchId(batchId);
    }

    /**
     * 撤销核销记录
     *
     * @param qrcodeId 二维码id
     * @return 结果
     */
    @Transactional
    @Override
    public int cancelBeerQrcodeScanLogByIds(Long qrcodeId) {
        final WyBeerQrcode wyBeerQrcode = wyBeerQrcodeService.selectWyBeerQrcodeById(qrcodeId);
        if (wyBeerQrcode == null) {
            throw new RuntimeException("未找到该二维码");
        }
        if (wyBeerQrcode.getQrcodeStatus() != 1) {
            throw new RuntimeException("该二维码未核销，无法撤销");
        }

        //检查有效扫码记录
        final WyBeerQrcodeScanLog scanLogQuery = new WyBeerQrcodeScanLog();
        scanLogQuery.setCodeId(qrcodeId);
        scanLogQuery.setScanStatus(1);
        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogMapper.selectWyBeerQrcodeScanLogList(scanLogQuery);
        if (wyBeerQrcodeScanLogs == null || wyBeerQrcodeScanLogs.size() == 0) {
            throw new RuntimeException("扫码记录不存在，无法撤销");
        }
        Long barId = wyBeerQrcodeScanLogs.get(0).getBarId();
        wyBeerQrcodeScanLogs.forEach(item -> {
            //参与过活动
            if (item.getActivityId() != null && item.getActivitySupplier() != null) {
                //最后参加的批次
                final WyActivityJoinBatch query = new WyActivityJoinBatch();
                query.setActivityId(item.getActivityId());
                query.setBarId(barId);
                query.setActivitySupplier(item.getActivitySupplier());
                final WyActivityJoinBatch lastJoinBatch = wyActivityJoinBatchService.selectLastJoinBatch(query);
                if (lastJoinBatch != null) {
                    //检查领奖记录
                    if (item.getActivitySupplier() == 1) {
                        final WyActivityReward wyActivityReward = wyActivityRewardService.selectWyActivityRewardByActivityIdAndBarId(item.getActivityId(), item.getBarId(), lastJoinBatch.getBatchId());
                        if (wyActivityReward != null) {
                            throw new RuntimeException("该记录已经领取过奖励，无法撤销");
                        }
                    } else if (item.getActivitySupplier() == 0) {
                        final WyPlatformActivityReward wyPlatformActivityReward = wyPlatformActivityRewardService.selectWyPlatformActivityRewardByActivityIdAndBarId(item.getActivityId(), item.getBarId(), lastJoinBatch.getBatchId());
                        if (wyPlatformActivityReward != null) {
                            throw new RuntimeException("该记录已经领取过奖励，无法撤销");
                        }
                    }
                }
            }
            //撤销扫码记录
            item.setScanStatus(0);
            wyBeerQrcodeScanLogMapper.updateWyBeerQrcodeScanLog(item);
        });
        //撤销啤酒币奖励
        final WyBarIcon wyBarIcon = wyBarIconService.selectWyBarIconByBreweryIdAndBarId(wyBeerQrcode.getBreweryId(), barId);
        if (wyBarIcon != null) {
            wyBarIcon.setBalance(wyBarIcon.getBalance() - wyBeerQrcode.getVerifyIconAmount());
            wyBarIconService.updateWyBarIcon(wyBarIcon);
            final WyBarIconLog wyBarIconLog = new WyBarIconLog();
            wyBarIconLog.setBarId(barId);
            wyBarIconLog.setBreweryId(wyBeerQrcode.getBreweryId());
            wyBarIconLog.setBalance(wyBarIcon.getBalance());
            wyBarIconLog.setAmount(-wyBeerQrcode.getVerifyIconAmount());
            wyBarIconLog.setRemark("撤销核销,酒品二维码编号：" + qrcodeId);
            wyBarIconLogService.insertWyBarIconLog(wyBarIconLog);
        }
        //还原二维码状态
        wyBeerQrcode.setVerifyTime(null);
        wyBeerQrcode.setQrcodeStatus(0);
        wyBeerQrcode.setVerifyCity("");
        wyBeerQrcode.setVerifyIp("");
        return wyBeerQrcodeService.updateWyBeerQrcode(wyBeerQrcode);
    }

    @Override
    public Integer selectWyBeerQrcodeScanCount(WyBeerQrcodeScanLog wyBeerQrcodeScanLog) {
        return wyBeerQrcodeScanLogMapper.selectWyBeerQrcodeScanCount(wyBeerQrcodeScanLog);
    }

    /**
     * 近X天的核销数
     */
    @Override
    public HashMap<String, Object> selectQrcodeScanCountByDays(Long breweryId, Integer lastDays) {
        HashMap<String, Object> map = new HashMap<>();
        WyBeerQrcodeScanLog wyBeerQrcodeScanLog = new WyBeerQrcodeScanLog();
        wyBeerQrcodeScanLog.setBreweryId(breweryId);
        wyBeerQrcodeScanLog.setScanStatus(1);
        final HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        final String LastDay = DateUtils.getTime();
        final String firstDay = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addDays(DateUtils.getNowDate(), -lastDays));
        objectObjectHashMap.put("beginTime", firstDay);
        objectObjectHashMap.put("endTime", LastDay);
        wyBeerQrcodeScanLog.setParams(objectObjectHashMap);

        final List<WyBeerQrcodeScanLog> wyBeerQrcodeScanLogs = wyBeerQrcodeScanLogMapper.selectWyBeerQrcodeScanLogList(wyBeerQrcodeScanLog);
        final Map<Long, List<WyBeerQrcodeScanLog>> logsGroupBarIds = wyBeerQrcodeScanLogs.stream().collect(Collectors.groupingBy(WyBeerQrcodeScanLog::getBarId));
        //
        map.put("scanCount", wyBeerQrcodeScanLogs.size());
        map.put("barCount", logsGroupBarIds.size());
        return map;
    }

    //参与活动的门店数
    @Override
    public Integer selectWyBeerQrcodeBarCount(WyBeerQrcodeScanLog wyBeerQrcodeScanLog) {
        return wyBeerQrcodeScanLogMapper.selectWyBeerQrcodeBarCount(wyBeerQrcodeScanLog);
    }

    @Override
    public List<BarScanCount> selectBarScanCount(WyBeerQrcodeScanLog wyBeerQrcodeScanLog) {
        return wyBeerQrcodeScanLogMapper.selectBarScanCount(wyBeerQrcodeScanLog);
    }


    /**
     * 酒吧扫码数量
     */
    @Override
    public Integer scanCountByBarId(Long barId, Integer lastDays) {
        final String LastDay = DateUtils.getTime();
        final String firstDay = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addDays(DateUtils.getNowDate(), -30));
        final WyBeerQrcodeScanLog wyBeerQrcodeScanLog = new WyBeerQrcodeScanLog();
        wyBeerQrcodeScanLog.setBarId(barId);
        wyBeerQrcodeScanLog.setScanStatus(1);
//        wyBeerQrcodeScanLog.setActivityId(0L); //仅仅核销的扫码数量
        final HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("beginTime", firstDay.toString());
        objectObjectHashMap.put("endTime", LastDay);
        wyBeerQrcodeScanLog.setParams(objectObjectHashMap);
        Integer scanCountOnlyVerify = selectWyBeerQrcodeScanCount(wyBeerQrcodeScanLog);
//        wyBeerQrcodeScanLog.setActivityId(null);
//        wyBeerQrcodeScanLog.setActivitySupplier(1);
//        Integer scanCountBreweryActivity = selectWyBeerQrcodeScanCount(wyBeerQrcodeScanLog);
//        wyBeerQrcodeScanLog.setActivityId(null);
//        wyBeerQrcodeScanLog.setActivitySupplier(0);
//        Integer scanCountPlatformActivity = selectWyBeerQrcodeScanCount(wyBeerQrcodeScanLog);
        if (scanCountOnlyVerify == null) {
            scanCountOnlyVerify = 0;
        }
//        if (scanCountBreweryActivity == null) {
//            scanCountBreweryActivity = 0;
//        }
//        if (scanCountPlatformActivity == null) {
//            scanCountPlatformActivity = 0;
//        }
        return scanCountOnlyVerify;
    }

    /**
     * 酒吧扫码数量百分比
     */
    @Override
    public Integer scanCountPercentByBarId(Long barId, Integer lastDays) {
        final String LastDay = DateUtils.getTime();
        final String firstDay = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.addDays(DateUtils.getNowDate(), -30));
        final WyBeerQrcodeScanLog wyBeerQrcodeScanLog = new WyBeerQrcodeScanLog();
        wyBeerQrcodeScanLog.setScanStatus(1);
        final HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("beginTime", firstDay.toString());
        objectObjectHashMap.put("endTime", LastDay.toString());
        wyBeerQrcodeScanLog.setParams(objectObjectHashMap);
        final List<BarScanCount> barScanCounts = selectBarScanCount(wyBeerQrcodeScanLog);
        System.out.println(barScanCounts);
        if (barScanCounts != null && barScanCounts.size() > 0) {
            //扫码数排序
            // 使用 Comparator 对学生列表按成绩降序排序
            barScanCounts.sort((s1, s2) -> {
                // 降序排序
                return Double.compare(s2.getScanNum(), s1.getScanNum());
            });
            System.out.println(barScanCounts);
            for (int i = 0; i < barScanCounts.size(); i++) {
                if (Objects.equals(barId, barScanCounts.get(i).getBarId())) {
                    System.out.println(barScanCounts.size() + "-" + i);
                    return (barScanCounts.size() - i) / barScanCounts.size() * 100;
                }
            }
        }
        return 0;
    }

//    public static void main(String[] args) {
////        final String LastDay = DateUtils.getTime();
////        final String firstDay = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.addDays(DateUtils.getNowDate(), -30));
////        System.out.println(LastDay);
////        System.out.println(firstDay);
//        final List<BarScanCount> barScanCounts = new ArrayList<>();
//        final BarScanCount barScanCount1 = new BarScanCount();
//        barScanCount1.setBarId(1L);
//        barScanCount1.setScanNum(17);
//        final BarScanCount barScanCount2 = new BarScanCount();
//        barScanCount2.setBarId(2L);
//        barScanCount2.setScanNum(18);
//        final BarScanCount barScanCount3 = new BarScanCount();
//        barScanCount2.setBarId(3L);
//        barScanCount2.setScanNum(1);
//        barScanCounts.add(barScanCount1);
//        barScanCounts.add(barScanCount2);
//        barScanCounts.add(barScanCount3);
//        Long barId = 2L;
//        if (barScanCounts != null) {
//            //扫码数排序
//            // 使用 Comparator 对学生列表按成绩降序排序
//            barScanCounts.sort((s1, s2) -> {
//                // 降序排序
//                return Double.compare(s2.getScanNum(), s1.getScanNum());
//            });
//
//            for (int i = 0; i < barScanCounts.size(); i++) {
//                if (Objects.equals(barId, barScanCounts.get(i).getBarId())) {
//                    System.out.println((barScanCounts.size()-i)/barScanCounts.size() * 100);
//                }
//            }
//        }
//    }
}
