refactor(crm order item): 移排序逻辑到后端并简化前端代码
1. 将原前端的分组排序逻辑迁移到后端service实现 2. 后端实现三级排序:交货日期倒序→订单ID升序→创建时间倒序,保证跨页排序一致性和合同组连续排列 3. 移除前端冗余的groupSort方法和排序状态变量,简化前端页面逻辑 4. 更新前端排序提示文案为固定描述
This commit is contained in:
@@ -69,6 +69,8 @@ public class CrmOrderItemServiceImpl implements ICrmOrderItemService {
|
||||
|
||||
/**
|
||||
* 查询正式订单明细列表
|
||||
* 实现逻辑:查出全部匹配记录 → Java内存排序(交货日期倒序→订单ID升序→创建时间倒序)→ 手动分页
|
||||
* 排序跨页生效,同一合同明细连续排列
|
||||
*/
|
||||
@Override
|
||||
public TableDataInfo<CrmOrderItemVo> queryPageList(CrmOrderItemBo bo, PageQuery pageQuery) {
|
||||
@@ -83,9 +85,47 @@ public class CrmOrderItemServiceImpl implements ICrmOrderItemService {
|
||||
if (orderIdScope != null) {
|
||||
lqw.in(CrmOrderItem::getOrderId, orderIdScope);
|
||||
}
|
||||
Page<CrmOrderItemVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||
fillOrderInfoOnItems(result.getRecords());
|
||||
return TableDataInfo.build(result);
|
||||
// 1. 查出全部匹配记录
|
||||
List<CrmOrderItemVo> allItems = baseMapper.selectVoList(lqw);
|
||||
// 2. 填充订单信息(含 deliveryDate)
|
||||
fillOrderInfoOnItems(allItems);
|
||||
// 3. 三级排序:deliveryDate DESC → orderId ASC → createTime DESC
|
||||
allItems.sort((a, b) -> {
|
||||
// 同一合同组内:按创建时间倒序
|
||||
if (Objects.equals(a.getOrderId(), b.getOrderId())) {
|
||||
return compareDate(a.getCreateTime(), b.getCreateTime(), true);
|
||||
}
|
||||
// 不同合同组:按交货日期倒序
|
||||
Date dateA = a.getOrderInfo() != null ? a.getOrderInfo().getDeliveryDate() : null;
|
||||
Date dateB = b.getOrderInfo() != null ? b.getOrderInfo().getDeliveryDate() : null;
|
||||
int cmp = compareDate(dateA, dateB, true);
|
||||
if (cmp != 0) return cmp;
|
||||
// 交货日期相同:按订单ID升序,保证同一合同组连续
|
||||
return Long.compare(a.getOrderId(), b.getOrderId());
|
||||
});
|
||||
// 4. 手动分页
|
||||
int total = allItems.size();
|
||||
int pageNum = ObjectUtil.defaultIfNull(pageQuery.getPageNum(), 1);
|
||||
int pageSize = ObjectUtil.defaultIfNull(pageQuery.getPageSize(), 10);
|
||||
int from = (pageNum - 1) * pageSize;
|
||||
int to = Math.min(from + pageSize, total);
|
||||
List<CrmOrderItemVo> pageItems = from >= total ? Collections.emptyList() : allItems.subList(from, to);
|
||||
|
||||
Page<CrmOrderItemVo> page = new Page<>(pageNum, pageSize, total);
|
||||
page.setRecords(pageItems);
|
||||
return TableDataInfo.build(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个日期,null 值排在末尾
|
||||
* @param desc true=倒序,false=升序
|
||||
*/
|
||||
private int compareDate(Date a, Date b, boolean desc) {
|
||||
if (a == null && b == null) return 0;
|
||||
if (a == null) return 1; // null 排在后面
|
||||
if (b == null) return -1;
|
||||
int cmp = a.compareTo(b);
|
||||
return desc ? -cmp : cmp;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -76,7 +76,7 @@
|
||||
/>
|
||||
<el-button type="primary" size="small" icon="el-icon-search" @click="handleQuery">筛选</el-button>
|
||||
<el-button size="small" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
|
||||
<span v-if="sortInfo" class="sort-hint">{{ sortInfo }}</span>
|
||||
<span class="sort-hint">已按合同交货日期倒序 + 明细创建时间倒序排列</span>
|
||||
</div>
|
||||
|
||||
<!-- 订单明细表格 -->
|
||||
@@ -345,9 +345,7 @@ export default {
|
||||
// 存储原始数据,用于判断是否有修改
|
||||
originalData: {},
|
||||
// 表面处理选项
|
||||
surfaceTreatmentOptions: [],
|
||||
// 排序状态提示
|
||||
sortInfo: ''
|
||||
surfaceTreatmentOptions: []
|
||||
}
|
||||
},
|
||||
created() {
|
||||
@@ -382,7 +380,7 @@ export default {
|
||||
// 批量获取合同信息
|
||||
this.loadContractInfo(orderIds, items)
|
||||
} else {
|
||||
this.orderItemList = this.groupSort(items)
|
||||
this.orderItemList = items
|
||||
this.loading = false
|
||||
}
|
||||
}).catch(e => {
|
||||
@@ -405,8 +403,8 @@ export default {
|
||||
contracts.forEach(contract => {
|
||||
this.contractMap[contract.orderId] = contract
|
||||
})
|
||||
// 合并数据
|
||||
const merged = items.map(item => {
|
||||
// 直接合并数据(后端已按 deliveryDate DESC + createTime DESC 排序)
|
||||
this.orderItemList = items.map(item => {
|
||||
const contract = this.contractMap[item.orderId] || {}
|
||||
return {
|
||||
...item,
|
||||
@@ -417,8 +415,6 @@ export default {
|
||||
deliveryDate: contract.deliveryDate || ''
|
||||
}
|
||||
})
|
||||
// 排序后赋值
|
||||
this.orderItemList = this.groupSort(merged)
|
||||
// 保存原始数据副本
|
||||
this.originalData = {}
|
||||
this.orderItemList.forEach(row => {
|
||||
@@ -426,53 +422,12 @@ export default {
|
||||
})
|
||||
}).catch(e => {
|
||||
console.error(e)
|
||||
this.orderItemList = this.groupSort(items)
|
||||
this.orderItemList = items
|
||||
}).finally(() => {
|
||||
this.loading = false
|
||||
})
|
||||
},
|
||||
|
||||
// 分组排序:组间按交货日期倒序,组内按创建时间倒序
|
||||
groupSort(items) {
|
||||
if (!items || items.length === 0) {
|
||||
this.sortInfo = ''
|
||||
this._groupColorIndex = 0
|
||||
return items
|
||||
}
|
||||
// 构建每个orderId对应的deliveryDate映射
|
||||
const deliveryDateMap = {}
|
||||
items.forEach(item => {
|
||||
if (item.orderId && !deliveryDateMap[item.orderId]) {
|
||||
deliveryDateMap[item.orderId] = item.deliveryDate || ''
|
||||
}
|
||||
})
|
||||
const sorted = [...items].sort((a, b) => {
|
||||
// 同组:按createTime倒序
|
||||
if (a.orderId && a.orderId === b.orderId) {
|
||||
const timeA = a.createTime || ''
|
||||
const timeB = b.createTime || ''
|
||||
if (timeA === timeB) return 0
|
||||
if (!timeA) return 1
|
||||
if (!timeB) return -1
|
||||
return timeB > timeA ? 1 : -1
|
||||
}
|
||||
// 异组:按deliveryDate倒序
|
||||
const dateA = deliveryDateMap[a.orderId] || ''
|
||||
const dateB = deliveryDateMap[b.orderId] || ''
|
||||
if (dateA === dateB) {
|
||||
// 交货日期相同或都为空时,按orderId排序保证分组连续
|
||||
return (a.orderId || 0) - (b.orderId || 0)
|
||||
}
|
||||
if (!dateA) return 1
|
||||
if (!dateB) return -1
|
||||
return dateB > dateA ? 1 : -1
|
||||
})
|
||||
// 设置排序状态提示
|
||||
const groupCount = Object.keys(deliveryDateMap).length
|
||||
this.sortInfo = `已按合同交货日期倒序 + 明细创建时间倒序排列(共${groupCount}个合同组)`
|
||||
return sorted
|
||||
},
|
||||
|
||||
// 分组行样式:同一合同组使用交替背景色
|
||||
groupRowClassName({ row, rowIndex }) {
|
||||
// 记录上一个orderId,切换时翻转颜色
|
||||
|
||||
Reference in New Issue
Block a user