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

import cn.hutool.core.collection.CollUtil;
import com.trade.common.utils.ConstantUtil;
import com.trade.service.DictConstants;
import com.trade.service.TransStruct;
import com.trade.service.enterprise.customer.dto.EnterpriseRelatedPersonDto;
import com.trade.service.enterprise.customer.domain.Enterprise;
import com.trade.service.enterprise.customer.domain.EnterpriseChangeRecord;
import com.trade.service.enterprise.customer.domain.EnterpriseRelatedPerson;
import com.trade.service.enterprise.customer.dto.EnterpriseDto;
import com.trade.service.filestorage.service.BusinessRelateFileBaseService;
import com.trade.service.enterprise.customer.service.EnterpriseRelatedPersonBaseService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component("enterpriseDataConverter")
public class EnterpriseDataConverter {

    @Resource
    private BusinessRelateFileBaseService businessRelateFileBaseService;
    @Resource
    private EnterpriseRelatedPersonBaseService enterpriseRelatedPersonBaseService;
    @Resource
    private TransStruct transStruct;

    public EnterpriseDto recordToEnterpriseDto(EnterpriseChangeRecord record) {
        EnterpriseDto enterpriseDto = transStruct.transEnterpriseChangeRecord2EnterpriseDto(record);
        if (enterpriseDto != null) {
            enterpriseDto.setId(record.getEnterpriseId());
            enterpriseDto.setChangeRecordId(record.getId());
            businessRelateFileBaseService.fillBizObjFiles(enterpriseDto, record.getId(), ConstantUtil.ENTERPRISE_ACCESS,
                    null);
            // 查询人员信息
            fillEnterprisePersons(record.getId(), enterpriseDto);
        }
        return enterpriseDto;
    }

    public EnterpriseDto EnterpriseDomainToDto(Enterprise enterprise) {
        EnterpriseDto enterpriseDto = transStruct.transEnterprise2Dto(enterprise);
        if (enterpriseDto != null) {
            businessRelateFileBaseService.fillBizObjFiles(enterpriseDto, enterprise.getChangeRecordId(),
                    ConstantUtil.ENTERPRISE_ACCESS, null);
            // 查询人员信息
            fillEnterprisePersons(enterprise.getChangeRecordId(), enterpriseDto);
        }
        return enterpriseDto;
    }

    private void fillEnterprisePersons(Long enterpriseChangeRecordId, EnterpriseDto enterpriseDto) {
        List<EnterpriseRelatedPerson> personList = enterpriseRelatedPersonBaseService.lambdaQuery()
                .eq(EnterpriseRelatedPerson::getEnterpriseChangeRecordId, enterpriseChangeRecordId).list();
        if (CollUtil.isNotEmpty(personList)) {
            for (EnterpriseRelatedPerson person : personList) {
                EnterpriseRelatedPersonDto personDto = transStruct.transDto2EnterpriseRelatedPerson(person);
                businessRelateFileBaseService.fillBizObjFiles(personDto, personDto.getId(),
                        ConstantUtil.ENTERPRISE_PERSON, null);
                if (DictConstants.SYS_YES.equals(person.getIsLegalPerson())) {
                    enterpriseDto.setLegalPerson(personDto);
                } else if (DictConstants.SYS_YES.equals(person.getIsControl())) {
                    enterpriseDto.setControlPerson(personDto);
                } else if (DictConstants.SYS_YES.equals(person.getIsAuthorizedPerson())) {
                    enterpriseDto.setContactPerson(personDto);
                }
            }
        }
    }
}
