代碼生成器增加controller的生成

This commit is contained in:
wulin 2023-09-20 13:49:26 +08:00
parent 43caf30d10
commit 6c41ec2cb2
4 changed files with 141 additions and 9 deletions

View File

@ -5,22 +5,25 @@ import com.qiuguo.iot.base.enums.DeviceTypeEnum;
import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.data.entity.device.DeviceInfoEntity;
import com.qiuguo.iot.data.request.device.DeviceInfoRequest;
import com.qiuguo.iot.data.resp.device.DeviceInfoResp;
import com.qiuguo.iot.data.service.device.DeviceBatchService;
import com.qiuguo.iot.data.service.device.DeviceInfoService;
import com.qiuguo.iot.user.api.resp.device.DeviceInitResp;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.rdb.mapping.ReactiveQuery;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
@RestController
@Slf4j
@ -30,7 +33,7 @@ public class DeviceController {
private String key;
@Value("${device.checkTimeout}")
private Boolean checkTimeout;
@Resource
@Autowired
private DeviceInfoService deviceInfoService;
@Resource
@ -43,6 +46,7 @@ public class DeviceController {
public Mono<DeviceInitResp> deviceInit(@RequestParam String wifiMac, @RequestParam String btMac,
@RequestParam Integer type, @RequestParam Long time,
@RequestParam String signature){
//deviceInfoService.
Long now = System.currentTimeMillis();
if(checkTimeout && now - time > timeOut){
//超时

View File

@ -82,7 +82,7 @@ public class MysqlMain {
}
List<TablesBean> list = new ArrayList<>();
list.add(new TablesBean("user_home"));
list.add(new TablesBean("device_info"));
list.add(new TablesBean("user_room"));
List<TablesBean> list2 = new ArrayList<TablesBean>();
@ -103,8 +103,9 @@ public class MysqlMain {
}
/*for (int i = 0; i < list2.size(); i++) {
MysqlUtilTable2Contoller.printController(list2.get(i));
for (int i = 0; i < list2.size(); i++) {
//MysqlUtilTable2Contoller.printController(list2.get(i));
MysqlUtilTable2Contoller.printReactorController(list2.get(i));
}

View File

@ -3,6 +3,10 @@ package com.qiuguo.iot.customer.http.api.mysql;
import com.qiuguo.iot.data.entity.device.DeviceInfoEntity;
import com.qiuguo.iot.data.resp.device.DeviceInfoResp;
import org.hswebframework.web.api.crud.entity.PagerResult;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
@ -12,6 +16,7 @@ import java.util.Date;
import java.util.List;
public class MysqlUtilTable2Contoller {
public static String TAB = " ";
/**
* 打印entity的信息
*/
@ -19,6 +24,7 @@ public class MysqlUtilTable2Contoller {
boolean hasDate = false;
List<FieldBean> list = tableBean.getFieldList();
StringBuffer bf = new StringBuffer();
@ -112,4 +118,125 @@ public class MysqlUtilTable2Contoller {
} finally {
}
}
public static void printReactorController(TablesBean tableBean) {
String realName = MysqlMain.pre + tableBean.getSpaceName();
String fileName = MysqlMain.save_path + "/" + realName + "Controller.java";
List<FieldBean> list = tableBean.getFieldList();
try {
String content = "package com.admin.service.impl;\n\n\n\n" +
"import org.apache.commons.lang3.StringUtils;\n" +
"import java.util.Date;\n";
content += "/**\n";
content += "* <p>\n";
content += "* " + tableBean.getComment().replace("", "") + "Controller类\n";
content += "* </p>\n";
content += "*\n";
content += "* @author wulin\n";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
content += "* @since " + format.format(new Date()) + "\n";
content += "*/\n";
content += "\n";
content += "@RestController\n@Slf4j\n@RequestMapping(\"/" + realName + "\")\n";
content += "public class " + tableBean.getSpaceName() + "Controller{\n";
content += "\n";
content += "\n";
content += TAB + "@Autowired\n";
content += TAB + "private " + realName + "Service " + MysqlUtil.getFirstToLower(realName) + "Service;\n";
content += TAB + "@PostMapping(\"/info\")\n";
content += TAB + "public Mono<" + realName + "Resp> select" + realName + "ByRequest(@RequestBody " + realName + "Request request){\n";
content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.select"
+ realName + "ByRequest(request).map(d -> {return new " + realName + "Resp(d);});\n";
content += TAB + "}\n";
content += "\n";
content += "\n";
content += "\n";
content += TAB + "@PostMapping(\"/list\")\n";
content += TAB + "public Mono<PagerResult<" + realName + "Resp>> select" + realName + "sByRequest(@RequestBody " + realName + "Request request){\n";
content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.selectDeviceInfosByRequest(request).map(d -> {\n" +
" PagerResult<" + realName + "Resp> result = new PagerResult<>();\n" +
" result.setPageIndex(d.getPageIndex());\n" +
" result.setPageSize(d.getPageSize());\n" +
" result.setTotal(d.getTotal());\n" +
" List<" + realName + "Resp> ds = d.getData().stream().map(new Function<" + realName + "Entity, " + realName + "Resp>() {\n" +
" @Override\n" +
" public DeviceInfoResp apply(" + realName + "Entity entity) {\n" +
" return new " + realName + "Resp(entity);\n" +
" }\n" +
" }\n" +
"\n" +
" ).collect(Collectors.toList());\n" +
" result.setData(ds);\n" +
" return result;\n" +
" });\n";
content += TAB + "}\n";
content += "\n";
content += "\n";
content += "\n";
content += TAB + "@GetMapping(\"/id\")\n";
content += TAB + "public Mono<" + realName + "Resp> select" + realName + "ById(@RequestParam Long id){\n";
content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.select"
+ realName + "ById(id).map(d -> {return new " + realName + "Resp(d);});\n";
content += TAB + "}\n";
content += "\n";
content += "\n";
content += "\n";
content += TAB + "@PostMapping(\"/save\")\n";
content += TAB + "public Mono<Integer> insert" + realName + "(@RequestBody " + realName + "Entity entity){\n";
content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.insert"
+ realName + "(entity);\n";
content += TAB + "}\n";
content += "\n";
content += "\n";
content += "\n";
content += TAB + "@PostMapping(\"/update\")\n";
content += TAB + "public Mono<Integer> update" + realName + "ById(@RequestBody " + realName + "Entity entity){\n";
content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.update"
+ realName + "ById(entity);\n";
content += TAB + "}\n";
content += "\n";
content += "\n";
content += "\n";
content += TAB + "@PostMapping(\"/updateCover\")\n";
content += TAB + "public Mono<Integer> updateCover" + realName + "ById(@RequestBody " + realName + "Entity entity){\n";
content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.updateCover"
+ realName + "ById(entity);\n";
content += TAB + "}\n";
content += "\n";
content += "\n";
content += "\n";
content += TAB + "@PostMapping(\"/delete\")\n";
content += TAB + "public Mono<Integer> delete" + tableBean.getSpaceName() + "ById(@RequestParam Long id){\n";
content += TAB + TAB + "return " + MysqlUtil.getFirstToLower(realName) + "Service.delete"
+ realName + "ById(id);\n";
content += TAB + "}\n";
content += "\n";
content += "\n";
content += "\n";
content += "}" + "\n";
FileOutputStream fos = new FileOutputStream(fileName);
Writer out = new OutputStreamWriter(fos, "UTF-8");
out.write(content);
out.close();
fos.close();
System.out.println("===" + realName + "Service.java" + "生成");
// 打开一个写文件器构造函数中的第二个参数true表示以追加形式写文件
// FileWriter writer = new FileWriter(fileName, false);
// writer.write(content);
// writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -129,7 +129,7 @@ public class MysqlUtilTable2Service {
content += "\n";
content += "\n";
content += TAB + "public Mono<Integer> insert" + realName + "(" + realName + "Entity entity){\n";
//content += TAB + TAB + "entity.setId(null);\n";让他报错抛异常
content += TAB + TAB + "entity.setId(null);\n";
content += TAB + TAB + "entity.setCreateTime(null);\n";
content += TAB + TAB + "entity.setModifyTime(null);\n";
content += TAB + TAB + "return insert(entity);\n";