编写故障日志的代码

冷调 2024-09-20 12:29:11 +08:00 committed by 张毅大神
parent 17623aad80
commit 4f3db278a5
14 changed files with 578 additions and 19 deletions

View File

@ -0,0 +1,93 @@
package com.muyu.breakdown.DTO;
import com.muyu.breakdown.domain.Messages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.*;
import java.util.*;
/**
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-18-15:00
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Component
public class MessageDTO {
private static final String DB_URL = "jdbc:mysql://106.54.193.225:3306/one";
private static final String USER = "root";
private static final String PASSWORD = "bawei2112A";
// 2. 建立数据库连接
Connection connection;
// 构造函数,初始化数据库连接
// 保存消息到数据库
public void saveMessage(Messages message) {
String sql = "INSERT INTO sys_messages (sender_id, receiver_id, content) VALUES (?, ?, ?)";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
try {
connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, message.getSenderId());
preparedStatement.setInt(2, message.getReceiverId());
preparedStatement.setString(3, message.getContent());
// 执行添加操作
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
// 获取所有消息
public List<Messages> getAllMessages(int receiverId){
String sql = "SELECT * FROM sys_messages WHERE receiver_id = ?";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
List<Messages> messages = new ArrayList<>();
try {
connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
} catch (SQLException e) {
throw new RuntimeException(e);
}
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, receiverId);
// 执行查询操作
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
Messages message = new Messages(rs.getInt("sender_id"), receiverId, rs.getString("content"));
// 添加到消息列表
messages.add(message);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
// 返回消息列表
return messages;
}
}

View File

@ -46,11 +46,6 @@ public class BreakDown extends BaseEntity {
*/ */
@Excel(name = "故障类型") @Excel(name = "故障类型")
private String faultType; private String faultType;
/**
* VIN
*/
@Excel(name = "车辆VIN")
private String carVin;
/** /**
* *
*/ */
@ -76,14 +71,4 @@ public class BreakDown extends BaseEntity {
*/ */
@Excel(name = "报警状态") @Excel(name = "报警状态")
private String faultStatus; private String faultStatus;
/**
*
*/
@Excel(name = "故障描述信息")
private String faultDesc;
/**
* (1. 2. 3. 4.)
*/
@Excel(name = "启用状态")
private String state;
} }

View File

@ -0,0 +1,67 @@
package com.muyu.breakdown.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.muyu.common.core.annotation.Excel;
import com.muyu.common.core.web.domain.BaseEntity;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
/**
* fault_log
*
* @author muyu
* @date 2024-09-20
*/
@EqualsAndHashCode(callSuper = true)
@Data
@Setter
@Getter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@TableName("fault_log")
public class FaultLog extends BaseEntity{
private static final long serialVersionUID = 1L;
/** id */
@TableId( type = IdType.AUTO)
private Long id;
/** 故障码 */
@Excel(name = "故障码")
private String faultCode;
/** 车辆VIN */
@Excel(name = "车辆VIN")
private String carVin;
/** 开始报警时间 */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "开始报警时间", width = 30, dateFormat = "yyyy-MM-dd")
private Date startTime;
/** 结束报警时间 */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "结束报警时间", width = 30, dateFormat = "yyyy-MM-dd")
private Date endTime;
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("faultCode", getFaultCode())
.append("carVin", getCarVin())
.append("startTime", getStartTime())
.append("endTime", getEndTime())
.toString();
}
}

View File

@ -0,0 +1,52 @@
package com.muyu.breakdown.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.muyu.common.core.annotation.Excel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-18-14:51
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("sys_messages")
public class Messages {
@TableId(value = "id", type = IdType.AUTO)
@Excel(name = "消息ID")
private Long id;
@Excel(name = "发送者ID")
private Integer senderId;
@Excel(name = "接收者ID")
private Integer receiverId;
@Excel(name = "消息内容")
private String content;
@Excel(name = "发送时间")
private Date createTime;
@Excel(name = "消息状态")
private String status;
public Messages(int senderId, int receiverId, String content) {
this.senderId = senderId;
this.receiverId = receiverId;
this.content = content;
}
public Messages(Integer senderId, Integer receiverId, String content, Date createTime) {
this.senderId = senderId;
this.receiverId = receiverId;
this.content = content;
this.createTime = createTime;
}
}

View File

