package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ISysConfigService;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.platform.bean.SysConfig;
import cn.estsh.i3plus.pojo.platform.repository.SysConfigRepository;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreHqlPack;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : 系统参数服务接口实现
 * @Reference :
 * @Author : yunhao
 * @Date : 2018-10-25 9:25
 * @Modify :
 **/
@Service
public class SysConfigService implements ISysConfigService {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysConfigService.class);

    @Autowired
    private SysConfigRepository SysConfigRDao;

    @Override
    @ApiOperation(value = "添加系统参数")
    public void insertSysConfig(SysConfig sysConfig) {
        LOGGER.info("系统参数 SYS_CONFIG :{}",sysConfig);
        SysConfigRDao.insert(sysConfig);
    }

    @Override
    @ApiOperation(value = "删除系统参数")
    public void deleteSysConfigById(Long id) {
        LOGGER.info("系统参数 SYS_CONFIG :{}",id);
        SysConfigRDao.deleteById(id);
    }

    @Override
    @ApiOperation(value = "修改系统参数")
    public void updateSysConfig(SysConfig sysConfig) {
        LOGGER.info("系统参数 SYS_CONFIG :{}",sysConfig);
        SysConfigRDao.update(sysConfig);
    }

    @Override
    @ApiOperation(value = "查询全部系统参数")
    public List<SysConfig> ListSysConfig() {
        LOGGER.info("系统参数 SYS_CONFIG list");
        return SysConfigRDao.findAll();
    }

    @Override
    @ApiOperation(value = "根据id查询系统参数")
    public SysConfig getSysConfigById(Long id) {
        LOGGER.info("系统参数 SYS_CONFIG id:{}",id);
        return SysConfigRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "系统配置复杂查询分页排序")
    public ListPager querySysConfigByPager(SysConfig sysConfig, Pager pager) {
        LOGGER.info("系统参数 SYS_CONFIG SysConfig:{}，Pager:{}",sysConfig,pager);
        if(sysConfig == null) {
            pager = PagerHelper.getPager(pager, SysConfigRDao.listCount());
            return new ListPager(SysConfigRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysConfig(sysConfig);
            pager = PagerHelper.getPager(pager, SysConfigRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(SysConfigRDao.findByHqlWherePage(hqlPack + sysConfig.orderBy(),pager),pager);
        }
    }

    @Override
    @ApiOperation(value = "查询系统邮件配置项")
    public SysConfig getSysConfigByCode(String code) {
        return SysConfigRDao.getByProperty("configCode",code);
    }

    @Override
    @ApiOperation(value = "查询系统邮件配置项")
    public List findMailConfig() {
        List  mailConfig = new ArrayList();
        mailConfig.add(SysConfigRDao.getByProperty("configCode", PlatformConstWords.MAIL_HOST));
        mailConfig.add(SysConfigRDao.getByProperty("configCode", PlatformConstWords.MAIL_PORT));
        mailConfig.add(SysConfigRDao.getByProperty("configCode", PlatformConstWords.MAIL_USER));
        mailConfig.add(SysConfigRDao.getByProperty("configCode", PlatformConstWords.MAIL_PASSWORD));
        mailConfig.add(SysConfigRDao.getByProperty("configCode", PlatformConstWords.MAIL_NICK));

        return mailConfig;
    }

    @Override
    @ApiOperation(value = "根据code修改系统配置")
    public void updateSysConfigByCode(String code, String value) {
        SysConfigRDao.updateByProperties("configCode",code,"configValue",value);
    }

    @Override
    @ApiOperation(value = "根据系统参数组来查找系统参数")
    public List findSysConfigByGroup(Integer group) {
        return SysConfigRDao.findByProperty("configGroup",group);
    }

}
