com.github.javaclub.base.service.impl.ConfigKeyServiceImpl Maven / Gradle / Ivy
/* Automatic generated by CrudCodeGenerator wirtten by Gerald Chen
*
* @(#)ConfigKeyServiceImpl.java 2023-09-08
*
* Copyright (c) 2023 - 2099. All Rights Reserved.
*
*/
package com.github.javaclub.base.service.impl;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.javaclub.base.domain.ConfigKey;
import com.github.javaclub.base.domain.ConfigList;
import com.github.javaclub.base.domain.query.ConfigKeyQuery;
import com.github.javaclub.base.mapper.ConfigKeyMapper;
import com.github.javaclub.base.service.ConfigKeyService;
import com.github.javaclub.base.service.ConfigListService;
import com.github.javaclub.sword.BizException;
import com.github.javaclub.sword.core.BizObjects;
import com.github.javaclub.sword.core.Collections;
import com.github.javaclub.sword.core.Numbers;
import com.github.javaclub.sword.domain.QueryResult;
import com.github.javaclub.sword.domain.ResultDO;
import com.github.javaclub.sword.domain.enumtype.BasicMessage;
/**
* ConfigKeyService 业务逻辑实现
*
* @version $Id: ConfigKeyServiceImpl.java 2023-09-08 16:25:13 Exp $
*/
@Service
public class ConfigKeyServiceImpl extends ServiceImpl implements ConfigKeyService {
static final Logger log = LoggerFactory.getLogger(ConfigKeyServiceImpl.class);
@Autowired
private ConfigKeyMapper configKeyMapper;
@Autowired
private ConfigListService configListService;
/**
* 创建时的业务校验
*
* @param configKey 业务实体
*/
void doCreateCheck(ConfigKey configKey) {
BizObjects.requireNotNull(configKey, "DB实体不能为空");
if (Objects.equals(ConfigKey.REFER_INTERNAL, configKey.getReferType())) {
BizObjects.requireNotEmpty(configKey.getConfigValue(), "配置取值不能为空!");
} else if (Objects.equals(ConfigKey.REFER_MULTI, configKey.getReferType())) {
// BizObjects.requireTrue(Collections.isNotEmpty(configKey.getValueList()), "多值列表不能为空!");
}
}
/**
* 更新时的业务校验
*
* @param configKey 业务实体
*/
ConfigKey doUpdateCheck(ConfigKey configKey) {
BizObjects.requireNotNull(configKey, "DB实体不能为空");
BizObjects.requireNotNullGtZero(configKey.getId(), "主键ID不能为空");
// TODO Auto-generated method stub
return null;
}
@Override
public ResultDO saveEntity(ConfigKey configKey) {
try {
if (null == configKey.getId()) {
Long id = create(configKey);
if (Numbers.isPositiveNumber(id)) {
return ResultDO.success(true);
}
}
ConfigKey db = configKeyMapper.selectById(configKey.getId());
if (null == db) {
return ResultDO.failure(BasicMessage.NOT_FOUND);
}
boolean suc = update(configKey);
if (suc) {
return ResultDO.success(true);
}
} catch (BizException e) {
return ResultDO.failure(e.getMessage(), e.getCode());
} catch (Exception e) {
return ResultDO.failure("数据库执行失败!", BasicMessage.EXCEPTIONED.getCode());
}
return ResultDO.failure();
}
@Transactional
@Override
public Long create(ConfigKey configKey) {
this.doCreateCheck(configKey);
ConfigKey db = this.selectByKey(configKey.getConfigKey());
BizObjects.requireTrue(null == db, "Key已经被占用!");
int num = configKeyMapper.insert(configKey);
if (0 >= num) {
throw new BizException(BasicMessage.DB_INSERT_FAILED.getCode(), "创建记录失败!");
}
if (Objects.equals(ConfigKey.REFER_MULTI, configKey.getReferType()) && Collections.isNotEmpty(configKey.getValueList())) {
for (ConfigList ele: configKey.getValueList()) {
BizObjects.requireNotEmpty(ele.getConfigValue(), "配置值不能为空!");
ele.setParentId(configKey.getId());
}
configListService.saveBatch(configKey.getValueList(), 100);
}
return configKey.getId();
}
@Override
public boolean update(ConfigKey configKey) {
this.doUpdateCheck(configKey);
int num = configKeyMapper.updateById(configKey);
return num > 0;
}
@Override
public ConfigKey selectById(Long id) {
return configKeyMapper.selectById(id);
}
@Override
public ConfigKey selectByKey(String key) {
ConfigKeyQuery query = ConfigKeyQuery.builder().key(key).build();
return this.selectOne(query);
}
@Override
public ConfigKey selectOne(ConfigKeyQuery query) {
QueryWrapper qw = query.queryWrapper();
List list = configKeyMapper.selectList(qw);
if(null != list && list.size() > 0) {
return list.get(0);
}
return null;
}
@Override
public int count(ConfigKeyQuery query) {
Integer count = configKeyMapper.selectCount(query.queryWrapper());
return null != count ? count.intValue() : 0;
}
@Override
public List findList(ConfigKeyQuery query) {
QueryWrapper qw = query.queryWrapper();
return configKeyMapper.selectList(qw);
}
@Override
public QueryResult findListWithCount(ConfigKeyQuery query) {
QueryResult queryResult = new QueryResult();
QueryWrapper qw = query.queryWrapper();
Page page = new Page<>(query.getPageNo(), query.getPageSize());
IPage pageResult = configKeyMapper.selectPage(page, qw);
if (null != pageResult) {
queryResult.setTotalCount(pageResult.getTotal());
queryResult.setEntry(pageResult.getRecords());
queryResult.setSuccess(true);
}
return queryResult;
}
}