package com.fs.live.service.impl;


import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.StringUtils;
import com.fs.live.domain.LiveLotteryConf;
import com.fs.live.domain.LiveUserLotteryRecord;
import com.fs.live.mapper.LiveUserLotteryRecordMapper;
import com.fs.live.service.*;
import com.fs.live.vo.LiveUserLotteryRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 直播用户中奖记录Service业务层处理
 *
 * @author fs
 * @date 2025-07-17
 */
@Service
public class LiveUserLotteryRecordServiceImpl implements ILiveUserLotteryRecordService {

    @Autowired
    private ILiveLotteryConfService liveLotteryConfService;

    @Autowired
    private ILiveUserFollowService liveUserFollowService;

    @Autowired
    private ILiveOrderService liveOrderService;

    @Autowired
    private ILiveGiftService liveGiftService;
    @Autowired
    private LiveUserLotteryRecordMapper baseMapper;
    /**
     * 查询直播用户中奖记录
     *
     * @param id 直播用户中奖记录主键
     * @return 直播用户中奖记录
     */
    @Override
    public LiveUserLotteryRecord selectLiveUserLotteryRecordById(Long id)
    {
        return baseMapper.selectLiveUserLotteryRecordById(id);
    }

    /**
     * 查询直播用户中奖记录列表
     *
     * @param liveUserLotteryRecord 直播用户中奖记录
     * @return 直播用户中奖记录
     */
    @Override
    public List<LiveUserLotteryRecord> selectLiveUserLotteryRecordList(LiveUserLotteryRecord liveUserLotteryRecord)
    {
        return baseMapper.selectLiveUserLotteryRecordList(liveUserLotteryRecord);
    }

    /**
     * 新增直播用户中奖记录
     *
     * @param liveUserLotteryRecord 直播用户中奖记录
     * @return 结果
     */
    @Override
    public int insertLiveUserLotteryRecord(LiveUserLotteryRecord liveUserLotteryRecord)
    {
        liveUserLotteryRecord.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertLiveUserLotteryRecord(liveUserLotteryRecord);
    }

    /**
     * 修改直播用户中奖记录
     *
     * @param liveUserLotteryRecord 直播用户中奖记录
     * @return 结果
     */
    @Override
    public int updateLiveUserLotteryRecord(LiveUserLotteryRecord liveUserLotteryRecord)
    {
        return baseMapper.updateLiveUserLotteryRecord(liveUserLotteryRecord);
    }

    /**
     * 批量删除直播用户中奖记录
     *
     * @param ids 需要删除的直播用户中奖记录主键
     * @return 结果
     */
    @Override
    public int deleteLiveUserLotteryRecordByIds(Long[] ids)
    {
        return baseMapper.deleteLiveUserLotteryRecordByIds(ids);
    }

    /**
     * 删除直播用户中奖记录信息
     *
     * @param id 直播用户中奖记录主键
     * @return 结果
     */
    @Override
    public int deleteLiveUserLotteryRecordById(Long id)
    {
        return baseMapper.deleteLiveUserLotteryRecordById(id);
    }

    @Override
    public String userLottery(Long liveId, Long lotteryId, String userId) {
        if (StringUtils.isEmpty(userId))
            return "请先登录";
        //获取抽奖配置
        LiveLotteryConf liveLotteryConf = liveLotteryConfService.getById(lotteryId);
        //update_time含义修改为抽奖开始时间 查看抽奖时间是否过期
        Date expireTime = DateUtils.addMinutes(liveLotteryConf.getUpdateTime(), liveLotteryConf.getDuration().intValue());
        if (expireTime.before(new Date())){
            return "本轮抽奖已经结束啦";
        }
        //参与条件检测
        switch (liveLotteryConf.getRequire().intValue()){
            case 0:
                //"参与抽奖方式 0:在线观众参与";
                break;
            case 1:
                //"参与抽奖方式 1:关注参与";
                if (liveUserFollowService.isFollow(liveId, Long.parseLong(userId)))
                    break;
                return "请先关注";
            case 2:

                //"参与抽奖方式 2:送礼参与";
                //return "暂不支持";
                //if (liveGiftService.isGift(liveId, Long.parseLong(userId)))
            case 3:
                //"参与抽奖方式 3:下单参与";
                break;
        }
        //插入记录表
        LiveUserLotteryRecord liveUserLotteryRecord = new LiveUserLotteryRecord();
        liveUserLotteryRecord.setLotteryId(lotteryId);
        liveUserLotteryRecord.setLiveId(liveId);
        liveUserLotteryRecord.setUserId(Long.parseLong(userId));
        liveUserLotteryRecord.setCreateTime(new Date());
        baseMapper.insertLiveUserLotteryRecord(liveUserLotteryRecord);
        return "参与成功";
    }

    @Override
    public boolean existsByUserIdAndRedId(Long lotteryId, String userId) {
        return baseMapper.existsByUserIdAndRedId(lotteryId,userId);
    }

    @Override
    public R selectRecordByLiveIdAndLotteryId(LiveUserLotteryRecord liveUserLotteryRecord) {
        return R.ok().put("data",baseMapper.selectRecordByLiveIdAndLotteryId(liveUserLotteryRecord));
    }


}
