package com.jwipc.finance.service.impl;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwipc.finance.entity.PaymentAdvanceApply;
import com.jwipc.finance.entity.PaymentAdvanceApplyDetail;
import com.jwipc.finance.entity.PaymentAdvanceRefundTransferApply;
import com.jwipc.finance.entity.PaymentAdvanceRefundTransferApplyDetail;
import com.jwipc.finance.mapper.PaymentAdvanceRefundTransferApplyMapper;
import com.jwipc.finance.model.po.PaymentAdvanceRefundTransferApplyPO;
import com.jwipc.finance.model.query.PaymentAdvanceRefundTransferApplyHeadQuery;
import com.jwipc.finance.service.IPaymentAdvanceRefundTransferApplyDetailService;
import com.jwipc.finance.service.IPaymentAdvanceRefundTransferApplyService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qzc
 * @since 2025-07-21
 */
@Service
@RequiredArgsConstructor
public class PaymentAdvanceRefundTransferApplyServiceImpl extends ServiceImpl<PaymentAdvanceRefundTransferApplyMapper, PaymentAdvanceRefundTransferApply> implements IPaymentAdvanceRefundTransferApplyService {
    private final IPaymentAdvanceRefundTransferApplyDetailService paymentAdvanceApplyDetailService;
    @Override
    public List<PaymentAdvanceRefundTransferApply> selectListByPage(Page<PaymentAdvanceApply> dbPage, PaymentAdvanceRefundTransferApplyHeadQuery query) {
        if(dbPage == null){
            return baseMapper.selectListByPage(query);
        }
        return baseMapper.selectListByPage(dbPage,query);
    }

    @Override
    public PaymentAdvanceRefundTransferApplyPO selectById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createRefundTransfer(PaymentAdvanceRefundTransferApply headData, List<PaymentAdvanceRefundTransferApplyDetail> refundTransferDetailList) {
        String message = "";
        this.save(headData);//保存头信息
        for (PaymentAdvanceRefundTransferApplyDetail detail : refundTransferDetailList) {
            detail.setPaymentChangeId(headData.getId());
            detail.setPaymentChangeNumber(headData.getPaymentChangeNo());
            if(StringUtils.isBlank(detail.getDataKey())){
                // 数据主键 采购订单号_订单行号
                String dataKey = detail.getAppNo()+"_"+detail.getAppItemNo();
                detail.setDataKey(dataKey);
            }
        }

        if (CollectionUtils.isNotEmpty(refundTransferDetailList)) {
            paymentAdvanceApplyDetailService.saveBatch(refundTransferDetailList);
        }
        // 更新明细行号
        paymentAdvanceApplyDetailService.updateDetailItemNo(headData.getId());
        return message;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editRefundTransfer(PaymentAdvanceRefundTransferApply headData, List<PaymentAdvanceRefundTransferApplyDetail> refundTransferDetailList) {
        this.updateById(headData);
        if (CollectionUtils.isNotEmpty(refundTransferDetailList)) {
            paymentAdvanceApplyDetailService.remove(Wrappers.lambdaQuery(PaymentAdvanceRefundTransferApplyDetail.class).eq(PaymentAdvanceRefundTransferApplyDetail::getPaymentChangeId, headData.getId()));
            paymentAdvanceApplyDetailService.saveBatch(refundTransferDetailList);
        }
        // 更新明细行号
        paymentAdvanceApplyDetailService.updateDetailItemNo(headData.getId());

        return "";
    }

    @Override
    public List<PaymentAdvanceRefundTransferApply> findDataStatusByIdList(List<Long> applyIdList) {
        return baseMapper.findDataStatusByIdList(applyIdList);
    }


}
