日常开发-20250917
问题描述
两个场景,和大家分享一下。
场景1
嗨,已经习惯了,作为一个后端java开发,写完一片代码,让ai看看(最喜欢豆包,deepseek有点抽象和复杂,kimi有点简单,还是豆包更懂程序员)。话不多说,看。
提问:
分析一下这个方法 并优化: public JSONObject getGiveUpInfo(String orderNo) {JSONObject ret = new JSONObject();/*** 是否改装*/ret.put("refitFlag", "0");/*** 是否放弃承保。否*/ret.put("giveUpFlag", "0");InsurenceAuditRuleLog auditRuleLog = insurenceAuditRuleLogDao.getOneOrderNo(orderNo);if(Objects.nonNull(auditRuleLog)){List<String> refitDesc = new ArrayList<>();if(auditRuleLog.getSgGzFlag()){refitDesc.add("事故改装");}if(auditRuleLog.getPsGzFlag()){refitDesc.add("泡水改装");}if(auditRuleLog.getHsGzFlag()){refitDesc.add("火烧改装");}if(Check.notNull(refitDesc)){/*** 设置为改装并设置文本描述*/ret.put("refitFlag", "1");ret.put("refitDesc", String.join(";", refitDesc));}}DetectionApplyGiveUpInfo giveUpInfo = detectionApplyGiveUpInfoDao.getOneOrderNo(orderNo);String giveUpDesc = "";if(Objects.nonNull(giveUpInfo)){/*** 状态。10:待审核 20:通过 30:驳回* 优先级1,如果驳回,直接返回。不承保*/Integer status = giveUpInfo.getStatus();if(Objects.equals(30, status)){return ret;}else {/*** 用户确认时间* 优先级2,如果存在时间,直接返回,是,放弃承保*/Date appAuditTime = giveUpInfo.getAppAuditTime();if(Objects.nonNull(appAuditTime)){giveUpDesc = "用户已确认";}else{/*** 审核时间* 优先级3,如果存在审核时间,直接返回,是,放弃承保*/Date auditTime = giveUpInfo.getAuditTime();if(Objects.nonNull(auditTime)){giveUpDesc = "待用户确认";}else{giveUpDesc = "待经理审核";}}}}/*** 如果命中 a 条件,则直接返回。否则继续走 b 条件*/if(Check.notNull(giveUpDesc)){ret.put("giveUpFlag", "1");ret.put("giveUpDesc", giveUpDesc);return ret;}/*** b 条件 。1和3是*/DetectionOperateLog logInfo = detectionOperateLogDao.getOneOrderNo(orderNo);if(Objects.isNull(logInfo)){return ret;}Integer operatorType = logInfo.getOperatType();if(Objects.equals(1, operatorType)){ret.put("giveUpFlag", "1");ret.put("giveUpDesc", "超年限确认");return ret;}if(Objects.equals(3, operatorType)){ret.put("giveUpFlag", "1");ret.put("giveUpDesc", "后台放弃保障");return ret;}return ret;}
回答:
// 定义常量接口,集中管理魔法值
interface Constants {String FLAG_NO = "0";String FLAG_YES = "1";Integer STATUS_REJECTED = 30;Integer OPERATOR_TYPE_OVERDUE = 1;Integer OPERATOR_TYPE_BACKGROUND_GIVE_UP = 3;
}public JSONObject getGiveUpInfo(String orderNo) {JSONObject result = new JSONObject();// 初始化默认值result.put("refitFlag", Constants.FLAG_NO);result.put("giveUpFlag", Constants.FLAG_NO);// 处理改装信息handleRefitInfo(orderNo, result);// 处理放弃承保信息String giveUpDesc = handleGiveUpInfo(orderNo);if (StringUtils.isNotBlank(giveUpDesc)) {result.put("giveUpFlag", Constants.FLAG_YES);result.put("giveUpDesc", giveUpDesc);return result;}// 处理操作日志信息String logDesc = handleOperationLog(orderNo);if (StringUtils.isNotBlank(logDesc)) {result.put("giveUpFlag", Constants.FLAG_YES);result.put("giveUpDesc", logDesc);}return result;
}/*** 处理改装信息*/
private void handleRefitInfo(String orderNo, JSONObject result) {InsuranceAuditRuleLog auditRuleLog = insuranceAuditRuleLogDao.getOneOrderNo(orderNo);if (Objects.isNull(auditRuleLog)) {return;}List<String> refitDescriptions = new ArrayList<>();if (auditRuleLog.getSgGzFlag()) {refitDescriptions.add("事故改装");}if (auditRuleLog.getPsGzFlag()) {refitDescriptions.add("泡水改装");}if (auditRuleLog.getHsGzFlag()) {refitDescriptions.add("火烧改装");}if (!refitDescriptions.isEmpty()) {result.put("refitFlag", Constants.FLAG_YES);result.put("refitDesc", String.join(";", refitDescriptions));}
}/*** 处理放弃承保信息*/
private String handleGiveUpInfo(String orderNo) {DetectionApplyGiveUpInfo giveUpInfo = detectionApplyGiveUpInfoDao.getOneOrderNo(orderNo);if (Objects.isNull(giveUpInfo)) {return null;}// 如果状态为驳回,直接返回空(使用默认值)if (Objects.equals(Constants.STATUS_REJECTED, giveUpInfo.getStatus())) {return null;}// 根据不同时间判断描述信息if (Objects.nonNull(giveUpInfo.getAppAuditTime())) {return "用户已确认";} else if (Objects.nonNull(giveUpInfo.getAuditTime())) {return "待用户确认";} else {return "待经理审核";}
}/*** 处理操作日志信息*/
private String handleOperationLog(String orderNo) {DetectionOperateLog logInfo = detectionOperateLogDao.getOneOrderNo(orderNo);if (Objects.isNull(logInfo)) {return null;}Integer operatorType = logInfo.getOperatType();if (Objects.equals(Constants.OPERATOR_TYPE_OVERDUE, operatorType)) {return "超年限确认";} else if (Objects.equals(Constants.OPERATOR_TYPE_BACKGROUND_GIVE_UP, operatorType)) {return "后台放弃保障";}return null;
}
直接copy,不需要改。检查一遍逻辑,符合预期,完美!
场景2
页面做了一个功能,产品要求发一个邮件。页面长这样,自己写也可以实现,肯定费时费力还不美观。AI一下试试吧。干
提问:
java 给一个集合 拼一个table表格,作为邮件内容发送:{"msg": "操作成功","code": 200,"data": [{"ningDe": "电池品牌:非宁德时代;电池信息-batteryManufacturer:非宁德时代;电池信息-packManufacturer:非宁德时代","label": "电池品牌","boShi": "-"},{"ningDe": "online.soh:1","label": "SOH","boShi": "-"},{"ningDe": "mileage:-;mileageSixMonth:-;mileageTwelveMonth:-;mileageTempRange1:-;mileageTempRange2:-;mileageTempRange3:-","label": "预估续航","boShi": "-"},{"ningDe": "电池内阻评分-dcrConsistenceScore:4.5;电池容量评分-sohScore:0.00;电压一致性评分-socConsistenceScore:4.5;温度一致性评分-tempConsistenceScore:4.5;充电习惯评分-chrgScore:-","label": "电池评分","boShi": "-"},{"ningDe": "-","label": "电池能量","boShi": "17.6"},{"ningDe": "-","label": "官标续航","boShi": "125.00公里"},{"ningDe": null,"label": "额定电压","boShi": "310.00V"},{"ningDe": null,"label": "额定容量","boShi": "123.00Ah"},{"ningDe": "-","label": "表显里程","boShi": "100.00km"},{"ningDe": "-","label": "制造年月","boShi": "2025-08"},{"ningDe": "-","label": "整车型号","boShi": "12"},{"ningDe": "-","label": "燃料类型","boShi": "纯电动"},{"ningDe": "-","label": "设备标识","boShi": ""},{"ningDe": "是否维修:否;是否拆包:否","label": "GSS数据","boShi": "-"}]
}
回答:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;public class EmailTableSender {// 邮件服务器配置private static final String SMTP_HOST = "smtp.example.com"; // 替换为你的SMTP服务器private static final String SMTP_PORT = "587"; // 通常是587或25private static final String USERNAME = "your-email@example.com"; // 你的邮箱private static final String PASSWORD = "your-password"; // 你的邮箱密码或授权码public static void main(String[] args) {// 示例JSON数据String jsonData = "{\n" +" \"msg\": \"操作成功\",\n" +" \"code\": 200,\n" +" \"data\": [\n" +" {\n" +" \"ningDe\": \"电池品牌:非宁德时代;电池信息-batteryManufacturer:非宁德时代;电池信息-packManufacturer:非宁德时代\",\n" +" \"label\": \"电池品牌\",\n" +" \"boShi\": \"-\"\n" +" },\n" +" {\n" +" \"ningDe\": \"online.soh:1\",\n" +" \"label\": \"SOH\",\n" +" \"boShi\": \"-\"\n" +" },\n" +" {\n" +" \"ningDe\": \"mileage:-;mileageSixMonth:-;mileageTwelveMonth:-;mileageTempRange1:-;mileageTempRange2:-;mileageTempRange3:-\",\n" +" \"label\": \"预估续航\",\n" +" \"boShi\": \"-\"\n" +" },\n" +" {\n" +" \"ningDe\": \"电池内阻评分-dcrConsistenceScore:4.5;电池容量评分-sohScore:0.00;电压一致性评分-socConsistenceScore:4.5;温度一致性评分-tempConsistenceScore:4.5;充电习惯评分-chrgScore:-\",\n" +" \"label\": \"电池评分\",\n" +" \"boShi\": \"-\"\n" +" },\n" +" {\n" +" \"ningDe\": \"-\",\n" +" \"label\": \"电池能量\",\n" +" \"boShi\": \"17.6\"\n" +" },\n" +" {\n" +" \"ningDe\": \"-\",\n" +" \"label\": \"官标续航\",\n" +" \"boShi\": \"125.00公里\"\n" +" },\n" +" {\n" +" \"ningDe\": null,\n" +" \"label\": \"额定电压\",\n" +" \"boShi\": \"310.00V\"\n" +" },\n" +" {\n" +" \"ningDe\": null,\n" +" \"label\": \"额定容量\",\n" +" \"boShi\": \"123.00Ah\"\n" +" },\n" +" {\n" +" \"ningDe\": \"-\",\n" +" \"label\": \"表显里程\",\n" +" \"boShi\": \"100.00km\"\n" +" },\n" +" {\n" +" \"ningDe\": \"-\",\n" +" \"label\": \"制造年月\",\n" +" \"boShi\": \"2025-08\"\n" +" },\n" +" {\n" +" \"ningDe\": \"-\",\n" +" \"label\": \"整车型号\",\n" +" \"boShi\": \"12\"\n" +" },\n" +" {\n" +" \"ningDe\": \"-\",\n" +" \"label\": \"燃料类型\",\n" +" \"boShi\": \"纯电动\"\n" +" },\n" +" {\n" +" \"ningDe\": \"-\",\n" +" \"label\": \"设备标识\",\n" +" \"boShi\": \"\"\n" +" },\n" +" {\n" +" \"ningDe\": \"是否维修:否;是否拆包:否\",\n" +" \"label\": \"GSS数据\",\n" +" \"boShi\": \"-\"\n" +" }\n" +" ]\n" +"}";try {// 解析JSON数据ObjectMapper objectMapper = new ObjectMapper();JsonNode rootNode = objectMapper.readTree(jsonData);JsonNode dataNode = rootNode.get("data");// 生成HTML表格String htmlContent = generateHtmlTable(dataNode);// 发送邮件sendEmail("收件人邮箱地址", "电池数据报表", htmlContent);System.out.println("邮件发送成功");} catch (Exception e) {e.printStackTrace();System.out.println("邮件发送失败: " + e.getMessage());}}/*** 生成HTML表格*/private static String generateHtmlTable(JsonNode dataNode) {if (dataNode == null || !dataNode.isArray()) {return "<p>没有数据可显示</p>";}StringBuilder html = new StringBuilder();// HTML头部和样式html.append("<!DOCTYPE html>\n");html.append("<html>\n");html.append("<head>\n");html.append("<meta charset=\"UTF-8\">\n");html.append("<style>\n");html.append("table {border-collapse: collapse; width: 100%; margin: 20px 0;}\n");html.append("th, td {border: 1px solid #ddd; padding: 12px; text-align: left;}\n");html.append("th {background-color: #f2f2f2; font-weight: bold;}\n");html.append("tr:nth-child(even) {background-color: #f9f9f9;}\n");html.append("tr:hover {background-color: #f1f1f1;}\n");html.append(".header {color: #2c3e50; font-size: 18px; margin-bottom: 10px;}\n");html.append("</style>\n");html.append("</head>\n");html.append("<body>\n");html.append("<h2 class=\"header\">电池数据对比表</h2>\n");// 表格开始html.append("<table>\n");// 表头html.append("<tr>\n");html.append("<th>项目</th>\n");html.append("<th>宁德时代</th>\n");html.append("<th>博世</th>\n");html.append("</tr>\n");// 表格内容for (JsonNode node : dataNode) {html.append("<tr>\n");// 标签列String label = node.get("label") != null ? node.get("label").asText() : "";html.append("<td>").append(escapeHtml(label)).append("</td>\n");// 宁德时代列String ningDe = "";if (node.get("ningDe") != null && !node.get("ningDe").isNull()) {ningDe = node.get("ningDe").asText();// 将分号分隔的内容换行显示ningDe = ningDe.replace(";", "<br>");}html.append("<td>").append(escapeHtml(ningDe)).append("</td>\n");// 博世列String boShi = node.get("boShi") != null ? node.get("boShi").asText() : "";html.append("<td>").append(escapeHtml(boShi)).append("</td>\n");html.append("</tr>\n");}// 表格结束html.append("</table>\n");html.append("</body>\n");html.append("</html>");return html.toString();}/*** 发送HTML格式的邮件*/private static void sendEmail(String to, String subject, String htmlContent) throws MessagingException {// 配置邮件服务器Properties props = new Properties();props.put("mail.smtp.host", SMTP_HOST);props.put("mail.smtp.port", SMTP_PORT);props.put("mail.smtp.auth", "true");props.put("mail.smtp.starttls.enable", "true");// 创建会话Session session = Session.getInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(USERNAME, PASSWORD);}});// 创建邮件消息Message message = new MimeMessage(session);message.setFrom(new InternetAddress(USERNAME));message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));message.setSubject(subject);// 设置邮件内容为HTMLmessage.setContent(htmlContent, "text/html; charset=UTF-8");// 发送邮件Transport.send(message);}/*** 转义HTML特殊字符,防止XSS和格式问题*/private static String escapeHtml(String input) {if (input == null || input.isEmpty()) {return "";}return input.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """).replace("'", "'");}
}
继续提问:table 第一列 宽度 140px// 新增:设置第一列宽度为140px
html.append("td:first-child, th:first-child {width: 140px;}\n");
效果:
产品眼前一亮,直夸我。
问题总结
致敬这个时代,致敬AI。AI不远,AI在身边。