package com.yolo.keyboard.service.userthemes;

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

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

import com.yolo.keyboard.dal.mysql.userthemes.KeyboardUserThemesMapper;

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

/**
 * 用户主题 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KeyboardUserThemesServiceImpl implements KeyboardUserThemesService {

    @Resource
    private KeyboardUserThemesMapper userThemesMapper;

    @Override
    public Long createUserThemes(KeyboardUserThemesSaveReqVO createReqVO) {
        // 插入
        KeyboardUserThemesDO userThemes = BeanUtils.toBean(createReqVO, KeyboardUserThemesDO.class);
        userThemesMapper.insert(userThemes);

        // 返回
        return userThemes.getId();
    }

    @Override
    public void updateUserThemes(KeyboardUserThemesSaveReqVO updateReqVO) {
        // 校验存在
        validateUserThemesExists(updateReqVO.getId());
        // 更新
        KeyboardUserThemesDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardUserThemesDO.class);
        userThemesMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserThemes(Long id) {
        // 校验存在
        validateUserThemesExists(id);
        // 删除
        userThemesMapper.deleteById(id);
    }

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


    private void validateUserThemesExists(Long id) {
        if (userThemesMapper.selectById(id) == null) {
            throw exception(USER_THEMES_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardUserThemesDO getUserThemes(Long id) {
        return userThemesMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardUserThemesDO> getUserThemesPage(KeyboardUserThemesPageReqVO pageReqVO) {
        return userThemesMapper.selectPage(pageReqVO);
    }

}