package com.ruoyi.model.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.model.mapper.AppletConfigMapper;
import com.ruoyi.model.domain.AppletConfig;
import com.ruoyi.model.service.IAppletConfigService;

/**
 * 配置信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-28
 */
@Service
public class AppletConfigServiceImpl extends ServiceImpl<AppletConfigMapper, AppletConfig> implements IAppletConfigService
{
    @Autowired
    private AppletConfigMapper appletConfigMapper;

    /**
     * 查询配置信息
     * 
     * @param id 配置信息主键
     * @return 配置信息
     */
    @Override
    public AppletConfig selectAppletConfigById(Long id)
    {
        return appletConfigMapper.selectById(id);
    }

    @Override
    public AppletConfig selectAppletConfigByClassAndNum(String paramClass,Integer paramValueNum){

//        return appletConfigMapper.selectAppletConfigByClassAndNum(paramClass,paramValueNum);

        return lambdaQuery().eq(AppletConfig::getParamClass, paramClass)
                .eq(AppletConfig::getParamValueNum, paramValueNum)
                .one();
    }

    /**
     * 查询配置信息列表
     * 
     * @param appletConfig 配置信息
     * @return 配置信息
     */
    @Override
    public List<AppletConfig> selectAppletConfigList(AppletConfig appletConfig)
    {
        if (appletConfig == null){
            appletConfig = new AppletConfig();
        }
        return appletConfigMapper.selectList(Wrappers.<AppletConfig>lambdaQuery()
                .eq(StringUtils.isNotEmpty(appletConfig.getParamCode()), AppletConfig::getParamCode, appletConfig.getParamCode())
                .like(StringUtils.isNotEmpty(appletConfig.getParamValueText()), AppletConfig::getParamValueText, appletConfig.getParamValueText())
                .like(StringUtils.isNotEmpty(appletConfig.getParamValue()), AppletConfig::getParamValue, appletConfig.getParamValue())
                .eq(StringUtils.isNotEmpty(appletConfig.getParamClass()), AppletConfig::getParamClass, appletConfig.getParamClass())
        );
    }

    /**
     * 新增配置信息
     * 
     * @param appletConfig 配置信息
     * @return 结果
     */
    @Override
    public int insertAppletConfig(AppletConfig appletConfig)
    {
        appletConfig.setCreateTime(DateUtils.getNowDate());
        return appletConfigMapper.insert(appletConfig);
    }

    /**
     * 修改配置信息
     * 
     * @param appletConfig 配置信息
     * @return 结果
     */
    @Override
    public int updateAppletConfig(AppletConfig appletConfig)
    {
        appletConfig.setUpdateTime(DateUtils.getNowDate());
        return appletConfigMapper.updateById(appletConfig);
    }

    /**
     * 批量删除配置信息
     * 
     * @param ids 需要删除的配置信息主键
     * @return 结果
     */
    @Override
    public int deleteAppletConfigByIds(Long[] ids)
    {
        return appletConfigMapper.deleteAppletConfigByIds(ids);
    }

    /**
     * 删除配置信息信息
     * 
     * @param id 配置信息主键
     * @return 结果
     */
    @Override
    public int deleteAppletConfigById(Long id)
    {
        return appletConfigMapper.deleteAppletConfigById(id);
    }

    @Override
    public AppletConfig getByCode(String code) {

        List<AppletConfig> appletConfigs = appletConfigMapper.selectList(Wrappers.<AppletConfig>lambdaQuery()
                .eq(AppletConfig::getParamCode, code));

        if (appletConfigs.size() > 0){
            return appletConfigs.get(0);
        }else {
            throw new ServiceException("配置缺失=" + code);
        }
    }
}
