package com.yolo.keyboard.service.warningmessage;

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

import java.util.*;
import com.yolo.keyboard.controller.admin.warningmessage.vo.*;
import com.yolo.keyboard.dal.dataobject.warningmessage.KeyboardWarningMessageDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;

import com.yolo.keyboard.framework.common.util.object.BeanUtils;

import com.yolo.keyboard.dal.mysql.warningmessage.KeyboardWarningMessageMapper;

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

/**
 * 用户注销提示信息 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardWarningMessageServiceImpl implements KeyboardWarningMessageService {

    @Resource
    private KeyboardWarningMessageMapper warningMessageMapper;

    @Override
    public Long createWarningMessage(KeyboardWarningMessageSaveReqVO createReqVO) {
        // 插入
        KeyboardWarningMessageDO warningMessage = BeanUtils.toBean(createReqVO, KeyboardWarningMessageDO.class);
        warningMessageMapper.insert(warningMessage);

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

    @Override
    public void updateWarningMessage(KeyboardWarningMessageSaveReqVO updateReqVO) {
        // 校验存在
        validateWarningMessageExists(updateReqVO.getId());
        // 更新
        KeyboardWarningMessageDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardWarningMessageDO.class);
        warningMessageMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarningMessage(Long id) {
        // 校验存在
        validateWarningMessageExists(id);
        // 删除
        warningMessageMapper.deleteById(id);
    }

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


    private void validateWarningMessageExists(Long id) {
        if (warningMessageMapper.selectById(id) == null) {
            throw exception(WARNING_MESSAGE_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardWarningMessageDO getWarningMessage(Long id) {
        return warningMessageMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardWarningMessageDO> getWarningMessagePage(KeyboardWarningMessagePageReqVO pageReqVO) {
        return warningMessageMapper.selectPage(pageReqVO);
    }

}