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

import java.util.List;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.cook.mapper.CookMaterialMapper;
import com.bonus.canteen.core.cook.domain.CookMaterial;
import com.bonus.canteen.core.cook.service.ICookMaterialService;

/**
 * 原料信息Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class CookMaterialServiceImpl implements ICookMaterialService {
    @Autowired
    private CookMaterialMapper cookMaterialMapper;

    /**
     * 查询原料信息
     * 
     * @param materialId 原料信息主键
     * @return 原料信息
     */
    @Override
    public CookMaterial selectCookMaterialByMaterialId(Long materialId) {
        return cookMaterialMapper.selectCookMaterialByMaterialId(materialId);
    }

    /**
     * 查询原料信息列表
     * 
     * @param cookMaterial 原料信息
     * @return 原料信息
     */
    @Override
    public List<CookMaterial> selectCookMaterialList(CookMaterial cookMaterial) {
        return cookMaterialMapper.selectCookMaterialList(cookMaterial);
    }

    /**
     * 新增原料信息
     * 
     * @param cookMaterial 原料信息
     * @return 结果
     */
    @Override
    public int insertCookMaterial(CookMaterial cookMaterial) {
        cookMaterial.setCreateTime(DateUtils.getNowDate());
        try {
            if (StringUtils.isBlank(cookMaterial.getMaterialCode())){
                throw new ServiceException("请输入原料编码");
            }
            if (StringUtils.isBlank(cookMaterial.getMaterialName())){
                throw new ServiceException("请输入原料名称");
            }
            if (StringUtils.isNull(cookMaterial.getMaterialTypeId()) || cookMaterial.getMaterialTypeId() == 0L){
                throw new ServiceException("请选择原料类别");
            }
            if (StringUtils.isNull(cookMaterial.getNutritionId()) || cookMaterial.getNutritionId() == 0L){
                throw new ServiceException("请选择营养信息名称");
            }
            if (cookMaterialMapper.checkIsExistByCode(cookMaterial.getMaterialCode(), null) > 0) {
                throw new ServiceException("该原料编码已存在");
            }
            if (cookMaterialMapper.checkIsExistByName(cookMaterial.getMaterialName(), null) > 0) {
                throw new ServiceException("该原料名称已存在");
            }
            cookMaterial.setGoodsType(1L);
            return cookMaterialMapper.insertCookMaterial(cookMaterial);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改原料信息
     * 
     * @param cookMaterial 原料信息
     * @return 结果
     */
    @Override
    public int updateCookMaterial(CookMaterial cookMaterial) {
        cookMaterial.setUpdateTime(DateUtils.getNowDate());
        try {
            if (StringUtils.isNull(cookMaterial.getMaterialId())){
                throw new ServiceException("未携带原料ID");
            }
            if (StringUtils.isBlank(cookMaterial.getMaterialCode())){
                throw new ServiceException("请输入原料编码");
            }
            if (StringUtils.isBlank(cookMaterial.getMaterialName())){
                throw new ServiceException("请输入原料名称");
            }
            if (StringUtils.isNull(cookMaterial.getMaterialTypeId()) || cookMaterial.getMaterialTypeId() == 0L){
                throw new ServiceException("请选择原料类别");
            }
            if (StringUtils.isNull(cookMaterial.getNutritionId()) || cookMaterial.getNutritionId() == 0L){
                throw new ServiceException("请选择营养信息名称");
            }
            if (cookMaterialMapper.checkIsExistByCode(cookMaterial.getMaterialCode(), cookMaterial.getMaterialId()) > 0) {
                throw new ServiceException("该原料编码已存在");
            }
            if (cookMaterialMapper.checkIsExistByName(cookMaterial.getMaterialName(), cookMaterial.getMaterialId()) > 0) {
                throw new ServiceException("该原料名称已存在");
            }
            cookMaterial.setGoodsType(1L);
            return cookMaterialMapper.updateCookMaterial(cookMaterial);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除原料信息
     * 
     * @param materialIds 需要删除的原料信息主键
     * @return 结果
     */
    @Override
    public int deleteCookMaterialByMaterialIds(Long[] materialIds) {
        //  判断该原料信息是否被使用
        if (cookMaterialMapper.checkIsUse(materialIds) > 0){
            throw new ServiceException("该原料信息被使用，不允许删除！");
        }
        return cookMaterialMapper.deleteCookMaterialByMaterialIds(materialIds);
    }

    /**
     * 删除原料信息信息
     * 
     * @param materialId 原料信息主键
     * @return 结果
     */
    @Override
    public int deleteCookMaterialByMaterialId(Long materialId) {
        return cookMaterialMapper.deleteCookMaterialByMaterialId(materialId);
    }
}
