package com.yolo.keyboard.service.themes;

import cn.hutool.core.collection.CollUtil;
import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesPageReqVO;
import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesSaveReqVO;
import com.yolo.keyboard.dal.dataobject.themes.KeyboardThemesDO;
import com.yolo.keyboard.dal.mysql.themes.KeyboardThemesMapper;
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.THEMES_NOT_EXISTS;

/**
 * 键盘皮肤 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardThemesServiceImpl implements KeyboardThemesService {

    @Resource
    private KeyboardThemesMapper themesMapper;

    @Override
    public Long createThemes(KeyboardThemesSaveReqVO createReqVO) {
        // 插入
        KeyboardThemesDO themes = BeanUtils.toBean(createReqVO, KeyboardThemesDO.class);
        themesMapper.insert(themes);

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

    @Override
    public void updateThemes(KeyboardThemesSaveReqVO updateReqVO) {
        // 校验存在
        validateThemesExists(updateReqVO.getId());
        // 更新
        KeyboardThemesDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardThemesDO.class);
        themesMapper.updateById(updateObj);
    }

    @Override
    public void deleteThemes(Long id) {
        // 校验存在
        validateThemesExists(id);
        // 删除
        themesMapper.deleteById(id);
    }

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


    private void validateThemesExists(Long id) {
        if (themesMapper.selectById(id) == null) {
            throw exception(THEMES_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardThemesDO getThemes(Long id) {
        return themesMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardThemesDO> getThemesPage(KeyboardThemesPageReqVO pageReqVO) {
        return themesMapper.selectPage(pageReqVO);
    }

}