package com.jwipc.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.finance.entity.CompanyInvoicedApplyPostResult;
import com.jwipc.finance.entity.InvoicedApplyPostResult;
import com.jwipc.finance.mapper.CompanyInvoicedApplyPostResultMapper;
import com.jwipc.finance.model.dto.BudgetReportDTO;
import com.jwipc.finance.model.query.BudgetReportQuery;
import com.jwipc.finance.service.ICompanyInvoicedApplyPostResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 对账模块-公司间销售开票申请过账结果表 服务实现类
 * </p>
 *
 * @author shenyang.ou
 * @since 2025-11-18
 */
@Service
public class CompanyInvoicedApplyPostResultServiceImpl extends ServiceImpl<CompanyInvoicedApplyPostResultMapper, CompanyInvoicedApplyPostResult> implements ICompanyInvoicedApplyPostResultService {
    /**
     * 获取最新的过账结果记录
     *
     * @param iaId 开票申请ID
     * @return 结果
     */
    @Override
    public CompanyInvoicedApplyPostResult findLastPostResult(Long iaId) {
        return baseMapper.findLastPostResult(iaId);
    }

    @Override
    public List<BudgetReportDTO> findPageByQuery(Page<BudgetReportDTO> dbPage, BudgetReportQuery query) {
        if (dbPage==null){
            return baseMapper.findPageByQuery(query);
        }
        return baseMapper.findPageByQuery(dbPage,query);
    }

    @Override
    public void updateNExpireDate(Long iaId, String paymentExpireDate) {
        paymentExpireDate= paymentExpireDate.replace(" ","").replace("00:00:00","");
        paymentExpireDate = paymentExpireDate + "T00:00:00";
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        LocalDateTime dateTime = LocalDateTime.parse(paymentExpireDate, formatter);
        UpdateWrapper<CompanyInvoicedApplyPostResult> updateWrapper = new  UpdateWrapper();
        updateWrapper.lambda().eq(CompanyInvoicedApplyPostResult::getInvoiceApplyId, iaId)
                .set(CompanyInvoicedApplyPostResult::getPaymentExpireDate,dateTime);
        this.update(null,updateWrapper);
    }

}
