package cn.iocoder.yudao.module.tkdata.service.language;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.LanguagePageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.LanguageSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.language.LanguageDO;
import cn.iocoder.yudao.module.tkdata.dal.mysql.language.LanguageMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;



import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.tkdata.enums.ErrorCodeConstants.LANGUAGE_NOT_EXISTS;


/**
 * AI 语言种类 Service 实现类
 *
 * @author 总后台
 */
@Service
@Validated
public class LanguageServiceImpl implements LanguageService {

    @Resource
    private LanguageMapper languageMapper;

    @Override
    public Integer createLanguage(LanguageSaveReqVO createReqVO) {
        // 插入
        LanguageDO language = BeanUtils.toBean(createReqVO, LanguageDO.class);
        languageMapper.insert(language);
        // 返回
        return language.getId();
    }

    @Override
    public void updateLanguage(LanguageSaveReqVO updateReqVO) {
        // 校验存在
        validateLanguageExists(updateReqVO.getId());
        // 更新
        LanguageDO updateObj = BeanUtils.toBean(updateReqVO, LanguageDO.class);
        languageMapper.updateById(updateObj);
    }

    @Override
    public void deleteLanguage(Integer id) {
        // 校验存在
        validateLanguageExists(id);
        // 删除
        languageMapper.deleteById(id);
    }

    @Override
        public void deleteLanguageListByIds(List<Integer> ids) {
        // 校验存在
        validateLanguageExists(ids);
        // 删除
        languageMapper.deleteByIds(ids);
        }

    private void validateLanguageExists(List<Integer> ids) {
        List<LanguageDO> list = languageMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(LANGUAGE_NOT_EXISTS);
        }
    }

    private void validateLanguageExists(Integer id) {
        if (languageMapper.selectById(id) == null) {
            throw exception(LANGUAGE_NOT_EXISTS);
        }
    }

    @Override
    public LanguageDO getLanguage(Integer id) {
        return languageMapper.selectById(id);
    }

    @Override
    public PageResult<LanguageDO> getLanguagePage(LanguagePageReqVO pageReqVO) {
        return languageMapper.selectPage(pageReqVO);
    }

}