package com.fs.course.service.impl;

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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.course.domain.FsCourseRedPacketLog;
import com.fs.course.domain.FsCourseWatchLog;
import com.fs.course.mapper.FsCourseRedPacketLogMapper;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.his.param.WxSendRedPacketParam;
import com.fs.his.service.IFsStorePaymentService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsCourseRedPacketRetryMapper;
import com.fs.course.domain.FsCourseRedPacketRetry;
import com.fs.course.service.IFsCourseRedPacketRetryService;

/**
 * 课程红包失败重试记录Service业务层处理
 *
 * @author fs
 * @date 2025-03-18
 */
@Service
public class FsCourseRedPacketRetryServiceImpl extends ServiceImpl<FsCourseRedPacketRetryMapper, FsCourseRedPacketRetry> implements IFsCourseRedPacketRetryService {
    @Autowired
    private IFsStorePaymentService paymentService;

    @Autowired
    private FsCourseRedPacketLogMapper redPacketLogMapper;


    @Autowired
    private FsCourseWatchLogMapper courseWatchLogMapper;

    /**
     * 查询课程红包失败重试记录
     *
     * @param logId 课程红包失败重试记录主键
     * @return 课程红包失败重试记录
     */
    @Override
    public FsCourseRedPacketRetry selectFsCourseRedPacketRetryByLogId(Long logId)
    {
        return baseMapper.selectFsCourseRedPacketRetryByLogId(logId);
    }

    /**
     * 查询课程红包失败重试记录列表
     *
     * @param fsCourseRedPacketRetry 课程红包失败重试记录
     * @return 课程红包失败重试记录
     */
    @Override
    public List<FsCourseRedPacketRetry> selectFsCourseRedPacketRetryList(FsCourseRedPacketRetry fsCourseRedPacketRetry)
    {
        return baseMapper.selectFsCourseRedPacketRetryList(fsCourseRedPacketRetry);
    }

    /**
     * 新增课程红包失败重试记录
     *
     * @param fsCourseRedPacketRetry 课程红包失败重试记录
     * @return 结果
     */
    @Override
    public int insertFsCourseRedPacketRetry(FsCourseRedPacketRetry fsCourseRedPacketRetry)
    {
        fsCourseRedPacketRetry.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsCourseRedPacketRetry(fsCourseRedPacketRetry);
    }

    /**
     * 修改课程红包失败重试记录
     *
     * @param fsCourseRedPacketRetry 课程红包失败重试记录
     * @return 结果
     */
    @Override
    public int updateFsCourseRedPacketRetry(FsCourseRedPacketRetry fsCourseRedPacketRetry)
    {
        fsCourseRedPacketRetry.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsCourseRedPacketRetry(fsCourseRedPacketRetry);
    }

    /**
     * 批量删除课程红包失败重试记录
     *
     * @param logIds 需要删除的课程红包失败重试记录主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseRedPacketRetryByLogIds(Long[] logIds)
    {
        return baseMapper.deleteFsCourseRedPacketRetryByLogIds(logIds);
    }

    /**
     * 删除课程红包失败重试记录信息
     *
     * @param logId 课程红包失败重试记录主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseRedPacketRetryByLogId(Long logId)
    {
        return baseMapper.deleteFsCourseRedPacketRetryByLogId(logId);
    }

    @Override
    public void redPacketRetry() {
        FsCourseRedPacketRetry param = new FsCourseRedPacketRetry();
        param.setStatus(0);
        //查询需要补发的红包
        List<FsCourseRedPacketRetry> retries = selectFsCourseRedPacketRetryList(param);
        for (FsCourseRedPacketRetry retry : retries){
            WxSendRedPacketParam packetParam = new WxSendRedPacketParam();
            packetParam.setOpenId(retry.getSendOpenId());
            packetParam.setAmount(retry.getAmount());
            packetParam.setSource(retry.getSource());
            R sendRedPacket = paymentService.sendRedPacket(packetParam);
            if (sendRedPacket.get("code").equals(200)){
                //添加红包记录
                FsCourseRedPacketLog redPacketLog = new FsCourseRedPacketLog();
                redPacketLog.setCourseId(retry.getCourseId());
                redPacketLog.setOutBatchNo(sendRedPacket.get("orderCode").toString());
                redPacketLog.setCompanyId(retry.getCompanyId());
                redPacketLog.setUserId(retry.getUserId());
                redPacketLog.setVideoId(retry.getVideoId());
                redPacketLog.setStatus(0);
                redPacketLog.setQwUserId(retry.getQwUserId() != null ? retry.getQwUserId() : null );
                redPacketLog.setCompanyUserId(retry.getCompanyUserId());
                redPacketLog.setCreateTime(new Date());
                redPacketLog.setAmount(retry.getAmount());
                redPacketLog.setWatchLogId(retry.getWatchLogId());
                redPacketLogMapper.insertFsCourseRedPacketLog(redPacketLog);
                //变更重试状态
                retry.setStatus(1);
                updateFsCourseRedPacketRetry(retry);
            }
        }
    }
}
