1.新增创建基础数据方法
This commit is contained in:
parent
5f79c574a8
commit
9f1994c9d6
|
@ -0,0 +1,36 @@
|
|||
package com.example.liteflow.mysql.controller;
|
||||
|
||||
import com.example.liteflow.mysql.model.CommonResult;
|
||||
import com.example.liteflow.mysql.model.baseData.CreateBaseDataFrom;
|
||||
import com.example.liteflow.mysql.service.BaseDataService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author: 陈韦龙
|
||||
* @date: 2022年12月16日 10:18
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/baseData")
|
||||
public class BaseDataController {
|
||||
|
||||
@Resource
|
||||
private BaseDataService baseDataService;
|
||||
|
||||
@PostMapping("/createBaseData")
|
||||
public CommonResult createBaseData(@RequestBody CreateBaseDataFrom createBaseDataFrom) {
|
||||
return baseDataService.createBaseData(createBaseDataFrom);
|
||||
}
|
||||
|
||||
@PostMapping("/batchCreateBaseData")
|
||||
public CommonResult batchCreateBaseData(@RequestBody List<CreateBaseDataFrom> createBaseDataFromList) {
|
||||
return baseDataService.batchCreateBaseData(createBaseDataFromList);
|
||||
}
|
||||
}
|
|
@ -26,6 +26,14 @@ public class CommonResult<T> {
|
|||
return commonResult;
|
||||
}
|
||||
|
||||
public static CommonResult fail(String message, Object obj) {
|
||||
CommonResult commonResult = new CommonResult();
|
||||
commonResult.setCode(999);
|
||||
commonResult.setMessage(message);
|
||||
commonResult.setData(obj);
|
||||
return commonResult;
|
||||
}
|
||||
|
||||
public static CommonResult success(String message) {
|
||||
CommonResult commonResult = new CommonResult();
|
||||
commonResult.setCode(200);
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package com.example.liteflow.mysql.model.baseData;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author: 陈韦龙
|
||||
* @date: 2022年12月16日 10:20
|
||||
*/
|
||||
@Data
|
||||
public class CreateBaseDataFrom {
|
||||
private String type;
|
||||
private String templateId;
|
||||
private String content;
|
||||
private String contentType;
|
||||
private String contentName;
|
||||
}
|
|
@ -2,6 +2,10 @@ package com.example.liteflow.mysql.service;
|
|||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.example.liteflow.mysql.entity.BaseDataEntity;
|
||||
import com.example.liteflow.mysql.model.CommonResult;
|
||||
import com.example.liteflow.mysql.model.baseData.CreateBaseDataFrom;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author: 陈韦龙
|
||||
|
@ -9,6 +13,7 @@ import com.example.liteflow.mysql.entity.BaseDataEntity;
|
|||
*/
|
||||
public interface BaseDataService extends IService<BaseDataEntity> {
|
||||
BaseDataEntity getByTypeAndTemplateId(String type, String attribution);
|
||||
BaseDataEntity getByTypeAndName(String type, String attribution, String contentName);
|
||||
CommonResult createBaseData(CreateBaseDataFrom createBaseDataFrom);
|
||||
|
||||
CommonResult batchCreateBaseData(List<CreateBaseDataFrom> createBaseDataFromList);
|
||||
}
|
||||
|
|
|
@ -1,13 +1,26 @@
|
|||
package com.example.liteflow.mysql.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.example.liteflow.mysql.entity.BaseDataEntity;
|
||||
import com.example.liteflow.mysql.enums.BaseDataContentTypeEnum;
|
||||
import com.example.liteflow.mysql.mapper.BaseDataMapper;
|
||||
import com.example.liteflow.mysql.model.BaseCenter;
|
||||
import com.example.liteflow.mysql.model.CommonResult;
|
||||
import com.example.liteflow.mysql.model.baseData.CreateBaseDataFrom;
|
||||
import com.example.liteflow.mysql.service.BaseDataService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author: 陈韦龙
|
||||
* @date: 2022年12月06日 09:59
|
||||
|
@ -25,12 +38,34 @@ public class BaseDataServiceImpl extends ServiceImpl<BaseDataMapper, BaseDataEnt
|
|||
}
|
||||
|
||||
@Override
|
||||
public BaseDataEntity getByTypeAndName(String type, String attribution, String contentName) {
|
||||
log.info("type = {}, attribution = {}", type, attribution);
|
||||
LambdaQueryWrapper<BaseDataEntity> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(BaseDataEntity::getType, type)
|
||||
.eq(BaseDataEntity::getTemplateId, attribution)
|
||||
.eq(BaseDataEntity::getContentName, contentName);
|
||||
return this.getOne(queryWrapper);
|
||||
public CommonResult createBaseData(CreateBaseDataFrom createBaseDataFrom) {
|
||||
log.info("进入【创建基础数据】方法 = {}", JSONUtil.toJsonStr(createBaseDataFrom));
|
||||
String type = createBaseDataFrom.getType();
|
||||
Field[] fields = ReflectUtil.getFields(BaseCenter.class);
|
||||
List<String> fieldNameList = Arrays.stream(fields).map(Field::getName).collect(Collectors.toList());
|
||||
if (!fieldNameList.contains(type)) {
|
||||
return CommonResult.fail("添加数据无对应字段");
|
||||
}
|
||||
BaseDataEntity baseDataEntity = BeanUtil.toBean(createBaseDataFrom, BaseDataEntity.class);
|
||||
this.save(baseDataEntity);
|
||||
return CommonResult.success("创建基础数据成功", baseDataEntity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult batchCreateBaseData(List<CreateBaseDataFrom> createBaseDataFromList) {
|
||||
log.info("进入【批量创建基础数据】方法 = {}", JSONUtil.toJsonStr(createBaseDataFromList));
|
||||
List<CreateBaseDataFrom> notExistsType = createBaseDataFromList.stream().filter(createBaseDataFrom -> {
|
||||
String type = createBaseDataFrom.getType();
|
||||
Field[] fields = ReflectUtil.getFields(BaseCenter.class);
|
||||
List<String> fieldNameList = Arrays.stream(fields).map(Field::getName).collect(Collectors.toList());
|
||||
return !fieldNameList.contains(type);
|
||||
}).collect(Collectors.toList());
|
||||
if (CollUtil.isNotEmpty(notExistsType)) {
|
||||
return CommonResult.fail("添加数据无对应字段", notExistsType);
|
||||
}
|
||||
List<BaseDataEntity> baseDataEntityList = createBaseDataFromList.stream()
|
||||
.map(createBaseDataFrom -> BeanUtil.toBean(createBaseDataFrom, BaseDataEntity.class)).collect(Collectors.toList());
|
||||
this.saveBatch(baseDataEntityList);
|
||||
return CommonResult.success("批量创建基础数据成功", baseDataEntityList);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue