feat:查询优化

This commit is contained in:
dzc committed 2026-08-09 14:44:53 +08:00
1 parent 2b2969dc92
commit 83fb0dd2a6
3 files changed
+66 -113

No files matched your search

@@ -57,11 +57,25 @@ public interface HardOrderMapper extends PjcloudBaseMapper<HardOrderEntity> {
}
default Page<HardOrderVo> pageJoin(Page page, HardOrderDto dto) {
MPJLambdaWrapper<HardOrderEntity> wrapper = new MPJLambdaWrapper<HardOrderEntity>()
.selectAll(HardOrderEntity.class)
MPJLambdaWrapper<HardOrderEntity> wrapper = new MPJLambdaWrapper<HardOrderEntity>("a")
.selectAll(HardOrderEntity.class, HardOrderEntity::getMerchantType)
.selectAs("coalesce(mch.types, a.merchant_type)", HardOrderVo::getMerchantType)
.selectAs(HardManagerEntity::getName, HardOrderVo::getHardName)
.selectAs(HardManagerEntity::getType, HardOrderVo::getHardType)
.selectAs("u.name", HardOrderVo::getUsername)
.selectAs("u.phone", HardOrderVo::getUserMobile)
.selectAs("u.avatar", HardOrderVo::getUserAvatar)
.selectAs("user_dept.name", HardOrderVo::getUserDeptName)
.selectAs("mch.name", HardOrderVo::getMerchantName)
.selectAs("mch.logo_pic", HardOrderVo::getLogoPic)
.selectAs("w_m.wallet_type_id", HardOrderVo::getWalletTypeId)
.selectAs("w_t.unique_code", HardOrderVo::getWalletTypeUniqueCode)
.leftJoin(HardManagerEntity.class, HardManagerEntity::getId, HardOrderEntity::getHardId)
.leftJoin("pjcloudx.sys_user u ON u.user_id = a.user_id AND u.tenant_id = a.tenant_id AND u.del_flag = '0'")
.leftJoin("pjcloudx.sys_dept user_dept ON user_dept.dept_id = a.user_dept_id AND user_dept.tenant_id = a.tenant_id AND user_dept.del_flag = '0'")
.leftJoin("pjcloudx_wallet.mch_manage mch ON mch.id = a.merchant_id AND mch.tenant_id = a.tenant_id AND mch.del_flag = '0'")
.leftJoin("pjcloudx_wallet.wallet_manage w_m ON w_m.id = a.wallet_id AND w_m.tenant_id = a.tenant_id AND w_m.del_flag = '0'")
.leftJoin("pjcloudx_wallet.wallet_type w_t ON w_t.id = w_m.wallet_type_id AND w_t.tenant_id = a.tenant_id AND w_t.del_flag = '0'")
.eqIfExists(HardOrderEntity::getMerchantId, dto.getMerchantId())
.eqIfExists(HardOrderEntity::getUserId, dto.getUserId())
.eqIfExists(HardOrderEntity::getDeptId, dto.getDeptId())
@@ -179,4 +193,4 @@ public interface HardOrderMapper extends PjcloudBaseMapper<HardOrderEntity> {
List<PanelPorductVo> shopGroupByTimeAndHourType(@Param("dto") PanelProductDto dto);
List<PanelPorductVo> shopGroupByProduct(@Param("dto") PanelProductDto dto);
}
}
@@ -10,9 +10,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bsd.pjcloud.admin.api.dto.UserInfo;
import com.bsd.pjcloud.admin.api.entity.SysDept;
import com.bsd.pjcloud.admin.api.entity.SysUser;
import com.bsd.pjcloud.admin.api.feign.RemoteDeptService;
import com.bsd.pjcloud.admin.api.feign.RemoteUserService;
import com.bsd.pjcloud.admin.api.utils.DataUtil;
import com.bsd.pjcloud.admin.api.vo.UserVO;
@@ -38,7 +36,6 @@ import com.bsd.pjcloud.hard.api.dto.ali.AliMicropayDto;
import com.bsd.pjcloud.hard.api.dto.ali.AliReverseDto;
import com.bsd.pjcloud.hard.api.dto.hard.HandCancelActCodeDto;
import com.bsd.pjcloud.hard.api.dto.hard.HardDoneHardQrCodeScanOrderDto;
import com.bsd.pjcloud.hard.api.dto.manager.HardManagerDto;
import com.bsd.pjcloud.hard.api.dto.hard.HandCancelCodeDto;
import com.bsd.pjcloud.hard.api.dto.hard.HardCreateOrderDto;
import com.bsd.pjcloud.hard.api.dto.order.HardCreateCarDto;
@@ -56,7 +53,6 @@ import com.bsd.pjcloud.hard.api.entity.order.HardCarEntity;
import com.bsd.pjcloud.hard.api.entity.order.HardOrderEntity;
import com.bsd.pjcloud.hard.api.entity.order.HardParentCarEntity;
import com.bsd.pjcloud.hard.api.entity.spu.HardSpuEntity;
import com.bsd.pjcloud.hard.api.utils.HardUtil;
import com.bsd.pjcloud.hard.api.vo.HardDoorFlowVo;
import com.bsd.pjcloud.hard.api.vo.manager.HardManagerVo;
import com.bsd.pjcloud.hard.api.vo.order.HardCarVo;
@@ -64,10 +60,8 @@ import com.bsd.pjcloud.hard.api.vo.order.HardCreateHardQrCodeScanOrderVo;
import com.bsd.pjcloud.hard.api.vo.order.HardOrderVo;
import com.bsd.pjcloud.hard.api.vo.wechat.WechatMicropayVo;
import com.bsd.pjcloud.hard.mapper.HardThirdMiOrderMapper;
import com.bsd.pjcloud.hard.mapper.manager.HardManagerMapper;
import com.bsd.pjcloud.hard.mapper.order.HardOrderMapper;
import com.bsd.pjcloud.hard.service.*;
import com.bsd.pjcloud.hard.utils.RemoteBatchQueryUtil;
import com.bsd.pjcloud.supermarket.api.feign.RemoteMallProductService;
import com.bsd.pjcloud.supermarket.api.feign.RemoteShopSkuService;
import com.bsd.pjcloud.supermarket.api.feign.RemoteSupermarketSpuService;
@@ -107,8 +101,6 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -117,6 +109,7 @@ import java.time.LocalTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* 订单表
@@ -150,7 +143,6 @@ public class HardOrderServiceImpl extends ServiceImpl<HardOrderMapper, HardOrder
private final RemoteWalletTransactionService walletTransactionService;
private final RemoteWalletSettingService walletSettingService;
private final RemoteUserService userService;
private final RemoteDeptService deptService;
private final RemoteMchRuleJoinService mchRuleJoinService;
private final RemoteOnlineCanteenSpuService onlineCanteenSpuService;
@@ -161,7 +153,6 @@ public class HardOrderServiceImpl extends ServiceImpl<HardOrderMapper, HardOrder
private final RemoteActCouponCancelService remoteActCouponCancelService;
private final RemoteMallProductService remoteMallProductService;
private final HardManagerMapper hardManagerMapper;
private final YyxService yyxService;
private final StringRedisTemplate redisTemplate;
@@ -1719,91 +1710,25 @@ public class HardOrderServiceImpl extends ServiceImpl<HardOrderMapper, HardOrder
private void disposeList(List<HardOrderVo> list, HardOrderDto dto) {
if (ObjectUtil.isEmpty(list)) return;
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
// 用户部门
CompletableFuture<Void> deptFuture = CompletableFuture.runAsync(() -> {
RequestContextHolder.setRequestAttributes(requestAttributes);
List<Long> userIds = list.stream().map(HardOrderVo::getUserId).filter(ObjectUtil::isNotEmpty).distinct().toList();
if (ObjectUtil.isNotEmpty(userIds)) {
List<UserVO> userList = userService.getUserDeptByIds(userIds).getData();
if (ObjectUtil.isNotEmpty(userList)) {
Map<Long, UserVO> map = CollectionUtils.convertMap(userList, UserVO::getUserId);
for (HardOrderVo item : list) {
UserVO userVO = map.get(item.getUserId());
if (ObjectUtil.isEmpty(userVO)) continue;
item.setUsername(userVO.getName());
item.setUserMobile(userVO.getPhone());
item.setUserAvatar(userVO.getAvatar());
}
}
}
});
// 当前部门
CompletableFuture<Void> currentDeptFuture = CompletableFuture.runAsync(() -> {
RequestContextHolder.setRequestAttributes(requestAttributes);
List<Long> deptIds = list.stream().map(HardOrderVo::getUserDeptId).filter(ObjectUtil::isNotEmpty).distinct().toList();
if (ObjectUtil.isNotEmpty(deptIds)) {
Map<Long, SysDept> deptList = deptService.getDeptByDeptIds(deptIds).getData();
if (ObjectUtil.isNotEmpty(deptList)) {
for (HardOrderVo item : list) {
SysDept dept = deptList.get(item.getUserDeptId());
if (ObjectUtil.isEmpty(dept)) continue;
item.setUserDeptName(dept.getName());
}
}
}
});
// 商户
CompletableFuture<Void> mchFuture = CompletableFuture.runAsync(() -> {
RequestContextHolder.setRequestAttributes(requestAttributes);
List<Long> mchIds = list.stream().map(HardOrderVo::getMerchantId).filter(ObjectUtil::isNotEmpty).distinct().toList();
if (ObjectUtil.isNotEmpty(mchIds)) {
R<List<MchManageVo>> mchList = remoteMchService.getByIds(mchIds);
if (ObjectUtil.isNotEmpty(mchList.getData())) {
WalletDataUtil.convertMchList(list, CollectionUtils.convertMap(mchList.getData(), MchManageVo::getId));
}
}
});
// 设备
CompletableFuture<Void> deviceFuture = CompletableFuture.runAsync(() -> {
RequestContextHolder.setRequestAttributes(requestAttributes);
List<Long> hardIds = list.stream().map(HardOrderVo::getHardId).filter(ObjectUtil::isNotEmpty).distinct().toList();
if (ObjectUtil.isNotEmpty(hardIds)) {
HardManagerDto hardManagerDto = new HardManagerDto();
hardManagerDto.setIds(hardIds);
List<HardManagerEntity> hardList = hardManagerMapper.list(hardManagerDto);
if (ObjectUtil.isNotEmpty(hardList)) {
HardUtil.convertHardList(list, CollectionUtils.convertMap(hardList, HardManagerEntity::getId));
}
}
});
// 钱包
CompletableFuture<Void> walletFuture = CompletableFuture.runAsync(() -> {
RequestContextHolder.setRequestAttributes(requestAttributes);
List<Long> walletIds = list.stream().map(HardOrderVo::getWalletId).filter(ObjectUtil::isNotEmpty).distinct().toList();
if (ObjectUtil.isNotEmpty(walletIds)) {
List<WalletManageVo> walletList = getWalletListByIds(walletIds);
Map<Long, WalletManageVo> walletMap = CollectionUtils.convertMap(walletList, WalletManageVo::getId);
for (HardOrderVo v : list) {
WalletManageVo target = walletMap.get(v.getWalletId());
if (ObjectUtil.isEmpty(target)) continue;
v.setWalletTypeUniqueCode(target.getWalletTypeUniqueCode());
}
}
});
// 购物车
if (dto.getIsFindCarList()) {
for (HardOrderVo v : list) {
if (ObjectUtil.isEmpty(v.getParentCarId())) continue;
List<HardCarEntity> carList = hardCarService.getByParentCarId(v.getParentCarId());
List<HardCarVo> carVoList = BeanUtils.toBean(carList, HardCarVo.class);
v.setCarList(carVoList);
}
}
CompletableFuture.allOf(deptFuture, mchFuture, deviceFuture, currentDeptFuture, walletFuture).join();
}
if (!Boolean.TRUE.equals(dto.getIsFindCarList())) return;
private List<WalletManageVo> getWalletListByIds(List<Long> walletIds) {
return RemoteBatchQueryUtil.queryByIds(walletIds, ids -> remoteWalletService.getWalletByIds(ids).getData());
List<Long> parentCarIds = list.stream()
.map(HardOrderVo::getParentCarId)
.filter(ObjectUtil::isNotEmpty)
.distinct()
.toList();
if (ObjectUtil.isEmpty(parentCarIds)) return;
List<HardCarVo> carList = BeanUtils.toBean(
hardCarService.list(new LambdaQueryWrapper<HardCarEntity>()
.in(HardCarEntity::getParentCarId, parentCarIds)),
HardCarVo.class
);
Map<Long, List<HardCarVo>> carMap = carList.stream()
.collect(Collectors.groupingBy(HardCarVo::getParentCarId));
for (HardOrderVo order : list) {
if (ObjectUtil.isEmpty(order.getParentCarId())) continue;
order.setCarList(carMap.getOrDefault(order.getParentCarId(), Collections.emptyList()));
}
}
}
@@ -203,7 +203,7 @@
a.user_id,
a.user_dept_id,
a.merchant_id,
a.merchant_type,
coalesce(mch.types, a.merchant_type) as merchant_type,
a.merchant_dept_id,
a.hard_id,
a.dept_id,
@@ -239,11 +239,36 @@
m.name as hardName,
m.type as hardType,
m.dept_id as hardTypeDeptId,
u.name as username,
u.phone as userMobile,
u.avatar as userAvatar,
user_dept.name as userDeptName,
mch.name as merchantName,
mch.logo_pic as logoPic,
s.id as afterSaleId,
s.no as afterSaleNo,
w_m.wallet_type_id as walletTypeId
from (select a.id
w_m.wallet_type_id as walletTypeId,
w_t.unique_code as walletTypeUniqueCode
from hard_order a
left join hard_manager m
on m.id = a.hard_id and m.tenant_id = a.tenant_id and m.del_flag = '0'
left join pjcloudx.sys_user u
on u.user_id = a.user_id and u.tenant_id = a.tenant_id and u.del_flag = '0'
left join pjcloudx.sys_dept user_dept
on user_dept.dept_id = a.user_dept_id and user_dept.tenant_id = a.tenant_id and user_dept.del_flag = '0'
left join pjcloudx_wallet.mch_manage mch
on mch.id = a.merchant_id and mch.tenant_id = a.tenant_id and mch.del_flag = '0'
left join pjcloudx_wallet.wallet_manage w_m
on w_m.id = a.wallet_id and w_m.tenant_id = a.tenant_id and w_m.del_flag = '0'
left join pjcloudx_wallet.wallet_type w_t
on w_t.id = w_m.wallet_type_id and w_t.tenant_id = a.tenant_id and w_t.del_flag = '0'
left join (select tenant_id, order_id, max(id) as id
from hard_after_sale
where del_flag = '0'
group by tenant_id, order_id) sx
on sx.order_id = a.id and sx.tenant_id = a.tenant_id
left join hard_after_sale s
on s.id = sx.id and s.tenant_id = a.tenant_id and s.del_flag = '0'
<where>
<include refid="whereA"/>
<if test="dto.hardTypeCode != null and dto.hardTypeCode != '' ">
@@ -258,17 +283,6 @@
<if test="dto.afterSaleNo != null and dto.afterSaleNo != ''">
and s.no = #{dto.afterSaleNo}
</if>
</where>
order by a.create_time desc, a.user_id desc) t
join hard_order a on a.id = t.id
left join hard_manager m on m.id = a.hard_id
left join pjcloudx_wallet.wallet_manage w_m on w_m.id = a.wallet_id
left join (select order_id, max(id) as id
from hard_after_sale
where tenant_id = 1
group by order_id) sx on sx.order_id = a.id
left join hard_after_sale s on s.id = sx.id
<where>
<if test="dto.walletTypeId != null">
and w_m.wallet_type_id = #{dto.walletTypeId}
</if>
@@ -1086,4 +1100,4 @@
</if>
</select>
</mapper>
</mapper>