相关概念:BOM 展开、LIST对象、JSON 字符串、JSON 对象
最近在开发一个管理软件,涉及到物料清单(又称产品结构)BOM管理的相关内容,其中之一就是BOM 的展开。BOM的展开一般有纵向优先展开、层次优先展开和汇总展开等形式,也有很多展开的算法。在开发中使用Spring MVC 框架完成了BOM按层级优先展开的程序,也涉及到了JAVA开发中的相关知识如LIST 对象、LIST 对象转JSON 串,JSON 串返回前端并还原成JSON 对象,在前端通过JSON 对象输出结果内容。
BOM 实体类
public class Bom {
private Integer id; // id 号
private Integer parentid;// 父项 id
private String parentcode; // 父项 代码
private String parentname; // 父项 名称
private Integer subitemid; // 子项 id
private String subitemcode; // 子项代码
private String subitemname; // 子项名称
private String unit; // 计量单位
private Integer qty; // 数量
private Integer level; // 层级号
private Date createDate; // 建立日期
private String username; // 建立用户
private String subitemflag; // 子项标识
get、set 方法略
}
传入到程序的参数 parentcode 待展开BOM的物品代码
程序中使用了3个 LIST 对象数组。
List listBom 存放BOM展开后的最后结果
List listBomt01 BOM 展开中使用的临时LIST 对象数组
List listBomt02 BOM 展开中使用的临时LIST 对象数组
由于BOM是树状结构循环展开,因此至少要使用两个LIST 对象数组,一个存放当前层级节点,一个存放下一层级节点。交替循环展开,每一层级展开的结果都要送到listBom 中。展开完成后需将 listBom 转JSON 串返回给前端数据展现页面。
JAVA 代码如下
//依据物料号逐层展开 BOM,后端handler 程序
@ResponseBody
@RequestMapping(value="/ajaxbomexplosionlevel", method=RequestMethod.POST,produces = "text/html;charset=UTF-8")
public String ajaxbomexplosionbylevel(@RequestParam(value="parentcode",required=false) String parentcode,Map map) throws JsonProcessingException {
List listBom = BomService.BomExplosionbyLevel(parentcode);
// LIST 对象转JSON 字符串
ObjectMapper mapper = new ObjectMapper();
String jsonlist = mapper.writeValueAsString(listBom);
return jsonlist;
}
//Service中的BOM 展开函数
//依据物料号逐层展开 BOM,Service 业务实现
@Transactional(readOnly=true)
public List BomExplosionbyLevel(String parentcode){
//listBom 存放逐层展开 BOM的结果,初始获取第一层
List listBom =BomRepository.listBomByparentcode(parentcode); //利用了Repository 中的查询函数
//第一层级 level 赋值
Integer level = 1;
for (int i = 0; i < listBom.size(); i++) {
Bom bom = listBom.get(i);
bom.setLevel(level);
}
//listBomt01 和listBomt02 交替逐层展开 BOM的临时两个 list 对象,并赋予初始值
List listBomt01 =BomRepository.listBomByparentcode(parentcode); //利用了Repository 中的查询函数
List listBomt02 =new ArrayList();
//开始逐层交替展开BOM,展开的每层BOM 节点要放入结果listBom 中,同时放入临时对象listBomt01 或 listBomt02中,等待继续循环往下展开,并且在放入之前临时对象要进行初始化
while ( listBomt01.size()>0 ) {
level = level + 1;listBomt02.clear();
//当前层为listBomt01时结果输出至listBom 和listBomt02
for (int i = 0; i < listBomt01.size(); i++) {
Bom bom = listBomt01.get(i);
String subcode = bom.getSubitemcode();
Integer itemqty = bom.getQty();
List listBomt01sub =BomRepository.listBomByparentcode(subcode);
for (int i1 = 0; i1 < listBomt01sub.size(); i1++) {
Bom bom1 = listBomt01sub.get(i1);
bom1.setLevel(level);
Integer qty = bom1.getQty();
bom1.setQty(qty * itemqty);
listBom.add( bom1);
listBomt02.add( bom1);
}
}
level = level + 1;listBomt01.clear();
//当前层为listBomt02时结果输出至listBom 和listBomt01
for (int i = 0; i < listBomt02.size(); i++) {
Bom bom = listBomt02.get(i);
String subcode = bom.getSubitemcode();
Integer itemqty = bom.getQty();
List listBomt02sub =BomRepository.listBomByparentcode(subcode);
for (int i2 = 0; i2 < listBomt02sub.size(); i2++) {
Bom bom2 = listBomt02sub.get(i2);
bom2.setLevel(level);
Integer qty = bom2.getQty();
bom2.setQty(qty * itemqty);
listBom.add( bom2);
listBomt01.add( bom2);
}
}
} //while 结束
//返回展开的结果 listBom
return listBom;
}
JSP页面输出结果
标签:JAVA,String,Spring,private,MVC,BOM,listBom,展开,listBomt01
来源: /rtyuhj67/article/details/100022988