package com.fs.live.service.impl;

import java.util.Collections;
import java.util.List;
import com.fs.common.utils.DateUtils;
import com.fs.live.domain.LiveCouponIssueRelation;
import com.fs.live.mapper.LiveCouponMapper;
import com.fs.live.param.CouponPO;
import com.fs.live.service.ILiveAutoTaskService;
import com.fs.live.vo.LiveCouponListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.live.mapper.LiveCouponIssueMapper;
import com.fs.live.domain.LiveCouponIssue;
import com.fs.live.service.ILiveCouponIssueService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券领取Service业务层处理
 *
 * @author fs
 * @date 2025-09-30
 */
@Service
public class LiveCouponIssueServiceImpl implements ILiveCouponIssueService
{
    @Autowired
    private LiveCouponIssueMapper liveCouponIssueMapper;
    @Autowired
    private LiveCouponMapper liveCouponMapper;
    @Autowired
    private ILiveAutoTaskService liveAutoTaskService;

    /**
     * 查询优惠券领取
     *
     * @param id 优惠券领取ID
     * @return 优惠券领取
     */
    @Override
    public LiveCouponIssue selectLiveCouponIssueById(Long id)
    {
        return liveCouponIssueMapper.selectLiveCouponIssueById(id);
    }

    /**
     * 查询优惠券领取列表
     *
     * @param liveCouponIssue 优惠券领取
     * @return 优惠券领取
     */
    @Override
    public List<LiveCouponIssue> selectLiveCouponIssueList(LiveCouponIssue liveCouponIssue)
    {
        return liveCouponIssueMapper.selectLiveCouponIssueList(liveCouponIssue);
    }

    /**
     * 新增优惠券领取
     *
     * @param liveCouponIssue 优惠券领取
     * @return 结果
     */
    @Override
    public int insertLiveCouponIssue(LiveCouponIssue liveCouponIssue)
    {
        liveCouponIssue.setCreateTime(DateUtils.getNowDate());
        return liveCouponIssueMapper.insertLiveCouponIssue(liveCouponIssue);
    }

    /**
     * 修改优惠券领取
     *
     * @param liveCouponIssue 优惠券领取
     * @return 结果
     */
    @Override
    public int updateLiveCouponIssue(LiveCouponIssue liveCouponIssue)
    {
        liveCouponIssue.setUpdateTime(DateUtils.getNowDate());
        return liveCouponIssueMapper.updateLiveCouponIssue(liveCouponIssue);
    }

    /**
     * 批量删除优惠券领取
     *
     * @param ids 需要删除的优惠券领取ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteLiveCouponIssueByIds(Long[] ids)
    {
        // 联动删除：删除所有直播间存在的优惠券、定时任务和缓存
        for (Long couponIssueId : ids) {
            // 查询优惠券发布信息，获取couponId
            LiveCouponIssue issue = liveCouponIssueMapper.selectLiveCouponIssueById(couponIssueId);
            if (issue != null && issue.getCouponId() != null) {
                // 删除所有直播间相关的自动化任务（liveId为null表示所有直播间）
                liveAutoTaskService
                        .deleteAutoTasksByCouponId(issue.getCouponId(), null);

                // 查询所有使用该优惠券发布的直播间关联关系
                List<LiveCouponIssueRelation> relations =
                        liveCouponMapper.selectCouponRelationByCouponIssueId(couponIssueId);

                // 删除所有直播间的关联关系
                for (LiveCouponIssueRelation relation : relations) {
                    LiveCouponListVo listVo = new LiveCouponListVo();
                    listVo.setCouponIds(java.util.Arrays.asList(couponIssueId));
                    listVo.setLiveId(relation.getLiveId().intValue());
                    liveCouponMapper.handleDeleteSelectedAdmin(listVo);
                }
            }
        }
        return liveCouponIssueMapper.deleteLiveCouponIssueByIds(ids);
    }

    /**
     * 删除优惠券领取信息
     *
     * @param id 优惠券领取ID
     * @return 结果
     */
    @Override
    public int deleteLiveCouponIssueById(Long id)
    {
        return liveCouponIssueMapper.deleteLiveCouponIssueById(id);
    }

    @Override
    public List<LiveCouponIssue> selectLiveCouponIssueUserByCouponPO(CouponPO coupon) {
        return liveCouponIssueMapper.selectLiveCouponIssueUserByCouponPO(coupon);
    }

    @Override
    public List<LiveCouponIssue> curCoupon(CouponPO coupon) {
        return liveCouponIssueMapper.curCoupon(coupon);
    }

    @Override
    public LiveCouponIssue selectLiveCouponIssueByCouponId(Long couponId) {
        return liveCouponIssueMapper.selectLiveCouponIssueByCouponId(couponId);
    }
}
