fix(province): improve error handling and logging in equipment configuration retrieval

This commit is contained in:
syruan 2025-09-16 18:12:03 +08:00
parent 97eb09437b
commit 8c16a8b9e7
4 changed files with 172 additions and 117 deletions

View File

@ -191,45 +191,56 @@ public class SysDeptServiceImpl implements ISysDeptService {
if (devices == null || devices.isEmpty()) continue;
Ownerdomin first = devices.get(0);
if (first == null || first.getCompanyId() == null || first.getMaName() == null) {
log.warn("设备信息不完整,跳过处理: companyId={}, devices={}", key, devices);
continue;
}
Long companyId = first.getCompanyId();
String maName = first.getMaName();
// devInfo 中找公司名称
// devInfo 中找公司名称添加空值检查
Optional<NewOwnerdomin> matchDev = ownListFromDevInfo.stream()
.filter(d -> d.getCompanyId() == companyId.intValue() && d.getMaName().equals(maName))
.filter(d -> d != null && d.getMaName() != null &&
d.getCompanyId() == companyId.intValue() && maName.equals(d.getMaName()))
.findFirst();
String companyName = matchDev.map(NewOwnerdomin::getCompanyName).orElse("未知公司");
// 获取或创建公司维度的汇总对象 如果这个公司没统计对象就创建一个
// 获取或创建公司维度的汇总对象
DeptConfigRateSummary dto = companyMap.computeIfAbsent(companyName, k -> {
DeptConfigRateSummary d = new DeptConfigRateSummary();
d.setDeptName(companyName);
// d.setConfigType(entry.getValue().get(0).getCompanyId());
d.setCompanyId(companyId);
d.setCompanyName(companyName);
d.setValueA(BigDecimal.ZERO);
d.setValueB(BigDecimal.ZERO);
d.setValueC(BigDecimal.ZERO);
d.setConfigType(BigDecimal.ZERO); // 设置默认的配置类型
return d;
});
// 汇总订单数量
// 汇总订单数量添加空值处理
BigDecimal orderCount = devices.stream()
.filter(Objects::nonNull)
.map(d -> Optional.ofNullable(d.getOrderCount()).orElse(BigDecimal.ZERO))
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 构造临时配置项用于计算
// 构造"临时配置项"用于计算
DeptConfigRateSummary dummyConfig = new DeptConfigRateSummary();
// 假设你已经找到了匹配的 NewOwnerdomin 对象 matchDev
BigDecimal fullScore = matchDev.map(NewOwnerdomin::getFullScore).orElse(BigDecimal.TEN);
BigDecimal baseNum = matchDev.map(NewOwnerdomin::getBaseNum).orElse(BigDecimal.ONE);
dummyConfig.setFullScore(fullScore);
dummyConfig.setFullScore(matchDev.map(NewOwnerdomin::getFullScore)
.filter(Objects::nonNull)
.orElse(BigDecimal.TEN));
dummyConfig.setOrderCount(orderCount);
dummyConfig.setDeptName(maName);
dummyConfig.setCompanyId(companyId);
dummyConfig.setCompanyName(companyName);
dummyConfig.setConfigValue(matchDev.map(NewOwnerdomin::getBaseNum)
.filter(Objects::nonNull)
.orElse(BigDecimal.ONE));
dummyConfig.setConfigRate(matchDev.map(NewOwnerdomin::getFullScore)
.filter(Objects::nonNull)
.orElse(BigDecimal.TEN));
// 计算得分
BigDecimal score = computeScoreByMaName(orderCount, devices, dummyConfig, "0");
@ -328,19 +339,19 @@ public class SysDeptServiceImpl implements ISysDeptService {
if (limitedType && result.compareTo(new BigDecimal("20")) > 0) {
result = new BigDecimal("20.00");
}
System.out.println("【配置率计算明细】");
System.err.println("类型:" + config.getConfigType());
System.out.println("设备名:" + config.getDeptName());
System.out.println("配置类型:" + config.getConfigType());
System.out.println("自有数量合计ownedTotal" + ownedTotal);
System.out.println("租赁数量合计leasedTotal" + leasedTotal);
System.out.println("订单数量ordercount" + ordercount);
System.out.println("基本配置数量base" + base);
System.out.println("满分值score" + score);
System.out.println("得分比例rate" + rate);
System.out.println("是否为限制类型3/4/5" + limitedType);
System.out.println("最终得分result" + result);
System.out.println("--------------------------------------------------");
// System.out.println("【配置率计算明细】");
// System.err.println("类型:" + config.getConfigType());
// System.out.println("设备名:" + config.getDeptName());
// System.out.println("配置类型:" + config.getConfigType());
// System.out.println("自有数量合计ownedTotal" + ownedTotal);
// System.out.println("租赁数量合计leasedTotal" + leasedTotal);
// System.out.println("订单数量ordercount" + ordercount);
// System.out.println("基本配置数量base" + base);
// System.out.println("满分值score" + score);
// System.out.println("得分比例rate" + rate);
// System.out.println("是否为限制类型3/4/5" + limitedType);
// System.out.println("最终得分result" + result);
// System.out.println("--------------------------------------------------");
return result;

View File

@ -6,11 +6,13 @@ import com.bonus.material.device.domain.DevInfo;
import com.bonus.material.largeScreen.service.ProvinceScreenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
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.HashMap;
import java.util.List;
import java.util.Map;
@ -19,6 +21,7 @@ import java.util.Map;
* @author 马三炮
* @date 2025/9/14
*/
@Slf4j
@Api(value = "网省大屏", tags = {"网省大屏"})
@RestController
@RequestMapping("/provinceScreen")
@ -66,9 +69,15 @@ public class ProvinceScreenController extends BaseController {
public AjaxResult getUnitEquipmentConfiguration() {
try {
List<Map<String,Object>> res = provinceScreenService.getUnitEquipmentConfiguration();
if (res == null) {
logger.warn("单位装备配置数据为空");
return AjaxResult.success(new ArrayList<>());
}
return AjaxResult.success(res);
} catch (Exception e) {
return AjaxResult.error("单位装备配置异常");
log.error("e: ", e);
logger.error("单位装备配置异常: ", e);
return AjaxResult.error("单位装备配置异常: " + e.getMessage());
}
}

View File

@ -112,55 +112,98 @@ public class ProvinceScreenServiceImpl implements ProvinceScreenService {
*/
@Override
public List<Map<String,Object>> getUnitEquipmentConfiguration() {
//获取所有单位 TODO 后期可能单位放到字典表这里要修改
SysDept sysDept = new SysDept();
DevInfoReq devInfoReq = new DevInfoReq();
List<SysDept> sysDeptList = sysDeptMapper.selectDeptList(sysDept);
List<Map<String,Object>> res = new ArrayList<>();
for (SysDept sysDeptNew:sysDeptList) {
Map<String,Object> dept = new HashMap<>();
if (sysDeptMapper == null) {
log.error("sysDeptMapper is null");
return new ArrayList<>();
}
dept.put("deptName",sysDeptNew.getDeptName());
devInfoReq.setOwnCo(Math.toIntExact(sysDeptNew.getDeptId()));
//装备价值
BigDecimal totalValue = devInfoMapper.getTotalValue(devInfoReq);
dept.put("totalValue",totalValue);
//装备总数
Integer totalEquipmentQuantity = devInfoMapper.getTotalEquipment(devInfoReq);
dept.put("totalEquipmentQuantity",totalEquipmentQuantity);
//线路
devInfoReq.setTypeId(1);
int lineNum = devInfoMapper.getLineNum(devInfoReq);
dept.put("lineNum",lineNum);
//变电
devInfoReq.setTypeId(2);
int substationNum = devInfoMapper.getLineNum(devInfoReq);
dept.put("substationNum",substationNum);
//电缆
devInfoReq.setTypeId(3);
int cableNum = devInfoMapper.getLineNum(devInfoReq);
dept.put("cableNum",cableNum);
//所在城市
String cityName = sysDeptMapper.getCityName(sysDeptNew.getCity());
dept.put("cityName",cityName);
// 获取装备转换率
DeptConfigRateSummary user = new DeptConfigRateSummary();
List<DeptConfigRateSummary> list = sysDeptService.selectDeptConfigRatePivot(user);
for (DeptConfigRateSummary deptConfigRateSummary: list) {
if (deptConfigRateSummary.getDeptId().equals(sysDeptNew.getDeptId())){
//总数
dept.put("configRate",deptConfigRateSummary.getConfigRate());
//线路
dept.put("valueA",deptConfigRateSummary.getValueA());
//变电
dept.put("valueA",deptConfigRateSummary.getValueB());
//电缆
dept.put("valueA",deptConfigRateSummary.getValueC());
}
}
res.add(dept);
SysDept sysDept = new SysDept();
DevInfoReq devInfoReq = new DevInfoReq();
List<SysDept> sysDeptList = sysDeptMapper.selectDeptList(sysDept);
if (sysDeptList == null) {
log.error("未找到部门数据");
return new ArrayList<>();
}
List<Map<String,Object>> res = new ArrayList<>();
for (SysDept sysDeptNew : sysDeptList) {
if (sysDeptNew == null || sysDeptNew.getDeptId() == null) {
log.warn("跳过无效的部门数据");
continue;
}
return res;
Map<String,Object> dept = new HashMap<>();
dept.put("deptName", sysDeptNew.getDeptName() != null ? sysDeptNew.getDeptName() : "未知部门");
devInfoReq.setOwnCo(Math.toIntExact(sysDeptNew.getDeptId()));
// 装备价值
BigDecimal totalValue = devInfoMapper.getTotalValue(devInfoReq);
dept.put("totalValue", totalValue != null ? totalValue : BigDecimal.ZERO);
// 装备总数
Integer totalEquipmentQuantity = devInfoMapper.getTotalEquipment(devInfoReq);
dept.put("totalEquipmentQuantity", totalEquipmentQuantity != null ? totalEquipmentQuantity : 0);
// 线路
devInfoReq.setTypeId(1);
int lineNum = devInfoMapper.getLineNum(devInfoReq);
dept.put("lineNum", lineNum);
// 变电
devInfoReq.setTypeId(2);
int substationNum = devInfoMapper.getLineNum(devInfoReq);
dept.put("substationNum", substationNum);
// 电缆
devInfoReq.setTypeId(3);
int cableNum = devInfoMapper.getLineNum(devInfoReq);
dept.put("cableNum", cableNum);
// 所在城市
String cityName = sysDeptMapper.getCityName(sysDeptNew.getCity());
dept.put("cityName", cityName != null ? cityName : "未知城市");
// 获取装备转换率
try {
DeptConfigRateSummary user = new DeptConfigRateSummary();
List<DeptConfigRateSummary> list = sysDeptService.selectDeptConfigRatePivot(user);
if (list != null) {
for (DeptConfigRateSummary deptConfigRateSummary : list) {
if (deptConfigRateSummary != null &&
deptConfigRateSummary.getDeptId() != null &&
deptConfigRateSummary.getDeptId().equals(sysDeptNew.getDeptId()))
{
// 总数
dept.put("configRate", deptConfigRateSummary.getConfigRate() != null ?
deptConfigRateSummary.getConfigRate() : 0.0);
// 线路
dept.put("valueA", deptConfigRateSummary.getValueA() != null ?
deptConfigRateSummary.getValueA() : 0.0);
// 变电
dept.put("valueB", deptConfigRateSummary.getValueB() != null ?
deptConfigRateSummary.getValueB() : 0.0);
// 电缆
dept.put("valueC", deptConfigRateSummary.getValueC() != null ?
deptConfigRateSummary.getValueC() : 0.0);
break;
}
}
}
} catch (Exception e) {
log.error("获取装备转换率异常: " + e.getMessage(), e);
// 设置默认值
dept.put("configRate", 0.0);
dept.put("valueA", 0.0);
dept.put("valueB", 0.0);
dept.put("valueC", 0.0);
}
res.add(dept);
}
return res;
}
/**
@ -200,7 +243,7 @@ public class ProvinceScreenServiceImpl implements ProvinceScreenService {
res.add(res2);
res.add(res3);
res.add(res4);
return null;
return res;
}
/**
@ -262,23 +305,20 @@ public class ProvinceScreenServiceImpl implements ProvinceScreenService {
res.add(dept);
}
// 核心排序逻辑按proportion数值降序排序高在用率优先
Collections.sort(res, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> map1, Map<String, Object> map2) {
// 1. 提取两个map中的proportion字符串
String proportion1 = (String) map1.get("proportion");
String proportion2 = (String) map2.get("proportion");
// 2. 处理null值默认视为0%
proportion1 = proportion1 == null ? "0%" : proportion1;
proportion2 = proportion2 == null ? "0%" : proportion2;
// 3. 去掉"%"符号转为Integer数值核心步骤
// 注意若proportion含小数"83.5%"需转为Double此处按整数处理适配原代码逻辑
int propValue1 = Integer.parseInt(proportion1.replace("%", ""));
int propValue2 = Integer.parseInt(proportion2.replace("%", ""));
// 4. 数值降序排序高在用率在前升序用propValue1 - propValue2
return propValue2 - propValue1;
}
});
// res.sort((map1, map2) -> {
// // 1. 提取两个map中的proportion字符串
// String proportion1 = (String) map1.get("proportion");
// String proportion2 = (String) map2.get("proportion");
// // 2. 处理null值默认视为0%
// proportion1 = proportion1 == null ? "0%" : proportion1;
// proportion2 = proportion2 == null ? "0%" : proportion2;
// // 3. 去掉"%"符号转为Integer数值核心步骤
// // 注意若proportion含小数"83.5%"需转为Double此处按整数处理适配原代码逻辑
// int propValue1 = Integer.parseInt(proportion1.replace("%", ""));
// int propValue2 = Integer.parseInt(proportion2.replace("%", ""));
// // 4. 数值降序排序高在用率在前升序用propValue1 - propValue2
// return propValue2 - propValue1;
// });
return res;
}
@ -303,7 +343,7 @@ public class ProvinceScreenServiceImpl implements ProvinceScreenService {
List<MaType> maTypeList = new ArrayList<>();
//获取装备在用率 查询全部
if (StringUtils.isNull(type)){
maTypeList = maTypeMapper.selectMaTypeTreeBy5Level(type);
maTypeList = maTypeMapper.selectMaTypeTreeBy5Level(null);
}else {
maTypeList = maTypeMapper.selectMaTypeTreeBy5Level2(type);
}
@ -330,24 +370,21 @@ public class ProvinceScreenServiceImpl implements ProvinceScreenService {
}
}
// 核心排序逻辑按proportion数值降序排序高在用率优先
Collections.sort(res, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> map1, Map<String, Object> map2) {
// 1. 提取两个map中的proportion字符串
String proportion1 = (String) map1.get("proportion");
String proportion2 = (String) map2.get("proportion");
// 2. 处理null值默认视为0%
proportion1 = proportion1 == null ? "0%" : proportion1;
proportion2 = proportion2 == null ? "0%" : proportion2;
// 3. 去掉"%"符号转为Integer数值核心步骤
// 注意若proportion含小数"83.5%"需转为Double此处按整数处理适配原代码逻辑
int propValue1 = Integer.parseInt(proportion1.replace("%", ""));
int propValue2 = Integer.parseInt(proportion2.replace("%", ""));
// 4. 数值降序排序高在用率在前升序用propValue1 - propValue2
return propValue2 - propValue1;
}
res.sort((map1, map2) -> {
// 1. 提取两个map中的proportion字符串
String proportion1 = (String) map1.get("proportion");
String proportion2 = (String) map2.get("proportion");
// 2. 处理null值默认视为0%
proportion1 = proportion1 == null ? "0%" : proportion1;
proportion2 = proportion2 == null ? "0%" : proportion2;
// 3. 去掉"%"符号转为Integer数值核心步骤
// 注意若proportion含小数"83.5%"需转为Double此处按整数处理适配原代码逻辑
int propValue1 = Integer.parseInt(proportion1.replace("%", ""));
int propValue2 = Integer.parseInt(proportion2.replace("%", ""));
// 4. 数值降序排序高在用率在前升序用propValue1 - propValue2
return propValue2 - propValue1;
});
return res;
return res.subList(0,10);
}
/**

View File

@ -621,16 +621,14 @@
<select id="selectMaTypeTreeBy5Level" resultType="com.bonus.material.ma.vo.MaType">
select type_id, type_name from ma_type where del_flag = '0' and level = '5'
</select>
<select id="selectMaTypeTreeBy5Level2" resultType="com.bonus.material.ma.vo.MaType">
select mt.type_id, mt.type_name
from ma_type mt
left join ma_type mt1 on mt.parent_id = mt1.type_id
left join ma_type mt2 on mt1.parent_id = mt2.type_id
left join ma_type mt3 on mt2.parent_id = mt3.type_id
left join ma_type mt4 on mt3.parent_id = mt4.type_id
where mt.del_flag = '0'
<if test="type != null ">
AND mt4.type_id = #{type}
select
mtv.typeId, mtv.devSubcategory as typeName
from
ma_type_view mtv
<if test="type != null and type != 0">
where mtv.maxTypeId = #{type}
</if>
</select>