package com.inkasso.factoring.purchManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.inkasso.adapter.exception.BusinessException;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.factoring.common.constant.ConstantUtil;
import com.inkasso.factoring.creditmanagement.entity.ProjectInformation;
import com.inkasso.factoring.creditmanagement.service.IProjectInformationService;
import com.inkasso.factoring.purchManagement.entity.CPurchContractBasicInfo;
import com.inkasso.factoring.purchManagement.dao.CPurchContractBasicInfoMapper;
import com.inkasso.factoring.purchManagement.entity.CPurchContractManagement;
import com.inkasso.factoring.purchManagement.entity.CPurchContractRetentionInfo;
import com.inkasso.factoring.purchManagement.service.ICPurchContractBasicInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.factoring.purchManagement.service.ICPurchContractManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 采购合同-基本信息表 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2023-12-02
 */
@Service
public class CPurchContractBasicInfoServiceImpl extends ServiceImpl<CPurchContractBasicInfoMapper, CPurchContractBasicInfo> implements ICPurchContractBasicInfoService {
    @Autowired
    private IProjectInformationService projectInformationService;
    @Autowired
    private ICPurchContractManagementService icPurchContractManagementService;

    @Autowired
    private ICPurchContractBasicInfoService icPurchContractBasicInfoService;

    @Override
    public CPurchContractBasicInfo findByBusinessNumberAndSupplierId(String businessNumber, Long supplierId) {
        ProjectInformation projectInformation = projectInformationService.getOne(new QueryWrapper<ProjectInformation>()
                .eq("business_number",businessNumber));
        CPurchContractManagement purchContractManagement = icPurchContractManagementService.getOne(
                new QueryWrapper<CPurchContractManagement>()
                        .eq("business_number", businessNumber)
                        .eq("c_supplier_info_id", supplierId));
        if (purchContractManagement == null || purchContractManagement.getId() == null) {
            throw new BusinessException(CodeUtil.FAIL, "采购合同管理不能为空");
        }
        CPurchContractBasicInfo purchContractBasicInfo = getLatestBasicInfo(purchContractManagement.getId(), ConstantUtil.PASS);
        if (purchContractBasicInfo == null || purchContractBasicInfo.getId() == null) {
            throw new BusinessException(CodeUtil.FAIL, "采购合同基础信息不能为空");
        }
        purchContractBasicInfo.setProjectInformation(projectInformation);
        return purchContractBasicInfo;
    }

    @Override
    public CPurchContractBasicInfo getLatestBasicInfo(Long purchContractManageId, String conclusion) {
        return baseMapper.getLatestBasicInfo(purchContractManageId, conclusion);
    }

}
