package com.ruoyi.ai.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ai.domain.AiRechargeGiftGear;
import com.ruoyi.ai.mapper.AiRechargeGiftGearMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ai.mapper.AiRechargeGiftMapper;
import com.ruoyi.ai.domain.AiRechargeGift;
import com.ruoyi.ai.service.IAiRechargeGiftService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 充值赠送活动Service业务层处理
 * 
 * @author shi
 * @date 2025-11-20
 */
@Service
public class AiRechargeGiftServiceImpl implements IAiRechargeGiftService {

    @Autowired
    private AiRechargeGiftMapper aiRechargeGiftMapper;

    @Autowired
    private AiRechargeGiftGearMapper aiRechargeGiftGearMapper;

    /**
     * 查询充值赠送活动
     * 
     * @param id 充值赠送活动主键
     * @return 充值赠送活动
     */
    @Override
    public AiRechargeGift selectAiRechargeGiftById(Long id) {
        return aiRechargeGiftMapper.selectAiRechargeGiftById(id);
    }

    /**
     * 查询充值赠送活动列表
     * 
     * @param aiRechargeGift 充值赠送活动
     * @return 充值赠送活动
     */
    @Override
    public List<AiRechargeGift> selectAiRechargeGiftList(AiRechargeGift aiRechargeGift) {
        return aiRechargeGiftMapper.selectAiRechargeGiftList(aiRechargeGift);
    }

    /**
     * 分页查询充值赠送活动列表
     *
     * @param aiRechargeGift 充值赠送活动
     * @return 充值赠送活动
     */
    @Override
    public IPage<AiRechargeGift> selectAiRechargeGiftPage(Page page, AiRechargeGift aiRechargeGift) {
        LambdaQueryWrapper<AiRechargeGift> query = Wrappers.lambdaQuery(aiRechargeGift);
        query.eq(AiRechargeGift::getDelFlag, "0");
        return aiRechargeGiftMapper.selectPage(page, query);
    }

    /**
     * 新增充值赠送活动
     * 
     * @param aiRechargeGift 充值赠送活动
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAiRechargeGift(AiRechargeGift aiRechargeGift) {
        aiRechargeGift.setDelFlag("0");
        aiRechargeGift.setCreateBy(SecurityUtils.getUsername());
        aiRechargeGift.setCreateTime(DateUtils.getNowDate());
        int insert = aiRechargeGiftMapper.insert(aiRechargeGift);
        for (AiRechargeGiftGear giftGear : aiRechargeGift.getGearList()) {
            giftGear.setRechargeId(aiRechargeGift.getId());
        }
        aiRechargeGiftGearMapper.insertBatch(aiRechargeGift.getGearList());
        return insert;
    }

    /**
     * 修改充值赠送活动
     * 
     * @param aiRechargeGift 充值赠送活动
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAiRechargeGift(AiRechargeGift aiRechargeGift) {
        aiRechargeGift.setUpdateBy(SecurityUtils.getUsername());
        aiRechargeGift.setUpdateTime(DateUtils.getNowDate());
        // 删除原有档位
        QueryWrapper<AiRechargeGiftGear> wrapper = new QueryWrapper<>();
        wrapper.eq("recharge_id", aiRechargeGift.getId());
        aiRechargeGiftGearMapper.delete(wrapper);
        for (AiRechargeGiftGear giftGear : aiRechargeGift.getGearList()) {
            giftGear.setRechargeId(aiRechargeGift.getId());
        }
        aiRechargeGiftGearMapper.insertBatch(aiRechargeGift.getGearList());
        return aiRechargeGiftMapper.updateById(aiRechargeGift);
    }

    /**
     * 批量删除充值赠送活动
     * 
     * @param ids 需要删除的充值赠送活动主键
     * @return 结果
     */
    @Override
    public int deleteAiRechargeGiftByIds(Long[] ids)
    {
        return aiRechargeGiftMapper.deleteAiRechargeGiftByIds(ids);
    }

    /**
     * 删除充值赠送活动信息
     * 
     * @param id 充值赠送活动主键
     * @return 结果
     */
    @Override
    public int deleteAiRechargeGiftById(Long id)
    {
        return aiRechargeGiftMapper.deleteAiRechargeGiftById(id);
    }

    @Override
    @Transactional
    public int changeUserStatus(AiRechargeGift aiRechargeGift) {
//        if (aiRechargeGift.getStatus() == 0) {
//            // 设置其他状态禁用
//            aiRechargeGiftMapper.updateAllStatus();
//        }
        return aiRechargeGiftMapper.updateById(aiRechargeGift);
    }

    @Override
    public boolean isActivityAvailable(Long rechargeId) {
        return aiRechargeGiftMapper.isActivityAvailable(rechargeId);
    }
}
