package com.yolo.keyboard.service.themestyles;

import cn.hutool.core.collection.CollUtil;
import com.yolo.keyboard.controller.admin.themestyles.vo.KeyboardThemeStylesPageReqVO;
import com.yolo.keyboard.controller.admin.themestyles.vo.KeyboardThemeStylesSaveReqVO;
import com.yolo.keyboard.dal.dataobject.themestyles.KeyboardThemeStylesDO;
import com.yolo.keyboard.dal.mysql.themestyles.KeyboardThemeStylesMapper;
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.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.convertList;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.diffList;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.KEYBOARD_THEME_STYLES_NOT_EXISTS;

/**
 * 主题风格 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardThemeStylesServiceImpl implements KeyboardThemeStylesService {

    @Resource
    private KeyboardThemeStylesMapper keyboardThemeStylesMapper;

    @Override
    public Long createKeyboardThemeStyles(KeyboardThemeStylesSaveReqVO createReqVO) {
        // 插入
        KeyboardThemeStylesDO keyboardThemeStyles = BeanUtils.toBean(createReqVO, KeyboardThemeStylesDO.class);
        keyboardThemeStylesMapper.insert(keyboardThemeStyles);

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

    @Override
    public void updateKeyboardThemeStyles(KeyboardThemeStylesSaveReqVO updateReqVO) {
        // 校验存在
        validateKeyboardThemeStylesExists(updateReqVO.getId());
        // 更新
        KeyboardThemeStylesDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardThemeStylesDO.class);
        keyboardThemeStylesMapper.updateById(updateObj);
    }

    @Override
    public void deleteKeyboardThemeStyles(Long id) {
        // 校验存在
        validateKeyboardThemeStylesExists(id);
        // 删除
        keyboardThemeStylesMapper.deleteById(id);
    }

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


    private void validateKeyboardThemeStylesExists(Long id) {
        if (keyboardThemeStylesMapper.selectById(id) == null) {
            throw exception(KEYBOARD_THEME_STYLES_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardThemeStylesDO getKeyboardThemeStyles(Long id) {
        return keyboardThemeStylesMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardThemeStylesDO> getKeyboardThemeStylesPage(KeyboardThemeStylesPageReqVO pageReqVO) {
        return keyboardThemeStylesMapper.selectPage(pageReqVO);
    }

}