package com.yolo.keyboard.service.usercharacter;

import com.yolo.keyboard.controller.admin.usercharacter.vo.KeyboardUserCharacterPageReqVO;
import com.yolo.keyboard.controller.admin.usercharacter.vo.KeyboardUserCharacterSaveReqVO;
import com.yolo.keyboard.dal.dataobject.usercharacter.KeyboardUserCharacterDO;
import com.yolo.keyboard.dal.mysql.usercharacter.KeyboardUserCharacterMapper;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

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

/**
 * 用户人设管理 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardUserCharacterServiceImpl implements KeyboardUserCharacterService {

    @Resource
    private KeyboardUserCharacterMapper userCharacterMapper;

    @Override
    public Long createUserCharacter(KeyboardUserCharacterSaveReqVO createReqVO) {
        // 插入
        KeyboardUserCharacterDO userCharacter = BeanUtils.toBean(createReqVO, KeyboardUserCharacterDO.class);
        userCharacterMapper.insert(userCharacter);

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

    @Override
    public void updateUserCharacter(KeyboardUserCharacterSaveReqVO updateReqVO) {
        // 校验存在
        validateUserCharacterExists(updateReqVO.getId());
        // 更新
        KeyboardUserCharacterDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardUserCharacterDO.class);
        userCharacterMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserCharacter(Long id) {
        // 校验存在
        validateUserCharacterExists(id);
        // 删除
        userCharacterMapper.deleteById(id);
    }

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


    private void validateUserCharacterExists(Long id) {
        if (userCharacterMapper.selectById(id) == null) {
            throw exception(USER_CHARACTER_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardUserCharacterDO getUserCharacter(Long id) {
        return userCharacterMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardUserCharacterDO> getUserCharacterPage(KeyboardUserCharacterPageReqVO pageReqVO) {
        return userCharacterMapper.selectPage(pageReqVO);
    }

}