package com.yolo.keyboard.service.character;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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

import com.yolo.keyboard.dal.mysql.character.KeyboardCharacterMapper;

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


/**
 * 键盘人设 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {

    @Resource
    private KeyboardCharacterMapper characterMapper;

    @Override
    public Long createCharacter(KeyboardCharacterSaveReqVO createReqVO) {
        // 插入
        KeyboardCharacterDO character = BeanUtils.toBean(createReqVO, KeyboardCharacterDO.class);
        characterMapper.insert(character);

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

    @Override
    public void updateCharacter(KeyboardCharacterSaveReqVO updateReqVO) {
        // 校验存在
        validateCharacterExists(updateReqVO.getId());
        // 更新
        KeyboardCharacterDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardCharacterDO.class);
        characterMapper.updateById(updateObj);
    }

    @Override
    public void deleteCharacter(Long id) {
        // 校验存在
        validateCharacterExists(id);
        // 删除
        characterMapper.deleteById(id);
    }

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


    private void validateCharacterExists(Long id) {
        if (characterMapper.selectById(id) == null) {
            throw exception(CHARACTER_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardCharacterDO getCharacter(Long id) {
        return characterMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardCharacterDO> getCharacterPage(KeyboardCharacterPageReqVO pageReqVO) {
        return characterMapper.selectPage(pageReqVO);
    }

}