package com.yolo.keyboard.service.i18nmessage;

import cn.hutool.core.collection.CollUtil;
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.controller.admin.i18nmessage.vo.*;
import com.yolo.keyboard.dal.dataobject.i18nmessage.KeyboardI18nMessageDO;
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 com.yolo.keyboard.dal.mysql.i18nmessage.KeyboardI18nMessageMapper;

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.I18N_MESSAGE_NOT_EXISTS;

/**
 * 国际化错误提醒配置 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class KeyboardI18nMessageServiceImpl implements KeyboardI18nMessageService {

    @Resource
    private KeyboardI18nMessageMapper i18nMessageMapper;

    @Override
    public Long createI18nMessage(KeyboardI18nMessageSaveReqVO createReqVO) {
        // 插入
        KeyboardI18nMessageDO i18nMessage = BeanUtils.toBean(createReqVO, KeyboardI18nMessageDO.class);
        i18nMessageMapper.insert(i18nMessage);

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

    @Override
    public void updateI18nMessage(KeyboardI18nMessageSaveReqVO updateReqVO) {
        // 校验存在
        validateI18nMessageExists(updateReqVO.getId());
        // 更新
        KeyboardI18nMessageDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardI18nMessageDO.class);
        i18nMessageMapper.updateById(updateObj);
    }

    @Override
    public void deleteI18nMessage(Long id) {
        // 校验存在
        validateI18nMessageExists(id);
        // 删除
        i18nMessageMapper.deleteById(id);
    }

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


    private void validateI18nMessageExists(Long id) {
        if (i18nMessageMapper.selectById(id) == null) {
            throw exception(I18N_MESSAGE_NOT_EXISTS);
        }
    }

    @Override
    public KeyboardI18nMessageDO getI18nMessage(Long id) {
        return i18nMessageMapper.selectById(id);
    }

    @Override
    public PageResult<KeyboardI18nMessageDO> getI18nMessagePage(KeyboardI18nMessagePageReqVO pageReqVO) {
        return i18nMessageMapper.selectPage(pageReqVO);
    }

}