package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.finance.convert.InvoicedApplyApproveConvert;
import com.jwipc.finance.dto.AccountStatementDTO;
import com.jwipc.finance.dto.InvoicedApplyApproveDTO;
import com.jwipc.finance.entity.AccountStatement;
import com.jwipc.finance.entity.InvoicedApplyApprove;
import com.jwipc.finance.logic.InvoicedApplyApproveLogic;
import com.jwipc.finance.model.po.AccountStatementPO;
import com.jwipc.finance.model.po.InvoicedApplyApprovePO;
import com.jwipc.finance.model.query.IaApproveQuery;
import com.jwipc.finance.service.IInvoicedApplyApproveService;
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.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author shenyang.ou
 * @date 2024/3/6 11:07
 * @desc
 */
@Service
@RequiredArgsConstructor
public class InvoicedApplyApproveLogicImpl implements InvoicedApplyApproveLogic {
    private final IInvoicedApplyApproveService invoicedApplyApproveService;
    private final InvoicedApplyApproveConvert invoicedApplyApproveConvert;
    @Override
    public PageView selectDataByQuery(IaApproveQuery query) {
        final Page<AccountStatement> dbPage = PageUtil.toPage(query);
        List<InvoicedApplyApprovePO> invoicedApplyApprovePOS = invoicedApplyApproveService.selectDataByQuery(dbPage, query);
        List<InvoicedApplyApproveDTO> invoicedApplyApproveDTOS = invoicedApplyApproveConvert.po2PageListDTO(invoicedApplyApprovePOS);
        return PageUtil.getPV(invoicedApplyApproveDTOS, Objects.requireNonNull(dbPage).getTotal());
    }

    @Override
    public List<InvoicedApplyApproveDTO> selectDataListByQuery(IaApproveQuery query) {
        List<InvoicedApplyApprovePO> invoicedApplyApprovePOS = invoicedApplyApproveService.selectDataByQuery(null, query);
        return invoicedApplyApproveConvert.po2PageListDTO(invoicedApplyApprovePOS);
    }

    /**
     * 添加开票申请单审批记录
     *
     * @param iaId           开票申请单ID
     * @param iaNo           开票申请单号
     * @param approveResult  审批结果 0:拒绝 1:同意
     * @param approveOpinion 审批意见
     * @param approveNode    审批节点 1:采购节点 2:财务节点
     * @param user           当前操作人
     * @return 结果
     */
    @Override
    public boolean addApproveRecord(Long iaId, String iaNo, Integer approveResult, String approveOpinion, Integer approveNode, CurrentUserDTO user) {
        InvoicedApplyApprove approve = new InvoicedApplyApprove();
        approve.setIaId(iaId);
        approve.setIaNo(iaNo);
        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 invoicedApplyApproveService.save(approve);
    }

}
