package com.qmth.themis.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dao.SysConfigMapper;
import com.qmth.themis.business.dto.response.SysConfigBean;
import com.qmth.themis.business.entity.SysConfig;
import com.qmth.themis.business.service.SysConfigService;
import com.qmth.themis.business.service.ThemisCacheService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 系统参数 服务实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Resource
    ThemisCacheService themisCacheService;

    @Resource
    SysConfigMapper sysConfigMapper;

    /**
     * 查询所有系统配置项
     *
     * @return
     */
    @Override
    public List<SysConfig> selectAll() {
        List<SysConfig> sysConfigList = null;
        try {
            QueryWrapper<SysConfig> sysConfigQueryWrapper = new QueryWrapper<>();
            sysConfigQueryWrapper.lambda().isNull(SysConfig::getOrgId);
            sysConfigList = this.list(sysConfigQueryWrapper);
            if (!CollectionUtils.isEmpty(sysConfigList)) {
                for (SysConfig s : sysConfigList) {
                    themisCacheService.updateSysConfigCacheForDb(s.getConfigKey(), s);
                }
            }
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
        }
        return sysConfigList;
    }

    /**
     * 系统配置分页查询
     *
     * @param iPage
     * @return
     */
    @Override
    public IPage<SysConfigBean> select(IPage<Map> iPage) {
        return sysConfigMapper.select(iPage);
    }
}
