package com.fs.company.service.impl;

import java.util.List;

import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.company.mapper.CompanyProductMapper;
import com.fs.company.domain.CompanyProduct;
import com.fs.company.service.ICompanyProductService;

/**
 * 产品Service业务层处理
 *
 * @author fs
 * @date 2025-07-01
 */
@Service
public class CompanyProductServiceImpl implements ICompanyProductService
{
    @Autowired
    private CompanyProductMapper companyProductMapper;

    /**
     * 查询产品
     *
     * @param id 产品主键
     * @return 产品
     */
    @Override
    public CompanyProduct selectCompanyProductById(Long id)
    {
        return companyProductMapper.selectCompanyProductById(id);
    }

    /**
     * 查询产品列表
     *
     * @param companyProduct 产品
     * @return 产品
     */
    @Override
    public List<CompanyProduct> selectCompanyProductList(CompanyProduct companyProduct)
    {
        return companyProductMapper.selectCompanyProductList(companyProduct);
    }

    @Override
    public List<CompanyProduct> queryAllProduct(Long companyId) {
        return companyProductMapper.queryAllProduct(companyId);
    }

    /**
     * 新增产品
     *
     * @param companyProduct 产品
     * @return 结果
     */
    @Override
    public int insertCompanyProduct(CompanyProduct companyProduct)
    {
        String productCode = companyProduct.getProductCode();
        CompanyProduct exist = companyProductMapper.selectByCompanyIdAndProductCode(productCode,companyProduct.getCompanyId());

        if (exist != null) {
            throw new ServiceException("产品代码已存在，请更换后再试");
        }
        companyProduct.setCreateTime(DateUtils.getNowDate());
        return companyProductMapper.insertCompanyProduct(companyProduct);
    }

    /**
     * 修改产品
     *
     * @param companyProduct 产品
     * @return 结果
     */
    @Override
    public int updateCompanyProduct(CompanyProduct companyProduct)
    {
        companyProduct.setUpdateTime(DateUtils.getNowDate());
        return companyProductMapper.updateCompanyProduct(companyProduct);
    }

    /**
     * 批量删除产品
     *
     * @param ids 需要删除的产品主键
     * @return 结果
     */
    @Override
    public int deleteCompanyProductByIds(Long[] ids)
    {
        return companyProductMapper.deleteCompanyProductByIds(ids);
    }

    /**
     * 删除产品信息
     *
     * @param id 产品主键
     * @return 结果
     */
    @Override
    public int deleteCompanyProductById(Long id)
    {
        return companyProductMapper.deleteCompanyProductById(id);
    }
}
