package com.trade.service.enterprise.customer.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.trade.service.DictConstants;
import com.trade.service.enterprise.customer.mapper.EnterpriseRelatedPersonMapper;
import com.trade.service.enterprise.customer.domain.EnterpriseRelatedPerson;
import com.trade.service.enterprise.customer.service.EnterpriseRelatedPersonBaseService;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * 企业相关人员基础服务实现类
 *
 * @author hlb
 * @since 2024-05-29 15:07:43
 */
@Service("enterpriseRelatedPersonBaseService")
public class EnterpriseRelatedPersonBaseServiceImpl extends ServiceImpl<EnterpriseRelatedPersonMapper, EnterpriseRelatedPerson> implements EnterpriseRelatedPersonBaseService {

    @Override
    public List<EnterpriseRelatedPerson> getPersonsByChangeRecordId(Long changeRecordId) {
        return this.lambdaQuery().eq(EnterpriseRelatedPerson::getEnterpriseChangeRecordId, changeRecordId).list();
    }

    @Override
    public EnterpriseRelatedPerson getLegalByChangeRecordId(Long changeRecordId) {
        List<EnterpriseRelatedPerson> persons = getPersonsByChangeRecordId(changeRecordId);
        if (CollUtil.isEmpty(persons)) {
            return null;
        }
        return persons.stream().filter(
                enterpriseRelatedPerson -> DictConstants.SYS_YES.equals(enterpriseRelatedPerson.getIsLegalPerson()))
                .max(Comparator.comparing(EnterpriseRelatedPerson::getId)).orElse(null);
    }

    @Override
    public EnterpriseRelatedPerson getContactByChangeRecordId(Long changeRecordId) {
        List<EnterpriseRelatedPerson> persons = getPersonsByChangeRecordId(changeRecordId);
        if (CollUtil.isEmpty(persons)) {
            return null;
        }
        return persons.stream()
                .filter(enterpriseRelatedPerson -> DictConstants.SYS_YES
                        .equals(enterpriseRelatedPerson.getIsAuthorizedPerson()))
                .max(Comparator.comparing(EnterpriseRelatedPerson::getId)).orElse(null);
    }
}
