package com.ruoyi.bar.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.bar.domain.WyBarIconLog;
import com.ruoyi.bar.service.IWyBarIconLogService;
import com.ruoyi.bar.service.IWyBarService;
import com.ruoyi.brewery.domain.WyGoods;
import com.ruoyi.brewery.domain.WyRedeemOrder;
import com.ruoyi.brewery.service.IWyGoodsService;
import com.ruoyi.brewery.service.IWyRedeemOrderService;
import com.ruoyi.common.constant.CoinObtainType;
import com.ruoyi.common.constant.OrderStatusConstants;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.bar.mapper.WyBarIconMapper;
import com.ruoyi.bar.domain.WyBarIcon;
import com.ruoyi.bar.service.IWyBarIconService;

import javax.annotation.Resource;

/**
 * 酒吧啤酒币余额Service业务层处理
 *
 * @author kxl
 * @date 2025-02-10
 */
@Service
public class WyBarIconServiceImpl implements IWyBarIconService {
    @Resource
    private WyBarIconMapper wyBarIconMapper;
    @Resource
    private IWyGoodsService wyGoodsService;
    @Resource
    private IWyBarService barService;
    @Resource
    private IWyBarIconLogService iconLogService;
    @Resource
    private IWyRedeemOrderService orderService;

    /**
     * 查询酒吧啤酒币余额
     *
     * @param id 酒吧啤酒币余额主键
     * @return 酒吧啤酒币余额
     */
    @Override
    public WyBarIcon selectWyBarIconById(Long id) {
        return wyBarIconMapper.selectWyBarIconById(id);
    }

    @Override
    public WyBarIcon selectWyBarIconByBreweryIdAndBarId(Long breweryId, Long barId) {
        return wyBarIconMapper.selectWyBarIconByBreweryIdAndBarId(breweryId, barId);
    }

    /**
     * 查询酒吧啤酒币余额列表
     *
     * @param wyBarIcon 酒吧啤酒币余额
     * @return 酒吧啤酒币余额
     */
    @Override
    public List<WyBarIcon> selectWyBarIconList(WyBarIcon wyBarIcon) {
        return wyBarIconMapper.selectWyBarIconList(wyBarIcon);
    }

    @Override
    public HashMap<String, Integer> selectBarIconDataByBreweryId(Long breweryId) {

        HashMap<String, Integer> result = new HashMap<>();
        WyBarIcon wyBarIcon = new WyBarIcon();
        wyBarIcon.setBreweryId(breweryId);
        final List<WyBarIcon> wyBarIcons = selectWyBarIconList(wyBarIcon);
        final long sum = wyBarIcons.stream().mapToLong(WyBarIcon::getBalance).sum();
        final WyRedeemOrder wyRedeemOrder = new WyRedeemOrder();
        wyRedeemOrder.setBreweryId(breweryId);
        wyRedeemOrder.setOrderStatus(OrderStatusConstants.ORDER_STATUS_PAID);
        final List<WyRedeemOrder> wyRedeemOrders = orderService.selectWyRedeemOrderList(wyRedeemOrder);
        final WyGoods wyGoods = new WyGoods();
        wyGoods.setBreweryId(breweryId);
        wyGoods.setOnSale(1);
        final Integer goodsCount = wyGoodsService.selectWyGoodsCount(wyGoods);
        result.put("remaining", (int) sum); //已发放未使用数量
        result.put("bars", wyBarIcons.size()); //门店数
        result.put("waitingOrders", wyRedeemOrders.size()); //门店数
        result.put("goodsCount", goodsCount); //发布商品数

        return result;
    }

    @Override
    public List<WyBarIcon> selectWyBarIconListWithData(WyBarIcon wyBarIcon) {
        final List<WyBarIcon> wyBarIcons = selectWyBarIconList(wyBarIcon);
        wyBarIcons.forEach(barIcon -> {
            final WyBarIconLog wyBarIconLog = new WyBarIconLog();
            wyBarIconLog.setBarId(barIcon.getBarId());
            wyBarIconLog.setBreweryId(barIcon.getBreweryId());
            wyBarIconLog.setObtainType(CoinObtainType.EXCHANGE);
            final Integer sum = iconLogService.selectWyBarIconSum(wyBarIconLog);
            barIcon.setUsedNum(sum);
            final WyRedeemOrder wyRedeemOrder = new WyRedeemOrder();
            wyRedeemOrder.setBarId(barIcon.getBarId());
            wyRedeemOrder.setBreweryId(barIcon.getBreweryId());
            final List<WyRedeemOrder> wyRedeemOrders = orderService.selectWyRedeemOrderList(wyRedeemOrder);
            barIcon.setExchangedOrderNum(wyRedeemOrders.size());
        });
        return wyBarIcons;
    }

    /**
     * 查询酒吧啤酒币余额列表
     *
     * @param wyBarIcon 酒吧啤酒币余额
     * @return 酒吧啤酒币余额
     */
    @Override
    public List<WyBarIcon> selectWyBarIconListWithGoodsNum(WyBarIcon wyBarIcon) {
        final List<WyBarIcon> wyBarIcons = wyBarIconMapper.selectWyBarIconList(wyBarIcon);
        wyBarIcons.forEach(barIcon -> {
            final WyGoods wyGoods = new WyGoods();
            wyGoods.setBreweryId(barIcon.getBreweryId());
            wyGoods.setOnSale(1);
            final Integer goodsCount = wyGoodsService.selectWyGoodsCount(wyGoods);
            barIcon.setGoodsNum(goodsCount);
        });
        return wyBarIcons;
    }

    /**
     * 新增酒吧啤酒币余额
     *
     * @param wyBarIcon 酒吧啤酒币余额
     * @return 结果
     */

    @Override
    public int insertWyBarIcon(WyBarIcon wyBarIcon) {
        wyBarIcon.setCreateTime(DateUtils.getNowDate());
        return wyBarIconMapper.insertWyBarIcon(wyBarIcon);
    }

    /**
     * 修改酒吧啤酒币余额
     *
     * @param wyBarIcon 酒吧啤酒币余额
     * @return 结果
     */
    @Override
    public int updateWyBarIcon(WyBarIcon wyBarIcon) {
        wyBarIcon.setUpdateTime(DateUtils.getNowDate());
        return wyBarIconMapper.updateWyBarIcon(wyBarIcon);
    }

    /**
     * 批量删除酒吧啤酒币余额
     *
     * @param ids 需要删除的酒吧啤酒币余额主键
     * @return 结果
     */
    @Override
    public int deleteWyBarIconByIds(Long[] ids) {
        return wyBarIconMapper.deleteWyBarIconByIds(ids);
    }

    /**
     * 删除酒吧啤酒币余额信息
     *
     * @param id 酒吧啤酒币余额主键
     * @return 结果
     */
    @Override
    public int deleteWyBarIconById(Long id) {
        return wyBarIconMapper.deleteWyBarIconById(id);
    }
}
