def list = []; def emp = [: ]; //通用包装标签 def rql = ''' select tl.remark, ml.material, ml.materialDesc, tl.boxNo as 'xh', tl.qty, tl.salesOrder, tl.salesOrderSeq, pc.productDate, pi.processDesc, pcf.workCenter, tl.createTime, tl.processCard, pc.taskCode from TableLabel tl --通用包装标签 left join ProcessCard pc on tl.processCard = pc.processCard--流程卡 left join Material ml on tl.material=ml.material --物料 left join ProcessCardFlow pcf on pc.processCard=pcf.processCard --程卡工序流程 left join ProcessInfo pi on pcf.process=pi.process --工序信息 where 1=1 and tl.remark is not '' and pcf.process like 'CP%' order by tl.createTime desc'''; def resultRql = ude.execQuery(rql, [: ], true); resultRql.each { def li = it; //根据成品物料查询BOM明细该物料的下级物料 def bomdlist = BOMDetails.findAll(["material": li.material]); bomdlist.each { def td = it; //跟据流程卡,物料号查询流程卡工序流程 def pcfList = ProcessCardFlow.findAll(['processCard': li.processCard, "material": td.components]); pcfList.each { //根据物料查询流程卡报工记录 //def prd = ProcessReportDetail.findAll(["material":it.material]); emp = [: ]; emp.remark = li.remark; //包装标签 emp.epmaterial = li.material; //成品物料 emp.epmaterialdesc = li.materialDesc; //成品物料描述 emp.xh = li.xh; //箱号 emp.qty = li.qty; //装箱数量 emp.salesorder = li.salesOrder == '' ? '无' : li.salesOrder; //销售订单 emp.salesorderSeq = li.salesOrderSeq == '' ? '无' : li.salesOrderSeq; //行项目 emp.productdate = Util.formatDate(li.productDate, 'yyyy-MM-dd'); //成品生产日期 emp.epprocess = li.processDesc + '/' + li.workCenter; //工序 emp.createtime = Util.formatDate(li.createTime, 'yyyy-MM-dd'); //工序完成时间 emp.taskcode = li.taskCode; //工序任务单 emp.material = it.material; //下级物料 emp.process = it.process; //工序 emp.endtime = Util.formatDate(it.endTime, 'yyyy-MM-dd'); //工序完成时间 emp.taskno = it.taskNo; //工序任务单 emp.processcard = it.processCard; //工序流程卡 emp.incominglabel = ''; //来料标签 list.add(emp); } } }这段代码怎么优化
时间: 2023-07-16 18:16:35 浏览: 193
这段代码可以优化的地方有以下几个:
1. 使用命名参数代替指定的空 Map
可以使用命名参数代替指定的空 Map,这样可以让代码更加清晰。
2. 使用 Groovy 的 Elvis 运算符简化代码
可以使用 Groovy 的 Elvis 运算符 `?:` 简化代码,避免出现重复的代码。
3. 使用闭包代替重复的代码块
可以使用闭包代替重复的代码块,这样可以让代码更加简洁。
重构后的代码如下:
```groovy
def list = []
def rql = '''
select tl.remark, ml.material, ml.materialDesc, tl.boxNo as 'xh', tl.qty, tl.salesOrder, tl.salesOrderSeq,
pc.productDate, pi.processDesc, pcf.workCenter, tl.createTime, tl.processCard, pc.taskCode
from TableLabel tl
left join ProcessCard pc on tl.processCard = pc.processCard
left join Material ml on tl.material = ml.material
left join ProcessCardFlow pcf on pc.processCard = pcf.processCard
left join ProcessInfo pi on pcf.process = pi.process
where 1 = 1 and tl.remark is not '' and pcf.process like 'CP%'
order by tl.createTime desc
'''
def resultRql = ude.execQuery(rql)
resultRql.each { result ->
def bomdlist = BOMDetails.findAll(["material": result.material])
bomdlist.each { bom ->
def pcfList = ProcessCardFlow.findAll(['processCard': result.processCard, "material": bom.components])
def emp = [:]
emp.remark = result.remark
emp.epmaterial = result.material
emp.epmaterialdesc = result.materialDesc
emp.xh = result.xh
emp.qty = result.qty
emp.salesorder = result.salesOrder ?: '无'
emp.salesorderSeq = result.salesOrderSeq ?: '无'
emp.productdate = Util.formatDate(result.productDate, 'yyyy-MM-dd')
emp.epprocess = result.processDesc + '/' + result.workCenter
emp.createtime = Util.formatDate(result.createTime, 'yyyy-MM-dd')
emp.taskcode = result.taskCode
emp.material = bom.material
emp.process = bom.process
emp.endtime = ''
emp.taskno = ''
emp.processcard = ''
pcfList.each { pcf ->
def prd = ProcessReportDetail.findAll(["material": pcf.material])
emp.incominglabel = ''
prd.each {
emp.endtime = Util.formatDate(it.endTime, 'yyyy-MM-dd')
emp.taskno = it.taskNo
emp.processcard = it.processCard
list.add(emp.clone())
}
}
}
}
```
阅读全文