@ -0,0 +1,113 @@
package com.muyu.breakdown.controller;
import java.util.Arrays;
import java.util.List;
import com.muyu.breakdown.domain.FaultLog;
import com.muyu.breakdown.service.IFaultLogService;
import jakarta.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.security.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import com.muyu.common.core.web.page.TableDataInfo;
/**
* Controller
*
* @author muyu
* @date 2024-09-20
*/
@RestController
@RequestMapping("/log")
public class FaultLogController extends BaseController
{
@Resource
private IFaultLogService faultLogService;
/**
*
*/
@RequiresPermissions("platform:log:list")
@GetMapping("/list")
public Result<TableDataInfo<FaultLog>> list(FaultLog faultLog)
{
startPage();
List<FaultLog> list = faultLogService.selectFaultLogList(faultLog);
return getDataTable(list);
}
/**
*
*/
@RequiresPermissions("platform:log:export")
@PostMapping("/export")
public void export(HttpServletResponse response, FaultLog faultLog)
{
List<FaultLog> list = faultLogService.selectFaultLogList(faultLog);
ExcelUtil<FaultLog> util = new ExcelUtil<FaultLog>(FaultLog.class);
util.exportExcel(response, list, "故障日志数据");
}
/**
*
*/
@RequiresPermissions("platform:log:query")
@GetMapping(value = "/{id}")
public Result<List<FaultLog>> getInfo(@PathVariable("id") Long id)
{
return success(faultLogService.selectFaultLogById(id));
}
/**
*
*/
@RequiresPermissions("platform:log:add")
@PostMapping
public Result<Integer> add(
@Validated @RequestBody FaultLog faultLog)
{
if (faultLogService.checkIdUnique(faultLog)) {
return error("新增 故障日志 '" + faultLog + "'失败,故障日志已存在");
}
faultLog.setCreateBy(SecurityUtils.getUsername());
return toAjax(faultLogService.save(faultLog));
}
/**
*
*/
@RequiresPermissions("platform:log:edit")
@PutMapping
public Result<Integer> edit(
@Validated @RequestBody FaultLog faultLog)
{
if (!faultLogService.checkIdUnique(faultLog)) {
return error("修改 故障日志 '" + faultLog + "'失败,故障日志不存在");
}
faultLog.setUpdateBy(SecurityUtils.getUsername());
return toAjax(faultLogService.updateById(faultLog));
}
/**
*
*/
@RequiresPermissions("platform:log:remove")
@DeleteMapping("/{ids}")
public Result<Integer> remove(@PathVariable("ids") Long[] ids)
{
faultLogService.removeBatchByIds(Arrays.asList(ids));
return success();
}
}

View File

@ -0,0 +1,37 @@
package com.muyu.breakdown.controller;
import com.muyu.breakdown.domain.Messages;
import com.muyu.breakdown.service.BreakDownService;
import com.muyu.breakdown.service.StationMessageService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-18-12:01
* @ Version1.0
* @ Description
* @author Lenovo
*/
@RestController
@RequestMapping("/stationMessage")
public class StationMessageController extends BaseController {
@Autowired
private StationMessageService stationMessageService;
@PostMapping("/send")
public void sendMessage(@RequestBody Messages messages) {
stationMessageService.sendMessage(messages.getSenderId(), messages.getReceiverId(), messages.getContent());
}
@GetMapping("/{userId}")
public List<Messages> getMessages(@PathVariable("userId") int userId) {
return stationMessageService.getUserMessages(userId);
}
}

View File

@ -0,0 +1,17 @@
package com.muyu.breakdown.mapper;
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.breakdown.domain.FaultLog;
import org.apache.ibatis.annotations.Mapper;
/**
* Mapper
*
* @author muyu
* @date 2024-09-20
*/
@Mapper
public interface FaultLogMapper extends BaseMapper<FaultLog>{
}

View File

@ -0,0 +1,18 @@
package com.muyu.breakdown.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.yulichang.base.MPJBaseMapper;
import com.muyu.breakdown.domain.Messages;
import org.apache.ibatis.annotations.Mapper;
/**
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-18-14:57
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Mapper
public interface StationMessageMapper extends MPJBaseMapper<Messages> {
}

View File

@ -0,0 +1,37 @@
package com.muyu.breakdown.service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.breakdown.domain.FaultLog;
/**
* Service
*
* @author muyu
* @date 2024-09-20
*/
public interface IFaultLogService extends IService<FaultLog> {
/**
*
*
* @param id
* @return
*/
public FaultLog selectFaultLogById(Long id);
/**
*
*
* @param faultLog
* @return
*/
public List<FaultLog> selectFaultLogList(FaultLog faultLog);
/**
* id
* @param faultLog
* @return
*/
Boolean checkIdUnique(FaultLog faultLog);
}

