|
|
@@ -17,35 +17,42 @@ public class MeterService {
|
|
17
|
17
|
private final JdbcTemplate jdbcTemplate;
|
|
18
|
18
|
|
|
19
|
19
|
/** 水表入库 */
|
|
20
|
|
- public void stockIn(String meterNo, String caliber, String meterType, String manufacturer, int quantity) {
|
|
|
20
|
+ @Transactional
|
|
|
21
|
+ public void stockIn(String meterNo, String caliber, String meterType, String manufacturer, int quantity, String operatorId, String operatorName) {
|
|
21
|
22
|
for (int i = 0; i < quantity; i++) {
|
|
22
|
23
|
String no = meterNo + "-" + (i + 1);
|
|
23
|
24
|
jdbcTemplate.update(
|
|
24
|
25
|
"INSERT INTO rev_meter (meter_no, caliber, meter_type, manufacturer, status) VALUES (?,?,?,?,?)",
|
|
25
|
26
|
no, caliber, meterType, manufacturer, "warehouse");
|
|
|
27
|
+
|
|
|
28
|
+ // 记录入库日志
|
|
|
29
|
+ jdbcTemplate.update(
|
|
|
30
|
+ "INSERT INTO rev_meter_log (meter_id, operation_type, operator_id, operator_name, remark) VALUES (?,?,?,?,?)",
|
|
|
31
|
+ jdbcTemplate.queryForObject("SELECT id FROM rev_meter WHERE meter_no = ?", Long.class, no),
|
|
|
32
|
+ "stock_in", operatorId, operatorName, "水表入库:" + meterNo + "-" + (i + 1));
|
|
26
|
33
|
}
|
|
27
|
34
|
log.info("Meter stock in: {} x{}", meterNo, quantity);
|
|
28
|
35
|
}
|
|
29
|
36
|
|
|
30
|
37
|
/** 水表出库安装 */
|
|
31
|
38
|
@Transactional
|
|
32
|
|
- public void install(Long meterId, Long customerId, String address, BigDecimal initialReading) {
|
|
|
39
|
+ public void install(Long meterId, Long customerId, String address, BigDecimal initialReading, String operatorId, String operatorName) {
|
|
33
|
40
|
jdbcTemplate.update(
|
|
34
|
41
|
"UPDATE rev_meter SET customer_id = ?, install_address = ?, initial_reading = ?, current_reading = ?, status = 'active', install_date = CURRENT_DATE WHERE id = ?",
|
|
35
|
42
|
customerId, address, initialReading, initialReading, meterId);
|
|
36
|
43
|
jdbcTemplate.update(
|
|
37
|
|
- "INSERT INTO rev_meter_log (meter_id, operation_type, old_reading, new_reading, remark) VALUES (?,?,?,?,?)",
|
|
38
|
|
- meterId, "install", BigDecimal.ZERO, initialReading, "新表安装");
|
|
|
44
|
+ "INSERT INTO rev_meter_log (meter_id, operation_type, operator_id, operator_name, old_reading, new_reading, remark) VALUES (?,?,?,?,?,?,?)",
|
|
|
45
|
+ meterId, "install", operatorId, operatorName, BigDecimal.ZERO, initialReading, "新表安装");
|
|
39
|
46
|
}
|
|
40
|
47
|
|
|
41
|
48
|
/** 故障换表 */
|
|
42
|
49
|
@Transactional
|
|
43
|
|
- public void replace(Long oldMeterId, Long newMeterId, BigDecimal oldReading, String remark) {
|
|
|
50
|
+ public void replace(Long oldMeterId, Long newMeterId, BigDecimal oldReading, String remark, String operatorId, String operatorName) {
|
|
44
|
51
|
// 旧表拆除
|
|
45
|
|
- jdbcTemplate.update("UPDATE rev_meter SET status = 'dismantled', current_reading = ? WHERE id = ?", oldReading, oldMeterId);
|
|
|
52
|
+ jdbcTemplate.update("UPDATE rev_meter SET status = 'dismantled', current_reading = ?, dismantle_date = CURRENT_DATE WHERE id = ?", oldReading, oldMeterId);
|
|
46
|
53
|
jdbcTemplate.update(
|
|
47
|
|
- "INSERT INTO rev_meter_log (meter_id, operation_type, old_reading, remark) VALUES (?,?,?,?)",
|
|
48
|
|
- oldMeterId, "dismantle", oldReading, remark);
|
|
|
54
|
+ "INSERT INTO rev_meter_log (meter_id, operation_type, operator_id, operator_name, old_reading, remark) VALUES (?,?,?,?,?,?)",
|
|
|
55
|
+ oldMeterId, "dismantle", operatorId, operatorName, oldReading, remark);
|
|
49
|
56
|
|
|
50
|
57
|
// 新表安装(继承客户信息)
|
|
51
|
58
|
Map<String, Object> oldMeter = jdbcTemplate.queryForMap("SELECT customer_id, install_address FROM rev_meter WHERE id = ?", oldMeterId);
|
|
|
@@ -53,16 +60,20 @@ public class MeterService {
|
|
53
|
60
|
"UPDATE rev_meter SET customer_id = ?, install_address = ?, status = 'active', install_date = CURRENT_DATE WHERE id = ?",
|
|
54
|
61
|
oldMeter.get("customer_id"), oldMeter.get("install_address"), newMeterId);
|
|
55
|
62
|
jdbcTemplate.update(
|
|
56
|
|
- "INSERT INTO rev_meter_log (meter_id, operation_type, new_meter_no, remark) VALUES (?,?,?,?)",
|
|
57
|
|
- newMeterId, "change", String.valueOf(newMeterId), "替换旧表 #" + oldMeterId);
|
|
|
63
|
+ "INSERT INTO rev_meter_log (meter_id, operation_type, operator_id, operator_name, new_meter_no, remark) VALUES (?,?,?,?,?,?)",
|
|
|
64
|
+ newMeterId, "change", operatorId, operatorName, String.valueOf(newMeterId), "替换旧表 #" + oldMeterId);
|
|
|
65
|
+
|
|
|
66
|
+ log.info("Meter replaced: old={}, new={}, operator={}", oldMeterId, newMeterId, operatorId);
|
|
58
|
67
|
}
|
|
59
|
68
|
|
|
60
|
69
|
/** 水表报废 */
|
|
61
|
|
- public void scrap(Long meterId, String reason) {
|
|
62
|
|
- jdbcTemplate.update("UPDATE rev_meter SET status = 'scrapped' WHERE id = ?", meterId);
|
|
|
70
|
+ @Transactional
|
|
|
71
|
+ public void scrap(Long meterId, String reason, String operatorId, String operatorName) {
|
|
|
72
|
+ jdbcTemplate.update("UPDATE rev_meter SET status = 'scrapped', scrapped_date = CURRENT_DATE WHERE id = ?", meterId);
|
|
63
|
73
|
jdbcTemplate.update(
|
|
64
|
|
- "INSERT INTO rev_meter_log (meter_id, operation_type, remark) VALUES (?,?,?)",
|
|
65
|
|
- meterId, "scrap", reason);
|
|
|
74
|
+ "INSERT INTO rev_meter_log (meter_id, operation_type, operator_id, operator_name, remark) VALUES (?,?,?,?,?)",
|
|
|
75
|
+ meterId, "scrap", operatorId, operatorName, reason);
|
|
|
76
|
+ log.info("Meter scrapped: id={}, reason={}", meterId, reason);
|
|
66
|
77
|
}
|
|
67
|
78
|
|
|
68
|
79
|
/** 查询水表生命周期记录 */
|
|
|
@@ -71,4 +82,92 @@ public class MeterService {
|
|
71
|
82
|
"SELECT * FROM rev_meter_log WHERE meter_id = ? ORDER BY created_at",
|
|
72
|
83
|
meterId);
|
|
73
|
84
|
}
|
|
|
85
|
+
|
|
|
86
|
+ /** 库存统计 */
|
|
|
87
|
+ public Map<String, Object> getInventoryStats() {
|
|
|
88
|
+ Map<String, Object> stats = new HashMap<>();
|
|
|
89
|
+
|
|
|
90
|
+ // 按状态统计
|
|
|
91
|
+ List<Map<String, Object>> statusStats = jdbcTemplate.queryForList(
|
|
|
92
|
+ "SELECT status, COUNT(*) as count FROM rev_meter GROUP BY status");
|
|
|
93
|
+ stats.put("statusStats", statusStats);
|
|
|
94
|
+
|
|
|
95
|
+ // 按口径统计
|
|
|
96
|
+ List<Map<String, Object>> caliberStats = jdbcTemplate.queryForList(
|
|
|
97
|
+ "SELECT caliber, COUNT(*) as count FROM rev_meter WHERE status = 'warehouse' GROUP BY caliber");
|
|
|
98
|
+ stats.put("caliberStats", caliberStats);
|
|
|
99
|
+
|
|
|
100
|
+ // 按类型统计
|
|
|
101
|
+ List<Map<String, Object>> typeStats = jdbcTemplate.queryForList(
|
|
|
102
|
+ "SELECT meter_type, COUNT(*) as count FROM rev_meter WHERE status = 'warehouse' GROUP BY meter_type");
|
|
|
103
|
+ stats.put("typeStats", typeStats);
|
|
|
104
|
+
|
|
|
105
|
+ // 按制造商统计
|
|
|
106
|
+ List<Map<String, Object>> manufacturerStats = jdbcTemplate.queryForList(
|
|
|
107
|
+ "SELECT manufacturer, COUNT(*) as count FROM rev_meter WHERE status = 'warehouse' GROUP BY manufacturer");
|
|
|
108
|
+ stats.put("manufacturerStats", manufacturerStats);
|
|
|
109
|
+
|
|
|
110
|
+ // 总库存
|
|
|
111
|
+ Long totalWarehouse = jdbcTemplate.queryForObject(
|
|
|
112
|
+ "SELECT COUNT(*) FROM rev_meter WHERE status = 'warehouse'", Long.class);
|
|
|
113
|
+ stats.put("totalWarehouse", totalWarehouse);
|
|
|
114
|
+
|
|
|
115
|
+ // 活跃水表数
|
|
|
116
|
+ Long totalActive = jdbcTemplate.queryForObject(
|
|
|
117
|
+ "SELECT COUNT(*) FROM rev_meter WHERE status = 'active'", Long.class);
|
|
|
118
|
+ stats.put("totalActive", totalActive);
|
|
|
119
|
+
|
|
|
120
|
+ // 其他状态计数
|
|
|
121
|
+ Long totalDismantled = jdbcTemplate.queryForObject(
|
|
|
122
|
+ "SELECT COUNT(*) FROM rev_meter WHERE status = 'dismantled'", Long.class);
|
|
|
123
|
+ stats.put("totalDismantled", totalDismantled);
|
|
|
124
|
+
|
|
|
125
|
+ Long totalScrapped = jdbcTemplate.queryForObject(
|
|
|
126
|
+ "SELECT COUNT(*) FROM rev_meter WHERE status = 'scrapped'", Long.class);
|
|
|
127
|
+ stats.put("totalScrapped", totalScrapped);
|
|
|
128
|
+
|
|
|
129
|
+ return stats;
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ /** 库存查询 */
|
|
|
133
|
+ public List<Map<String, Object>> getWarehouseMeters(String caliber, String meterType, int page, int size) {
|
|
|
134
|
+ String sql = "SELECT * FROM rev_meter WHERE status = 'warehouse'";
|
|
|
135
|
+ List<Object> params = new ArrayList<>();
|
|
|
136
|
+
|
|
|
137
|
+ if (caliber != null && !caliber.isEmpty()) {
|
|
|
138
|
+ sql += " AND caliber = ?";
|
|
|
139
|
+ params.add(caliber);
|
|
|
140
|
+ }
|
|
|
141
|
+ if (meterType != null && !meterType.isEmpty()) {
|
|
|
142
|
+ sql += " AND meter_type = ?";
|
|
|
143
|
+ params.add(meterType);
|
|
|
144
|
+ }
|
|
|
145
|
+
|
|
|
146
|
+ sql += " ORDER BY created_at DESC LIMIT ? OFFSET ?";
|
|
|
147
|
+ params.add(size);
|
|
|
148
|
+ params.add(page * size);
|
|
|
149
|
+
|
|
|
150
|
+ return jdbcTemplate.queryForList(sql, params.toArray());
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ /** 获取最近操作记录 */
|
|
|
154
|
+ public List<Map<String, Object>> getRecentOperations(int limit) {
|
|
|
155
|
+ return jdbcTemplate.queryForList(
|
|
|
156
|
+ "SELECT rml.*, rm.meter_no " +
|
|
|
157
|
+ "FROM rev_meter_log rml " +
|
|
|
158
|
+ "JOIN rev_meter rm ON rml.meter_id = rm.id " +
|
|
|
159
|
+ "ORDER BY rml.created_at DESC LIMIT ?", limit);
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ /** 获取水表详情 */
|
|
|
163
|
+ public Map<String, Object> getMeterDetails(Long meterId) {
|
|
|
164
|
+ Map<String, Object> meter = jdbcTemplate.queryForMap(
|
|
|
165
|
+ "SELECT * FROM rev_meter WHERE id = ?", meterId);
|
|
|
166
|
+
|
|
|
167
|
+ // 获取生命周期记录
|
|
|
168
|
+ List<Map<String, Object>> lifecycle = getLifecycle(meterId);
|
|
|
169
|
+ meter.put("lifecycle", lifecycle);
|
|
|
170
|
+
|
|
|
171
|
+ return meter;
|
|
|
172
|
+ }
|
|
74
|
173
|
}
|