|
|
@@ -0,0 +1,275 @@
|
|
|
+package shop.alien.lawyer.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import shop.alien.entity.result.R;
|
|
|
+import shop.alien.entity.store.LawFirm;
|
|
|
+import shop.alien.entity.store.vo.LawFirmReconciliationVO;
|
|
|
+import shop.alien.entity.store.vo.LawyerConsultationOrderVO;
|
|
|
+import shop.alien.entity.store.vo.LawyerReconciliationDetailVO;
|
|
|
+import shop.alien.lawyer.service.LawFirmReconciliationService;
|
|
|
+import shop.alien.lawyer.service.LawFirmService;
|
|
|
+import shop.alien.mapper.LawFirmMapper;
|
|
|
+import shop.alien.mapper.LawyerConsultationOrderMapper;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 律所对账结算服务实现类
|
|
|
+ *
|
|
|
+ * @author system
|
|
|
+ * @since 2025-01-XX
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Transactional
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class LawFirmReconciliationServiceImpl implements LawFirmReconciliationService {
|
|
|
+
|
|
|
+ private final LawFirmMapper lawFirmMapper;
|
|
|
+ private final LawFirmService lawFirmService;
|
|
|
+ private final LawyerConsultationOrderMapper consultationOrderMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<LawFirmReconciliationVO> getLawFirmReconciliation(
|
|
|
+ Integer firmId,
|
|
|
+ String lawyerName,
|
|
|
+ Date startDate,
|
|
|
+ Date endDate,
|
|
|
+ Integer pageNum,
|
|
|
+ Integer pageSize) {
|
|
|
+ log.info("LawFirmReconciliationServiceImpl.getLawFirmReconciliation?firmId={},lawyerName={},startDate={},endDate={},pageNum={},pageSize={}",
|
|
|
+ firmId, lawyerName, startDate, endDate, pageNum, pageSize);
|
|
|
+
|
|
|
+ if (firmId == null) {
|
|
|
+ return R.fail("律所ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取律所信息
|
|
|
+ LawFirm lawFirm = lawFirmService.getById(firmId);
|
|
|
+ if (lawFirm == null || (lawFirm.getDeleteFlag() != null && lawFirm.getDeleteFlag() == 1)) {
|
|
|
+ return R.fail("律所不存在或已被删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询统计信息
|
|
|
+ java.util.Map<String, Object> statistics = lawFirmMapper.getLawFirmReconciliationStatistics(firmId, startDate, endDate);
|
|
|
+ Long totalOrderCount = statistics != null && statistics.get("order_count") != null
|
|
|
+ ? ((Number) statistics.get("order_count")).longValue() : 0L;
|
|
|
+ Long totalOrderAmount = statistics != null && statistics.get("total_amount") != null
|
|
|
+ ? ((Number) statistics.get("total_amount")).longValue() : 0L;
|
|
|
+
|
|
|
+ // 获取平台佣金比例
|
|
|
+ Float commissionRatio = lawFirm.getPlatformCommissionRatio();
|
|
|
+ if (commissionRatio == null) {
|
|
|
+ commissionRatio = 5.0f; // 默认5%
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算平台信息服务费
|
|
|
+ Long platformServiceFee = Math.round(totalOrderAmount * commissionRatio / 100.0);
|
|
|
+
|
|
|
+ // 构建返回对象
|
|
|
+ LawFirmReconciliationVO vo = new LawFirmReconciliationVO();
|
|
|
+ vo.setFirmId(firmId);
|
|
|
+ vo.setFirmName(lawFirm.getFirmName());
|
|
|
+ vo.setTotalOrderCount(totalOrderCount);
|
|
|
+ vo.setTotalOrderAmount(totalOrderAmount);
|
|
|
+ vo.setTotalOrderAmountYuan(convertFenToYuan(totalOrderAmount));
|
|
|
+ vo.setPlatformServiceFee(platformServiceFee);
|
|
|
+ vo.setPlatformServiceFeeYuan(convertFenToYuan(platformServiceFee));
|
|
|
+ vo.setPlatformCommissionRatio(commissionRatio);
|
|
|
+
|
|
|
+ // 查询律师明细(分页)
|
|
|
+ List<LawyerReconciliationDetailVO> lawyerDetails = lawFirmMapper.getLawyerReconciliationDetails(
|
|
|
+ firmId, lawyerName, startDate, endDate);
|
|
|
+
|
|
|
+ // 手动分页
|
|
|
+ if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
|
|
|
+ int start = (pageNum - 1) * pageSize;
|
|
|
+ int end = Math.min(start + pageSize, lawyerDetails.size());
|
|
|
+ if (start < lawyerDetails.size()) {
|
|
|
+ lawyerDetails = lawyerDetails.subList(start, end);
|
|
|
+ } else {
|
|
|
+ lawyerDetails = Collections.emptyList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换金额单位并计算平台服务费
|
|
|
+ for (LawyerReconciliationDetailVO detail : lawyerDetails) {
|
|
|
+ detail.setOrderAmountYuan(convertFenToYuan(detail.getOrderAmount()));
|
|
|
+ // 计算平台服务费
|
|
|
+ Long platformFee = Math.round(detail.getOrderAmount() * commissionRatio / 100.0);
|
|
|
+ detail.setPlatformServiceFee(platformFee);
|
|
|
+ detail.setPlatformServiceFeeYuan(convertFenToYuan(platformFee));
|
|
|
+ }
|
|
|
+
|
|
|
+ vo.setLawyerDetails(lawyerDetails);
|
|
|
+
|
|
|
+ return R.data(vo, "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<IPage<LawyerReconciliationDetailVO>> getLawyerReconciliationDetails(
|
|
|
+ Integer firmId,
|
|
|
+ String lawyerName,
|
|
|
+ Date startDate,
|
|
|
+ Date endDate,
|
|
|
+ Integer pageNum,
|
|
|
+ Integer pageSize) {
|
|
|
+ log.info("LawFirmReconciliationServiceImpl.getLawyerReconciliationDetails?firmId={},lawyerName={},startDate={},endDate={},pageNum={},pageSize={}",
|
|
|
+ firmId, lawyerName, startDate, endDate, pageNum, pageSize);
|
|
|
+
|
|
|
+ if (firmId == null) {
|
|
|
+ return R.fail("律所ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取律所信息以获取平台佣金比例
|
|
|
+ LawFirm lawFirm = lawFirmService.getById(firmId);
|
|
|
+ Float commissionRatio = lawFirm != null && lawFirm.getPlatformCommissionRatio() != null
|
|
|
+ ? lawFirm.getPlatformCommissionRatio() : 5.0f; // 默认5%
|
|
|
+
|
|
|
+ // 查询律师明细
|
|
|
+ List<LawyerReconciliationDetailVO> lawyerDetails = lawFirmMapper.getLawyerReconciliationDetails(
|
|
|
+ firmId, lawyerName, startDate, endDate);
|
|
|
+
|
|
|
+ // 转换金额单位并计算平台服务费
|
|
|
+ for (LawyerReconciliationDetailVO detail : lawyerDetails) {
|
|
|
+ detail.setOrderAmountYuan(convertFenToYuan(detail.getOrderAmount()));
|
|
|
+ // 计算平台服务费
|
|
|
+ Long platformFee = Math.round(detail.getOrderAmount() * commissionRatio / 100.0);
|
|
|
+ detail.setPlatformServiceFee(platformFee);
|
|
|
+ detail.setPlatformServiceFeeYuan(convertFenToYuan(platformFee));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 手动分页
|
|
|
+ Page<LawyerReconciliationDetailVO> page = new Page<>();
|
|
|
+ if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
|
|
|
+ page.setCurrent(pageNum);
|
|
|
+ page.setSize(pageSize);
|
|
|
+ page.setTotal(lawyerDetails.size());
|
|
|
+
|
|
|
+ int start = (pageNum - 1) * pageSize;
|
|
|
+ int end = Math.min(start + pageSize, lawyerDetails.size());
|
|
|
+ if (start < lawyerDetails.size()) {
|
|
|
+ page.setRecords(lawyerDetails.subList(start, end));
|
|
|
+ } else {
|
|
|
+ page.setRecords(Collections.emptyList());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ page.setCurrent(1);
|
|
|
+ page.setSize(lawyerDetails.size());
|
|
|
+ page.setTotal(lawyerDetails.size());
|
|
|
+ page.setRecords(lawyerDetails);
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.data(page, "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<IPage<LawyerConsultationOrderVO>> getOrderList(
|
|
|
+ Integer firmId,
|
|
|
+ Integer lawyerId,
|
|
|
+ String lawyerName,
|
|
|
+ String orderNumber,
|
|
|
+ Integer orderStatus,
|
|
|
+ Date startDate,
|
|
|
+ Date endDate,
|
|
|
+ Integer pageNum,
|
|
|
+ Integer pageSize) {
|
|
|
+ log.info("LawFirmReconciliationServiceImpl.getOrderList?firmId={},lawyerId={},lawyerName={},orderNumber={},orderStatus={},startDate={},endDate={},pageNum={},pageSize={}",
|
|
|
+ firmId, lawyerId, lawyerName, orderNumber, orderStatus, startDate, endDate, pageNum, pageSize);
|
|
|
+
|
|
|
+ if (firmId == null) {
|
|
|
+ return R.fail("律所ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证律所是否存在
|
|
|
+ LawFirm lawFirm = lawFirmService.getById(firmId);
|
|
|
+ if (lawFirm == null || (lawFirm.getDeleteFlag() != null && lawFirm.getDeleteFlag() == 1)) {
|
|
|
+ return R.fail("律所不存在或已被删除");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果指定了律师ID,直接使用;否则查询律所下的所有律师ID
|
|
|
+ List<Integer> lawyerUserIds = null;
|
|
|
+ if (lawyerId != null) {
|
|
|
+ lawyerUserIds = Collections.singletonList(lawyerId);
|
|
|
+ } else {
|
|
|
+ // 查询律所下的所有律师ID
|
|
|
+ lawyerUserIds = lawFirmMapper.getLawyerIdsByFirmId(firmId, lawyerName);
|
|
|
+ if (lawyerUserIds == null || lawyerUserIds.isEmpty()) {
|
|
|
+ // 如果没有找到律师,返回空结果
|
|
|
+ Page<LawyerConsultationOrderVO> emptyPage = new Page<>(pageNum != null && pageNum > 0 ? pageNum : 1,
|
|
|
+ pageSize != null && pageSize > 0 ? pageSize : 10);
|
|
|
+ emptyPage.setRecords(Collections.emptyList());
|
|
|
+ emptyPage.setTotal(0);
|
|
|
+ return R.data(emptyPage, "未找到相关订单");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建分页对象
|
|
|
+ int pageNumValue = pageNum != null && pageNum > 0 ? pageNum : 1;
|
|
|
+ int pageSizeValue = pageSize != null && pageSize > 0 ? pageSize : 10;
|
|
|
+ Page<LawyerConsultationOrderVO> page = new Page<>(pageNumValue, pageSizeValue);
|
|
|
+
|
|
|
+ // 如果按律师姓名搜索但已经通过lawyerUserIds过滤,则不再传递lawyerName
|
|
|
+ String searchLawyerName = (lawyerId != null || lawyerName == null) ? null : lawyerName;
|
|
|
+
|
|
|
+ // 使用现有的订单查询方法
|
|
|
+ IPage<LawyerConsultationOrderVO> voPage = consultationOrderMapper.getConsultationOrderListWithLawyer(
|
|
|
+ page, orderNumber, null, null, searchLawyerName, orderStatus, lawyerUserIds);
|
|
|
+
|
|
|
+ // 如果指定了日期范围,需要手动过滤
|
|
|
+ if ((startDate != null || endDate != null) && voPage != null && voPage.getRecords() != null) {
|
|
|
+ List<LawyerConsultationOrderVO> filteredRecords = voPage.getRecords().stream()
|
|
|
+ .filter(order -> {
|
|
|
+ if (order.getOrderTime() == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Date orderDate = order.getOrderTime();
|
|
|
+ if (startDate != null && orderDate.before(startDate)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (endDate != null && orderDate.after(endDate)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ })
|
|
|
+ .collect(java.util.stream.Collectors.toList());
|
|
|
+
|
|
|
+ // 重新计算分页
|
|
|
+ long total = filteredRecords.size();
|
|
|
+ int start = (pageNumValue - 1) * pageSizeValue;
|
|
|
+ int end = Math.min(start + pageSizeValue, filteredRecords.size());
|
|
|
+ List<LawyerConsultationOrderVO> pagedRecords = start < filteredRecords.size()
|
|
|
+ ? filteredRecords.subList(start, end)
|
|
|
+ : Collections.emptyList();
|
|
|
+
|
|
|
+ voPage.setRecords(pagedRecords);
|
|
|
+ voPage.setTotal(total);
|
|
|
+ }
|
|
|
+
|
|
|
+ return R.data(voPage, "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将分转换为元(字符串格式)
|
|
|
+ *
|
|
|
+ * @param fen 分
|
|
|
+ * @return 元(字符串格式,保留2位小数)
|
|
|
+ */
|
|
|
+ private String convertFenToYuan(Long fen) {
|
|
|
+ if (fen == null || fen == 0) {
|
|
|
+ return "0.00";
|
|
|
+ }
|
|
|
+ BigDecimal yuan = BigDecimal.valueOf(fen).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
|
|
|
+ return yuan.toString();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|