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

import java.util.List;

import javax.annotation.Resource;

import com.trade.client.enterprise.companyrelated.service.VendorEntityCService;
import com.trade.service.enterprise.companyrelated.domain.vo.VendorEntityVO;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.trade.client.security.utils.UserUtils;
import com.trade.common.constant.HttpStatus;
import com.trade.common.exception.ServiceException;
import com.trade.common.utils.uuid.BizNoUtil;
import com.trade.service.enterprise.companyrelated.domain.VendorEntity;
import com.trade.service.enterprise.companyrelated.domain.vo.VendorEntitySearch;
import com.trade.service.enterprise.companyrelated.mapper.VendorEntityMapper;
import com.trade.service.enterprise.companyrelated.service.IVendorEntityService;

/**
 * <p>
 * 供应商信息 服务实现类
 * </p>
 */
@Service
public class VendorEntityCServiceImp extends ServiceImpl<VendorEntityMapper, VendorEntity> implements VendorEntityCService {

    @Resource
    private IVendorEntityService VendorEntityService;

    @Override
    public void add(VendorEntity addEntity) {
        // 参数校验
        preCheckAndSetParams(addEntity);
        // 保存供应商信息
        this.save(addEntity);
    }

    /**
     * 参数校验
     * 
     * @param vendorEntity 供应商信息
     */
    private void preCheckAndSetParams(VendorEntity vendorEntity) {
        VendorEntity exist = getBaseMapper().getByUserIdAndEntityName(vendorEntity.getCustomerUserId(),
                vendorEntity.getEntityName());
        if (exist != null) {
            throw new ServiceException("企业已存在", HttpStatus.BAD_REQUEST);
        }
        vendorEntity.setEntityNo(BizNoUtil.generateVendorEntityNo());
        vendorEntity.setCustomerUserId(UserUtils.getCustomerUserId());
    }

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

    @Override
    public PageInfo<VendorEntityVO> queryListBySearch(VendorEntitySearch search) {
        return VendorEntityService.queryListBySearch(search);
    }

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

    @Override
    public VendorEntity getByEntityId(Long bizEntityId) {
        return VendorEntityService.getByEntityId(bizEntityId);
    }
}
