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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.hutool.system.UserInfo;
import com.bonus.canteen.core.menu.mapper.MenuNutritionMapper;
import com.bonus.canteen.core.menu.vo.NutritionTypeV2VO;
import com.bonus.canteen.core.menu.vo.NutritionTypeVo;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.menu.mapper.MenuNutritionTypeMapper;
import com.bonus.canteen.core.menu.domain.MenuNutritionType;
import com.bonus.canteen.core.menu.service.IMenuNutritionTypeService;

/**
 * 营养基础类型Service业务层处理
 *
 * @author xsheng
 * @date 2025-04-03
 */
@Service
public class MenuNutritionTypeServiceImpl implements IMenuNutritionTypeService {
    @Autowired
    private MenuNutritionTypeMapper menuNutritionTypeMapper;

    /**
     * 查询营养基础类型
     *
     * @param id 营养基础类型主键
     * @return 营养基础类型
     */
    @Override
    public MenuNutritionType selectMenuNutritionTypeById(Long id) {
        return menuNutritionTypeMapper.selectMenuNutritionTypeById(id);
    }

    /**
     * 查询营养基础类型列表
     *
     * @param menuNutritionType 营养基础类型
     * @return 营养基础类型
     */
    @Override
    public List<MenuNutritionType> selectMenuNutritionTypeList(MenuNutritionType menuNutritionType) {
        return menuNutritionTypeMapper.selectMenuNutritionTypeList(menuNutritionType);
    }

    /**
     * 新增营养基础类型
     *
     * @param menuNutritionType 营养基础类型
     * @return 结果
     */
    @Override
    public int insertMenuNutritionType(MenuNutritionType menuNutritionType) {
        menuNutritionType.setCreateBy(SecurityUtils.getUserId()+"");
        MenuNutritionType one = menuNutritionTypeMapper.getOne(menuNutritionType);
        try {
        if(one !=null ){
            throw new ServiceException("该营养基础类型已存在");
        }
        MenuNutritionType getParentOne = menuNutritionTypeMapper.getParentOne(menuNutritionType);
        if(menuNutritionType.getParentId() == 0L){
            return menuNutritionTypeMapper.insertMenuNutritionType(menuNutritionType);
        }else{
            if(getParentOne == null){
                throw new ServiceException("无效的父级节点");
            }else{
                if("2".equals(getParentOne.getLevel())){
                    throw new ServiceException("不允许添加三级节点");
                }else{
                    menuNutritionType.setLevel("2");
                }
            }
            return menuNutritionTypeMapper.insertMenuNutritionType(menuNutritionType);
        }
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改营养基础类型
     *
     * @param menuNutritionType 营养基础类型
     * @return 结果
     */
    @Override
    public int updateMenuNutritionType(MenuNutritionType menuNutritionType) {
        menuNutritionType.setCreateBy(SecurityUtils.getUserId()+"");
        MenuNutritionType one = menuNutritionTypeMapper.getOne(menuNutritionType);
        if(one !=null ){
            throw new ServiceException("该营养基础类型已存在");
        }
        try {
            return menuNutritionTypeMapper.updateMenuNutritionType(menuNutritionType);
        } catch (Exception e) {
            throw new ServiceException("错误信息描述");
        }
    }

    /**
     * 批量删除营养基础类型
     *
     * @param ids 需要删除的营养基础类型主键
     * @return 结果
     */
    @Override
    public int deleteMenuNutritionTypeByIds(Integer[] ids) {
        if(ids == null || ids.length == 0){
            throw new ServiceException("请选择要删除的营养基础类型");
        }
        return menuNutritionTypeMapper.deleteMenuNutritionTypeByIds(ids);
    }

    /**
     * 删除营养基础类型信息
     *
     * @param id 营养基础类型主键
     * @return 结果
     */
    @Override
    public int deleteMenuNutritionTypeById(Long id) {
        return menuNutritionTypeMapper.deleteMenuNutritionTypeById(id);
    }


    @Override
    public List<NutritionTypeV2VO> getNutritionTypeList() {
        List<MenuNutritionType> bigTypeList =menuNutritionTypeMapper.selectBigTypeList();
        List<NutritionTypeV2VO> nutritionTypeVOList = new ArrayList();
        for(MenuNutritionType menuNutritionType : bigTypeList){
            NutritionTypeV2VO nutritionTypeVO = new NutritionTypeV2VO();
            List<NutritionTypeV2VO> littleTypeList = menuNutritionTypeMapper.selectLittleTypeList(menuNutritionType.getId()+"");
            nutritionTypeVO.setId(Integer.parseInt(menuNutritionType.getId()+""));
            nutritionTypeVO.setBigType(menuNutritionType.getName());
            nutritionTypeVO.setLittleTypeList(littleTypeList);
            nutritionTypeVOList.add(nutritionTypeVO);
        }

//
//        while(var3.hasNext()) {
//            String bigType = (String)var3.next();
//            NutritionTypeV2VO nutritionTypeVO = new NutritionTypeV2VO();
//            List<MenuNutritionType> littleTypeList = menuNutritionTypeMapper.selectLittleTypeList(bigType);
//            nutritionTypeVO.setBigType(bigType);
//            nutritionTypeVO.setLittleTypeList(littleTypeList);
//            nutritionTypeVOList.add(nutritionTypeVO);
//        }
      //  TreeNodeUtil.assembleTree(list);
        return nutritionTypeVOList;
    }
}
