From 6c41ec2cb216ea4c52bd54d65016f26e4bca00b0 Mon Sep 17 00:00:00 2001 From: wulin Date: Wed, 20 Sep 2023 13:49:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A2=BC=E7=94=9F=E6=88=90=E5=99=A8?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0controller=E7=9A=84=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/device/DeviceController.java | 14 +- .../customer/http/api/mysql/MysqlMain.java | 7 +- .../api/mysql/MysqlUtilTable2Contoller.java | 127 ++++++++++++++++++ .../api/mysql/MysqlUtilTable2Service.java | 2 +- 4 files changed, 141 insertions(+), 9 deletions(-) diff --git a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java index 4040e56..64fa753 100644 --- a/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java +++ b/iot-modules/iot-box-user-api/src/main/java/com/qiuguo/iot/user/api/controller/device/DeviceController.java @@ -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 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){ //超时 diff --git a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlMain.java b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlMain.java index 7430625..5fa9428 100644 --- a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlMain.java +++ b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlMain.java @@ -82,7 +82,7 @@ public class MysqlMain { } List list = new ArrayList<>(); - list.add(new TablesBean("user_home")); + list.add(new TablesBean("device_info")); list.add(new TablesBean("user_room")); List list2 = new ArrayList(); @@ -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)); } diff --git a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Contoller.java b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Contoller.java index e6352be..008a4a3 100644 --- a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Contoller.java +++ b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Contoller.java @@ -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 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 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 += "*

\n"; + content += "* " + tableBean.getComment().replace("表", "") + "Controller类\n"; + content += "*

\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> 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 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 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 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 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(); + } + } } diff --git a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Service.java b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Service.java index 08c49b1..0bbeb59 100644 --- a/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Service.java +++ b/iot-modules/iot-customer-http-api/src/test/java/com/qiuguo/iot/customer/http/api/mysql/MysqlUtilTable2Service.java @@ -129,7 +129,7 @@ public class MysqlUtilTable2Service { content += "\n"; content += "\n"; content += TAB + "public Mono 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";