package com.yn.bftl.thirdparty.modules.configuration.service.impl;

import com.yn.bftl.common.modules.auth.entity.User;
import com.yn.bftl.common.modules.company.entity.Company;
import com.yn.bftl.common.modules.company.enums.RelationType;
import com.yn.bftl.common.modules.configuration.entity.ThirdPartyPaySet;
import com.yn.bftl.common.modules.relation.entity.CompanyRelation;
import com.yn.bftl.thirdparty.common.repository.CompanyRelationRepository;
import com.yn.bftl.thirdparty.common.service.BaseMemberService;
import com.yn.bftl.thirdparty.modules.business.util.ThirdPartyConfigUtil;
import com.yn.bftl.thirdparty.modules.configuration.service.ThirdPartyPaySetService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author：Vance
 * @Description：第三方配置service实现
 * @Date：2023/6/26 14:11
 */
@Service
@RequiredArgsConstructor
public class ThirdPartyPaySetServiceImpl extends BaseMemberService implements ThirdPartyPaySetService {

    private final ThirdPartyConfigUtil thirdPartyConfigUtil;
    private final CompanyRelationRepository companyRelationRepository;

    /**
     * 获取登录企业对应的上传第三方配置
     * 1.如果登录的账号是B端销售则取企业对应的配置，如果登录的账号有供销关系则取关系的甲方企业配置
     * 2.如果未获取到配置，或者配置所有值为空，则取平台的配置
     * 3.如果未获取到平台配置，或者配置所有值为空，则返回null，最终使用配置文件中的配置
     *
     * @return
     */
    @Override
    public ThirdPartyPaySet getUploadThirdPartyPaySet() {
        User user = getUser();
        Company company = user.getCompany();
        if (company == null) {
            return getPlatformUploadThirdPartyPaySet();
        }
        ThirdPartyPaySet thirdPartyPaySet = thirdPartyConfigUtil.getCompany(company.getId());
        if (thirdPartyPaySet != null) {
            if (isNotConfigureUpload(thirdPartyPaySet)) {
                return getPlatformUploadThirdPartyPaySet();
            }
            return thirdPartyPaySet;
        }
        CompanyRelation companyRelation = companyRelationRepository.findFirstByPartyBAndType(company, RelationType.SUPPLYMARKETING);
        if (companyRelation == null) {
            return getPlatformUploadThirdPartyPaySet();
        }
        thirdPartyPaySet = thirdPartyConfigUtil.getCompany(companyRelation.getPartyA().getId());
        if (thirdPartyPaySet != null) {
            if (isNotConfigureUpload(thirdPartyPaySet)) {
                return getPlatformUploadThirdPartyPaySet();
            }
            return thirdPartyPaySet;
        } else {
            return getPlatformUploadThirdPartyPaySet();
        }
    }

    @Override
    public ThirdPartyPaySet getThirdPartyPaySetByCompany() {
        Company company = getUser().getCompany();
        if (company == null) {
            return null;
        }
        // 获取当前企业第三方配置
        ThirdPartyPaySet thirdPartyPaySet = thirdPartyConfigUtil.getCompany(company.getId());
        if (thirdPartyPaySet != null) {
            return thirdPartyPaySet;
        }
        // 获取供方企业第三方配置
        CompanyRelation companyRelation = companyRelationRepository.findFirstByPartyBAndType(company, RelationType.SUPPLYMARKETING);
        if (companyRelation == null) {
            return null;
        }
        return thirdPartyConfigUtil.getCompany(companyRelation.getPartyA().getId());
    }

    /**
     * 获取平台对应的上传第三方配置
     *
     * @return
     */
    private ThirdPartyPaySet getPlatformUploadThirdPartyPaySet() {
        ThirdPartyPaySet thirdPartyPaySet = thirdPartyConfigUtil.getPlatform();
        if (thirdPartyPaySet == null) {
            return null;
        }
        if (isNotConfigureUpload(thirdPartyPaySet)) {
            return null;
        }
        return thirdPartyPaySet;
    }

    /**
     * 是否未配置 上传配置 信息
     *
     * @param thirdPartyPaySet
     * @return
     */
    private boolean isNotConfigureUpload(ThirdPartyPaySet thirdPartyPaySet) {
        return StringUtils.isBlank(thirdPartyPaySet.getQiniuAccessKey()) &&
                StringUtils.isBlank(thirdPartyPaySet.getQiniuSecretKey()) &&
                StringUtils.isBlank(thirdPartyPaySet.getQiniuBucket()) &&
                StringUtils.isBlank(thirdPartyPaySet.getQiniuDomainImage()) &&
                StringUtils.isBlank(thirdPartyPaySet.getQiniuDomainFile());
    }
}
