MerchantFundServiceImpl.java 4.82 KB
package com.diligrp.rider.service.impl;

import com.diligrp.rider.common.exception.BizException;
import com.diligrp.rider.mapper.MerchantFundMapper;
import com.diligrp.rider.service.MerchantFundService;
import com.diligrp.rider.vo.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;

@Service
@RequiredArgsConstructor
public class MerchantFundServiceImpl implements MerchantFundService {

    private static final int PAGE_SIZE = 20;
    private static final ZoneId ZONE_ID = ZoneId.of("Asia/Shanghai");

    private final MerchantFundMapper merchantFundMapper;

    @Override
    public PageResultVO<MerchantBillItemVO> merchantBillList(Long cityId, String keyword, String startDate, String endDate, int page) {
        long startTime = parseStartTime(startDate);
        long endTime = parseEndTime(endDate);
        int currentPage = normalizePage(page);
        int offset = (currentPage - 1) * PAGE_SIZE;
        PageResultVO<MerchantBillItemVO> result = new PageResultVO<>();
        result.setList(merchantFundMapper.selectMerchantBills(cityId, normalizeKeyword(keyword), startTime, endTime, offset, PAGE_SIZE));
        result.setPage(currentPage);
        result.setPageSize(PAGE_SIZE);
        result.setTotal(merchantFundMapper.countMerchantBills(cityId, normalizeKeyword(keyword), startTime, endTime));
        return result;
    }

    @Override
    public PageResultVO<MerchantBillDetailItemVO> merchantBillDetail(Long cityId, String outStoreId, String startDate, String endDate, int page) {
        if (outStoreId == null || outStoreId.isBlank()) {
            throw new BizException("店铺编号不能为空");
        }
        long startTime = parseStartTime(startDate);
        long endTime = parseEndTime(endDate);
        int currentPage = normalizePage(page);
        int offset = (currentPage - 1) * PAGE_SIZE;
        PageResultVO<MerchantBillDetailItemVO> result = new PageResultVO<>();
        result.setList(merchantFundMapper.selectMerchantBillDetails(cityId, outStoreId, startTime, endTime, offset, PAGE_SIZE));
        result.setPage(currentPage);
        result.setPageSize(PAGE_SIZE);
        result.setTotal(merchantFundMapper.countMerchantBillDetails(cityId, outStoreId, startTime, endTime));
        return result;
    }

    @Override
    public PageResultVO<RiderBillItemVO> riderBillList(Long cityId, String keyword, String startDate, String endDate, int page) {
        long startTime = parseStartTime(startDate);
        long endTime = parseEndTime(endDate);
        int currentPage = normalizePage(page);
        int offset = (currentPage - 1) * PAGE_SIZE;
        PageResultVO<RiderBillItemVO> result = new PageResultVO<>();
        result.setList(merchantFundMapper.selectRiderBills(cityId, normalizeKeyword(keyword), startTime, endTime, offset, PAGE_SIZE));
        result.setPage(currentPage);
        result.setPageSize(PAGE_SIZE);
        result.setTotal(merchantFundMapper.countRiderBills(cityId, normalizeKeyword(keyword), startTime, endTime));
        return result;
    }

    @Override
    public PageResultVO<RiderBillDetailItemVO> riderBillDetail(Long cityId, Long riderId, String startDate, String endDate, int page) {
        if (riderId == null || riderId < 1) {
            throw new BizException("骑手ID不能为空");
        }
        long startTime = parseStartTime(startDate);
        long endTime = parseEndTime(endDate);
        int currentPage = normalizePage(page);
        int offset = (currentPage - 1) * PAGE_SIZE;
        PageResultVO<RiderBillDetailItemVO> result = new PageResultVO<>();
        result.setList(merchantFundMapper.selectRiderBillDetails(cityId, riderId, startTime, endTime, offset, PAGE_SIZE));
        result.setPage(currentPage);
        result.setPageSize(PAGE_SIZE);
        result.setTotal(merchantFundMapper.countRiderBillDetails(cityId, riderId, startTime, endTime));
        return result;
    }

    private int normalizePage(int page) {
        return Math.max(page, 1);
    }

    private String normalizeKeyword(String keyword) {
        return keyword == null || keyword.isBlank() ? null : keyword.trim();
    }

    private long parseStartTime(String startDate) {
        LocalDate date = parseDate(startDate, "开始日期不能为空");
        return date.atStartOfDay(ZONE_ID).toEpochSecond();
    }

    private long parseEndTime(String endDate) {
        LocalDate date = parseDate(endDate, "结束日期不能为空");
        return date.plusDays(1).atStartOfDay(ZONE_ID).toEpochSecond();
    }

    private LocalDate parseDate(String value, String errorMessage) {
        if (value == null || value.isBlank()) {
            throw new BizException(errorMessage);
        }
        try {
            return LocalDate.parse(value);
        } catch (Exception ex) {
            throw new BizException(errorMessage);
        }
    }
}