package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.finance.convert.CompanyInvoicedApplyApproveConvert;
import com.jwipc.finance.dto.CompanyInvoicedApplyApproveDTO;
import com.jwipc.finance.entity.CompanyAccountStatement;
import com.jwipc.finance.entity.CompanyInvoicedApplyApprove;
import com.jwipc.finance.logic.CompanyInvoicedApplyApproveLogic;
import com.jwipc.finance.model.po.CompanyInvoicedApplyApprovePO;
import com.jwipc.finance.model.query.CompanyIaApproveQuery;
import com.jwipc.finance.model.query.CostIaApproveQuery;
import com.jwipc.finance.service.ICompanyInvoicedApplyApproveService;
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;
import java.util.Objects;

/**
 * @author shenyang.ou
 * @date 2024/3/6 11:07
 * @desc
 */
@Service
@RequiredArgsConstructor
public class CompanyInvoicedApplyApproveLogicImpl implements CompanyInvoicedApplyApproveLogic {
    private final ICompanyInvoicedApplyApproveService invoicedApplyApproveService;
    private final CompanyInvoicedApplyApproveConvert invoicedApplyApproveConvert;
    @Override
    public PageView selectDataByQuery(CompanyIaApproveQuery query) {
        final Page<CompanyAccountStatement> dbPage = PageUtil.toPage(query);
        List<CompanyInvoicedApplyApprovePO> invoicedApplyApprovePOS = invoicedApplyApproveService.selectDataByQuery(dbPage, query);
        List<CompanyInvoicedApplyApproveDTO> invoicedApplyApproveDTOS = invoicedApplyApproveConvert.po2PageListDTO(invoicedApplyApprovePOS);
        return PageUtil.getPV(invoicedApplyApproveDTOS, Objects.requireNonNull(dbPage).getTotal());
    }

    @Override
    public List<CompanyInvoicedApplyApproveDTO> selectDataListByQuery(CompanyIaApproveQuery query) {
        List<CompanyInvoicedApplyApprovePO> 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) {
        CompanyInvoicedApplyApprove approve = new CompanyInvoicedApplyApprove();
        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);
    }

}
