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

import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.util.RandomUtil;
import com.trade.client.security.utils.UserUtils;
import com.trade.common.utils.ConstantUtil;
import com.trade.service.serialNum.service.SerialNumberGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.trade.client.companyrelated.service.ImporterEntityCService;
import com.trade.common.constant.HttpStatus;
import com.trade.common.exception.ServiceException;
import com.trade.service.companyrelated.domain.ImporterEntity;
import com.trade.service.companyrelated.domain.vo.ImporterEntitySearch;
import com.trade.service.companyrelated.mapper.ImporterEntityMapper;
import com.trade.service.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;

    @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) {
        ImporterEntity exist = getBaseMapper().getByUserIdAndCertNo(importEntity.getUserId(), 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) {
        ImporterEntity importerEntity = this.getById(updateEntity.getId());
        if (importerEntity == null) {
            throw new ServiceException("进口商信息不存在", HttpStatus.BAD_REQUEST);
        }
        this.updateById(updateEntity);
    }

    @Override
    public void deleteByEntityId(Long entityId) {
        // todo 校验是否存在在融资
        this.removeById(entityId);
    }

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

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

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