package cn.iocoder.yudao.module.system.service.dict;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.language.LanguageDictPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.language.LanguageDictImportVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.LanguageDictDO;
import cn.iocoder.yudao.module.system.dal.mysql.dict.LanguageDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.Date;
import java.util.List;


/**
 * 系统语言字典 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class LanguageDictServiceImpl implements LanguageDictService {

    @Resource
    private LanguageDictMapper languageDictMapper;


    @Override
    public void createLanguageDict(List<LanguageDictDO> createReqVO) {
        for (LanguageDictDO languageDictDO : createReqVO) {
            languageDictDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            languageDictDO.setCreatedTime(new Date());
            languageDictMapper.insert(languageDictDO);
        }
    }

    @Override
    public void updateLanguageDict(LanguageDictDO updateReqVO) {
        languageDictMapper.updateById(updateReqVO);
    }

    @Override
    public void deleteLanguageDict(Long[] ids) {
        for (Long id : ids) {
            languageDictMapper.deleteById(id);
        }
    }

    @Override
    public LanguageDictDO getLanguageDict(Long id) {
        return languageDictMapper.selectById(id);
    }

    @Override
    public PageResult<LanguageDictDO> getLanguageDictPage(LanguageDictPageReqVO pageReqVO) {
        return languageDictMapper.selectPage(pageReqVO);
    }

    @Override
    public void importLanguageDictExcel(List<LanguageDictImportVO> list) {
        for (LanguageDictImportVO importExcelData : list) {
            LanguageDictDO dbLanguage = languageDictMapper.selectOne(new LambdaQueryWrapperX<LanguageDictDO>()
                    .eq(LanguageDictDO::getType, importExcelData.getType())
                    .eq(LanguageDictDO::getLanguageKey, importExcelData.getLanguageKey()));

            if (dbLanguage != null){
                dbLanguage.setLanguageKey(importExcelData.getLanguageKey());
                dbLanguage.setLanguageValue(importExcelData.getLanguageValue());
                languageDictMapper.updateById(dbLanguage);
            }else {
                LanguageDictDO languageDictDO = new LanguageDictDO();
                BeanUtils.copyProperties(importExcelData,languageDictDO);
                languageDictDO.setCreatedTime(new Date());
                languageDictDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
                languageDictMapper.insert(languageDictDO);
            }
        }
    }
}