package com.trade.client.enterprise.companyrelated.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.trade.client.enterprise.companyrelated.service.ImporterEntityCService;
import com.trade.common.utils.ConstantUtil;
import com.trade.common.utils.StringUtils;
import com.trade.service.DictConstants;
import com.trade.service.enterprise.companyrelated.domain.vo.ImporterEntityVO;
import com.trade.service.system.domain.SysCountry;
import com.trade.service.system.service.SysCountryService;
import com.trade.service.trade.salescontract.service.SalesContractService;
import com.trade.service.serialNum.service.SerialNumberGenerator;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.trade.common.constant.HttpStatus;
import com.trade.common.exception.ServiceException;
import com.trade.service.enterprise.companyrelated.domain.ImporterEntity;
import com.trade.service.enterprise.companyrelated.domain.vo.ImporterEntitySearch;
import com.trade.service.enterprise.companyrelated.mapper.ImporterEntityMapper;
import com.trade.service.enterprise.companyrelated.service.IImporterEntityService;

/**
 * <p>
 * 进口商信息 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-05-28
 */
@Service
public class ImporterEntityCServiceImp extends ServiceImpl<ImporterEntityMapper, ImporterEntity> implements ImporterEntityCService {

    @Resource
    private IImporterEntityService importerEntityService;
    @Resource
    private SerialNumberGenerator serialNumberGenerator;
    @Resource
    private SalesContractService salesContractService;
    @Resource
    private SysCountryService sysCountryService;

    @Override
    public void add(ImporterEntity addEntity) {
        // 参数校验
        preCheckAndSetParams(addEntity);
        // 保存进口商信息
        if (addEntity.getId() == null) {
            this.save(addEntity);
        } else {
            this.updateById(addEntity);
        }
    }

    /**
     * 参数校验
     * 
     * @param importEntity 进口商信息
     */
    private void preCheckAndSetParams(ImporterEntity importEntity) {
        if (DictConstants.DISTRICT_FOREIGN.equals(importEntity.getRegion())) {
            if (StringUtils.isEmpty(importEntity.getCountry())) {
                throw new ServiceException("境外进口商国家不能为空", HttpStatus.BAD_REQUEST);
            }
            // 是否被制裁
            SysCountry countryInfo = sysCountryService.queryCountryByCode(importEntity.getCountry());
            if (BooleanUtils.isTrue(countryInfo.getSanctioned())) {
                throw new ServiceException("进口商准入不通过", HttpStatus.ERROR);
            }
        }
        ImporterEntity exist = getBaseMapper().getByUserIdAndCertNo(importEntity.getCustomerUserId(),
                importEntity.getCertNo());
        if (null == importEntity.getId()) {
            if (exist != null) {
                throw new ServiceException("证件号码已存在", HttpStatus.BAD_REQUEST);
            }
            // importEntity.setStatus(0);
            // importEntity.setUserId(UserUtils.getCustomerUserId());
            importEntity.setEntityNo(serialNumberGenerator.nextValue(ConstantUtil.IMPORTER_CODE));
        } else {
            if (exist != null && !exist.getId().equals(importEntity.getId())) {
                throw new ServiceException("证件号码已存在", HttpStatus.BAD_REQUEST);
            }
            // importEntity.setStatus(1);
        }
        importEntity.setStatus(1);
    }

    @Override
    public void update(ImporterEntity updateEntity) {
        preCheckAndSetParams(updateEntity);
        ImporterEntity importerEntity = this.getById(updateEntity.getId());
        if (importerEntity == null) {
            throw new ServiceException("进口商信息不存在", HttpStatus.BAD_REQUEST);
        }
        this.updateById(updateEntity);
    }

    @Override
    public void deleteByEntityId(Long entityId) {
        ImporterEntity importerEntity = this.getById(entityId);
        if (importerEntity == null) {
            throw new ServiceException("进口商信息不存在", HttpStatus.BAD_REQUEST);
        }
        if (salesContractService.existSalesContractByImporterCertNo(importerEntity.getCertNo(),
                importerEntity.getCustomerUserId())) {
            throw new ServiceException("存在关联销售合同，不能删除", HttpStatus.BAD_REQUEST);
        }
        // todo 校验是否存在在融资
        this.removeById(entityId);
    }

    @Override
    public PageInfo<ImporterEntityVO> queryListBySearch(ImporterEntitySearch search) {
        return importerEntityService.queryListBySearch(search);
    }

    @Override
    public List<ImporterEntity> getListByUserId(Long userId) {
        return importerEntityService.getListByUserId(userId);
    }

    @Override
    public List<ImporterEntity> getEnabledListByUserId(Long userId) {
        return importerEntityService.getEnabledListByUserId(userId);
    }

    @Override
    public ImporterEntityVO getByEntityId(Long bizEntityId) {
        return importerEntityService.getVOByEntityId(bizEntityId);
    }
}
