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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.BusinessException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.entity.ValidatorEntity;
import com.sunxung.factoring.entity.financing.review.ReviewBasicContract;
import com.sunxung.factoring.entity.financing.review.ReviewBasicContractChangeContent;
import com.sunxung.factoring.entity.financing.review.ReviewQuotaDisburse;
import com.sunxung.factoring.entity.tradingPlatform.Platform;
import com.sunxung.factoring.mapper.financing.review.ReviewBasicContractMapper;
import com.sunxung.factoring.service.financing.review.ReviewBasicContractChangeContentService;
import com.sunxung.factoring.service.financing.review.ReviewBasicContractService;
import com.sunxung.factoring.service.tradingPlatform.PlatformService;
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:42
 */

@Service
public class ReviewBasicContractServiceImpl extends ServiceImpl<ReviewBasicContractMapper, ReviewBasicContract>
        implements ReviewBasicContractService {

    @Autowired
    private PlatformService platformService;
    @Autowired
    private ReviewBasicContractChangeContentService reviewBasicContractChangeContentService;

    @Override
    public ReviewBasicContract addReviewBasicContract(ReviewBasicContract reviewBasicContract,
                                                      ReviewQuotaDisburse quotaDisburse) {
        if (quotaDisburse.isVisible()) {
            ValidatorUtil.validator(ValidatorEntity.ValidatorType.ADD, reviewBasicContract);
        } else {
            ValidatorUtil.validator(ValidatorEntity.ValidatorType.TEMP, reviewBasicContract);
        }
        // 该额度支用下：三方协议中不能有相同的债务人企业
        ReviewBasicContract basicContract = baseMapper.selectOne(new QueryWrapper<ReviewBasicContract>()
                .eq("review_quota_disburse_id", quotaDisburse.getId())
                .eq("debtor_company_id", reviewBasicContract.getDebtorCompanyId()).last("limit 1"));

        if (null != basicContract && null != basicContract.getId()) {
            throw new BusinessException(CodeUtil.FAIL,
                    "三方协议信息中已经存在“" + reviewBasicContract.getDebtorCompanyName() + "”债务人，请重新选择");
        }
        //根据债务人ID，填充债务人统一社会信用码
        Platform platform = platformService.getById(reviewBasicContract.getDebtorCompanyId());
        if (null != platform) {
            reviewBasicContract.setDebtorSocialCreditCode(platform.getSocialCreditCode());
        }
        baseMapper.insert(reviewBasicContract);
        List<ReviewBasicContractChangeContent> list = reviewBasicContract.getContractContentList();
        if (null != list && list.size() > 0) {
            for (ReviewBasicContractChangeContent changeContent : list) {
                changeContent.setReviewBasicContractId(reviewBasicContract.getId());
                if (quotaDisburse.isVisible()) {
                    ValidatorUtil.validator(ValidatorEntity.ValidatorType.ADD, changeContent);
                } else {
                    ValidatorUtil.validator(ValidatorEntity.ValidatorType.TEMP, changeContent);
                }
                reviewBasicContractChangeContentService.save(changeContent);
            }
        }
        return reviewBasicContract;
    }
}
