if (item.tmsdType != null) { return item.tmsdType == "1" ? _iDerCardsG(item) : item.tmsdType == "2" ? _iDerCardsQ(item) : const Center(); } else if (item.tdrWorkType != null) { return _iDerCards(item); } else if (item.tmdiMemberId != null) { return _itemBuilderD(item); } else if (item.tmdMemberId != null) { return _itemBuilder(item); }该判断能给我优化一下吗?
时间: 2024-02-14 15:08:51 浏览: 28
这段代码中的判断逻辑看起来比较繁琐,可以考虑使用 switch case 语句来进行优化。具体实现方式如下:
```
switch (true) {
case item.tmsdType == "1":
return _iDerCardsG(item);
case item.tmsdType == "2":
return _iDerCardsQ(item);
case item.tdrWorkType != null:
return _iDerCards(item);
case item.tmdiMemberId != null:
return _itemBuilderD(item);
case item.tmdMemberId != null:
return _itemBuilder(item);
default:
return const Center();
}
```
使用 switch case 语句可以减少代码行数,使代码更加清晰易懂。同时,通过将表达式放在 case 语句后面,可以避免使用多个 if else 语句的情况。
相关问题
优化这段代码 res.data.availablePlayItemVoList.forEach((item: any) => { this.playDateProductInfoList.forEach((v: any) => { v.playScreeningList.forEach((items: any) => { if (items.playItemCode == item.playItemCode) { this.$set( items, "availableTotal", item.availableTotal != null ? item.availableTotal : null ); } if (item.screeningList.length > 0) { item.screeningList.forEach((itemA: any) => { if (items.sysTimeQuantumList != null) { items.sysTimeQuantumList.forEach((itemB: any) => { if (itemA.screeningCode == itemB.screeningCode) { this.$set( itemB, "surplusTotal", itemA.surplusTotal != null ? itemA.surplusTotal : null ); } }); } }); } }); }); }); });
这段代码也可以使用ES6的数组方法进行优化,如下所示:
```javascript
res.data.availablePlayItemVoList.forEach(item => {
this.playDateProductInfoList.forEach(v => {
v.playScreeningList.filter(items => items.playItemCode === item.playItemCode)
.forEach(items => {
this.$set(
items,
"availableTotal",
item.availableTotal != null ? item.availableTotal : null
);
item.screeningList.forEach(itemA => {
if (items.sysTimeQuantumList != null) {
items.sysTimeQuantumList.filter(itemB => itemA.screeningCode === itemB.screeningCode)
.forEach(itemB => {
this.$set(
itemB,
"surplusTotal",
itemA.surplusTotal != null ? itemA.surplusTotal : null
);
});
}
});
});
});
});
```
同样地,使用`filter`方法可以减少循环嵌套,使代码更加清晰易读。另外,可以使用ES6的箭头函数来简化代码。
<select id="selectAll" resultMap="tblTicketCardDistributionMap"> select * from TBL_TICKET_CARD_DISTRIBUTION <where> <choose> <when test="query.orderType != null and query.orderType != '' and query.orderType =='03'"> and ORDER_TYPE = #{query.orderType} and ( <if test="query.inStationId != null and query.inStationId != '' "> IN_STATION_ID = #{query.inStationId} </if> <if test="query.outStationId != null and query.outStationId != '' "> or OUT_STATION_ID = #{query.outStationId} </if> ) </when> <otherwise> <if test="query.orderType != null and query.orderType != '' "> and ORDER_TYPE = #{query.orderType} </if> <if test="query.inStationId != null and query.inStationId != '' "> and IN_STATION_ID = #{query.inStationId} </if> <if test="query.outStationId != null and query.outStationId != '' "> or OUT_STATION_ID = #{query.outStationId} </if> </otherwise> </choose> <if test="query.lineId != null and query.lineId != '' "> and LINE_ID = #{query.lineId} </if> <if test="query.stationId != null and query.stationId != '' "> and STATION_ID = #{query.stationId} </if> <if test="query.operatorId != null and query.operatorId != '' "> and OPERATOR_ID = #{query.operatorId} </if> <if test="query.curDay != null and query.curDay != '' "> and CUR_DAY = #{query.curDay} </if> <if test="query.beginTime != null and query.beginTime != '' "> and OPERATE_TIME between #{query.beginTime} and #{query.endTime} </if> </where> </select>报错语法有问题,修改正确的
根据报错信息,需要看一下具体是哪个语法有问题。不过根据代码,有一个语法问题:
在`<when>`标签中,使用了`<if>`标签,应该改为`${}`,例如:
```
<when test="query.orderType != null and query.orderType != '' and query.orderType =='03'">
and ORDER_TYPE = #{query.orderType} and (
${query.inStationId != null and query.inStationId != '' ? 'IN_STATION_ID = #{query.inStationId}' : ''}
${query.outStationId != null and query.outStationId != '' ? ' or OUT_STATION_ID = #{query.outStationId}' : ''}
)
</when>
```
这样应该就能解决语法问题了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)