package com.ruoyi.system.service.impl;

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

import com.ruoyi.bar.domain.WyBeerQrcodeScanLog;
import com.ruoyi.bar.service.IWyBeerQrcodeScanLogService;
import com.ruoyi.brewery.domain.WyActivity;
import com.ruoyi.brewery.domain.WyBeer;
import com.ruoyi.brewery.service.IWyBeerService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WyPlatformActivityBeer;
import com.ruoyi.system.service.IWyPlatformActivityBeerService;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WyPlatformActivityMapper;
import com.ruoyi.system.domain.WyPlatformActivity;
import com.ruoyi.system.service.IWyPlatformActivityService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 营销活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class WyPlatformActivityServiceImpl implements IWyPlatformActivityService 
{
    @Resource
    private WyPlatformActivityMapper wyPlatformActivityMapper;
    @Resource
    private IWyPlatformActivityBeerService wyPlatformActivityBeerService;
    @Resource
    private IWyBeerService wyBeerService;
    @Resource
    private IWyBeerQrcodeScanLogService beerQrcodeScanLogService;
    /**
     * 查询营销活动
     * 
     * @param id 营销活动主键
     * @return 营销活动
     */
    @Override
    public WyPlatformActivity selectWyPlatformActivityById(Long id)
    {
        final WyPlatformActivity platformActivity = wyPlatformActivityMapper.selectWyPlatformActivityById(id);
        if (platformActivity == null){
            return null;
        }
        //根据beerScope判断是否需要查询啤酒
        if (platformActivity.getBeerScope()==0){
            platformActivity.setBeerIds(null);
            platformActivity.setBeers(null);
        }else{
            final List<WyPlatformActivityBeer> wyPlatformActivityBeers = wyPlatformActivityBeerService.selectWyPlatformActivityBeerByActivityId(id);
            List<Long> beerIds = wyPlatformActivityBeers.stream().map(WyPlatformActivityBeer::getBeerId).collect(Collectors.toList());
            platformActivity.setBeerIds(beerIds);
            if(beerIds == null || beerIds.size() == 0){
                platformActivity.setBeers(null);
            }else{
                final List<WyBeer> wyBeers = wyBeerService.seleteWyBeerByIds(beerIds.toArray(new Long[0]));
                platformActivity.setBeers(wyBeers);
            }
        }
        //根据activityScope判断是否需要查询活动时间

        //查询活动商品信息
        
        return platformActivity;
    }

    /**
     * 更新活动热度
     * @param activityId 活动ID
     * @return 操作结果
     */
    @Transactional
    @Override
    public int updateActivityPopularity(Long activityId) {
        final WyBeerQrcodeScanLog scanLog = new WyBeerQrcodeScanLog();
        scanLog.setActivityId(activityId);
        scanLog.setScanStatus(1);
        scanLog.setActivitySupplier(0);
        final Integer scanCount = beerQrcodeScanLogService.selectWyBeerQrcodeScanCount(scanLog);
        final WyPlatformActivity activity = this.selectWyPlatformActivityById(activityId);
        activity.setPopularity(scanCount.longValue());
        return this.updateWyPlatformActivity(activity);
    }

    /**
     * 查询营销活动列表
     * 
     * @param wyPlatformActivity 营销活动
     * @return 营销活动
     */
    @Override
    public List<WyPlatformActivity> selectWyPlatformActivityList(WyPlatformActivity wyPlatformActivity)
    {
        return wyPlatformActivityMapper.selectWyPlatformActivityList(wyPlatformActivity);
    }

    /**
     * 新增营销活动
     * 
     * @param wyPlatformActivity 营销活动
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWyPlatformActivity(WyPlatformActivity wyPlatformActivity)
    {
        if(wyPlatformActivity.getBeerScope() == 1 && wyPlatformActivity.getBeerIds() == null){
            throw new RuntimeException("请选择啤酒");
        }
        if(wyPlatformActivity.getBeerScope() == 0){
           wyPlatformActivity.setBeerIds(null);
        }
        wyPlatformActivity.setCreateTime(DateUtils.getNowDate());
        wyPlatformActivityMapper.insertWyPlatformActivity(wyPlatformActivity);
        if(wyPlatformActivity.getBeerIds() != null) {
            wyPlatformActivity.getBeerIds().forEach(beerId -> {
                WyPlatformActivityBeer wyPlatformActivityBeer = new WyPlatformActivityBeer();
                wyPlatformActivityBeer.setActivityId(wyPlatformActivity.getId());
                wyPlatformActivityBeer.setBeerId(beerId);
                wyPlatformActivityBeerService.insertWyPlatformActivityBeer(wyPlatformActivityBeer);
            });
        }
        return 1;
    }

    /**
     * 修改营销活动
     * 
     * @param wyPlatformActivity 营销活动
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWyPlatformActivity(WyPlatformActivity wyPlatformActivity)
    {

        WyPlatformActivityBeer platformActivityBeer = new WyPlatformActivityBeer();
        platformActivityBeer.setActivityId(wyPlatformActivity.getId());
        final List<WyPlatformActivityBeer> wyPlatformActivityBeers = wyPlatformActivityBeerService.selectWyPlatformActivityBeerList(platformActivityBeer);
        if(wyPlatformActivity.getBeerScope() == 1 && wyPlatformActivity.getBeerIds() == null && wyPlatformActivityBeers == null){
            throw new RuntimeException("请选择啤酒");
        }
        wyPlatformActivity.setUpdateTime(DateUtils.getNowDate());
        wyPlatformActivityMapper.updateWyPlatformActivity(wyPlatformActivity);
        wyPlatformActivityBeerService.deleteWyPlatformActivityBeerByActivityId(wyPlatformActivity.getId()); //删除所有老的关联关系
        if(wyPlatformActivity.getBeerIds() != null) {
            wyPlatformActivity.getBeerIds().forEach(beerId -> {
                WyPlatformActivityBeer wyPlatformActivityBeer = new WyPlatformActivityBeer();
                wyPlatformActivityBeer.setActivityId(wyPlatformActivity.getId());
                wyPlatformActivityBeer.setBeerId(beerId);
                wyPlatformActivityBeerService.insertWyPlatformActivityBeer(wyPlatformActivityBeer);
            });
        }
        return 1;
    }

    /**
     * 批量删除营销活动
     * 
     * @param ids 需要删除的营销活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWyPlatformActivityByIds(Long[] ids)
    {
        for(Long id : ids){
            wyPlatformActivityBeerService.deleteWyPlatformActivityBeerByActivityId(id); //删除所有老的关联关系
        }
        return wyPlatformActivityMapper.deleteWyPlatformActivityByIds(ids);
    }

    /**
     * 删除营销活动信息
     * 
     * @param id 营销活动主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWyPlatformActivityById(Long id)
    {
        wyPlatformActivityBeerService.deleteWyPlatformActivityBeerByActivityId(id); //删除所有老的关联关系
        return wyPlatformActivityMapper.deleteWyPlatformActivityById(id);
    }
}
