package com.ruoyi.export.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.export.domain.bo.ExportLanguageBo;
import com.ruoyi.export.domain.vo.ExportLanguageVo;
import com.ruoyi.export.domain.ExportLanguage;
import com.ruoyi.export.mapper.ExportLanguageMapper;
import com.ruoyi.export.service.IExportLanguageService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 语言管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-17
 */
@RequiredArgsConstructor
@Service
public class ExportLanguageServiceImpl implements IExportLanguageService {

    private final ExportLanguageMapper baseMapper;

    /**
     * 查询语言管理
     */
    @Override
    public ExportLanguageVo queryById(Long languageId){
        return baseMapper.selectVoById(languageId);
    }

    /**
     * 查询语言管理列表
     */
    @Override
    public TableDataInfo<ExportLanguageVo> queryPageList(ExportLanguageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExportLanguage> lqw = buildQueryWrapper(bo);
        Page<ExportLanguageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询语言管理列表
     */
    @Override
    public List<ExportLanguageVo> queryList(ExportLanguageBo bo) {
        LambdaQueryWrapper<ExportLanguage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExportLanguage> buildQueryWrapper(ExportLanguageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExportLanguage> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLangCode()), ExportLanguage::getLangCode, bo.getLangCode());
        lqw.like(StringUtils.isNotBlank(bo.getLanguageName()), ExportLanguage::getLanguageName, bo.getLanguageName());
        lqw.eq(bo.getIsDefault() != null, ExportLanguage::getIsDefault, bo.getIsDefault());
        lqw.eq(bo.getSortOrder() != null, ExportLanguage::getSortOrder, bo.getSortOrder());
        return lqw;
    }

    /**
     * 新增语言管理
     */
    @Override
    public Boolean insertByBo(ExportLanguageBo bo) {
        ExportLanguage add = BeanUtil.toBean(bo, ExportLanguage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLanguageId(add.getLanguageId());
        }
        return flag;
    }

    /**
     * 修改语言管理
     */
    @Override
    public Boolean updateByBo(ExportLanguageBo bo) {
        ExportLanguage update = BeanUtil.toBean(bo, ExportLanguage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExportLanguage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除语言管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
