diff --git a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/pojo/Gate.java b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/pojo/Gate.java new file mode 100644 index 0000000..1face25 --- /dev/null +++ b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/pojo/Gate.java @@ -0,0 +1,16 @@ +package com.cxyxiaomo.epp.common.pojo; + +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +import java.io.Serializable; + +@Data +@NoArgsConstructor +@Accessors(chain = true) // 链式写法 +public class Gate implements Serializable { + private Long id; + private String name; + private Boolean open; +} diff --git a/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/GateVO.java b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/GateVO.java new file mode 100644 index 0000000..10b15ab --- /dev/null +++ b/backend/microservice-common/src/main/java/com/cxyxiaomo/epp/common/vo/GateVO.java @@ -0,0 +1,65 @@ +package com.cxyxiaomo.epp.common.vo; + +import com.cxyxiaomo.epp.common.pojo.Gate; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; +import org.springframework.beans.BeanUtils; + +import java.io.Serializable; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +// 数据库关系映射 + +@Data +@NoArgsConstructor +@Accessors(chain = true) // 链式写法 +// 微服务必须要实现Serializable +public class GateVO implements Serializable { + + private String id; + private String name; + private Boolean open; + + public static GateVO convertFrom(Gate gate) { + if (gate == null) { + return null; + } + GateVO gateVO = new GateVO(); + BeanUtils.copyProperties(gate, gateVO); + if (Objects.nonNull(gate.getId())) { + gateVO.setId(String.valueOf(gate.getId())); + } + return gateVO; + } + + public static List convertFrom(List gateList) { + if (gateList == null) { + return null; + } + List gateVOList = gateList.stream() + .map(GateVO::convertFrom).collect(Collectors.toList()); + return gateVOList; + } + + public static Gate convertTo(GateVO gateVO) { + if (gateVO == null) { + return null; + } + Gate gate = new Gate(); + BeanUtils.copyProperties(gateVO, gate); + try { + if (Objects.nonNull(gateVO.getId())) { + Long gateId = Long.valueOf(gateVO.getId()); + gate.setId(gateId); + } else { + gate.setId(null); + } + } catch (Exception e) { + gate.setId(null); + } + return gate; + } +} diff --git a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/GateController.java b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/GateController.java new file mode 100644 index 0000000..cdc7e46 --- /dev/null +++ b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/controller/GateController.java @@ -0,0 +1,194 @@ +package com.cxyxiaomo.epp.access.controller; + +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.cxyxiaomo.epp.PageTable.enums.AddType; +import com.cxyxiaomo.epp.PageTable.enums.EditType; +import com.cxyxiaomo.epp.PageTable.enums.FieldType; +import com.cxyxiaomo.epp.PageTable.enums.SearchType; +import com.cxyxiaomo.epp.PageTable.query.PageQuery; +import com.cxyxiaomo.epp.PageTable.utils.FieldBuilder; +import com.cxyxiaomo.epp.PageTable.utils.FieldMapperBuilder; +import com.cxyxiaomo.epp.PageTable.utils.FieldRuleBuilder; +import com.cxyxiaomo.epp.PageTable.utils.FieldRuleListBuilder; +import com.cxyxiaomo.epp.access.service.GateService; +import com.cxyxiaomo.epp.common.pojo.Gate; +import com.cxyxiaomo.epp.common.response.Res; +import com.cxyxiaomo.epp.common.vo.GateVO; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; + +@RestController +@RequestMapping("/access/gate") +public class GateController { + + @Resource + GateService gateService; + + /** + * 获取大门列表(门禁端) + * + * @return + */ + @GetMapping("/guard-client/getGateList") + @ResponseBody + public Res getAllGateList() { + GateVO gateVO = new GateVO(); + List gateList = gateService.getGateList(gateVO); + List gateVOList = GateVO.convertFrom(gateList); + return Res.success(gateVOList); + } + + /** + * 获取大门列表 + * + * @return + */ + @GetMapping("/manage/getGateList") + @ResponseBody + public Res getGateList(PageQuery pageQuery, GateVO gateVO) { + // 查询分页数据 + PageHelper.startPage(pageQuery.getPageIndex(), pageQuery.getPageSize()); + List gateList = gateService.getGateList(gateVO); + PageInfo gatePageInfo = new PageInfo<>(gateList); + List list = gatePageInfo.getList(); + List voList = GateVO.convertFrom(list); + + // id列 字段名(区分大小写;以VO中的变量名为准) + // 新增、修改弹窗时,使用该列作为主键列进行操作 + String idFieldName = "id"; + + // 当前管理页面 + String pageName = "大门管理"; + + // 指定前端表格显示列 + JSONArray columns = FieldBuilder.create() + .add("name", "name", "大门名称", "", + FieldType.TEXT, SearchType.INPUT, AddType.INPUT, EditType.INPUT, + FieldBuilder.SEARCH_PLACEHOLDER_SAME_AS_FIELDNAME, + "大门名称", FieldBuilder.EDIT_PLACEHOLDER_SAME_AS_ADD_PLACEHOLDER, + FieldRuleListBuilder.create() + .add(FieldRuleBuilder.create("大门名称").required()) + .add(FieldRuleBuilder.create("大门名称").minMax(2, 15)), + "DPD @cword(2, 15)" + ) + .add("open", "open", "状态", true, + FieldType.TEXT, SearchType.SELECT, AddType.SELECT, EditType.SELECT, + FieldBuilder.SEARCH_PLACEHOLDER_SAME_AS_FIELDNAME, + "状态", FieldBuilder.EDIT_PLACEHOLDER_SAME_AS_ADD_PLACEHOLDER, + FieldRuleListBuilder.create() + .add(FieldRuleBuilder.create("状态").required()), + "DPD @pick(['true', 'false'])" + ) + .build(); + + // 指定需要翻译的字段 + HashMap stateMap = new HashMap<>(2); + stateMap.put(true, "允许通行"); + stateMap.put(false, "禁止通行"); + // build + JSONArray fieldMapper = FieldMapperBuilder.create() + .add("open", "open", stateMap) + .build(); + + // 拼装返回结果 + JSONObject map = new JSONObject(6); + map.put("total", gatePageInfo.getTotal()); + map.put("list", voList); + map.put("columns", columns); + map.put("fieldMapper", fieldMapper); + map.put("idFieldName", idFieldName); + map.put("pageName", pageName); + + // 返回结果 + return Res.success(map); + } + + /** + * 新增 / 编辑大门 + * + * @return + */ + @PostMapping("/manage/editGate") + @ResponseBody + public Res editGate(@ModelAttribute GateVO gateVO) throws Exception { + Gate gate = GateVO.convertTo(gateVO); + + // 先查询大门是否存在 + Gate existGate = gateService.getGateById(gate.getId()); + + if (gate.getId() == null || gate.getId() < 1) { + // 新增大门 + if (existGate != null) { + return Res.error("大门已存在,操作失败"); + } + + if (gate.getName() == null || "".equals(gate.getName())) { + return Res.error("大门名称不能为空"); + } + gate.setId(null); + gateService.addGate(gate); + } else { + // 修改大门 + if (existGate == null) { + return Res.error("大门不存在,操作失败"); + } + + gateService.updateGate(gate); + } + return Res.success(true); + } + + /** + * 删除大门 + * + * @param id + * @return + */ + @PostMapping("/manage/deleteGate") + @ResponseBody + public Res deleteGate(Long id) { + if (id == null || id <= 0) { + return Res.error("大门不存在,删除失败"); + } + // 先查询大门是否存在 + Gate existGate = gateService.getGateById(id); + if (existGate == null) { + return Res.error("大门不存在,删除失败"); + } + boolean b = gateService.deleteGate(existGate.getId()); + return Res.success(b); + } + + /** + * 导出大门列表 + * + * @return + */ + @GetMapping("/manage/exportGateList") + @ResponseBody + public Res exportGateList(GateVO gateVO) { + List gateList = gateService.getGateList(gateVO); + List gateVOList = GateVO.convertFrom(gateList); + + // 当前时间 + Date now = Calendar.getInstance().getTime(); + SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss"); + String dateTime = format.format(now); + + HashMap map = new HashMap<>(); + map.put("list", gateVOList); + map.put("sheetName", "社区大门表-" + System.currentTimeMillis()); + map.put("fileName", "社区大门表_导出时间_" + dateTime); + + return Res.success(map); + } +} diff --git a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/dao/GateDao.java b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/dao/GateDao.java new file mode 100644 index 0000000..31efe1e --- /dev/null +++ b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/dao/GateDao.java @@ -0,0 +1,23 @@ +package com.cxyxiaomo.epp.access.dao; + +import com.cxyxiaomo.epp.common.pojo.Gate; +import com.cxyxiaomo.epp.common.vo.GateVO; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Mapper +@Repository +public interface GateDao { + + public boolean addGate(Gate gate); + + public boolean updateGate(Gate gate); + + public List getGateList(GateVO gateVO); + + public Gate getGateById(Long gateId); + + public boolean deleteGateById(Long gateId); +} diff --git a/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/service/GateService.java b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/service/GateService.java new file mode 100644 index 0000000..fbfed93 --- /dev/null +++ b/backend/microservice-provider-access-8002/src/main/java/com/cxyxiaomo/epp/access/service/GateService.java @@ -0,0 +1,42 @@ +package com.cxyxiaomo.epp.access.service; + +import com.cxyxiaomo.epp.access.dao.GateDao; +import com.cxyxiaomo.epp.common.pojo.Gate; +import com.cxyxiaomo.epp.common.utils.SnowflakeManager; +import com.cxyxiaomo.epp.common.vo.GateVO; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +@Service +public class GateService { + + @Resource + GateDao gateDao; + + + public Gate getGateById(Long id) { + return gateDao.getGateById(id); + } + + public List getGateList(GateVO gateVO) { + return gateDao.getGateList(gateVO); + } + + public boolean addGate(Gate gate) throws Exception { + // 创建订单编号 + SnowflakeManager snowflakeManager = new SnowflakeManager(1L, 1L); + long orderId = snowflakeManager.nextValue(); + gate.setId(orderId); + return gateDao.addGate(gate); + } + + public boolean updateGate(Gate gate) { + return gateDao.updateGate(gate); + } + + public boolean deleteGate(Long gateId) { + return gateDao.deleteGateById(gateId); + } +} diff --git a/backend/microservice-provider-access-8002/src/main/resources/mybatis/mapper/GateDao.xml b/backend/microservice-provider-access-8002/src/main/resources/mybatis/mapper/GateDao.xml new file mode 100644 index 0000000..66087b2 --- /dev/null +++ b/backend/microservice-provider-access-8002/src/main/resources/mybatis/mapper/GateDao.xml @@ -0,0 +1,43 @@ + + + + + + INSERT INTO gate (id, name, open) + VALUES (#{id}, #{name}, #{open}) + + + + UPDATE gate + + name = #{name}, + open = #{open}, + + WHERE id = #{id} + + + + + + + + DELETE FROM gate WHERE id = #{id} + + diff --git a/database/epp.sql b/database/epp.sql index 8e14cd0..ae825b9 100644 --- a/database/epp.sql +++ b/database/epp.sql @@ -17,6 +17,23 @@ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; +-- ---------------------------- +-- Table structure for access_log +-- ---------------------------- +DROP TABLE IF EXISTS `access_log`; +CREATE TABLE `access_log` ( + `id` bigint(20) NOT NULL COMMENT '雪花id', + `time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '进出时间', + `user_id` int(11) NOT NULL COMMENT '用户id', + `user_real_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '用户真实姓名', + `gate_id` bigint(20) NOT NULL COMMENT '大门id', + `type` enum('IN','OUT') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '类型(进门 OR 出门)' +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '人员进出记录表' ROW_FORMAT = Dynamic; + +-- ---------------------------- +-- Records of access_log +-- ---------------------------- + -- ---------------------------- -- Table structure for apply1 -- ---------------------------- @@ -48,21 +65,6 @@ INSERT INTO `apply1` VALUES (10, 12, '1', '飞机', '1', '2022-06-15 10:53:00', INSERT INTO `apply1` VALUES (11, 2, '1', '飞机', '1', '2022-10-23 16:17:00', '2022-10-23 18:17:00', 1, ''); INSERT INTO `apply1` VALUES (12, 13, '1', '飞机', '1', '2022-10-26 19:25:00', '2022-10-26 21:25:00', 1, ''); --- ---------------------------- --- Table structure for community_gate1 --- ---------------------------- -DROP TABLE IF EXISTS `community_gate1`; -CREATE TABLE `community_gate1` ( - `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '雪花id', - `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '大门显示名称', - `open` tinyint(1) NOT NULL DEFAULT 1 COMMENT '大门是否开放 1为开放 2为关闭', - PRIMARY KEY (`id`) USING BTREE -) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '社区大门' ROW_FORMAT = DYNAMIC; - --- ---------------------------- --- Records of community_gate1 --- ---------------------------- - -- ---------------------------- -- Table structure for count1 -- ---------------------------- @@ -102,6 +104,21 @@ INSERT INTO `feedback1` VALUES (3, '22', '33', '2022-02-14 10:20:29', 1, 'no'); INSERT INTO `feedback1` VALUES (4, '``', '11', '2022-02-14 10:22:12', 1, '111'); INSERT INTO `feedback1` VALUES (5, '123', '123456', '2022-03-28 19:34:04', 2, NULL); +-- ---------------------------- +-- Table structure for gate +-- ---------------------------- +DROP TABLE IF EXISTS `gate`; +CREATE TABLE `gate` ( + `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '雪花id', + `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '大门显示名称', + `open` tinyint(1) NOT NULL DEFAULT 1 COMMENT '大门是否开放 1为开放 2为关闭', + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '社区大门' ROW_FORMAT = DYNAMIC; + +-- ---------------------------- +-- Records of gate +-- ---------------------------- + -- ---------------------------- -- Table structure for goods -- ---------------------------- diff --git a/docs/ChatGPT/生成Mybatis xml SQL语句2.md b/docs/ChatGPT/生成Mybatis xml SQL语句2.md new file mode 100644 index 0000000..878397d --- /dev/null +++ b/docs/ChatGPT/生成Mybatis xml SQL语句2.md @@ -0,0 +1,23 @@ +CREATE TABLE `gate` ( + `id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '雪花id', + `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '大门显示名称', + `open` tinyint(1) NOT NULL DEFAULT 1 COMMENT '大门是否开放 1为开放 2为关闭', + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '社区大门' ROW_FORMAT = DYNAMIC; + +创建该表的 add update getList deleteById 的 Mybatils xml片段 +(例如: