All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lframework.starter.mybatis.impl.system.SysParameterServiceImpl Maven / Gradle / Ivy

There is a newer version: 3.0.6
Show newest version
package com.lframework.starter.mybatis.impl.system;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.common.exceptions.impl.ParameterNotFoundException;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.common.utils.StringUtil;
import com.lframework.starter.mybatis.annotations.OpLog;
import com.lframework.starter.mybatis.entity.SysParameter;
import com.lframework.starter.mybatis.enums.DefaultOpLogType;
import com.lframework.starter.mybatis.impl.BaseMpServiceImpl;
import com.lframework.starter.mybatis.mappers.system.SysParameterMapper;
import com.lframework.starter.mybatis.resp.PageResult;
import com.lframework.starter.mybatis.service.system.SysParameterService;
import com.lframework.starter.mybatis.utils.OpLogUtil;
import com.lframework.starter.mybatis.utils.PageHelperUtil;
import com.lframework.starter.mybatis.utils.PageResultUtil;
import com.lframework.starter.mybatis.vo.system.parameter.CreateSysParameterVo;
import com.lframework.starter.mybatis.vo.system.parameter.QuerySysParameterVo;
import com.lframework.starter.mybatis.vo.system.parameter.UpdateSysParameterVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
public class SysParameterServiceImpl extends
    BaseMpServiceImpl implements SysParameterService,
    com.lframework.starter.web.service.SysParameterService {

  @Override
  public PageResult query(Integer pageIndex, Integer pageSize,
      QuerySysParameterVo vo) {

    Assert.greaterThanZero(pageIndex);
    Assert.greaterThanZero(pageSize);

    PageHelperUtil.startPage(pageIndex, pageSize);
    List datas = this.query(vo);

    return PageResultUtil.convert(new PageInfo<>(datas));
  }

  @Override
  public List query(QuerySysParameterVo vo) {

    return getBaseMapper().query(vo);
  }

  @Cacheable(value = SysParameter.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
  @Override
  public SysParameter findById(Long id) {

    return getBaseMapper().selectById(id);
  }

  @Cacheable(value = SysParameter.CACHE_NAME, key = "@cacheVariables.tenantId() + #key", unless = "#result == null")
  @Override
  public String findByKey(String key) {

    Wrapper queryWrapper = Wrappers.lambdaQuery(SysParameter.class)
        .eq(SysParameter::getPmKey, key);
    SysParameter data = getBaseMapper().selectOne(queryWrapper);

    return data == null ? null : data.getPmValue();
  }

  @Override
  public String findRequiredByKey(String key) throws ParameterNotFoundException {
    com.lframework.starter.web.service.SysParameterService thisService = getThis(this.getClass());
    String data = thisService.findByKey(key);
    if (data == null) {
      throw new ParameterNotFoundException();
    }

    return data;
  }

  @Override
  public String findByKey(String key, String defaultValue) {
    com.lframework.starter.web.service.SysParameterService thisService = getThis(this.getClass());
    String data = thisService.findByKey(key);
    if (data == null) {
      return defaultValue;
    }

    return data;
  }

  @OpLog(type = DefaultOpLogType.OTHER, name = "新增系统参数,ID:{}", params = {"#id"})
  @Transactional(rollbackFor = Exception.class)
  @Override
  public Long create(CreateSysParameterVo vo) {

    Wrapper checkWrapper = Wrappers.lambdaQuery(SysParameter.class)
        .eq(SysParameter::getPmKey, vo.getPmKey());
    if (this.count(checkWrapper) > 0) {
      throw new DefaultClientException("键重复,请重新输入!");
    }
    SysParameter data = new SysParameter();
    data.setPmKey(vo.getPmKey());
    if (!StringUtil.isEmpty(vo.getPmValue())) {
      data.setPmValue(vo.getPmValue());
    }
    if (!StringUtil.isBlank(vo.getDescription())) {
      data.setDescription(vo.getDescription());
    }

    getBaseMapper().insert(data);

    OpLogUtil.setVariable("id", String.valueOf(data.getId()));
    OpLogUtil.setExtra(vo);

    return data.getId();
  }

  @OpLog(type = DefaultOpLogType.OTHER, name = "修改系统参数,ID:{}", params = {"#id"})
  @Transactional(rollbackFor = Exception.class)
  @Override
  public void update(UpdateSysParameterVo vo) {

    SysParameter data = getBaseMapper().selectById(vo.getId());
    if (ObjectUtil.isNull(data)) {
      throw new DefaultClientException("系统参数不存在!");
    }

    LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(SysParameter.class)
        .set(SysParameter::getPmValue, StringUtil.isBlank(vo.getPmValue()) ? null : vo.getPmValue())
        .set(SysParameter::getDescription,
            StringUtil.isBlank(vo.getDescription()) ? null : vo.getDescription())
        .eq(SysParameter::getId, vo.getId());

    getBaseMapper().update(updateWrapper);

    OpLogUtil.setVariable("id", String.valueOf(data.getId()));
    OpLogUtil.setExtra(vo);
  }

  @OpLog(type = DefaultOpLogType.OTHER, name = "删除系统参数,ID:{}", params = {"#id"})
  @Transactional(rollbackFor = Exception.class)
  @Override
  public void deleteById(Long id) {

    getBaseMapper().deleteById(id);
  }

  @Override
  public Boolean getBoolean(String key) {
    com.lframework.starter.web.service.SysParameterService thisService = getThis(getClass());
    String value = thisService.findByKey(key);
    return value == null ? null : "true".equalsIgnoreCase(value);
  }

  @Override
  public Boolean getBoolean(String key, Boolean defaultValue) {
    Boolean value = getBoolean(key);
    if (value == null) {
      return defaultValue;
    }

    return value;
  }

  @Override
  public Integer getInteger(String key) {
    com.lframework.starter.web.service.SysParameterService thisService = getThis(getClass());
    String value = thisService.findByKey(key);
    try {
      return value == null ? null : Integer.valueOf(value);
    }catch (NumberFormatException e) {
      // 转换失败
      return null;
    }
  }

  @Override
  public Integer getInteger(String key, Integer defaultValue) {
    Integer value = getInteger(key);
    if (value == null) {
      return defaultValue;
    }

    return value;
  }

  @CacheEvict(value = SysParameter.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
  @Override
  public void cleanCacheByKey(Serializable key) {

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy