package com.yolo.keyboard.service.user;


import com.yolo.keyboard.dal.dataobject.user.KeyboardUserDO;
import com.yolo.keyboard.dal.mysql.user.KeyboardUserMapper;
import com.yolo.keyboard.controller.admin.user.vo.KeyboardUserPageReqVO;
import com.yolo.keyboard.controller.admin.user.vo.KeyboardUserSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yolo.keyboard.framework.common.pojo.PageResult;
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.module.infra.enums.ErrorCodeConstants.*;

/**
 * 键盘用户 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardUserServiceImpl implements KeyboardUserService {

    @Resource
    private KeyboardUserMapper keyboardUserMapper;

    @Override
    public Long createKeyboardUser(KeyboardUserSaveReqVO createReqVO) {
        // 插入
        KeyboardUserDO keyboardUser = BeanUtils.toBean(createReqVO, KeyboardUserDO.class);
        keyboardUserMapper.insert(keyboardUser);

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

    @Override
    public void updateKeyboardUser(KeyboardUserSaveReqVO updateReqVO) {
        // 校验存在
        validateKeyboardUserExists(updateReqVO.getId());
        // 更新
        KeyboardUserDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardUserDO.class);
        keyboardUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteKeyboardUser(Long id) {
        // 校验存在
        validateKeyboardUserExists(id);
        // 删除
        keyboardUserMapper.deleteById(id);
    }

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


    private void validateKeyboardUserExists(Long id) {
        if (keyboardUserMapper.selectById(id) == null) {
            throw exception(KEYBOARD_USER_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardUserDO getKeyboardUser(Long id) {
        return keyboardUserMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardUserDO> getKeyboardUserPage(KeyboardUserPageReqVO pageReqVO) {
        return keyboardUserMapper.selectPage(pageReqVO);
    }

}