package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.constant.enums.common.TrueOrFalseEnum;
import org.wcs.mapper.AppBaseConfigMapper;
import org.wcs.mapper.intf.AppBaseConfigService;
import org.wcs.model.po.app.AppBaseConfig;
import org.wcs.utils.AppBeanUtils;
import org.wcs.utils.AppStringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * config 表的操作类
 */
@Service
@Slf4j
public class AppBaseConfigDao extends ServiceImpl<AppBaseConfigMapper, AppBaseConfig> implements AppBaseConfigService {

    /**
     * 查询所有config数据
     * @return 查询结果
     */
    @Override
    public List<AppBaseConfig> queryAll() {
        try{
            return super.list();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 模糊查询config表
     *
     * @param ConfigKey 配置键
     * @param ConfigName 配置名称
     * @param isQueryAll 是否查询所有
     * @return 查询结果
     */
    @Override
    public List<AppBaseConfig> queryIndistinct(String ConfigKey, String ConfigName, boolean isQueryAll) {
        LambdaQueryWrapper<AppBaseConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AppStringUtils.isNotEmpty(ConfigKey), AppBaseConfig::getConfigKey, ConfigKey);
        queryWrapper.like(AppStringUtils.isNotEmpty(ConfigName), AppBaseConfig::getConfigName, ConfigName);
        if(!isQueryAll) {
            queryWrapper.eq(AppBaseConfig::getClientShow, TrueOrFalseEnum.TRUE.getCode());
        }
        queryWrapper.orderByAsc(AppBaseConfig::getCreateTime);
        try {
            return super.list(queryWrapper);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 更新config表
     * @param appBaseConfig 更新条件
     * @return 查询结果
     */
    @Override
    public int update(AppBaseConfig appBaseConfig) {
        LambdaUpdateWrapper<AppBaseConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(appBaseConfig.getConfigKey() != null, AppBaseConfig::getConfigKey, appBaseConfig.getConfigKey())
                .set(AppStringUtils.isNotEmpty(appBaseConfig.getConfigName()), AppBaseConfig::getConfigName, appBaseConfig.getConfigName())
                .set(appBaseConfig.getConfigType() != null, AppBaseConfig::getConfigType, appBaseConfig.getConfigType())
                .set(appBaseConfig.getImmediately() != null, AppBaseConfig::getImmediately, appBaseConfig.getImmediately())
                .set(AppStringUtils.isNotEmpty(appBaseConfig.getConfigValue()), AppBaseConfig::getConfigValue, appBaseConfig.getConfigValue())
                .set(AppStringUtils.isNotEmpty(appBaseConfig.getTagText()), AppBaseConfig::getTagText, appBaseConfig.getTagText())
                .set(appBaseConfig.getClientShow() != null, AppBaseConfig::getClientShow, appBaseConfig.getClientShow())
                .set(AppStringUtils.isNotEmpty(appBaseConfig.getValueType()), AppBaseConfig::getValueType, appBaseConfig.getValueType())
                .set(AppBaseConfig::getUpdateTime, LocalDateTime.now())
                .set(AppStringUtils.isNotEmpty(appBaseConfig.getRemark()), AppBaseConfig::getRemark, appBaseConfig.getRemark());
        try {
            return super.update(appBaseConfig, updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            return 0;
        }
    }
}
