package com.sunxung.factoring.service.financing.review.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.entity.ValidatorEntity;
import com.sunxung.factoring.entity.financing.review.ReviewMasterContract;
import com.sunxung.factoring.entity.financing.review.ReviewMasterContractDebtor;
import com.sunxung.factoring.mapper.financing.review.ReviewMasterContractDebtorMapper;
import com.sunxung.factoring.mapper.financing.review.ReviewMasterContractMapper;
import com.sunxung.factoring.service.financing.review.ReviewMasterContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description :
 * @Author : L.Jia
 * @Date: 2023-12-15 19:51
 */
@Service
public class ReviewMasterContractServiceImpl extends ServiceImpl<ReviewMasterContractMapper, ReviewMasterContract>
        implements ReviewMasterContractService {

    @Autowired
    private ReviewMasterContractDebtorMapper debtorMapper;

    @Override
    public void addMasterContract(ReviewMasterContract contract, boolean visible) {
        if (visible) {
            ValidatorUtil.validator(ValidatorEntity.ValidatorType.ADD, contract);
        } else {
            ValidatorUtil.validator(ValidatorEntity.ValidatorType.TEMP, contract);
        }
        save(contract);
        // 新增债务人信息
        addDebtor(contract);
    }

    /**
     * 新增债务人的信息
     *
     * @param contract
     */
    private void addDebtor(ReviewMasterContract contract) {
        if (null != contract && null != contract.getDebtorList() && contract.getDebtorList().size() > 0) {
            List<ReviewMasterContractDebtor> debtorList = contract.getDebtorList();
            for (ReviewMasterContractDebtor debtor : debtorList) {
                debtor.setReviewMasterContractId(contract.getId());
                ValidatorUtil.validator(ValidatorEntity.ValidatorType.ADD, debtor);
                debtorMapper.insert(debtor);
            }
        }
    }
}
