package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.finance.entity.PaymentPlanApprove;
import com.jwipc.finance.logic.PaymentPlanApproveLogic;
import com.jwipc.finance.model.po.PaymentPlanApprovePO;
import com.jwipc.finance.model.query.PpApproveQuery;
import com.jwipc.finance.service.IPaymentPlanApproveService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.dto.CurrentUserDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author 邓京术
 * @date 2025/7/22 17:19
 * @desc
 */
@Service
@RequiredArgsConstructor
public class PaymentPlanApproveLogicImpl implements PaymentPlanApproveLogic {

    private final IPaymentPlanApproveService paymentPlanApproveService;
    /**
     * 添加付款计划审批记录
     *
     * @param ppId           付款计划单ID
     * @param ppNo           付款计划单号
     * @param approveResult  审批结果 0:拒绝 1:同意
     * @param approveOpinion 审批意见
     * @param approveNode    审批节点 1:采购节点 2:财务节点
     * @param user           当前操作人
     * @return 结果
     */
    @Override
    public boolean addApproveRecord(Long ppId, String ppNo, Integer approveResult, String approveOpinion, Integer approveNode, CurrentUserDTO user) {
        PaymentPlanApprove approve = new PaymentPlanApprove();
        approve.setPpId(ppId);
        approve.setPpNo(ppNo);
        approve.setApproveResult(approveResult);
        approve.setApproveOpinion(approveOpinion);
        approve.setApproveNode(approveNode);
        approve.setApproveTime(LocalDateTime.now());
        approve.setApproveUserId(user.getUserId());
        approve.setApproveUserName(user.getName());
        approve.setApproveUserCode(user.getLoginName());
        return paymentPlanApproveService.save(approve);
    }

    @Override
    public PageView findByPage(PpApproveQuery query) {
        final Page<PaymentPlanApprove> dbPage = PageUtil.toPage(query);
        List<PaymentPlanApprovePO> paymentPlanApproves = paymentPlanApproveService.findPageByQuery(dbPage, query);
        return PageUtil.getPV(paymentPlanApproves, dbPage.getTotal());
    }
}
