package com.storlead.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.system.pojo.entity.SystemConfigItemEntity;
import com.storlead.system.mapper.SystemConfigItemMapper;
import com.storlead.system.service.SystemConfigItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统配置项 服务实现类
 * </p>
 *
 * @author chenkq
 * @since 2023-07-26
 */
@Service
public class SystemConfigItemServiceImpl extends ServiceImpl<SystemConfigItemMapper, SystemConfigItemEntity> implements SystemConfigItemService {
    @Override
    public Map<String,SystemConfigItemEntity> getSystemConfigItemMap() {
        LambdaQueryWrapper<SystemConfigItemEntity> systemConfigWrapper = new LambdaQueryWrapper<>();
        systemConfigWrapper.eq(SystemConfigItemEntity::getIsDelete,0);
        List<SystemConfigItemEntity> systemConfigItemList = this.list(systemConfigWrapper);
        Map<String,SystemConfigItemEntity> systemConfigItemMap = systemConfigItemList.stream().collect(Collectors.toMap(SystemConfigItemEntity::getItemCode, systemConfigItem -> systemConfigItem));
        return systemConfigItemMap;
    }

    @Override
    public SystemConfigItemEntity getSystemConfigItemByCode(String itemCode) {
        LambdaQueryWrapper<SystemConfigItemEntity> systemConfigWrapper = new LambdaQueryWrapper<>();
        systemConfigWrapper.eq(SystemConfigItemEntity::getIsDelete,0);
        systemConfigWrapper.eq(SystemConfigItemEntity::getItemCode,itemCode);
        List<SystemConfigItemEntity> systemConfigItemList = this.list(systemConfigWrapper);
        if (CollectionUtil.isNotEmpty(systemConfigItemList)) {
            return systemConfigItemList.get(0);
        }
        return null;
    }

    @Override
    public String getSystemConfigItemValueByCode(String itemCode) {
        SystemConfigItemEntity item = getSystemConfigItemByCode(itemCode);
        if(Objects.nonNull(item)) {
            return item.getItemFormatValue();
        }
        return null;
    }
}
