package com.czg.service.order.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.order.dto.CreditPaymentRecordDTO;
import com.czg.order.entity.CreditPaymentRecord;
import com.czg.order.param.CreditPaymentRecordQueryParam;
import com.czg.order.service.CreditPaymentRecordService;
import com.czg.service.order.mapper.CreditPaymentRecordMapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 挂账账单付款记录
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-03-04
 */
@Service
public class CreditPaymentRecordServiceImpl extends ServiceImpl<CreditPaymentRecordMapper, CreditPaymentRecord> implements CreditPaymentRecordService {

    private QueryWrapper buildQueryWrapper(CreditPaymentRecordQueryParam param) {
        QueryWrapper queryWrapper = PageUtil.buildSortQueryWrapper();
        if (StrUtil.isNotEmpty(param.getCreditBuyerId())) {
            queryWrapper.eq(CreditPaymentRecord::getCreditBuyerId, param.getCreditBuyerId());
        }
        if (ObjUtil.isNotNull(param.getOrderId())) {
            queryWrapper.eq(CreditPaymentRecord::getOrderId, param.getOrderId());
        }
        if (StrUtil.isNotEmpty(param.getPaymentMethod())) {
            queryWrapper.eq(CreditPaymentRecord::getPaymentMethod, param.getPaymentMethod());
        }
        queryWrapper.orderBy(CreditPaymentRecord::getId, false);
        return queryWrapper;
    }

    @Override
    public Page<CreditPaymentRecordDTO> getCreditPaymentRecordPage(CreditPaymentRecordQueryParam param) {
        QueryWrapper queryWrapper = buildQueryWrapper(param);
        return super.pageAs(PageUtil.buildPage(), queryWrapper, CreditPaymentRecordDTO.class);
    }


}