package com.ruoyi.bar.service.impl;

import java.util.List;

import com.ruoyi.bar.domain.WyBeerQrcodeScanLog;
import com.ruoyi.bar.service.IWyBeerQrcodeScanLogService;
import com.ruoyi.brewery.domain.WyActivity;
import com.ruoyi.brewery.service.IWyActivityService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WyPlatformActivity;
import com.ruoyi.system.service.IWyPlatformActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bar.mapper.WyActivityJoinBatchMapper;
import com.ruoyi.bar.domain.WyActivityJoinBatch;
import com.ruoyi.bar.service.IWyActivityJoinBatchService;

import javax.annotation.Resource;

/**
 * 获得参与批次信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-15
 */
@Service
public class WyActivityJoinBatchServiceImpl implements IWyActivityJoinBatchService 
{
    @Autowired
    private WyActivityJoinBatchMapper wyActivityJoinBatchMapper;
    @Resource
    private IWyActivityService wyActivityService;
    @Resource
    private IWyPlatformActivityService wyPlatformActivityService;
    @Resource
    private IWyBeerQrcodeScanLogService wyBeerQrcodeScanLogService;
    /**
     * 查询获得参与批次信息
     * 
     * @param batchId 获得参与批次信息主键
     * @return 获得参与批次信息
     */
    @Override
    public WyActivityJoinBatch selectWyActivityJoinBatchByBatchId(Long batchId)
    {
        return wyActivityJoinBatchMapper.selectWyActivityJoinBatchByBatchId(batchId);
    }

    @Override
    public WyActivityJoinBatch selectWyActivityJoinBatchByBatchIdWithData(Long batchId)
    {
        final WyActivityJoinBatch batch = wyActivityJoinBatchMapper.selectWyActivityJoinBatchByBatchId(batchId);
        final WyBeerQrcodeScanLog wyBeerQrcodeScanLog = new WyBeerQrcodeScanLog();
        wyBeerQrcodeScanLog.setActivityId(batch.getActivityId());
        wyBeerQrcodeScanLog.setActivitySupplier(batch.getActivitySupplier());
        wyBeerQrcodeScanLog.setJoinBatchId(batch.getBatchId());
        wyBeerQrcodeScanLog.setScanStatus(1);
        final Integer joinNum = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanCount(wyBeerQrcodeScanLog);

        if(batch.getActivitySupplier()==0){
            final WyActivity activity = wyActivityService.selectWyActivityById(batch.getActivityId());
            batch.setStartDate(batch.getCreateTime());
            batch.setEndDate(DateUtils.addDays(batch.getCreateTime(),activity.getDuration()));
            batch.setJoinNum(joinNum);
            batch.setRemainNum(activity.getActivityTarget() - joinNum);
        }
        if(batch.getActivitySupplier()==1){
            final WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(batch.getActivityId());
            batch.setStartDate(batch.getCreateTime());
            batch.setEndDate(DateUtils.addDays(batch.getCreateTime(), Math.toIntExact(platformActivity.getDuration())));
            batch.setJoinNum(joinNum);
            batch.setRemainNum((int) (platformActivity.getActivityTarget() - joinNum));
        }
        return batch;
    }

    /**
     * 查询符合条件的最新批次信息
     */
    @Override
    public WyActivityJoinBatch selectLastJoinBatch(WyActivityJoinBatch wyActivityJoinBatch){
        return wyActivityJoinBatchMapper.selectLastJoinBatch(wyActivityJoinBatch);
    }


    /**
     * 查询获得参与批次信息列表
     * 
     * @param wyActivityJoinBatch 获得参与批次信息
     * @return 获得参与批次信息
     */
    @Override
    public List<WyActivityJoinBatch> selectWyActivityJoinBatchList(WyActivityJoinBatch wyActivityJoinBatch)
    {
        return wyActivityJoinBatchMapper.selectWyActivityJoinBatchList(wyActivityJoinBatch);
    }

    /**
     * 查询获得参与批次信息列表（含参信息）
     */
    @Override
    public List<WyActivityJoinBatch> selectWyActivityJoinBatchListWithData(WyActivityJoinBatch wyActivityJoinBatch)
    {
        final List<WyActivityJoinBatch> wyActivityJoinBatches = wyActivityJoinBatchMapper.selectWyActivityJoinBatchList(wyActivityJoinBatch);
        wyActivityJoinBatches.forEach(batch -> {
            final WyBeerQrcodeScanLog wyBeerQrcodeScanLog = new WyBeerQrcodeScanLog();
            wyBeerQrcodeScanLog.setActivityId(batch.getActivityId());
            wyBeerQrcodeScanLog.setActivitySupplier(batch.getActivitySupplier());
            wyBeerQrcodeScanLog.setJoinBatchId(batch.getBatchId());
            wyBeerQrcodeScanLog.setScanStatus(1);
            final Integer joinNum = wyBeerQrcodeScanLogService.selectWyBeerQrcodeScanCount(wyBeerQrcodeScanLog);

            if(batch.getActivitySupplier()==0){
                final WyActivity activity = wyActivityService.selectWyActivityById(batch.getActivityId());
                batch.setStartDate(batch.getCreateTime());
                batch.setEndDate(DateUtils.addDays(batch.getCreateTime(),activity.getDuration()));
                batch.setJoinNum(joinNum);
                batch.setRemainNum(activity.getActivityTarget() - joinNum);
            }
            if(batch.getActivitySupplier()==1){
                final WyPlatformActivity platformActivity = wyPlatformActivityService.selectWyPlatformActivityById(batch.getActivityId());
                batch.setStartDate(batch.getCreateTime());
                batch.setEndDate(DateUtils.addDays(batch.getCreateTime(), Math.toIntExact(platformActivity.getDuration())));
                batch.setJoinNum(joinNum);
                batch.setRemainNum((int) (platformActivity.getActivityTarget() - joinNum));
            }
        });
        return wyActivityJoinBatches;
    }

    /**
     * 新增获得参与批次信息
     * 
     * @param wyActivityJoinBatch 获得参与批次信息
     * @return 结果
     */
    @Override
    public int insertWyActivityJoinBatch(WyActivityJoinBatch wyActivityJoinBatch)
    {
        wyActivityJoinBatch.setCreateTime(DateUtils.getNowDate());
        return wyActivityJoinBatchMapper.insertWyActivityJoinBatch(wyActivityJoinBatch);
    }

    /**
     * 修改获得参与批次信息
     * 
     * @param wyActivityJoinBatch 获得参与批次信息
     * @return 结果
     */
    @Override
    public int updateWyActivityJoinBatch(WyActivityJoinBatch wyActivityJoinBatch)
    {
        wyActivityJoinBatch.setUpdateTime(DateUtils.getNowDate());
        return wyActivityJoinBatchMapper.updateWyActivityJoinBatch(wyActivityJoinBatch);
    }

    /**
     * 批量删除获得参与批次信息
     * 
     * @param batchIds 需要删除的获得参与批次信息主键
     * @return 结果
     */
    @Override
    public int deleteWyActivityJoinBatchByBatchIds(Long[] batchIds)
    {
        return wyActivityJoinBatchMapper.deleteWyActivityJoinBatchByBatchIds(batchIds);
    }

    /**
     * 删除获得参与批次信息信息
     * 
     * @param batchId 获得参与批次信息主键
     * @return 结果
     */
    @Override
    public int deleteWyActivityJoinBatchByBatchId(Long batchId)
    {
        return wyActivityJoinBatchMapper.deleteWyActivityJoinBatchByBatchId(batchId);
    }
}
