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

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.menu.domain.MenuDishesEffectRelation;
import com.bonus.canteen.core.menu.domain.MenuSuitDishes;
import com.bonus.canteen.core.menu.dto.MenuNutritionDTO;
import com.bonus.canteen.core.menu.service.IMenuNutritionService;
import com.bonus.canteen.core.utils.BnsUtils;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.LeBeanUtil;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.menu.mapper.MenuNutritionMapper;
import com.bonus.canteen.core.menu.domain.MenuNutrition;

/**
 * 食材营养基础信息Service业务层处理
 *
 * @author xsheng
 * @date 2025-04-03
 */
@Service
@Slf4j
public class MenuNutritionServiceImpl implements IMenuNutritionService {
    @Autowired
    private MenuNutritionMapper menuNutritionMapper;

    /**
     * 查询食材营养基础信息
     *
     * @param id 食材营养基础信息主键
     * @return 食材营养基础信息
     */
    @Override
    public MenuNutrition selectMenuNutritionById(Long id) {
        MenuNutrition menuNutrition = menuNutritionMapper.selectMenuNutritionById(id);
        return menuNutrition;
    }

    /**
     * 查询食材营养基础信息列表
     *
     * @param menuNutrition 食材营养基础信息
     * @return 食材营养基础信息
     */
    @Override
    public List<MenuNutrition> selectMenuNutritionList(MenuNutrition menuNutrition) {
        return menuNutritionMapper.selectMenuNutritionList(menuNutrition);
    }

    /**
     * 新增食材营养基础信息
     *
     * @param menuNutrition 食材营
     *                         养基础信息
     * @return 结果
     */
    @Override
    public int insertMenuNutrition(MenuNutrition menuNutrition) {
//        MenuNutrition existMenuNutrition = menuNutritionMapper.queryIfExistMenuNutrition(menuNutrition);
//        if (Objects.nonNull(existMenuNutrition)) {
//            throw new ServiceException("应用编码或名称重复");
//        }
        MenuNutrition getOneByCode =menuNutritionMapper.getOneByCode(menuNutrition);
        if(getOneByCode != null){
            throw new ServiceException("营养编码重复");
        }
        MenuNutrition getOneByName =menuNutritionMapper.getOneByName(menuNutrition);
        if(getOneByName != null){
            throw new ServiceException("营养名称重复");
        }
        menuNutrition.setNutritionId(Id.next());
        menuNutrition.setCreateBy(SecurityUtils.getUsername());
        menuNutrition.setCreateTime(DateUtils.getNowDate());
        return menuNutritionMapper.insertMenuNutrition(menuNutrition);
    }

    /**
     * 修改食材营养基础信息
     *
     * @param menuNutrition 食材营养基础信息
     * @return 结果
     */
    @Override
    public int updateMenuNutrition(MenuNutrition menuNutrition) {
        try {
            menuNutrition.setUpdateBy(SecurityUtils.getUsername());
            menuNutrition.setUpdateTime(DateUtils.getNowDate());
             MenuNutrition getOneByCode =menuNutritionMapper.getOneByCode(menuNutrition);
             if(getOneByCode != null){
                 throw new ServiceException("营养编码重复");
             }
            MenuNutrition getOneByName =menuNutritionMapper.getOneByName(menuNutrition);
            if(getOneByName != null){
                throw new ServiceException("营养名称重复");
            }
            return menuNutritionMapper.updateMenuNutrition(menuNutrition);
        } catch (Exception e) {
            log.error("更新营养数据出错, ", e.getMessage());
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除食材营养基础信息
     *
     * @param ids 需要删除的食材营养基础信息主键
     * @return 结果
     */
    @Override
    public int deleteMenuNutritionByIds(String[] ids) {
        return menuNutritionMapper.deleteMenuNutritionByIds(ids);
    }

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