package com.yolo.keyboard.service.userquotatotal;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yolo.keyboard.controller.admin.userquotatotal.vo.*;
import com.yolo.keyboard.dal.dataobject.userquotatotal.KeyboardUserQuotaTotalDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;

import com.yolo.keyboard.dal.mysql.userquotatotal.KeyboardUserQuotaTotalMapper;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.USER_QUOTA_TOTAL_NOT_EXISTS;

/**
 * 用户免费功能永久总次数额度表（所有功能共用） Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KeyboardUserQuotaTotalServiceImpl implements KeyboardUserQuotaTotalService {

    @Resource
    private KeyboardUserQuotaTotalMapper userQuotaTotalMapper;

    @Override
    public Long createUserQuotaTotal(KeyboardUserQuotaTotalSaveReqVO createReqVO) {
        // 插入
        KeyboardUserQuotaTotalDO userQuotaTotal = BeanUtils.toBean(createReqVO, KeyboardUserQuotaTotalDO.class);
        userQuotaTotalMapper.insert(userQuotaTotal);

        // 返回
        return userQuotaTotal.getUserId();
    }

    @Override
    public void updateUserQuotaTotal(KeyboardUserQuotaTotalSaveReqVO updateReqVO) {
        // 校验存在
        validateUserQuotaTotalExists(updateReqVO.getUserId());
        // 更新
        KeyboardUserQuotaTotalDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardUserQuotaTotalDO.class);
        userQuotaTotalMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserQuotaTotal(Long id) {
        // 校验存在
        validateUserQuotaTotalExists(id);
        // 删除
        userQuotaTotalMapper.deleteById(id);
    }

    @Override
        public void deleteUserQuotaTotalListByIds(List<Long> ids) {
        // 删除
        userQuotaTotalMapper.deleteByIds(ids);
        }


    private void validateUserQuotaTotalExists(Long id) {
        if (userQuotaTotalMapper.selectById(id) == null) {
            throw exception(USER_QUOTA_TOTAL_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardUserQuotaTotalDO getUserQuotaTotal(Long id) {
        return userQuotaTotalMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardUserQuotaTotalDO> getUserQuotaTotalPage(KeyboardUserQuotaTotalPageReqVO pageReqVO) {
        return userQuotaTotalMapper.selectPage(pageReqVO);
    }

}