package com.ruoyi.brewery.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.bar.domain.WyBarIcon;
import com.ruoyi.bar.domain.WyBarIconLog;
import com.ruoyi.bar.service.IWyBarIconLogService;
import com.ruoyi.bar.service.IWyBarIconService;
import com.ruoyi.brewery.domain.WyBrewerySharePlan;
import com.ruoyi.brewery.service.IWyBrewerySharePlanService;
import com.ruoyi.common.constant.CoinObtainType;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.brewery.mapper.WyBreweryShareLogMapper;
import com.ruoyi.brewery.domain.WyBreweryShareLog;
import com.ruoyi.brewery.service.IWyBreweryShareLogService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 品牌方分享方案参与记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-23
 */
@Service
public class WyBreweryShareLogServiceImpl implements IWyBreweryShareLogService 
{
    @Autowired
    private WyBreweryShareLogMapper wyBreweryShareLogMapper;
    @Resource
    private IWyBrewerySharePlanService wyBrewerySharePlanService;

    @Resource
    private IWyBarIconService wyBarIconService;
    @Resource
    private IWyBarIconLogService wyBarIconLogService;
    /**
     * 查询品牌方分享方案参与记录
     * 
     * @param id 品牌方分享方案参与记录主键
     * @return 品牌方分享方案参与记录
     */
    @Override
    public WyBreweryShareLog selectWyBreweryShareLogById(Long id)
    {
        return wyBreweryShareLogMapper.selectWyBreweryShareLogById(id);
    }

    /**
     * 查询品牌方分享方案参与记录列表
     * 
     * @param wyBreweryShareLog 品牌方分享方案参与记录
     * @return 品牌方分享方案参与记录
     */
    @Override
    public List<WyBreweryShareLog> selectWyBreweryShareLogList(WyBreweryShareLog wyBreweryShareLog)
    {
        return wyBreweryShareLogMapper.selectWyBreweryShareLogList(wyBreweryShareLog);
    }

    /**
     * 新增品牌方分享方案参与记录
     * 
     * @param wyBreweryShareLog 品牌方分享方案参与记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWyBreweryShareLog(WyBreweryShareLog wyBreweryShareLog)
    {
        final WyBrewerySharePlan wyBrewerySharePlan = wyBrewerySharePlanService.selectWyBrewerySharePlanById(wyBreweryShareLog.getPlanId());
        if (wyBrewerySharePlan == null) {
            throw new IllegalArgumentException("分享方案不存在");
        }
        if (wyBrewerySharePlan.getPlanStatus()!=1) {
            throw new IllegalArgumentException("分享方案未启用");
        }
        if (wyBrewerySharePlan.getStartTime().after(DateUtils.getNowDate()) || wyBrewerySharePlan.getEndTime().before(DateUtils.getNowDate())) {
            throw new IllegalArgumentException("分享方案未在有效期内");
        }
        final WyBreweryShareLog log = new WyBreweryShareLog();
        log.setPlanId(wyBreweryShareLog.getPlanId());
        final List<WyBreweryShareLog> wyBreweryShareLogs = this.selectWyBreweryShareLogList(log);
        final int size = wyBreweryShareLogs.size();
        if (size >= wyBrewerySharePlan.getMaxPersons()) {
            throw new IllegalArgumentException("分享方案已达到最大限制");
        }
        final List<WyBreweryShareLog> collect = wyBreweryShareLogs.stream().filter(item -> item.getBarId().equals(wyBreweryShareLog.getBarId())).collect(Collectors.toList());
        if (collect.size() >= wyBrewerySharePlan.getClaimTimes()) {
            throw new IllegalArgumentException("该门店已达到最大认领次数");
        }
        wyBreweryShareLog.setCreateTime(DateUtils.getNowDate());
        final int i = wyBreweryShareLogMapper.insertWyBreweryShareLog(wyBreweryShareLog);
        if (i > 0) {
            rewardBeerIcon(wyBrewerySharePlan.getBreweryId(), "分享方案参与奖励："+wyBrewerySharePlan.getPlanName(), wyBreweryShareLog.getBarId(), wyBrewerySharePlan.getQuantityAvailable(), CoinObtainType.Directly, null);
        }
        return i;
    }

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

    /**
     * 修改品牌方分享方案参与记录
     * 
     * @param wyBreweryShareLog 品牌方分享方案参与记录
     * @return 结果
     */
    @Override
    public int updateWyBreweryShareLog(WyBreweryShareLog wyBreweryShareLog)
    {
        wyBreweryShareLog.setUpdateTime(DateUtils.getNowDate());
        return wyBreweryShareLogMapper.updateWyBreweryShareLog(wyBreweryShareLog);
    }

    /**
     * 批量删除品牌方分享方案参与记录
     * 
     * @param ids 需要删除的品牌方分享方案参与记录主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryShareLogByIds(Long[] ids)
    {
        return wyBreweryShareLogMapper.deleteWyBreweryShareLogByIds(ids);
    }

    /**
     * 删除品牌方分享方案参与记录信息
     * 
     * @param id 品牌方分享方案参与记录主键
     * @return 结果
     */
    @Override
    public int deleteWyBreweryShareLogById(Long id)
    {
        return wyBreweryShareLogMapper.deleteWyBreweryShareLogById(id);
    }
}
