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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.bonus.canteen.core.cook.vo.NutritionTypeVO;
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.CookNutritionMapper;
import com.bonus.canteen.core.cook.domain.CookNutrition;
import com.bonus.canteen.core.cook.service.ICookNutritionService;

/**
 * 食材营养基础信息Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class CookNutritionServiceImpl implements ICookNutritionService {
    @Autowired
    private CookNutritionMapper cookNutritionMapper;

    /**
     * 查询食材营养基础信息
     * 
     * @param nutritionId 食材营养基础信息主键
     * @return 食材营养基础信息
     */
    @Override
    public CookNutrition selectCookNutritionByNutritionId(Long nutritionId) {
        return cookNutritionMapper.selectCookNutritionByNutritionId(nutritionId);
    }

    /**
     * 查询食材营养基础信息列表
     * 
     * @param cookNutrition 食材营养基础信息
     * @return 食材营养基础信息
     */
    @Override
    public List<CookNutrition> selectCookNutritionList(CookNutrition cookNutrition) {
        return cookNutritionMapper.selectCookNutritionList(cookNutrition);
    }

    /**
     * 新增食材营养基础信息
     * 
     * @param cookNutrition 食材营养基础信息
     * @return 结果
     */
    @Override
    public int insertCookNutrition(CookNutrition cookNutrition) {
        cookNutrition.setCreateTime(DateUtils.getNowDate());
        try {
            if (StringUtils.isBlank(cookNutrition.getNutritionName())){
                throw new ServiceException("食材名称不能为空！");
            }
            if (StringUtils.isNull(cookNutrition.getNutritionTypeId()) || cookNutrition.getNutritionTypeId() == 0){
                throw new ServiceException("食材类别不能为空！");
            }
            if (cookNutritionMapper.checkIsExistByName(cookNutrition.getNutritionName(),null) > 0) {
                throw new ServiceException("该食材营养基础信息已存在！");
            }
            return cookNutritionMapper.insertCookNutrition(cookNutrition);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改食材营养基础信息
     * 
     * @param cookNutrition 食材营养基础信息
     * @return 结果
     */
    @Override
    public int updateCookNutrition(CookNutrition cookNutrition) {
        cookNutrition.setUpdateTime(DateUtils.getNowDate());
        try {
            if (StringUtils.isNull(cookNutrition.getNutritionId())){
                throw new ServiceException("未携带食材营养基础信息ID");
            }
            if (StringUtils.isBlank(cookNutrition.getNutritionName())){
                throw new ServiceException("食材名称不能为空！");
            }
            if (StringUtils.isNull(cookNutrition.getNutritionTypeId()) || cookNutrition.getNutritionTypeId() == 0){
                throw new ServiceException("食材类别不能为空！");
            }
            if (cookNutritionMapper.checkIsExistByName(cookNutrition.getNutritionName(),cookNutrition.getNutritionId()) > 0) {
                throw new ServiceException("该食材营养基础信息已存在！");
            }
            return cookNutritionMapper.updateCookNutrition(cookNutrition);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除食材营养基础信息
     * 
     * @param nutritionIds 需要删除的食材营养基础信息主键
     * @return 结果
     */
    @Override
    public int deleteCookNutritionByNutritionIds(Long[] nutritionIds) {
        //判断营养基础信息是否被关联
        for (Long nutritionId : nutritionIds) {
            int code = cookNutritionMapper.checkIsExist(nutritionId);
            if (code > 0) {
                throw new ServiceException("存在关联数据，不允许删除！");
            }
        }
        return cookNutritionMapper.deleteCookNutritionByNutritionIds(nutritionIds);
    }

    /**
     * 删除食材营养基础信息信息
     * 
     * @param nutritionId 食材营养基础信息主键
     * @return 结果
     */
    @Override
    public int deleteCookNutritionByNutritionId(Long nutritionId) {
        return cookNutritionMapper.deleteCookNutritionByNutritionId(nutritionId);
    }

    /**
     * 根据categoryId获取营养类别集合
     *
     * @param menuNutrition 食材营养信息
     * @return 营养类别集合
     */
    @Override
    public List<CookNutrition> getListByCategoryId(CookNutrition menuNutrition) {
        return cookNutritionMapper.getListByCategoryId(menuNutrition);
    }


}
