package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.finance.dto.AccountStatementDTO;
import com.jwipc.finance.entity.AccountStatement;
import com.jwipc.finance.entity.AccountStatementApprove;
import com.jwipc.finance.logic.AccountStatementApproveLogic;
import com.jwipc.finance.model.po.AccountStatementPO;
import com.jwipc.finance.model.query.AsApproveQuery;
import com.jwipc.finance.service.IAccountStatementApproveService;
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;

/**
 * @author shenyang.ou
 * @date 2024/3/4 17:19
 * @desc
 */
@Service
@RequiredArgsConstructor
public class AccountStatementApproveLogicImpl implements AccountStatementApproveLogic {
    private final IAccountStatementApproveService accountStatementApproveService;
    /**
     * 添加对账单审批记录
     *
     * @param asId           对账单ID
     * @param asNo           对账单号
     * @param approveResult  审批结果 0:拒绝 1:同意
     * @param approveOpinion 审批意见
     * @param approveNode    审批节点 1:采购节点 2:财务节点
     * @param user           当前操作人
     * @return 结果
     */
    @Override
    public boolean addApproveRecord(Long asId, String asNo, Integer approveResult, String approveOpinion, Integer approveNode, CurrentUserDTO user) {
        AccountStatementApprove approve = new AccountStatementApprove();
        approve.setAsId(asId);
        approve.setAsNo(asNo);
        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 accountStatementApproveService.save(approve);
    }

    @Override
    public PageView findByPage(AsApproveQuery query) {
        final Page<AccountStatementApprove> dbPage = PageUtil.toPage(query);
        List<AccountStatementApprove> accountStatementApproves = accountStatementApproveService.findPageByQuery(dbPage, query);
        return PageUtil.getPV(accountStatementApproves, dbPage.getTotal());
    }
}
