package com.easyproject.system.service.coze.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.easyproject.common.utils.DateUtils;
import com.easyproject.service.domain.coze.CozeTokenSet;
import com.easyproject.service.mapper.coze.CozeTokenSetMapper;
import com.easyproject.system.service.coze.ICozeTokenSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * AI令牌Service业务层处理
 *
 * @author easyproject
 * @date 2025-03-21
 */
@Service
public class CozeTokenSetServiceImpl implements ICozeTokenSetService {
    @Autowired
    private CozeTokenSetMapper cozeTokenSetMapper;

    /**
     * 查询AI令牌
     *
     * @param id AI令牌主键
     * @return AI令牌
     */
    @Override
    public CozeTokenSet selectCozeTokenSetById(Long id) {
        return cozeTokenSetMapper.selectCozeTokenSetById(id);
    }

    /**
     * 查询AI令牌列表
     *
     * @param cozeTokenSet AI令牌
     * @return AI令牌
     */
    @Override
    public List<CozeTokenSet> selectCozeTokenSetList(CozeTokenSet cozeTokenSet) {
        return cozeTokenSetMapper.selectCozeTokenSetList(cozeTokenSet);
    }

    /**
     * 新增AI令牌
     *
     * @param cozeTokenSet AI令牌
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public int insertCozeTokenSet(CozeTokenSet cozeTokenSet) {
        int result = 0;
        if (cozeTokenSet.getStatus() == 1) {
            LambdaUpdateWrapper<CozeTokenSet> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(CozeTokenSet::getStatus, 2);
            updateWrapper.set(CozeTokenSet::getUpdateTime, DateUtil.date());
            updateWrapper.eq(CozeTokenSet::getStatus, 1);
            result = cozeTokenSetMapper.update(null, updateWrapper);
        }
        if (result > 0) {
            cozeTokenSet.setTokenStatus(1);
            cozeTokenSet.setCreateTime(DateUtils.getNowDate());
            result = cozeTokenSetMapper.insertCozeTokenSet(cozeTokenSet);
        }
        return result;
    }

    /**
     * 修改AI令牌
     *
     * @param cozeTokenSet AI令牌
     * @return 结果
     */
    @Override
    public int updateCozeTokenSet(CozeTokenSet cozeTokenSet) {
        if (cozeTokenSet.getStatus() == 1) {
            LambdaUpdateWrapper<CozeTokenSet> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.set(CozeTokenSet::getStatus, 2);
            updateWrapper.set(CozeTokenSet::getUpdateTime, DateUtil.date());
            updateWrapper.eq(CozeTokenSet::getStatus, 1);
            cozeTokenSetMapper.update(null, updateWrapper);
        }
        cozeTokenSet.setUpdateTime(DateUtils.getNowDate());
        return cozeTokenSetMapper.updateCozeTokenSet(cozeTokenSet);
    }

    /**
     * 批量删除AI令牌
     *
     * @param ids 需要删除的AI令牌主键
     * @return 结果
     */
    @Override
    public int deleteCozeTokenSetByIds(Long[] ids) {
        long count = cozeTokenSetMapper.selectCount(Wrappers.<CozeTokenSet>lambdaQuery().in(CozeTokenSet::getId, ids).eq(CozeTokenSet::getStatus, 1));
        if (count > 0) {
            throw new RuntimeException("删除失败，存在[正常]的令牌！");
        }
        return cozeTokenSetMapper.deleteCozeTokenSetByIds(ids);
    }
}