View File

@ -0,0 +1,20 @@
package com.muyu.breakdown.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.breakdown.domain.Messages;
import java.util.List;
/**
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-18-14:56
* @ Version1.0
* @ Description
* @author Lenovo
*/
public interface StationMessageService extends IService<Messages> {
void sendMessage(Integer senderId, Integer receiverId, String content);
List<Messages> getUserMessages(int userId);
}

View File

@ -53,9 +53,6 @@ public class BreakDownServiceImpl extends ServiceImpl<BreakDownMapper, BreakDown
if (StringUtils.isNotEmpty(breakDown.getFaultType())){ if (StringUtils.isNotEmpty(breakDown.getFaultType())){
queryWrapper.eq(BreakDown::getFaultType, breakDown.getFaultType()); queryWrapper.eq(BreakDown::getFaultType, breakDown.getFaultType());
} }
if (StringUtils.isNotEmpty(breakDown.getCarVin())){
queryWrapper.eq(BreakDown::getCarVin, breakDown.getCarVin());
}
return this.list(queryWrapper); return this.list(queryWrapper);
} }

View File

@ -0,0 +1,78 @@
package com.muyu.breakdown.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.breakdown.domain.FaultLog;
import com.muyu.breakdown.mapper.FaultLogMapper;
import com.muyu.breakdown.service.IFaultLogService;
import com.muyu.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.List;
/**
* Service
*
* @author muyu
* @date 2024-09-20
*/
@Service
public class FaultLogServiceImpl
extends ServiceImpl<FaultLogMapper, FaultLog>
implements IFaultLogService {
/**
*
*
* @param id
* @return
*/
@Override
public FaultLog selectFaultLogById(Long id)
{
LambdaQueryWrapper<FaultLog> queryWrapper = new LambdaQueryWrapper<>();
Assert.notNull(id, "id不可为空");
queryWrapper.eq(FaultLog::getId, id);
return this.getOne(queryWrapper);
}
/**
*
*
* @param faultLog
* @return
*/
@Override
public List<FaultLog> selectFaultLogList(FaultLog faultLog)
{
LambdaQueryWrapper<FaultLog> queryWrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotEmpty(faultLog.getFaultCode())){
queryWrapper.eq(FaultLog::getFaultCode, faultLog.getFaultCode());
}
if (StringUtils.isNotEmpty(faultLog.getCarVin())){
queryWrapper.eq(FaultLog::getCarVin, faultLog.getCarVin());
}
if (faultLog.getStartTime()!=null){
queryWrapper.eq(FaultLog::getStartTime, faultLog.getStartTime());
}
if (faultLog.getEndTime()!=null){
queryWrapper.eq(FaultLog::getEndTime, faultLog.getEndTime());
}
return this.list(queryWrapper);
}
/**
*
* @param faultLog
* @return
*/
@Override
public Boolean checkIdUnique(FaultLog faultLog) {
LambdaQueryWrapper<FaultLog> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(FaultLog::getId, faultLog.getId());
return this.count(queryWrapper) > 0;
}
}

View File

@ -0,0 +1,45 @@
package com.muyu.breakdown.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.breakdown.DTO.MessageDTO;
import com.muyu.breakdown.domain.Messages;
import com.muyu.breakdown.mapper.StationMessageMapper;
import com.muyu.breakdown.service.StationMessageService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-09-18-14:56
* @ Version1.0
* @ Description
* @author Lenovo
*/
@Service
public class StationMessageServiceImpl
extends ServiceImpl<StationMessageMapper, Messages>
implements StationMessageService {
@Autowired
private MessageDTO messageDTO;
@Override
public void sendMessage(Integer senderId, Integer receiverId, String content) {
Messages message = new Messages(senderId, receiverId, content);
// 保存消息到数据库
messageDTO.saveMessage(message);
}
@Override
public List<Messages> getUserMessages(int userId) {
// 获取用户收到的消息
return messageDTO.getAllMessages(userId);
}
}

View File

@ -4,7 +4,7 @@ server:
# nacos线上地址 # nacos线上地址
nacos: nacos:
addr: 47.116.173.119:8848 addr: 106.54.193.225:8848
user-name: nacos user-name: nacos
password: nacos password: nacos
namespace: one namespace: one