package com.ruoyi.brewery.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.bar.domain.WyBar;
import com.ruoyi.bar.service.IWyBarService;
import com.ruoyi.brewery.domain.WyActivity;
import com.ruoyi.brewery.domain.WyActivityRewardVO;
import com.ruoyi.brewery.service.IWyActivityService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.brewery.mapper.WyActivityRewardMapper;
import com.ruoyi.brewery.domain.WyActivityReward;
import com.ruoyi.brewery.service.IWyActivityRewardService;

import javax.annotation.Resource;

/**
 * 品牌方活动奖励Service业务层处理
 *
 * @author kxl
 * @date 2025-02-10
 */
@Service
public class WyActivityRewardServiceImpl implements IWyActivityRewardService {
    @Resource
    private WyActivityRewardMapper wyActivityRewardMapper;
    @Resource
    private IWyActivityService wyActivityService;
    @Resource
    private IWyBarService barService;
    /**
     * 查询品牌方活动奖励
     *
     * @param id 品牌方活动奖励主键
     * @return 品牌方活动奖励
     */
    @Override
    public WyActivityReward selectWyActivityRewardById(Long id) {
        return wyActivityRewardMapper.selectWyActivityRewardById(id);
    }

    @Override
    public WyActivityReward selectWyActivityRewardByActivityIdAndBarId(Long activityId, Long barId, Long joinBatchId) {
        return wyActivityRewardMapper.selectWyActivityRewardByActivityIdAndBarId(activityId, barId, joinBatchId);
    }

    /**
     * 查询品牌方活动奖励列表
     *
     * @param wyActivityReward 品牌方活动奖励
     * @return 品牌方活动奖励
     */
    @Override
    public List<WyActivityReward> selectWyActivityRewardList(WyActivityReward wyActivityReward) {
        return wyActivityRewardMapper.selectWyActivityRewardList(wyActivityReward);
    }

    @Override
    public Integer selectWyActivityRewardCount(WyActivityReward wyActivityReward) {
        final List<WyActivityReward> wyActivityRewards = wyActivityRewardMapper.selectWyActivityRewardList(wyActivityReward);
        return wyActivityRewards.size();
    }

    /**
     * 查询某款兑付商品的返利活动
     */
    @Override
    public List<WyActivityRewardVO> selectWyActivityRewardListWithData(WyActivityReward wyActivityReward) {
        List<WyActivityRewardVO> rewardVOS = new ArrayList<>();
        final List<WyActivityReward> wyActivityRewards = wyActivityRewardMapper.selectWyActivityRewardList(wyActivityReward);
        wyActivityRewards.forEach(reward -> {
            final WyActivity activity = wyActivityService.selectWyActivityById(reward.getActivityId());
            final WyBar wyBar = barService.selectWyBarById(reward.getBarId());
            final WyActivityRewardVO wyActivityRewardVO = new WyActivityRewardVO();
            BeanUtils.copyProperties(reward, wyActivityRewardVO);
            wyActivityRewardVO.setActivityTarget(activity.getActivityTarget());
            wyActivityRewardVO.setBarCity(wyBar.getCity());
            wyActivityRewardVO.setBarNumber(wyBar.getBarNumber());
            rewardVOS.add(wyActivityRewardVO);
        });
        return rewardVOS;
    }

    /**
     * 新增品牌方活动奖励
     *
     * @param wyActivityReward 品牌方活动奖励
     * @return 结果
     */
    @Override
    public int insertWyActivityReward(WyActivityReward wyActivityReward) {
        wyActivityReward.setCreateTime(DateUtils.getNowDate());
        return wyActivityRewardMapper.insertWyActivityReward(wyActivityReward);
    }

    /**
     * 修改品牌方活动奖励
     *
     * @param wyActivityReward 品牌方活动奖励
     * @return 结果
     */
    @Override
    public int updateWyActivityReward(WyActivityReward wyActivityReward) {
        wyActivityReward.setUpdateTime(DateUtils.getNowDate());
        return wyActivityRewardMapper.updateWyActivityReward(wyActivityReward);
    }

    /**
     * 批量删除品牌方活动奖励
     *
     * @param ids 需要删除的品牌方活动奖励主键
     * @return 结果
     */
    @Override
    public int deleteWyActivityRewardByIds(Long[] ids) {
        return wyActivityRewardMapper.deleteWyActivityRewardByIds(ids);
    }

    /**
     * 删除品牌方活动奖励信息
     *
     * @param id 品牌方活动奖励主键
     * @return 结果
     */
    @Override
    public int deleteWyActivityRewardById(Long id) {
        return wyActivityRewardMapper.deleteWyActivityRewardById(id);
    }
}
