package com.codvision.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.codvision.base.entity.SysConfig;
import com.codvision.base.entity.vo.SysConfigVO;
import com.codvision.base.mapper.SysConfigMapper;
import com.codvision.base.service.ISysConfigService;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.codvision.webcore.util.PageUtil;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统配置 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-09
 */
@Service
@DependsOn("dbVersionCtlInitializer")
public class SysSysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfig, SysConfigVO> implements ISysConfigService {

    @Override
    public SysConfigVO getDetail(Long id) {
        SysConfigVO vo = new SysConfigVO();
        SysConfig po = this.getById(id);
        if (null != po) {
            BeanUtil.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public IPage<SysConfigVO> getPageResult(QueryPage<SysConfig> queryPage) {
        QueryWrapper<SysConfig> queryWrapper = queryPage.getWrapper();
        IPage<SysConfigVO> pageResult = this.getBasePage(queryPage.getPage(), queryWrapper);
        return pageResult;
    }

    @Override
    public List<SysConfigVO> listAll(SysConfig queryBean) {
        QueryWrapper<SysConfig> wrapper = PageUtil.initWrapper(queryBean);
        List<SysConfig> list = this.list(wrapper);
        return BeanUtil.copyToList(list, SysConfigVO.class);
    }

    @Override
    public SysConfig getByKey(String key) {
        return this.getOne(Wrappers.<SysConfig>lambdaQuery().eq(SysConfig::getK, key));
    }

    @Override
    public String getValueByKey(String key) {
        SysConfig sysConfig = this.getOne(Wrappers.<SysConfig>lambdaQuery().eq(SysConfig::getK, key));
        if (ObjectUtil.isEmpty(sysConfig)) {
            return null;
        }
        return sysConfig.getV();
    }

    @Override
    public String getValueThrowException(String key) {
        SysConfig sysConfig = this.getOne(Wrappers.<SysConfig>lambdaQuery().eq(SysConfig::getK, key));
        if (ObjectUtil.isEmpty(sysConfig)) {
            throw new IllegalArgumentException(String.format("系统参数：【%s】值为空或不存在，请检查设置！", key));
        }
        return sysConfig.getV();
    }

    @Override
    public Map<String, List<SysConfig>> getListByLargeCategory(String largeCategory) {
        Map<String, List<SysConfig>> map = new HashMap<>(16);
        List<SysConfig> sysConfigs = this.list(Wrappers.<SysConfig>lambdaQuery().eq(SysConfig::getLargeCategory, largeCategory));
        if (CollectionUtil.isEmpty(sysConfigs)) {
            return map;
        }
        List<SysConfig> subclassList = sysConfigs.stream().filter(x -> StrUtil.isNotBlank(x.getSubclass())).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(subclassList)) {
            map.put(largeCategory, sysConfigs);
        } else {
            map = subclassList.stream().collect(Collectors.groupingBy(SysConfig::getSubclass));
        }
        return map;
    }

    @Override
    public void updateAll(List<SysConfig> sysConfigs) {
        this.updateBatchById(sysConfigs);
    }

    @Override
    public List<SysConfig> getListByKeys(List<String> keys) {
        return this.list(Wrappers.<SysConfig>lambdaQuery().in(SysConfig::getK, keys));
    }

    @Override
    public boolean save(SysConfig sysConfig) {
        Assert.notNull(sysConfig, "系统配置不能为空");
        getBaseMapper().insert(sysConfig);
        return true;
    }

    @Override
    public boolean updateById(SysConfig sysConfig) {
        Assert.notNull(sysConfig, "系统配置不能为空");
        Assert.notNull(sysConfig.getId(), "系统配置id不能为空");
        getBaseMapper().updateById(sysConfig);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        SysConfig sysConfig = getById(id);
        Assert.notNull(sysConfig, "系统配置不存在");
        getBaseMapper().deleteById(id);
        return true;
    }
}
