package com.bonus.canteen.core.supermarket.service.impl;

import java.util.List;

import com.bonus.canteen.core.supermarket.domain.SupermarketProduct;
import com.bonus.canteen.core.supermarket.mapper.SupermarketProductMapper;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.supermarket.mapper.SupermarketMaterialMapper;
import com.bonus.canteen.core.supermarket.domain.SupermarketMaterial;
import com.bonus.canteen.core.supermarket.service.ISupermarketMaterialService;
import org.springframework.util.CollectionUtils;

/**
 * 商品信息Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class SupermarketMaterialServiceImpl implements ISupermarketMaterialService {
    @Autowired
    private SupermarketMaterialMapper supermarketMaterialMapper;
    @Autowired
    private SupermarketProductMapper supermarketProductMapper;

    /**
     * 查询商品信息
     * 
     * @param materialId 商品信息主键
     * @return 商品信息
     */
    @Override
    public SupermarketMaterial selectSupermarketMaterialByMaterialId(Long materialId) {
        return supermarketMaterialMapper.selectSupermarketMaterialByMaterialId(materialId);
    }

    /**
     * 查询商品信息列表
     * 
     * @param supermarketMaterial 商品信息
     * @return 商品信息
     */
    @Override
    public List<SupermarketMaterial> selectSupermarketMaterialList(SupermarketMaterial supermarketMaterial) {
        return supermarketMaterialMapper.selectSupermarketMaterialList(supermarketMaterial);
    }

    /**
     * 新增商品信息
     * 
     * @param supermarketMaterial 商品信息
     * @return 结果
     */
    @Override
    public int insertSupermarketMaterial(SupermarketMaterial supermarketMaterial) {
        supermarketMaterial.setCreateTime(DateUtils.getNowDate());
        try {
            return supermarketMaterialMapper.insertSupermarketMaterial(supermarketMaterial);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改商品信息
     * 
     * @param supermarketMaterial 商品信息
     * @return 结果
     */
    @Override
    public int updateSupermarketMaterial(SupermarketMaterial supermarketMaterial) {
        supermarketMaterial.setUpdateTime(DateUtils.getNowDate());
        try {
            return supermarketMaterialMapper.updateSupermarketMaterial(supermarketMaterial);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除商品信息
     * 
     * @param materialIds 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketMaterialByMaterialIds(Long[] materialIds) {
        List<SupermarketProduct> list = supermarketProductMapper.selectSupermarketProductByMaterialIds(materialIds);
        if (!CollectionUtils.isEmpty(list)) {
            throw new ServiceException("该原料下含有商品，不能删除");
        }
        return supermarketMaterialMapper.deleteSupermarketMaterialByMaterialIds(materialIds);
    }

    /**
     * 删除商品信息信息
     * 
     * @param materialId 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketMaterialByMaterialId(Long materialId) {
        return supermarketMaterialMapper.deleteSupermarketMaterialByMaterialId(materialId);
    }
}
