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.conditions.update.UpdateWrapper;
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.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.AiSampleAmountMapper;
import com.ruoyi.ai.domain.AiSampleAmount;
import com.ruoyi.ai.service.IAiSampleAmountService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 体验金活动管理Service业务层处理
 * 
 * @author shi
 * @date 2025-11-13
 */
@Service
public class AiSampleAmountServiceImpl implements IAiSampleAmountService {

    @Autowired
    private AiSampleAmountMapper aiSampleAmountMapper;

    /**
     * 查询体验金活动管理
     * 
     * @param id 体验金活动管理主键
     * @return 体验金活动管理
     */
    @Override
    public AiSampleAmount selectAiSampleAmountById(Long id) {
        return aiSampleAmountMapper.selectById(id);
    }

    /**
     * 查询体验金活动管理列表
     * 
     * @param aiSampleAmount 体验金活动管理
     * @return 体验金活动管理
     */
    @Override
    public List<AiSampleAmount> selectAiSampleAmountList(AiSampleAmount aiSampleAmount) {
        LambdaQueryWrapper<AiSampleAmount> query = Wrappers.lambdaQuery(aiSampleAmount);
        query.orderByDesc(AiSampleAmount::getId);
        query.eq(AiSampleAmount::getDelFlag, "0");
        return aiSampleAmountMapper.selectList(query);
    }

    /**
     * 分页查询体验金活动管理列表
     *
     * @param aiSampleAmount 体验金活动管理
     * @return 体验金活动管理
     */
    @Override
    public IPage<AiSampleAmount> selectAiSampleAmountPage(Page page, AiSampleAmount aiSampleAmount) {
        LambdaQueryWrapper<AiSampleAmount> query = Wrappers.lambdaQuery(aiSampleAmount);
        query.eq(AiSampleAmount::getDelFlag, "0");
        return aiSampleAmountMapper.selectPage(page, query);
    }

    /**
     * 新增体验金活动管理
     * 
     * @param aiSampleAmount 体验金活动管理
     * @return 结果
     */
    @Override
    public int insertAiSampleAmount(AiSampleAmount aiSampleAmount) {
        aiSampleAmount.setDelFlag("0");
        aiSampleAmount.setCreateBy(SecurityUtils.getUsername());
        aiSampleAmount.setCreateTime(DateUtils.getNowDate());
        return aiSampleAmountMapper.insert(aiSampleAmount);
    }

    /**
     * 修改体验金活动管理
     * 
     * @param aiSampleAmount 体验金活动管理
     * @return 结果
     */
    @Override
    public int updateAiSampleAmount(AiSampleAmount aiSampleAmount) {
        aiSampleAmount.setUpdateBy(SecurityUtils.getUsername());
        aiSampleAmount.setUpdateTime(DateUtils.getNowDate());
        return aiSampleAmountMapper.updateById(aiSampleAmount);
    }

    /**
     * 批量删除体验金活动管理
     * 
     * @param ids 需要删除的体验金活动管理主键
     * @return 结果
     */
    @Override
    public int deleteAiSampleAmountByIds(Long[] ids)
    {
        return aiSampleAmountMapper.deleteAiSampleAmountByIds(ids);
    }

    /**
     * 删除体验金活动管理信息
     * 
     * @param id 体验金活动管理主键
     * @return 结果
     */
    @Override
    public int deleteAiSampleAmountById(Long id)
    {
        return aiSampleAmountMapper.deleteAiSampleAmountById(id);
    }

    @Override
    @Transactional
    public int changeUserStatus(AiSampleAmount aiSampleAmount) {
        if (aiSampleAmount.getStatus() == 0) {
            // 设置全部为禁用状态
            aiSampleAmountMapper.updateAllStatus();
        }
        return aiSampleAmountMapper.updateById(aiSampleAmount);
    }
}
