package shop.alien.lawyer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.LawyerPaymentTransaction;
import shop.alien.lawyer.service.LawyerPaymentTransactionService;
import shop.alien.mapper.LawyerPaymentTransactionMapper;

/**
 * 支付交易 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class LawyerPaymentTransactionServiceImpl extends ServiceImpl<LawyerPaymentTransactionMapper, LawyerPaymentTransaction> implements LawyerPaymentTransactionService {

    private final LawyerPaymentTransactionMapper paymentTransactionMapper;

    @Override
    public R<IPage<LawyerPaymentTransaction>> getPaymentTransactionList(int pageNum, int pageSize, Integer consultationOrderId,
                                                                        Integer clientUserId, Integer transactionStatus) {
        log.info("LawyerPaymentTransactionServiceImpl.getPaymentTransactionList?pageNum={},pageSize={},consultationOrderId={},clientUserId={},transactionStatus={}",
                pageNum, pageSize, consultationOrderId, clientUserId, transactionStatus);
        Page<LawyerPaymentTransaction> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<LawyerPaymentTransaction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LawyerPaymentTransaction::getDeleteFlag, 0);
        if (consultationOrderId != null) {
            queryWrapper.eq(LawyerPaymentTransaction::getConsultationOrderId, consultationOrderId);
        }
        if (clientUserId != null) {
            queryWrapper.eq(LawyerPaymentTransaction::getClientUserId, clientUserId);
        }
        if (transactionStatus != null) {
            queryWrapper.eq(LawyerPaymentTransaction::getTransactionStatus, transactionStatus);
        }
        queryWrapper.orderByDesc(LawyerPaymentTransaction::getTransactionTime);
        IPage<LawyerPaymentTransaction> pageResult = this.page(page, queryWrapper);
        return R.data(pageResult);
    }

    @Override
    public R<LawyerPaymentTransaction> addPaymentTransaction(LawyerPaymentTransaction paymentTransaction) {
        log.info("LawyerPaymentTransactionServiceImpl.addPaymentTransaction?paymentTransaction={}", paymentTransaction);
        boolean result = this.save(paymentTransaction);
        if (result) {
            return R.data(paymentTransaction);
        }
        return R.fail("新增失败");
    }

    @Override
    public R<LawyerPaymentTransaction> editPaymentTransaction(LawyerPaymentTransaction paymentTransaction) {
        log.info("LawyerPaymentTransactionServiceImpl.editPaymentTransaction?paymentTransaction={}", paymentTransaction);
        boolean result = this.updateById(paymentTransaction);
        if (result) {
            return R.data(paymentTransaction);
        }
        return R.fail("修改失败");
    }

    @Override
    public R<Boolean> deletePaymentTransaction(Integer id) {
        log.info("LawyerPaymentTransactionServiceImpl.deletePaymentTransaction?id={}", id);
        boolean result = this.removeById(id);
        if (result) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }
}

