|
|
@@ -0,0 +1,190 @@
|
|
|
+package com.etotem.cfc.controller.admin;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.FamilyMetricAggConfig;
|
|
|
+import com.etotem.cfc.mapper.FamilyMetricAggConfigMapper;
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 管理端 - 家庭指标聚合方式配置(平台级全局一份)。
|
|
|
+ * <p>
|
|
|
+ * 配置作用域为平台级(不分家庭),供五维能量汇总 / 珍珠图 / 章鱼图汇集使用。
|
|
|
+ * agg_type:SUM=合计 / AVG=平均 / COLLECT=汇集。
|
|
|
+ */
|
|
|
+@Tag(name = "管理端 - 家庭指标聚合方式配置")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/admin/family-metric-agg")
|
|
|
+public class FamilyMetricAggConfigController {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FamilyMetricAggConfigMapper familyMetricAggConfigMapper;
|
|
|
+
|
|
|
+ /** 全量配置列表(配置项 < 50,无需分页) */
|
|
|
+ @Operation(summary = "家庭指标聚合方式配置列表")
|
|
|
+ @PostMapping("/list")
|
|
|
+ public Result<List<Map<String, Object>>> list(@RequestAttribute("role") String role) {
|
|
|
+ if (!"admin".equals(role)) {
|
|
|
+ return Result.error("无权限");
|
|
|
+ }
|
|
|
+ List<FamilyMetricAggConfig> configs = familyMetricAggConfigMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<FamilyMetricAggConfig>()
|
|
|
+ .orderByAsc(FamilyMetricAggConfig::getSortOrder)
|
|
|
+ .orderByAsc(FamilyMetricAggConfig::getId));
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
+ for (FamilyMetricAggConfig c : configs) {
|
|
|
+ result.add(toMap(c));
|
|
|
+ }
|
|
|
+ return Result.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 新增/更新配置(body 带 id 则更新,否则新增) */
|
|
|
+ @Operation(summary = "保存家庭指标聚合方式配置")
|
|
|
+ @PostMapping("/save")
|
|
|
+ public Result<Boolean> save(@RequestBody Map<String, Object> params,
|
|
|
+ @RequestAttribute("role") String role) {
|
|
|
+ if (!"admin".equals(role)) {
|
|
|
+ return Result.error("无权限");
|
|
|
+ }
|
|
|
+ String metricKey = str(params.get("metricKey"));
|
|
|
+ String metricName = str(params.get("metricName"));
|
|
|
+ if (metricKey == null || metricKey.trim().isEmpty()) {
|
|
|
+ return Result.error("metricKey不能为空");
|
|
|
+ }
|
|
|
+ if (metricName == null || metricName.trim().isEmpty()) {
|
|
|
+ return Result.error("metricName不能为空");
|
|
|
+ }
|
|
|
+ String aggType = str(params.get("aggType"));
|
|
|
+ String valueType = str(params.get("valueType"));
|
|
|
+ if (aggType == null || aggType.trim().isEmpty()) {
|
|
|
+ return Result.error("aggType不能为空");
|
|
|
+ }
|
|
|
+ if (valueType == null || valueType.trim().isEmpty()) {
|
|
|
+ return Result.error("valueType不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long id = longVal(params.get("id"));
|
|
|
+ FamilyMetricAggConfig config;
|
|
|
+ if (id != null) {
|
|
|
+ config = familyMetricAggConfigMapper.selectById(id);
|
|
|
+ if (config == null) {
|
|
|
+ return Result.error("配置不存在");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FamilyMetricAggConfig existing = familyMetricAggConfigMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<FamilyMetricAggConfig>()
|
|
|
+ .eq(FamilyMetricAggConfig::getMetricKey, metricKey.trim())
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ if (existing != null) {
|
|
|
+ return Result.error("metricKey已存在,请直接编辑对应记录");
|
|
|
+ }
|
|
|
+ config = new FamilyMetricAggConfig();
|
|
|
+ config.setCreatedAt(new Date());
|
|
|
+ }
|
|
|
+ config.setMetricKey(metricKey.trim());
|
|
|
+ config.setMetricName(metricName.trim());
|
|
|
+ config.setDimensionCode(str(params.get("dimensionCode")));
|
|
|
+ config.setAggType(aggType.trim().toUpperCase());
|
|
|
+ config.setValueType(valueType.trim().toUpperCase());
|
|
|
+ config.setDisplayMode(str(params.get("displayMode")));
|
|
|
+ config.setSortOrder(intVal(params.get("sortOrder"), 0));
|
|
|
+ config.setStatus(str(params.get("status")) != null ? str(params.get("status")).trim() : "active");
|
|
|
+ config.setRemark(str(params.get("remark")));
|
|
|
+ config.setUpdatedAt(new Date());
|
|
|
+
|
|
|
+ if (id != null) {
|
|
|
+ familyMetricAggConfigMapper.updateById(config);
|
|
|
+ } else {
|
|
|
+ familyMetricAggConfigMapper.insert(config);
|
|
|
+ }
|
|
|
+ return Result.success(Boolean.TRUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 删除配置 */
|
|
|
+ @Operation(summary = "删除家庭指标聚合方式配置")
|
|
|
+ @PostMapping("/delete")
|
|
|
+ public Result<Boolean> delete(@RequestBody Map<String, Object> params,
|
|
|
+ @RequestAttribute("role") String role) {
|
|
|
+ if (!"admin".equals(role)) {
|
|
|
+ return Result.error("无权限");
|
|
|
+ }
|
|
|
+ Long id = longVal(params.get("id"));
|
|
|
+ if (id == null) {
|
|
|
+ return Result.error("id不能为空");
|
|
|
+ }
|
|
|
+ familyMetricAggConfigMapper.deleteById(id);
|
|
|
+ return Result.success(Boolean.TRUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 更新状态(active/disabled) */
|
|
|
+ @Operation(summary = "更新家庭指标聚合方式配置状态")
|
|
|
+ @PostMapping("/update-status")
|
|
|
+ public Result<Boolean> updateStatus(@RequestBody Map<String, Object> params,
|
|
|
+ @RequestAttribute("role") String role) {
|
|
|
+ if (!"admin".equals(role)) {
|
|
|
+ return Result.error("无权限");
|
|
|
+ }
|
|
|
+ Long id = longVal(params.get("id"));
|
|
|
+ String status = str(params.get("status"));
|
|
|
+ if (id == null) {
|
|
|
+ return Result.error("id不能为空");
|
|
|
+ }
|
|
|
+ if (status == null || status.trim().isEmpty()) {
|
|
|
+ return Result.error("status不能为空");
|
|
|
+ }
|
|
|
+ FamilyMetricAggConfig config = familyMetricAggConfigMapper.selectById(id);
|
|
|
+ if (config == null) {
|
|
|
+ return Result.error("配置不存在");
|
|
|
+ }
|
|
|
+ config.setStatus(status.trim());
|
|
|
+ config.setUpdatedAt(new Date());
|
|
|
+ familyMetricAggConfigMapper.updateById(config);
|
|
|
+ return Result.success(Boolean.TRUE);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> toMap(FamilyMetricAggConfig c) {
|
|
|
+ Map<String, Object> m = new java.util.LinkedHashMap<>();
|
|
|
+ m.put("id", c.getId());
|
|
|
+ m.put("metricKey", c.getMetricKey());
|
|
|
+ m.put("metricName", c.getMetricName());
|
|
|
+ m.put("dimensionCode", c.getDimensionCode());
|
|
|
+ m.put("aggType", c.getAggType());
|
|
|
+ m.put("valueType", c.getValueType());
|
|
|
+ m.put("displayMode", c.getDisplayMode());
|
|
|
+ m.put("sortOrder", c.getSortOrder());
|
|
|
+ m.put("status", c.getStatus());
|
|
|
+ m.put("remark", c.getRemark());
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String str(Object o) {
|
|
|
+ return o == null ? null : o.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long longVal(Object o) {
|
|
|
+ return o == null ? null : Long.valueOf(o.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer intVal(Object o, Integer def) {
|
|
|
+ if (o == null) {
|
|
|
+ return def;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Integer.valueOf(o.toString());
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return def;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|