package com.dxhy.admin.sys.service.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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.dxhy.admin.sys.dao.SysConfigDao;
import com.dxhy.admin.sys.entity.SysConfigEntity;
import com.dxhy.admin.sys.service.SysConfigService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.Aops;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.Query;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@Service("sysConfigService")
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigDao, SysConfigEntity> implements SysConfigService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String paramKey = (String)params.get("paramKey");

        IPage<SysConfigEntity> page =
            this.page(new Query<SysConfigEntity>().getPage(params), new QueryWrapper<SysConfigEntity>()
                .like(StringUtils.isNotBlank(paramKey), "param_key", paramKey).eq("status", 1));

        return new PageUtils(page);
    }

    @Override
    public void saveConfig(SysConfigEntity config) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).insertConfig(config);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertConfig(SysConfigEntity config) {
        baseMapper.insert(config);
    }

    @Override
    public void update(SysConfigEntity config) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).updateConfig(config);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateConfig(SysConfigEntity config) {
        baseMapper.updateById(config);
    }

    @Override
    public void updateValueByKey(String key, String value) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).updateByKey(key, value);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateByKey(String key, String value) {
        baseMapper.updateValueByKey(key, value);
    }

    @Override
    public void delete(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        Aops.getSelf(this).deleteConfig(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteConfig(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public SysConfigEntity queryCode(String code) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectOne(new QueryWrapper<SysConfigEntity>().eq("param_key", code));
    }

    @Override
    public SysConfigEntity queryConfigById(Long id) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return baseMapper.selectById(id);
    }
}
