package com.ruoyi.warehouse.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.warehouse.domain.ProductInfo;
import com.ruoyi.warehouse.mapper.ProductInfoMapper;
import com.ruoyi.warehouse.service.IProductInfoService;

@Service
public class ProductInfoServiceImpl implements IProductInfoService
{
    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Override
    public List<ProductInfo> selectProductInfoList(ProductInfo productInfo)
    {
        return productInfoMapper.selectProductInfoList(productInfo);
    }

    @Override
    public List<ProductInfo> selectProductInfoAll()
    {
        return productInfoMapper.selectProductInfoAll();
    }

    @Override
    public ProductInfo selectProductInfoById(Long id)
    {
        return productInfoMapper.selectProductInfoById(id);
    }

    @Override
    public boolean checkProductCodeUnique(ProductInfo productInfo)
    {
        if (StringUtils.isBlank(productInfo.getProductCode()))
        {
            return UserConstants.UNIQUE;
        }
        Long id = StringUtils.isNull(productInfo.getId()) ? -1L : productInfo.getId();
        ProductInfo info = productInfoMapper.checkProductCodeUnique(productInfo.getProductCode());
        return StringUtils.isNull(info) || info.getId().longValue() == id.longValue() ? UserConstants.UNIQUE : UserConstants.NOT_UNIQUE;
    }

    @Override
    public int insertProductInfo(ProductInfo productInfo)
    {
        normalizeProductInfo(productInfo);
        validateProductInfoUnique(productInfo);
        return productInfoMapper.insertProductInfo(productInfo);
    }

    @Override
    public int updateProductInfo(ProductInfo productInfo)
    {
        normalizeProductInfo(productInfo);
        validateProductInfoUnique(productInfo);
        return productInfoMapper.updateProductInfo(productInfo);
    }

    @Override
    public int deleteProductInfoByIds(Long[] ids)
    {
        return productInfoMapper.deleteProductInfoByIds(ids);
    }

    private void normalizeProductInfo(ProductInfo productInfo)
    {
        if (StringUtils.isBlank(productInfo.getProductCode()))
        {
            productInfo.setProductCode(null);
        }
        productInfo.setProductName(trimToNull(productInfo.getProductName()));
        productInfo.setBrand(trimToNull(productInfo.getBrand()));
        productInfo.setCategory(trimToNull(productInfo.getCategory()));
        productInfo.setSpec(trimToNull(productInfo.getSpec()));
        productInfo.setUnit(trimToNull(productInfo.getUnit()));
    }

    private void validateProductInfoUnique(ProductInfo productInfo)
    {
        ProductInfo info = productInfoMapper.checkProductInfoUnique(productInfo);
        Long id = StringUtils.isNull(productInfo.getId()) ? -1L : productInfo.getId();
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            throw new ServiceException("该产品已存在");
        }
    }

    private String trimToNull(String value)
    {
        return StringUtils.isBlank(value) ? null : value.trim();
    }
}
