package com.ruoyi.brewery.service.impl;

import java.util.List;

import com.ruoyi.brewery.domain.*;
import com.ruoyi.brewery.service.*;
import com.ruoyi.common.constant.OrderStatusConstants;
import com.ruoyi.common.utils.CodeGeneratorUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.brewery.mapper.WyGoodsMapper;

import javax.annotation.Resource;

/**
 * 兑换商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-21
 */
@Service
public class WyGoodsServiceImpl implements IWyGoodsService 
{
    @Autowired
    private WyGoodsMapper wyGoodsMapper;
    @Resource
    private IWyRedeemOrderService wyRedeemOrderService;
    @Resource
    private IWyActivityService wyActivityService;
    @Resource
    private IWyActivityRewardService wyActivityRewardService;
    /**
     * 查询兑换商品
     * 
     * @param id 兑换商品主键
     * @return 兑换商品
     */
    @Override
    public WyGoods selectWyGoodsById(Long id)
    {
        final WyGoods wyGoods = wyGoodsMapper.selectWyGoodsById(id);
        final WyRedeemOrder query = new WyRedeemOrder();
        query.setGoodsId(id);
        final List<WyRedeemOrder> wyRedeemOrders = wyRedeemOrderService.selectWyRedeemOrderList(query);
        if (wyRedeemOrders!= null && wyRedeemOrders.size() > 0) {
            wyGoods.setSalesCount(wyRedeemOrders.stream().mapToInt(WyRedeemOrder::getRedeemNum).sum());
        }else{
            wyGoods.setSalesCount(0);
        }
        return wyGoods;
    }

    /**
     * 查询兑换商品列表
     * 
     * @param wyGoods 兑换商品
     * @return 兑换商品
     */
    @Override
    public List<WyGoods> selectWyGoodsList(WyGoods wyGoods)
    {
        return wyGoodsMapper.selectWyGoodsList(wyGoods);
    }

    @Override
    public List<WyGoods> selectWyGoodsListWithData(WyGoods wyGoods)
    {
        final List<WyGoods> wyGoodsList = wyGoodsMapper.selectWyGoodsList(wyGoods);
        wyGoodsList.forEach(item -> {
            final WyActivity wyActivity = new WyActivity();
            wyActivity.setActivityRewardGoodsId(item.getId());
            final List<WyActivity> wyActivities = wyActivityService.selectWyActivityList(wyActivity);
            item.setActivityCount(wyActivities.size());

            final WyActivityReward wyActivityReward = new WyActivityReward();
            wyActivityReward.setRewardGoodsId(item.getId());
            wyActivityReward.setRewardStatus(0);
            wyActivityReward.setRewardType(2);
            final List<WyActivityReward> wyActivityRewards = wyActivityRewardService.selectWyActivityRewardList(wyActivityReward);
            item.setExchangeWaitingCount(wyActivityRewards.size());

            final WyRedeemOrder wyRedeemOrder = new WyRedeemOrder();
            wyRedeemOrder.setGoodsId(item.getId());
            wyRedeemOrder.setOrderStatus(OrderStatusConstants.ORDER_STATUS_PAID); // 待发货
            final List<WyRedeemOrder> wyRedeemOrders = wyRedeemOrderService.selectWyRedeemOrderList(wyRedeemOrder);
            item.setExchangeWaitingCount(wyRedeemOrders.size());
        });
        return wyGoodsList;
    }

    @Override
    public Integer selectWyGoodsCount(WyGoods wyGoods){
        return wyGoodsMapper.selectWyGoodsCount(wyGoods);
    }

    /**
     * 新增兑换商品
     * 
     * @param wyGoods 兑换商品
     * @return 结果
     */
    @Override
    public int insertWyGoods(WyGoods wyGoods)
    {
        if (wyGoods.getPurchaseMethod() == null) {
            wyGoods.setPurchaseMethod(1); //默认为啤酒币兑换
        }
        wyGoods.setGoodsSn(generateSn());
        wyGoods.setCreateTime(DateUtils.getNowDate());
        return wyGoodsMapper.insertWyGoods(wyGoods);
    }

    /**
     * 修改兑换商品
     * 
     * @param wyGoods 兑换商品
     * @return 结果
     */
    @Override
    public int updateWyGoods(WyGoods wyGoods)
    {
        wyGoods.setGoodsSn(null); // 不更新商品编号
        wyGoods.setUpdateTime(DateUtils.getNowDate());
        return wyGoodsMapper.updateWyGoods(wyGoods);
    }

    /**
     * 批量删除兑换商品
     * 
     * @param ids 需要删除的兑换商品主键
     * @return 结果
     */
    @Override
    public int deleteWyGoodsByIds(Long[] ids)
    {
        return wyGoodsMapper.deleteWyGoodsByIds(ids);
    }

    /**
     * 删除兑换商品信息
     * 
     * @param id 兑换商品主键
     * @return 结果
     */
    @Override
    public int deleteWyGoodsById(Long id)
    {
        return wyGoodsMapper.deleteWyGoodsById(id);
    }

    /**
     * 生成编号
     */
    private String generateSn() {
        final Integer integer = wyGoodsMapper.selectWyGoodsCount(new WyGoods());
        String sn = CodeGeneratorUtils.generateGoodsCode(integer);
        //生成订单号
        final WyGoods query = new WyGoods();
        query.setGoodsSn(sn);
        final List<WyGoods> beers = selectWyGoodsList(query);
        if (beers.size() > 0) {
            return generateSn();
        }
        return sn;
    }
}
